From fce387108f70a2181629ad50220a34dc34367954 Mon Sep 17 00:00:00 2001 From: "zhanghang.heal" Date: Thu, 28 Nov 2024 10:48:37 +0800 Subject: [PATCH] fix: the moduleIds should be deterministic if the env is production and format is mf --- .changeset/smooth-parents-pull.md | 5 ++++ packages/core/src/cli/mf.ts | 1 + packages/core/src/config.ts | 2 +- scripts/dictionary.txt | 1 + tests/integration/minify/index.test.ts | 33 +++++++++++++------------- 5 files changed, 24 insertions(+), 18 deletions(-) create mode 100644 .changeset/smooth-parents-pull.md diff --git a/.changeset/smooth-parents-pull.md b/.changeset/smooth-parents-pull.md new file mode 100644 index 000000000..be7919269 --- /dev/null +++ b/.changeset/smooth-parents-pull.md @@ -0,0 +1,5 @@ +--- +'@rslib/core': patch +--- + +fix: the moduleIds should be deterministic if the env is production and format is mf diff --git a/packages/core/src/cli/mf.ts b/packages/core/src/cli/mf.ts index 352f89f01..9c96d6235 100644 --- a/packages/core/src/cli/mf.ts +++ b/packages/core/src/cli/mf.ts @@ -41,6 +41,7 @@ function changeEnvToDev(rsbuildConfig: RsbuildConfig) { rspack: { optimization: { nodeEnv: 'development', + moduleIds: 'named', }, }, }, diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index 010d7bdd8..df4916ffe 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -594,9 +594,9 @@ const composeFormatConfig = ({ }, // can not set nodeEnv to false, because mf format should build shared module. // If nodeEnv is false, the process.env.NODE_ENV in third-party packages's will not be replaced - // now we have not provide dev mode for users, so we can always set nodeEnv as 'production' optimization: { nodeEnv: 'production', + moduleIds: 'deterministic', }, }, }, diff --git a/scripts/dictionary.txt b/scripts/dictionary.txt index 00180decb..72e884d9b 100644 --- a/scripts/dictionary.txt +++ b/scripts/dictionary.txt @@ -141,3 +141,4 @@ vnode watchpack webm webp +unstubAllEnvs \ No newline at end of file diff --git a/tests/integration/minify/index.test.ts b/tests/integration/minify/index.test.ts index 1df0d410a..a8abc26dd 100644 --- a/tests/integration/minify/index.test.ts +++ b/tests/integration/minify/index.test.ts @@ -33,29 +33,28 @@ test('minify is enabled by default in mf format, bar and baz should be minified' const fixturePath = join(__dirname, 'mf/default'); const { mfExposeEntry } = await buildAndGetResults({ fixturePath }); // biome-ignore format: snapshot - expect(mfExposeEntry).toMatchInlineSnapshot(`""use strict";(globalThis["default_minify"]=globalThis["default_minify"]||[]).push([["249"],{"../../__fixtures__/src/index.ts":function(__unused_webpack_module,__webpack_exports__,__webpack_require__){__webpack_require__.r(__webpack_exports__);__webpack_require__.d(__webpack_exports__,{foo:function(){return foo}});const foo=()=>{}}}]);"`); + expect(mfExposeEntry).toMatchInlineSnapshot(`""use strict";(globalThis["default_minify"]=globalThis["default_minify"]||[]).push([["249"],{163:function(__unused_webpack_module,__webpack_exports__,__webpack_require__){__webpack_require__.r(__webpack_exports__);__webpack_require__.d(__webpack_exports__,{foo:function(){return foo}});const foo=()=>{}}}]);"`); }); test('minify is disabled by the user, bar and baz should not be minified', async () => { const fixturePath = join(__dirname, 'mf/config'); const { mfExposeEntry } = await buildAndGetResults({ fixturePath }); - expect(mfExposeEntry).toMatchInlineSnapshot(` - ""use strict"; - (globalThis['disable_minify'] = globalThis['disable_minify'] || []).push([["249"], { - "../../__fixtures__/src/index.ts": (function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { - __webpack_require__.r(__webpack_exports__); - __webpack_require__.d(__webpack_exports__, { - foo: function() { return foo; } - }); - const foo = ()=>{}; - const bar = ()=>{}; - const baz = ()=>{ - return bar(); - }; + + expect(mfExposeEntry).toMatchInlineSnapshot(`""use strict"; +(globalThis['disable_minify'] = globalThis['disable_minify'] || []).push([["249"], { +"163": (function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { +__webpack_require__.r(__webpack_exports__); +__webpack_require__.d(__webpack_exports__, { + foo: function() { return foo; } +}); +const foo = ()=>{}; +const bar = ()=>{}; +const baz = ()=>{ + return bar(); +}; - }), +}), - }]);" - `); +}]);"`); });