Skip to content

Commit d99c6e5

Browse files
authored
fix: CJS shims should not affect other environments (#234)
1 parent c1218f5 commit d99c6e5

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

e2e/cases/shims/cjs/rslib.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { generateBundleCjsConfig } from '@e2e/helper';
1+
import { generateBundleCjsConfig, generateBundleEsmConfig } from '@e2e/helper';
22
import { defineConfig } from '@rslib/core';
33

44
export default defineConfig({
5-
lib: [generateBundleCjsConfig()],
5+
lib: [generateBundleEsmConfig(), generateBundleCjsConfig()],
66
output: {
77
target: 'node',
88
},

e2e/cases/shims/index.test.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { join } from 'node:path';
22
import { buildAndGetResults } from '@e2e/helper';
3-
import { expect, test } from 'vitest';
3+
import { describe, expect, test } from 'vitest';
44

55
test('shims for __dirname and __filename in ESM', async () => {
66
const fixturePath = join(__dirname, 'esm');
@@ -16,15 +16,29 @@ test('shims for __dirname and __filename in ESM', async () => {
1616
}
1717
});
1818

19-
test('shims for import.meta.url in CJS', async () => {
20-
const fixturePath = join(__dirname, 'cjs');
21-
const { entries } = await buildAndGetResults(fixturePath);
22-
for (const shim of [
23-
`var __rslib_import_meta_url__ = /*#__PURE__*/ function() {
19+
describe('shims for `import.meta.url` in CJS', () => {
20+
test('CJS should apply shims', async () => {
21+
const fixturePath = join(__dirname, 'cjs');
22+
const { entries } = await buildAndGetResults(fixturePath);
23+
for (const shim of [
24+
`var __rslib_import_meta_url__ = /*#__PURE__*/ function() {
2425
return 'undefined' == typeof document ? new (require('url'.replace('', ''))).URL('file:' + __filename).href : document.currentScript && document.currentScript.src || new URL('main.js', document.baseURI).href;
2526
}();`,
26-
'console.log(__rslib_import_meta_url__);',
27-
]) {
28-
expect(entries.cjs).toContain(shim);
29-
}
27+
'console.log(__rslib_import_meta_url__);',
28+
]) {
29+
expect(entries.cjs).toContain(shim);
30+
}
31+
});
32+
33+
test('ESM should not be affected by CJS shims configuration', async () => {
34+
const fixturePath = join(__dirname, 'cjs');
35+
const { entries } = await buildAndGetResults(fixturePath);
36+
expect(entries.esm).toMatchInlineSnapshot(`
37+
"const foo = ()=>{
38+
console.log(import.meta.url);
39+
};
40+
export { foo };
41+
"
42+
`);
43+
});
3044
});

packages/core/src/plugins/cjsShim.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ export const pluginCjsShim = (): RsbuildPlugin => ({
1616
name: 'rsbuild-plugin-cjs-shim',
1717

1818
setup(api) {
19-
api.modifyRsbuildConfig((config) => {
20-
config.source ||= {};
19+
api.modifyEnvironmentConfig((config) => {
2120
config.source.define = {
2221
...config.source.define,
2322
'import.meta.url': '__rslib_import_meta_url__',

0 commit comments

Comments
 (0)