Skip to content

Commit 5a50330

Browse files
committed
shim only on current format
1 parent 5424189 commit 5a50330

File tree

3 files changed

+23
-28
lines changed

3 files changed

+23
-28
lines changed

packages/core/src/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,8 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
11241124
);
11251125
const cssConfig = composeCssConfig(lcp, config.bundle);
11261126
const entryChunkConfig = composeEntryChunkConfig({
1127-
enabledImportMetaUrlShim: !!resolvedShims?.cjs?.['import.meta.url'],
1127+
enabledImportMetaUrlShim:
1128+
format === 'cjs' && !!resolvedShims?.cjs?.['import.meta.url'],
11281129
});
11291130
const dtsConfig = await composeDtsConfig(config, dtsExtension);
11301131
const externalsWarnConfig = composeExternalsWarnConfig(

packages/core/src/plugins/EntryChunkPlugin.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -112,28 +112,28 @@ class EntryChunkPlugin {
112112

113113
compiler.hooks.make.tap(PLUGIN_NAME, (compilation) => {
114114
compilation.hooks.processAssets.tap(PLUGIN_NAME, (assets) => {
115+
if (!this.enabledImportMetaUrlShim) return;
116+
115117
const chunkAsset = Object.keys(assets);
116118
for (const name of chunkAsset) {
117-
if (this.enabledImportMetaUrlShim) {
118-
compilation.updateAsset(name, (old) => {
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);
132-
}
133-
134-
return replaceSource;
135-
});
136-
}
119+
compilation.updateAsset(name, (old) => {
120+
const oldSource = old.source().toString();
121+
const replaceSource = new rspack.sources.ReplaceSource(old);
122+
if (
123+
oldSource.startsWith('use strict;') ||
124+
oldSource.startsWith('"use strict";')
125+
) {
126+
replaceSource.replace(
127+
0,
128+
11, // 'use strict;'.length,
129+
`"use strict";${os.EOL}${importMetaUrlShim}`,
130+
);
131+
} else {
132+
replaceSource.insert(0, importMetaUrlShim);
133+
}
134+
135+
return replaceSource;
136+
});
137137
}
138138
});
139139

tests/integration/minify/index.test.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,7 @@ test('tree shaking is disabled by the user, bar and baz should be kept', async (
1616
const fixturePath = join(__dirname, 'config');
1717
const { entries } = await buildAndGetResults({ fixturePath });
1818
expect(entries.esm).toMatchInlineSnapshot(`
19-
"const __rslib_import_meta_url__ = /*#__PURE__*/ (function () {
20-
return typeof document === 'undefined'
21-
? new (require('url'.replace('', '')).URL)('file:' + __filename).href
22-
: (document.currentScript && document.currentScript.src) ||
23-
new URL('main.js', document.baseURI).href;
24-
})();
25-
19+
"
2620
;// CONCATENATED MODULE: ../__fixtures__/src/index.ts?__rslib_entry__
2721
const foo = ()=>{};
2822
const bar = ()=>{};

0 commit comments

Comments
 (0)