Skip to content

Commit 06c38e9

Browse files
committed
fix: inline import.meta.url shims in CJS
1 parent 228bf3c commit 06c38e9

File tree

4 files changed

+19
-37
lines changed

4 files changed

+19
-37
lines changed

packages/core/src/plugins/cjsShim.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import { type RsbuildPlugin, rspack } from '@rsbuild/core';
1+
import type { RsbuildPlugin } from '@rsbuild/core';
22

3-
const importMetaUrlShim = `var __rslib_import_meta_url__ = /*#__PURE__*/ (function () {
3+
const importMetaUrlShim = `/*#__PURE__*/ (function () {
44
return typeof document === 'undefined'
55
? new (require('url'.replace('', '')).URL)('file:' + __filename).href
66
: (document.currentScript && document.currentScript.src) ||
77
new URL('main.js', document.baseURI).href;
8-
})();
9-
`;
8+
})()`;
109

1110
// This Rsbuild plugin will shim `import.meta.url` for CommonJS modules.
1211
// - Replace `import.meta.url` with `importMetaUrl`.
@@ -19,22 +18,8 @@ export const pluginCjsShim = (): RsbuildPlugin => ({
1918
api.modifyEnvironmentConfig((config) => {
2019
config.source.define = {
2120
...config.source.define,
22-
'import.meta.url': '__rslib_import_meta_url__',
21+
'import.meta.url': importMetaUrlShim,
2322
};
2423
});
25-
26-
api.modifyRspackConfig((config) => {
27-
config.plugins ??= [];
28-
config.plugins.push(
29-
new rspack.BannerPlugin({
30-
banner: importMetaUrlShim,
31-
// Just before minify stage, to perform tree shaking.
32-
stage: rspack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE - 1,
33-
raw: true,
34-
footer: true,
35-
include: /\.(js|cjs)$/,
36-
}),
37-
);
38-
});
3924
},
4025
});

tests/integration/require/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ test('require.resolve', async () => {
2323
];
2424

2525
const cjsStatements = [
26-
'const _require = (0, external_node_module_namespaceObject.createRequire)(__rslib_import_meta_url__)',
26+
'const _require = (0, external_node_module_namespaceObject.createRequire)(',
2727
];
2828

2929
for (const statement of [...statements, ...esmStatements]) {
@@ -52,7 +52,7 @@ test('require dynamic', async () => {
5252
];
5353

5454
const cjsStatements = [
55-
'const _require = (0, external_node_module_namespaceObject.createRequire)(__rslib_import_meta_url__)',
55+
'const _require = (0, external_node_module_namespaceObject.createRequire)(',
5656
];
5757

5858
for (const statement of [...statements, ...esmStatements]) {
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
export const foo = () => {
2-
console.log(import.meta.url);
3-
};
1+
const url = import.meta.url;
2+
const readUrl = url;
3+
4+
export default readUrl;

tests/integration/shims/index.test.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { join } from 'node:path';
2+
import { pathToFileURL } from 'node:url';
23
import { buildAndGetResults } from 'test-helper';
34
import { describe, expect, test } from 'vitest';
45

@@ -21,25 +22,20 @@ test('shims for __dirname and __filename in ESM', async () => {
2122
describe('shims for `import.meta.url` in CJS', () => {
2223
test('CJS should apply shims', async () => {
2324
const fixturePath = join(__dirname, 'cjs');
24-
const { entries } = await buildAndGetResults(fixturePath);
25-
for (const shim of [
26-
`var __rslib_import_meta_url__ = /*#__PURE__*/ function() {
27-
return 'undefined' == typeof document ? new (require('url'.replace('', ''))).URL('file:' + __filename).href : document.currentScript && document.currentScript.src || new URL('main.js', document.baseURI).href;
28-
}();`,
29-
'console.log(__rslib_import_meta_url__);',
30-
]) {
31-
expect(entries.cjs).toContain(shim);
32-
}
25+
const { entryFiles } = await buildAndGetResults(fixturePath);
26+
const exported = await import(entryFiles.cjs);
27+
const fileUrl = pathToFileURL(entryFiles.cjs).href;
28+
expect(exported.default).toBe(fileUrl);
3329
});
3430

3531
test('ESM should not be affected by CJS shims configuration', async () => {
3632
const fixturePath = join(__dirname, 'cjs');
3733
const { entries } = await buildAndGetResults(fixturePath);
3834
expect(entries.esm).toMatchInlineSnapshot(`
39-
"const foo = ()=>{
40-
console.log(import.meta.url);
41-
};
42-
export { foo };
35+
"const url = import.meta.url;
36+
const readUrl = url;
37+
/* harmony default export */ const src = readUrl;
38+
export { src as default };
4339
"
4440
`);
4541
});

0 commit comments

Comments
 (0)