Skip to content

Commit 8f9352b

Browse files
authored
Ref: disabling dynamic imports in CJS build (#1757)
There is a Jest issue on handling dynamic imports that is really annoying. I used to apply an approach discussed in this issue: evanw/esbuild#2651 But now I'm going to apply another approach from the same one, just by disabling the dynamic imports for CJS, as a cleaner way (in my opinion). https://esbuild.github.io/api/#supported
1 parent f755608 commit 8f9352b

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

src/peer-helpers.ts

-10
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,6 @@ export const loadPeer = async <T>(
77
try {
88
return (await import(moduleName))[moduleExport];
99
} catch {}
10-
try {
11-
return await Promise.resolve().then(
12-
/**
13-
* alternative way for environments that do not support dynamic imports even it's CJS compatible
14-
* @example jest with ts-jest
15-
* @link https://github.com/evanw/esbuild/issues/2651
16-
*/
17-
() => require(moduleName)[moduleExport],
18-
);
19-
} catch {}
2010
throw new MissingPeerError(moduleName);
2111
};
2212

tsup.config.ts

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ export default defineConfig({
1010
dts: true,
1111
minify: true,
1212
esbuildOptions: (options, { format }) => {
13+
options.supported = {};
14+
if (format === "cjs") {
15+
/**
16+
* Downgrade dynamic imports for CJS even they are actually supported, but still are problematic for Jest
17+
* @example jest with ts-jest
18+
* @link https://github.com/evanw/esbuild/issues/2651
19+
*/
20+
options.supported["dynamic-import"] = false;
21+
}
1322
options.define = {
1423
"process.env.TSUP_BUILD": `"v${version} (${format.toUpperCase()})"`,
1524
};

0 commit comments

Comments
 (0)