Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime): use require in CJS build #289

Merged
merged 3 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/calm-dragons-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-mesh/fusion-runtime': patch
---

Use \`require\` directly on CommonJS
2 changes: 1 addition & 1 deletion packages/fusion-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
@@ -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');
53 changes: 5 additions & 48 deletions packages/fusion-runtime/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,66 +52,23 @@ export type Transports =
}
| ((kind: string) => MaybePromise<Transport | { default: Transport }>);

function tryImportThenRequireTransport(
kind: string,
): MaybePromise<Transport & { default?: Transport }> {
function defaultTransportsGetter(kind: string): Promise<Transport> {
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<Transport> {
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;
Expand Down
Loading