From cde21d9084edc846a2c81880636f5f471cec8d1d Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU Date: Fri, 6 Dec 2024 15:22:19 +0300 Subject: [PATCH 1/3] fix(runtime): use `require` in CJS build --- .changeset/calm-dragons-nail.md | 5 +++ packages/fusion-runtime/package.json | 2 +- .../scripts/replace-import-with-require.mjs | 6 +++ packages/fusion-runtime/src/utils.ts | 44 +------------------ 4 files changed, 13 insertions(+), 44 deletions(-) create mode 100644 .changeset/calm-dragons-nail.md create mode 100644 packages/fusion-runtime/scripts/replace-import-with-require.mjs diff --git a/.changeset/calm-dragons-nail.md b/.changeset/calm-dragons-nail.md new file mode 100644 index 000000000..063f40323 --- /dev/null +++ b/.changeset/calm-dragons-nail.md @@ -0,0 +1,5 @@ +--- +'@graphql-mesh/fusion-runtime': patch +--- + +Use \`require\` directly on CommonJS diff --git a/packages/fusion-runtime/package.json b/packages/fusion-runtime/package.json index 2c07d26a5..710209220 100644 --- a/packages/fusion-runtime/package.json +++ b/packages/fusion-runtime/package.json @@ -36,7 +36,7 @@ "dist" ], "scripts": { - "build": "pkgroll --clean-dist", + "build": "pkgroll --clean-dist && node scripts/replace-import-with-require.mjs", "prepack": "yarn build" }, "peerDependencies": { diff --git a/packages/fusion-runtime/scripts/replace-import-with-require.mjs b/packages/fusion-runtime/scripts/replace-import-with-require.mjs new file mode 100644 index 000000000..9141a02d2 --- /dev/null +++ b/packages/fusion-runtime/scripts/replace-import-with-require.mjs @@ -0,0 +1,6 @@ +import fs from 'node:fs'; + +const cjsFile = './dist/index.cjs'; +const fileContent = fs.readFileSync(cjsFile, 'utf8'); +const newContent = fileContent.replace('import(kind)', 'require(kind)'); +fs.writeFileSync(cjsFile, newContent, 'utf8'); diff --git a/packages/fusion-runtime/src/utils.ts b/packages/fusion-runtime/src/utils.ts index 5ea89eeb6..a96054c0f 100644 --- a/packages/fusion-runtime/src/utils.ts +++ b/packages/fusion-runtime/src/utils.ts @@ -52,50 +52,8 @@ export type Transports = } | ((kind: string) => MaybePromise); -function tryImportThenRequireTransport( - kind: string, -): MaybePromise { - const moduleName = `@graphql-mesh/transport-${kind}`; - function handleModuleNotFoundError(e: unknown) { - if ( - e != null && - typeof e === 'object' && - 'code' in e && - e.code === 'MODULE_NOT_FOUND' - ) { - throw new Error( - `No transport found for ${kind}. Please make sure you have installed @graphql-mesh/transport-${kind} or defined the transport config in "mesh.config.ts" or "gateway.config.ts"`, - ); - } - } - function tryRequire(moduleName: string, e: unknown) { - if (!globalThis.require) { - throw e; - } - try { - return globalThis.require(moduleName); - } catch (e) { - handleModuleNotFoundError(e); - throw e; - } - } - try { - return mapMaybePromise( - import(moduleName), - (transport) => transport, - (e) => { - handleModuleNotFoundError(e); - return tryRequire(moduleName, e); - }, - ); - } catch (e) { - handleModuleNotFoundError(e); - throw e; - } -} - async function defaultTransportsGetter(kind: string): Promise { - return mapMaybePromise(tryImportThenRequireTransport(kind), (transport) => { + return mapMaybePromise(import(kind), (transport) => { if (typeof transport !== 'object') { throw new Error( `@graphql-mesh/transport-${kind} module does not export an object`, From fafd27bac27b8be4b0018f8c248e5d6422ab3a28 Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU Date: Fri, 6 Dec 2024 15:24:18 +0300 Subject: [PATCH 2/3] No need for async --- packages/fusion-runtime/src/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/fusion-runtime/src/utils.ts b/packages/fusion-runtime/src/utils.ts index a96054c0f..02747ea89 100644 --- a/packages/fusion-runtime/src/utils.ts +++ b/packages/fusion-runtime/src/utils.ts @@ -52,7 +52,7 @@ export type Transports = } | ((kind: string) => MaybePromise); -async function defaultTransportsGetter(kind: string): Promise { +function defaultTransportsGetter(kind: string): Promise { return mapMaybePromise(import(kind), (transport) => { if (typeof transport !== 'object') { throw new Error( From a3a41b16b3318f0502de1434dd573fd7504d0965 Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU Date: Fri, 6 Dec 2024 15:38:32 +0300 Subject: [PATCH 3/3] Go --- packages/fusion-runtime/src/utils.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/fusion-runtime/src/utils.ts b/packages/fusion-runtime/src/utils.ts index 02747ea89..d4551d2ba 100644 --- a/packages/fusion-runtime/src/utils.ts +++ b/packages/fusion-runtime/src/utils.ts @@ -53,23 +53,22 @@ export type Transports = | ((kind: string) => MaybePromise); function defaultTransportsGetter(kind: string): Promise { - return mapMaybePromise(import(kind), (transport) => { + const moduleName = `@graphql-mesh/transport-${kind}`; + return mapMaybePromise(import(moduleName), (transport) => { if (typeof transport !== 'object') { - throw new Error( - `@graphql-mesh/transport-${kind} module does not export an object`, - ); + throw new Error(`${moduleName} module does not export an object`); } if (transport?.default?.getSubgraphExecutor) { transport = transport.default; } if (!transport?.getSubgraphExecutor) { throw new Error( - `@graphql-mesh/transport-${kind} module does not export "getSubgraphExecutor"`, + `${moduleName} module does not export "getSubgraphExecutor"`, ); } if (typeof transport?.getSubgraphExecutor !== 'function') { throw new Error( - `@graphql-mesh/transport-${kind} module's export "getSubgraphExecutor" is not a function`, + `${moduleName} module's export "getSubgraphExecutor" is not a function`, ); } return transport;