Skip to content

Commit 876690b

Browse files
authored
fix(shim): use module.require to avoid variable collision (#282)
1 parent 8c95826 commit 876690b

File tree

6 files changed

+29
-12
lines changed

6 files changed

+29
-12
lines changed

packages/core/src/plugins/cjsShim.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { RsbuildPlugin } from '@rsbuild/core';
22

33
const importMetaUrlShim = `/*#__PURE__*/ (function () {
44
return typeof document === 'undefined'
5-
? new (require('url'.replace('', '')).URL)('file:' + __filename).href
5+
? new (module.require('url'.replace('', '')).URL)('file:' + __filename).href
66
: (document.currentScript && document.currentScript.src) ||
77
new URL('main.js', document.baseURI).href;
88
})()`;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export default defineConfig({
55
lib: [generateBundleEsmConfig(), generateBundleCjsConfig()],
66
output: {
77
target: 'node',
8+
copy: [{ from: 'src/ok.cjs' }],
89
},
910
source: {
1011
entry: {
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
const url = import.meta.url;
2-
const readUrl = url;
1+
import { createRequire } from 'node:module';
2+
const importMetaUrl = import.meta.url;
3+
const require = createRequire(import.meta.url);
4+
const requiredModule = require('./ok.cjs');
35

4-
export default readUrl;
6+
export { importMetaUrl, requiredModule };
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 'ok';

tests/integration/shims/index.test.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { join } from 'node:path';
22
import { pathToFileURL } from 'node:url';
3+
import vm from 'node:vm';
34
import { buildAndGetResults } from 'test-helper';
45
import { describe, expect, test } from 'vitest';
56

@@ -22,20 +23,31 @@ test('shims for __dirname and __filename in ESM', async () => {
2223
describe('shims for `import.meta.url` in CJS', () => {
2324
test('CJS should apply shims', async () => {
2425
const fixturePath = join(__dirname, 'cjs');
25-
const { entryFiles } = await buildAndGetResults(fixturePath);
26-
const exported = await import(entryFiles.cjs);
26+
const { entryFiles, entries } = await buildAndGetResults(fixturePath);
27+
// `module.require` is not available in Vitest runner context. Manually create a context to run the CJS code.
28+
// As a temporary solution, we use `module.require` to avoid potential collision with module scope variable `require`.
29+
const cjsCode = entries.cjs;
30+
const context = vm.createContext({
31+
require,
32+
exports,
33+
module: { require },
34+
__filename: entryFiles.cjs,
35+
});
36+
const { importMetaUrl, requiredModule } = vm.runInContext(cjsCode, context);
2737
const fileUrl = pathToFileURL(entryFiles.cjs).href;
28-
expect(exported.default).toBe(fileUrl);
38+
expect(importMetaUrl).toBe(fileUrl);
39+
expect(requiredModule).toBe('ok');
2940
});
3041

3142
test('ESM should not be affected by CJS shims configuration', async () => {
3243
const fixturePath = join(__dirname, 'cjs');
3344
const { entries } = await buildAndGetResults(fixturePath);
3445
expect(entries.esm).toMatchInlineSnapshot(`
35-
"const url = import.meta.url;
36-
const readUrl = url;
37-
/* harmony default export */ const src = readUrl;
38-
export { src as default };
46+
"import * as __WEBPACK_EXTERNAL_MODULE_node_module__ from \\"node:module\\";
47+
const importMetaUrl = import.meta.url;
48+
const src_require = (0, __WEBPACK_EXTERNAL_MODULE_node_module__.createRequire)(import.meta.url);
49+
const requiredModule = src_require('./ok.cjs');
50+
export { importMetaUrl, requiredModule };
3951
"
4052
`);
4153
});

tests/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"integration/**/*.ts",
1010
"benchmark/**/*.ts",
1111
"playwright.config.ts",
12-
"scripts"
12+
"scripts",
13+
"integration/shims/cjs/src/ok.cjs"
1314
],
1415
"exclude": ["**/node_modules", "**/.*/"],
1516
"references": [

0 commit comments

Comments
 (0)