Skip to content

Commit 159f492

Browse files
committed
feat!: use 'commonjs-import' as CJS external type by default
1 parent c90a94e commit 159f492

File tree

8 files changed

+75
-159
lines changed

8 files changed

+75
-159
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
},
6565
"pnpm": {
6666
"overrides": {
67+
"@rspack/core": "npm:@rspack/[email protected]",
6768
"zx>@types/node": "-"
6869
}
6970
}

packages/core/src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ const composeExternalsConfig = (
683683

684684
const externalsTypeMap = {
685685
esm: 'module-import',
686-
cjs: 'commonjs',
686+
cjs: 'commonjs-import',
687687
umd: 'umd',
688688
// If use 'var', when projects import an external package like '@pkg', this will cause a syntax error such as 'var pkg = @pkg'.
689689
// If use 'umd', the judgement conditions may be affected by other packages that define variables like 'define'.

packages/core/tests/__snapshots__/config.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
396396
},
397397
},
398398
{
399-
"externalsType": "commonjs",
399+
"externalsType": "commonjs-import",
400400
"module": {
401401
"parser": {
402402
"javascript": {

pnpm-lock.yaml

Lines changed: 47 additions & 151 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/integration/externals/index.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,11 @@ test('should get warn when use require in ESM', async () => {
6666

6767
restore();
6868
});
69+
70+
test('require ESM from CJS', async () => {
71+
const fixturePath = join(__dirname, 'node');
72+
const { entryFiles } = await buildAndGetResults({ fixturePath });
73+
const baz = (await import(entryFiles.cjs)).baz;
74+
const bazValue = await baz();
75+
expect(bazValue).toBe('baz');
76+
});

tests/integration/externals/node/rslib.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export default defineConfig({
1818
},
1919
},
2020
output: {
21-
externals: { react: 'react', bar: 'bar' },
21+
externals: { react: 'react', bar: 'bar', './baz.mjs': './baz.mjs' },
22+
copy: [{ from: './src/baz.mjs' }],
2223
},
2324
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const baz = 'baz';

tests/integration/externals/node/src/index.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,20 @@ import fs from 'fs'; // handle bare node built-in modules
22
import assert from 'node:assert'; // handle node built-in modules with node: protocol
33
import React from 'react'; // works with the externals option in rslib.config.ts
44

5-
export const foo = () => {
5+
export const foo = async () => {
66
assert(fs, 'fs exists');
7+
const fooModule = require('foo'); // ESM: specified externals type
8+
fooModule();
9+
};
10+
11+
export const bar = async () => {
712
assert(React.version);
8-
const foo = require('foo'); // ESM: specified externals type
9-
const bar = require('bar'); // ESM: fallback to "module" when not specify externals type
10-
foo();
11-
bar();
13+
const barModule = require('bar'); // ESM: fallback to "module" when not specify externals type
14+
barModule();
15+
};
16+
17+
export const baz = async () => {
18+
// @ts-ignore
19+
const bazModule = await import('./baz.mjs'); // should be kept dynamic import by default
20+
return bazModule.baz;
1221
};

0 commit comments

Comments
 (0)