Skip to content

Commit b0e5e09

Browse files
authored
fix: Node shims for 'javascript/esm' files (#1274)
1 parent 810fc51 commit b0e5e09

File tree

6 files changed

+86
-10
lines changed

6 files changed

+86
-10
lines changed

packages/core/src/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,11 +596,13 @@ const composeFormatConfig = ({
596596
bundle = true,
597597
umdName,
598598
pkgJson,
599+
enabledShims,
599600
}: {
600601
format: Format;
601602
pkgJson: PkgJson;
602603
bundle?: boolean;
603604
umdName?: Rspack.LibraryName;
605+
enabledShims: DeepRequired<Shims>;
604606
}): EnvironmentConfig => {
605607
const jsParserOptions: Record<string, Rspack.JavascriptParserOptions> = {
606608
cjs: {
@@ -625,6 +627,7 @@ const composeFormatConfig = ({
625627
const plugins = [
626628
new rspack.experiments.RslibPlugin({
627629
interceptApiPlugin: true,
630+
forceNodeShims: enabledShims.esm.__dirname || enabledShims.esm.__filename,
628631
}),
629632
];
630633

@@ -1730,6 +1733,7 @@ async function composeLibRsbuildConfig(
17301733
pkgJson: pkgJson!,
17311734
bundle,
17321735
umdName,
1736+
enabledShims,
17331737
});
17341738
const externalHelpersConfig = composeExternalHelpersConfig(
17351739
externalHelpers,

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,8 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
975975
name: 'RslibPlugin',
976976
_args: [
977977
{
978-
interceptApiPlugin: true
978+
interceptApiPlugin: true,
979+
forceNodeShims: false
979980
}
980981
]
981982
},
@@ -1673,7 +1674,8 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
16731674
name: 'RslibPlugin',
16741675
_args: [
16751676
{
1676-
interceptApiPlugin: true
1677+
interceptApiPlugin: true,
1678+
forceNodeShims: false
16771679
}
16781680
]
16791681
},
@@ -2276,7 +2278,8 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
22762278
name: 'RslibPlugin',
22772279
_args: [
22782280
{
2279-
interceptApiPlugin: true
2281+
interceptApiPlugin: true,
2282+
forceNodeShims: false
22802283
}
22812284
]
22822285
},
@@ -2878,7 +2881,8 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
28782881
name: 'RslibPlugin',
28792882
_args: [
28802883
{
2881-
interceptApiPlugin: true
2884+
interceptApiPlugin: true,
2885+
forceNodeShims: false
28822886
}
28832887
]
28842888
},
@@ -3708,6 +3712,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
37083712
RslibPlugin {
37093713
"_args": [
37103714
{
3715+
"forceNodeShims": false,
37113716
"interceptApiPlugin": true,
37123717
},
37133718
],
@@ -3990,6 +3995,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
39903995
RslibPlugin {
39913996
"_args": [
39923997
{
3998+
"forceNodeShims": false,
39933999
"interceptApiPlugin": true,
39944000
},
39954001
],
@@ -4233,6 +4239,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
42334239
RslibPlugin {
42344240
"_args": [
42354241
{
4242+
"forceNodeShims": false,
42364243
"interceptApiPlugin": true,
42374244
},
42384245
],
@@ -4475,6 +4482,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
44754482
RslibPlugin {
44764483
"_args": [
44774484
{
4485+
"forceNodeShims": false,
44784486
"interceptApiPlugin": true,
44794487
},
44804488
],

tests/integration/shims/esm/rslib.config.ts

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import { generateBundleEsmConfig } from 'test-helper';
44
export default defineConfig({
55
lib: [
66
generateBundleEsmConfig({
7-
shims: { esm: { __dirname: true, __filename: true } },
7+
shims: {
8+
esm: {
9+
__dirname: true,
10+
__filename: true,
11+
},
12+
},
813
source: {
914
entry: {
1015
index: './src/index.ts',
@@ -15,7 +20,12 @@ export default defineConfig({
1520
},
1621
}),
1722
generateBundleEsmConfig({
18-
shims: { esm: { __dirname: true, __filename: true } },
23+
shims: {
24+
esm: {
25+
__dirname: true,
26+
__filename: true,
27+
},
28+
},
1929
syntax: 'esnext',
2030
source: {
2131
entry: {
@@ -27,17 +37,41 @@ export default defineConfig({
2737
},
2838
}),
2939
generateBundleEsmConfig({
30-
shims: { esm: { __dirname: true, __filename: true, require: true } },
40+
shims: {
41+
esm: {
42+
require: true,
43+
},
44+
},
3145
syntax: 'esnext',
3246
source: {
3347
entry: {
3448
index: './src/require.ts',
3549
},
3650
},
3751
output: {
38-
copy: [{ from: './src/ok.cjs' }],
52+
copy: [
53+
{
54+
from: './src/ok.cjs',
55+
},
56+
],
3957
distPath: './dist/enabled/esm2',
4058
},
4159
}),
60+
generateBundleEsmConfig({
61+
shims: {
62+
esm: {
63+
__dirname: true,
64+
__filename: true,
65+
},
66+
},
67+
source: {
68+
entry: {
69+
index: './src/node.mjs',
70+
},
71+
},
72+
output: {
73+
distPath: './dist/enabled/esm3',
74+
},
75+
}),
4276
],
4377
});

tests/integration/shims/esm/rslibShimsDisabled.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import config from './rslib.config';
33

44
export default defineConfig({
55
...config,
6-
lib: [config.lib[0]!, config.lib[2]!].map((libConfig) => {
6+
lib: config.lib.map((libConfig) => {
77
if (typeof libConfig.output!.distPath === 'string') {
88
libConfig.output!.distPath = libConfig.output!.distPath.replace(
99
'./dist/enabled',
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const d1 = __dirname;
2+
const f1 = __filename;
3+
4+
export default () => {
5+
const d2 = __dirname;
6+
const f2 = __filename;
7+
8+
return { d1, d2, f1, f2 };
9+
};

tests/integration/shims/index.test.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,24 @@ describe('ESM shims', async () => {
5757
"
5858
`);
5959
});
60+
61+
test('Node.js shims in mjs file', async () => {
62+
for (const shim of [
63+
'import { fileURLToPath as __webpack_fileURLToPath__ } from "node:url";',
64+
'import { dirname as __webpack_dirname__ } from "node:path";',
65+
'var node_dirname = __webpack_dirname__(__webpack_fileURLToPath__(import.meta.url));',
66+
'var node_filename = __webpack_fileURLToPath__(import.meta.url);',
67+
]) {
68+
expect(entries.esm3).toContain(shim);
69+
}
70+
71+
const entry3Result = (await import(entryFiles.esm3!)).default();
72+
73+
expect(entry3Result.d1).toBe(path.dirname(entryFiles.esm3!));
74+
expect(entry3Result.d1).toBe(entry3Result.d2);
75+
expect(entry3Result.f1).toBe(entryFiles.esm3);
76+
expect(entry3Result.f1).toBe(entry3Result.f2);
77+
});
6078
});
6179

6280
describe('ESM shims disabled', async () => {
@@ -68,9 +86,10 @@ describe('ESM shims disabled', async () => {
6886
});
6987

7088
expect(entries.esm0).not.toContain('fileURLToPath');
89+
expect(entries.esm1).not.toContain('fileURLToPath');
7190

7291
const context = vm.createContext({});
73-
const module = new vm.SourceTextModule(entries.esm1!, {
92+
const module = new vm.SourceTextModule(entries.esm2!, {
7493
context,
7594
});
7695

@@ -80,6 +99,8 @@ describe('ESM shims disabled', async () => {
8099

81100
await module.link(linker);
82101
await expect(module.evaluate()).rejects.toThrow('require is not defined');
102+
103+
expect(entries.esm3).not.toContain('fileURLToPath');
83104
});
84105
});
85106

0 commit comments

Comments
 (0)