Skip to content

Commit 09c0fda

Browse files
authored
fix(shims): require shims should inject to mjs as well (#1032)
1 parent e7a95b8 commit 09c0fda

File tree

4 files changed

+10
-9
lines changed

4 files changed

+10
-9
lines changed

packages/core/src/plugins/shims.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const pluginEsmRequireShim = (): RsbuildPlugin => ({
3232
// Just before minify stage, to perform tree shaking.
3333
stage: rspack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE - 1,
3434
raw: true,
35-
include: /\.(js|cjs)$/,
35+
include: /\.(js|mjs)$/,
3636
}),
3737
);
3838
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export default defineConfig({
3131
},
3232
}),
3333
generateBundleEsmConfig({
34-
shims: { esm: { __dirname: true, __filename: true } },
34+
shims: { esm: { __dirname: true, __filename: true, require: true } },
3535
syntax: 'esnext',
3636
source: {
3737
entry: {
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
const ok = require != null && require('./ok.cjs');
2-
const okPath = require.resolve('./ok.cjs');
3-
4-
export { ok, okPath };
1+
export const randomFile = require(process.env.RANDOM_FILE!);

tests/integration/shims/index.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,13 @@ describe('ESM shims', async () => {
4949
});
5050

5151
test('require', async () => {
52-
const { ok, okPath } = await import(entryFiles.esm2!);
53-
expect(ok).toBe('ok');
54-
expect(okPath).toBe(path.resolve(path.dirname(entryFiles.esm2!), 'ok.cjs'));
52+
expect(entries.esm2).toMatchInlineSnapshot(`
53+
"import __rslib_shim_module__ from 'module';
54+
const require = /*#__PURE__*/ __rslib_shim_module__.createRequire(import.meta.url);
55+
const randomFile = require(process.env.RANDOM_FILE);
56+
export { randomFile };
57+
"
58+
`);
5559
});
5660
});
5761

0 commit comments

Comments
 (0)