Skip to content

Commit 5c8a73b

Browse files
committed
fix: the moduleIds should be deterministic if the env is production and format is mf
1 parent c0226b9 commit 5c8a73b

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

.changeset/smooth-parents-pull.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@rslib/core': patch
3+
---
4+
5+
fix: the moduleIds should be deterministic if the env is production and format is mf

packages/core/src/config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import {
5353
calcLongestCommonPath,
5454
checkMFPlugin,
5555
color,
56+
composeMFPlugin,
5657
isEmptyObject,
5758
isObject,
5859
nodeBuiltInModules,
@@ -1077,6 +1078,7 @@ const composeExternalHelpersConfig = (
10771078

10781079
async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
10791080
checkMFPlugin(config);
1081+
const mfConfig = composeMFPlugin(config);
10801082
const rootPath = dirname(configPath);
10811083
const pkgJson = readPackageJson(rootPath);
10821084
const { compilerOptions } = await loadTsconfig(
@@ -1160,6 +1162,7 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
11601162
);
11611163

11621164
return mergeRsbuildConfig(
1165+
mfConfig,
11631166
formatConfig,
11641167
shimsConfig,
11651168
externalHelpersConfig,

packages/core/src/utils/helper.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from 'node:fs';
22
import fsP from 'node:fs/promises';
33
import path from 'node:path';
4-
import type { RsbuildPlugins } from '@rsbuild/core';
4+
import type { RsbuildConfig, RsbuildPlugins } from '@rsbuild/core';
55
import color from 'picocolors';
66

77
import type { LibConfig, PkgJson } from '../types';
@@ -201,4 +201,20 @@ export function checkMFPlugin(config: LibConfig): boolean {
201201
return added;
202202
}
203203

204+
export function composeMFPlugin(config: LibConfig): RsbuildConfig {
205+
if (config.format !== 'mf' || process.env.NODE_ENV !== 'production') {
206+
return {};
207+
}
208+
209+
return {
210+
tools: {
211+
rspack: {
212+
optimization: {
213+
moduleIds: 'deterministic',
214+
},
215+
},
216+
},
217+
};
218+
}
219+
204220
export { color };

tests/integration/minify/index.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,31 @@ test('minify is disabled by the user, bar and baz should not be minified', async
5959
}]);"
6060
`);
6161
});
62+
63+
test('enable minify and set NODE_ENV to production, the moduleIds should be the deterministic', async () => {
64+
const fixturePath = join(__dirname, 'mf/config');
65+
const prevNodeEnv = process.env.NODE_ENV;
66+
process.env.NODE_ENV = 'production';
67+
const { mfExposeEntry } = await buildAndGetResults({ fixturePath });
68+
expect(mfExposeEntry).toMatchInlineSnapshot(`
69+
""use strict";
70+
(globalThis['disable_minify'] = globalThis['disable_minify'] || []).push([["249"], {
71+
"163": (function (__unused_webpack_module, __webpack_exports__, __webpack_require__) {
72+
__webpack_require__.r(__webpack_exports__);
73+
__webpack_require__.d(__webpack_exports__, {
74+
foo: function() { return foo; }
75+
});
76+
const foo = ()=>{};
77+
const bar = ()=>{};
78+
const baz = ()=>{
79+
return bar();
80+
};
81+
82+
83+
}),
84+
85+
}]);"
86+
`);
87+
88+
process.env.NODE_ENV = prevNodeEnv;
89+
});

0 commit comments

Comments
 (0)