From 1d96641a90d87757d6f8f16067360e96631d74a7 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Thu, 10 Nov 2022 17:15:21 +0100 Subject: [PATCH 1/2] fix(npm/js-api): Lazy load backend implementations The wasm and backendjsonrpc dependencies are both marked as optional but the implementation loads packages statically, making the `js-api` fail immediately if one of the two isn't installed. This PR moves the imports into lazy async calls. --- npm/js-api/package.json | 2 +- npm/js-api/src/daemon.ts | 10 +++++----- npm/js-api/src/nodeWasm.ts | 9 ++++----- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/npm/js-api/package.json b/npm/js-api/package.json index c31985b601b..73107ff5f1d 100644 --- a/npm/js-api/package.json +++ b/npm/js-api/package.json @@ -1,6 +1,6 @@ { "name": "@rometools/js-api", - "version": "0.1.2", + "version": "0.1.3", "description": "JavaScript APIs for the Rome package", "scripts": { "tsc": "tsc --noEmit", diff --git a/npm/js-api/src/daemon.ts b/npm/js-api/src/daemon.ts index 5fadd347189..d7a87eb6369 100644 --- a/npm/js-api/src/daemon.ts +++ b/npm/js-api/src/daemon.ts @@ -1,8 +1,4 @@ -import { - createWorkspace, - createWorkspaceWithBinary, - Workspace, -} from "@rometools/backend-jsonrpc"; +import type { Workspace } from "@rometools/backend-jsonrpc"; /** * Class responsible to communicate with the Rome daemon. @@ -20,6 +16,10 @@ export class Deamon { * It creates a new instance of a workspace connected to the Daemon */ public static async connectToDaemon(pathToBinary?: string): Promise { + const { createWorkspace, createWorkspaceWithBinary } = await import( + "@rometools/backend-jsonrpc", + ); + if (pathToBinary) { let workspace = await createWorkspaceWithBinary(pathToBinary); if (workspace) { diff --git a/npm/js-api/src/nodeWasm.ts b/npm/js-api/src/nodeWasm.ts index 4133355e398..d1838a61b1d 100644 --- a/npm/js-api/src/nodeWasm.ts +++ b/npm/js-api/src/nodeWasm.ts @@ -1,4 +1,4 @@ -import { main, Workspace } from "@rometools/wasm-nodejs"; +import type { Workspace } from "@rometools/wasm-nodejs"; /** * Class responsible to connect with the WebAssembly backend of Rome. @@ -13,13 +13,12 @@ export class NodeWasm { * It creates a new instance of a workspace connected to the WebAssembly backend */ public static async loadWebAssembly(): Promise { - return new NodeWasm(await NodeWasm.loadWorkspace()); - } + const { main, Workspace } = await import("@rometools/wasm-nodejs"); - private static async loadWorkspace(): Promise { // load the web assembly module main(); - return Promise.resolve(new Workspace()); + + return new NodeWasm(new Workspace()); } } From 1d8739ace63cf0c57391f52b54797627b707bc19 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Thu, 10 Nov 2022 17:16:49 +0100 Subject: [PATCH 2/2] fix trailing comma --- npm/js-api/src/daemon.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/npm/js-api/src/daemon.ts b/npm/js-api/src/daemon.ts index d7a87eb6369..c64d7428850 100644 --- a/npm/js-api/src/daemon.ts +++ b/npm/js-api/src/daemon.ts @@ -17,7 +17,7 @@ export class Deamon { */ public static async connectToDaemon(pathToBinary?: string): Promise { const { createWorkspace, createWorkspaceWithBinary } = await import( - "@rometools/backend-jsonrpc", + "@rometools/backend-jsonrpc" ); if (pathToBinary) {