Skip to content

Commit 5424189

Browse files
committed
shim test
1 parent 711eda5 commit 5424189

File tree

5 files changed

+20
-39
lines changed

5 files changed

+20
-39
lines changed

packages/core/src/plugins/EntryChunkPlugin.ts

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ class EntryChunkPlugin {
4040
private shebangInjectedAssets: Set<string> = new Set();
4141

4242
private enabledImportMetaUrlShim: boolean;
43-
private importMetaUrlShims: Record<string, { startsWithUseStrict: boolean }> =
44-
{};
4543

4644
constructor({
4745
enabledImportMetaUrlShim = true,
@@ -89,17 +87,6 @@ class EntryChunkPlugin {
8987
if (reactDirective) {
9088
this.reactDirectives[name] = reactDirective;
9189
}
92-
93-
// import.meta.url shim
94-
if (this.enabledImportMetaUrlShim) {
95-
this.importMetaUrlShims[name] = {
96-
startsWithUseStrict:
97-
// This is a hypothesis that no comments will occur before "use strict;".
98-
// But it should cover most cases.
99-
content.startsWith('use strict;') ||
100-
content.startsWith('"use strict";'),
101-
};
102-
}
10390
}
10491
});
10592

@@ -120,11 +107,6 @@ class EntryChunkPlugin {
120107
if (reactDirective) {
121108
this.reactDirectives[filename] = reactDirective;
122109
}
123-
124-
const importMetaUrlShimInfo = this.importMetaUrlShims[name];
125-
if (importMetaUrlShimInfo) {
126-
this.importMetaUrlShims[filename] = importMetaUrlShimInfo;
127-
}
128110
});
129111
});
130112

@@ -134,24 +116,22 @@ class EntryChunkPlugin {
134116
for (const name of chunkAsset) {
135117
if (this.enabledImportMetaUrlShim) {
136118
compilation.updateAsset(name, (old) => {
137-
const importMetaUrlShimInfo = this.importMetaUrlShims[name];
138-
if (importMetaUrlShimInfo) {
139-
const replaceSource = new rspack.sources.ReplaceSource(old);
140-
141-
if (importMetaUrlShimInfo.startsWithUseStrict) {
142-
replaceSource.replace(
143-
0,
144-
11, // 'use strict;'.length,
145-
`"use strict";${os.EOL}${importMetaUrlShim}`,
146-
);
147-
} else {
148-
replaceSource.insert(0, importMetaUrlShim);
149-
}
150-
151-
return replaceSource;
119+
const oldSource = old.source().toString();
120+
const replaceSource = new rspack.sources.ReplaceSource(old);
121+
if (
122+
oldSource.startsWith('use strict;') ||
123+
oldSource.startsWith('"use strict";')
124+
) {
125+
replaceSource.replace(
126+
0,
127+
11, // 'use strict;'.length,
128+
`"use strict";${os.EOL}${importMetaUrlShim}`,
129+
);
130+
} else {
131+
replaceSource.insert(0, importMetaUrlShim);
152132
}
153133

154-
return old;
134+
return replaceSource;
155135
});
156136
}
157137
}

packages/core/tests/__snapshots__/config.test.ts.snap

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
204204
"plugins": [
205205
EntryChunkPlugin {
206206
"enabledImportMetaUrlShim": true,
207-
"importMetaUrlShims": {},
208207
"reactDirectives": {},
209208
"shebangChmod": 493,
210209
"shebangEntries": {},
@@ -430,7 +429,6 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
430429
"plugins": [
431430
EntryChunkPlugin {
432431
"enabledImportMetaUrlShim": true,
433-
"importMetaUrlShims": {},
434432
"reactDirectives": {},
435433
"shebangChmod": 493,
436434
"shebangEntries": {},
@@ -640,7 +638,6 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
640638
"plugins": [
641639
EntryChunkPlugin {
642640
"enabledImportMetaUrlShim": true,
643-
"importMetaUrlShims": {},
644641
"reactDirectives": {},
645642
"shebangChmod": 493,
646643
"shebangEntries": {},

tests/integration/directive/index.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { join } from 'node:path';
44
import { buildAndGetResults, queryContent } from 'test-helper';
55
import { describe, expect, test } from 'vitest';
66

7+
// The test for `import.meta.url` shim is in tests/integration/shims/index.test.ts
8+
79
describe('shebang', async () => {
810
const fixturePath = join(__dirname, 'shebang');
911
const { entries, contents, entryFiles } = await buildAndGetResults({

tests/integration/shims/cjs/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// import.meta.url
21
import { createRequire } from 'node:module';
32
const importMetaUrl = import.meta.url;
43
const require = createRequire(import.meta.url);

tests/integration/shims/index.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,17 @@ describe('CJS shims', () => {
9595
const fileUrl = pathToFileURL(entryFiles.cjs).href;
9696
expect(importMetaUrl).toBe(fileUrl);
9797
expect(requiredModule).toBe('ok');
98+
expect(cjsCode.startsWith('"use strict"')).toBe(true);
99+
expect(cjsCode).toContain(
100+
'const __rslib_import_meta_url__ = /*#__PURE__*/ function() {',
101+
);
98102
});
99103

100104
test('ESM should not be affected by CJS shims configuration', async () => {
101105
const fixturePath = join(__dirname, 'cjs');
102106
const { entries } = await buildAndGetResults({ fixturePath });
103107
expect(entries.esm).toMatchInlineSnapshot(`
104108
"import * as __WEBPACK_EXTERNAL_MODULE_node_module__ from "node:module";
105-
// import.meta.url
106109
const importMetaUrl = import.meta.url;
107110
const src_rslib_entry_require = (0, __WEBPACK_EXTERNAL_MODULE_node_module__.createRequire)(import.meta.url);
108111
const requiredModule = src_rslib_entry_require('./ok.cjs');

0 commit comments

Comments
 (0)