Skip to content

Commit c11971a

Browse files
committed
fix: Node shims for js/esm files
1 parent 810fc51 commit c11971a

File tree

5 files changed

+74
-6
lines changed

5 files changed

+74
-6
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,

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)