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..d4551d2ba 100644 --- a/packages/fusion-runtime/src/utils.ts +++ b/packages/fusion-runtime/src/utils.ts @@ -52,66 +52,23 @@ export type Transports = } | ((kind: string) => MaybePromise); -function tryImportThenRequireTransport( - kind: string, -): MaybePromise { +function defaultTransportsGetter(kind: string): Promise { 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(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;