Skip to content

Commit 6909167

Browse files
authored
fix: should only make entry doppelganger in non-advanced ESM (#1352)
1 parent b7b8bdf commit 6909167

File tree

5 files changed

+18
-54
lines changed

5 files changed

+18
-54
lines changed

packages/core/src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1832,7 +1832,7 @@ async function composeLibRsbuildConfig(
18321832
const assetConfig = composeAssetConfig(bundle, format);
18331833

18341834
const entryChunkConfig = composeEntryChunkConfig({
1835-
useLoader: advancedEsm !== true,
1835+
useLoader: advancedEsm !== true && (format === 'esm' || format === 'iife'),
18361836
enabledImportMetaUrlShim: enabledShims.cjs['import.meta.url'],
18371837
contextToWatch: outBase,
18381838
});

packages/core/src/plugins/EntryChunkPlugin.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,17 @@ class EntryChunkPlugin {
7272
compilation.updateAsset(name, (old) => {
7373
const oldSource = old.source().toString();
7474
const replaceSource = new rspack.sources.ReplaceSource(old);
75-
if (
76-
oldSource.startsWith('use strict;') ||
77-
oldSource.startsWith('"use strict";')
75+
76+
if (oldSource.startsWith('#!')) {
77+
const firstLineEnd = oldSource.indexOf('\n');
78+
replaceSource.insert(firstLineEnd + 1, IMPORT_META_URL_SHIM);
79+
} else if (
80+
oldSource.startsWith("'use strict'") ||
81+
oldSource.startsWith('"use strict"')
7882
) {
7983
replaceSource.replace(
8084
0,
81-
11, // 'use strict;'.length,
85+
11,
8286
`"use strict";\n${IMPORT_META_URL_SHIM}`,
8387
);
8488
} else {

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

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2425,17 +2425,6 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
24252425
}
24262426
}
24272427
]
2428-
},
2429-
/* config.module.rule('Rslib:js-entry-loader') */
2430-
{
2431-
test: /\\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/,
2432-
issuer: /^$/,
2433-
use: [
2434-
/* config.module.rule('Rslib:js-entry-loader').use('rsbuild:lib-entry-module') */
2435-
{
2436-
loader: '<WORKSPACE>/dist/entryModuleLoader.js'
2437-
}
2438-
]
24392428
}
24402429
]
24412430
},
@@ -3072,17 +3061,6 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
30723061
}
30733062
}
30743063
]
3075-
},
3076-
/* config.module.rule('Rslib:js-entry-loader') */
3077-
{
3078-
test: /\\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/,
3079-
issuer: /^$/,
3080-
use: [
3081-
/* config.module.rule('Rslib:js-entry-loader').use('rsbuild:lib-entry-module') */
3082-
{
3083-
loader: '<WORKSPACE>/dist/entryModuleLoader.js'
3084-
}
3085-
]
30863064
}
30873065
]
30883066
},
@@ -4312,17 +4290,6 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
43124290
generator: {
43134291
filename: 'static/wasm/[contenthash:8].module.wasm'
43144292
}
4315-
},
4316-
/* config.module.rule('Rslib:js-entry-loader') */
4317-
{
4318-
test: /\\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/,
4319-
issuer: /^$/,
4320-
use: [
4321-
/* config.module.rule('Rslib:js-entry-loader').use('rsbuild:lib-entry-module') */
4322-
{
4323-
loader: '<WORKSPACE>/dist/entryModuleLoader.js'
4324-
}
4325-
]
43264293
}
43274294
]
43284295
},
@@ -4834,10 +4801,6 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
48344801
],
48354802
"setup": [Function],
48364803
},
4837-
{
4838-
"name": "rsbuild:lib-entry-chunk",
4839-
"setup": [Function],
4840-
},
48414804
],
48424805
"resolve": {
48434806
"alias": {
@@ -5096,10 +5059,6 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
50965059
"name": "rsbuild:fix-js-module-type",
50975060
"setup": [Function],
50985061
},
5099-
{
5100-
"name": "rsbuild:lib-entry-chunk",
5101-
"setup": [Function],
5102-
},
51035062
],
51045063
"resolve": {
51055064
"alias": {
@@ -5541,10 +5500,6 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
55415500
"name": "rsbuild:fix-js-module-type",
55425501
"setup": [Function],
55435502
},
5544-
{
5545-
"name": "rsbuild:lib-entry-chunk",
5546-
"setup": [Function],
5547-
},
55485503
{
55495504
"name": "rsbuild:module-federation-enhanced",
55505505
"setup": [Function],

tests/integration/directive/shebang/rslib.config.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { defineConfig } from '@rslib/core';
2-
import { generateBundleEsmConfig } from 'test-helper';
2+
import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper';
33

44
const esmShared = {
55
source: {
@@ -20,6 +20,11 @@ const esmSharedBundleFalse = {
2020

2121
export default defineConfig({
2222
lib: [
23+
generateBundleCjsConfig({
24+
output: {
25+
distPath: './dist/bundle/cjs0',
26+
},
27+
}),
2328
generateBundleEsmConfig({
2429
...esmShared,
2530
shims: { esm: { __dirname: true, __filename: true } },

tests/integration/minify/index.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ describe('minify config (mf)', () => {
7474
const { mfExposeEntry } = await buildAndGetResults({ fixturePath });
7575
// biome-ignore format: snapshot
7676
expect(mfExposeEntry).toMatchInlineSnapshot(`
77-
"/*! For license information please see __federation_expose_default_export.fd95b46e.js.LICENSE.txt */
78-
"use strict";(globalThis["default_minify"]=globalThis["default_minify"]||[]).push([["525"],{807:function(__unused_webpack_module,__webpack_exports__,__webpack_require__){__webpack_require__.r(__webpack_exports__);__webpack_require__.d(__webpack_exports__,{Button:()=>Button,foo:()=>foo});var react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0__=__webpack_require__(491);/*! Legal Comment */const foo=()=>{};const Button=()=>/*#__PURE__*/(0,react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0__.jsx)("button",{})}}]);"
77+
"/*! For license information please see __federation_expose_default_export.8ba7966f.js.LICENSE.txt */
78+
"use strict";(globalThis["default_minify"]=globalThis["default_minify"]||[]).push([["525"],{510:function(__unused_webpack_module,__webpack_exports__,__webpack_require__){__webpack_require__.r(__webpack_exports__);__webpack_require__.d(__webpack_exports__,{Button:()=>Button,foo:()=>foo});var react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0__=__webpack_require__(491);/*! Legal Comment */const foo=()=>{};const Button=()=>/*#__PURE__*/(0,react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0__.jsx)("button",{})}}]);"
7979
`);
8080
});
8181

@@ -86,7 +86,7 @@ describe('minify config (mf)', () => {
8686
expect(mfExposeEntry).toMatchInlineSnapshot(`
8787
""use strict";
8888
(globalThis["disable_minify"] = globalThis["disable_minify"] || []).push([["525"], {
89-
807: (function (__unused_webpack_module, __webpack_exports__, __webpack_require__) {
89+
510: (function (__unused_webpack_module, __webpack_exports__, __webpack_require__) {
9090
__webpack_require__.r(__webpack_exports__);
9191
__webpack_require__.d(__webpack_exports__, {
9292
Button: () => (Button),

0 commit comments

Comments
 (0)