Skip to content

Commit 2925523

Browse files
committed
chore: set moduleIds in config
1 parent 5c8a73b commit 2925523

File tree

3 files changed

+14
-50
lines changed

3 files changed

+14
-50
lines changed

packages/core/src/config.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ import {
5353
calcLongestCommonPath,
5454
checkMFPlugin,
5555
color,
56-
composeMFPlugin,
5756
isEmptyObject,
5857
isObject,
5958
nodeBuiltInModules,
@@ -589,9 +588,12 @@ const composeFormatConfig = ({
589588
},
590589
// can not set nodeEnv to false, because mf format should build shared module.
591590
// If nodeEnv is false, the process.env.NODE_ENV in third-party packages's will not be replaced
592-
// now we have not provide dev mode for users, so we can always set nodeEnv as 'production'
593591
optimization: {
594592
nodeEnv: 'production',
593+
moduleIds:
594+
process.env.NODE_ENV === 'production'
595+
? 'deterministic'
596+
: 'named',
595597
},
596598
},
597599
},
@@ -1078,7 +1080,6 @@ const composeExternalHelpersConfig = (
10781080

10791081
async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
10801082
checkMFPlugin(config);
1081-
const mfConfig = composeMFPlugin(config);
10821083
const rootPath = dirname(configPath);
10831084
const pkgJson = readPackageJson(rootPath);
10841085
const { compilerOptions } = await loadTsconfig(
@@ -1162,7 +1163,6 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
11621163
);
11631164

11641165
return mergeRsbuildConfig(
1165-
mfConfig,
11661166
formatConfig,
11671167
shimsConfig,
11681168
externalHelpersConfig,

packages/core/src/utils/helper.ts

Lines changed: 1 addition & 17 deletions
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 { RsbuildConfig, RsbuildPlugins } from '@rsbuild/core';
4+
import type { RsbuildPlugins } from '@rsbuild/core';
55
import color from 'picocolors';
66

77
import type { LibConfig, PkgJson } from '../types';
@@ -201,20 +201,4 @@ 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-
220204
export { color };

tests/integration/minify/index.test.ts

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { join } from 'node:path';
2+
import { afterEach, beforeEach, describe } from 'node:test';
23
import { buildAndGetResults } from 'test-helper';
3-
import { expect, test } from 'vitest';
4+
import { afterAll, beforeAll, expect, test, vi } from 'vitest';
45

56
test('tree shaking is enabled by default, bar and baz should be shaken', async () => {
67
const fixturePath = join(__dirname, 'default');
@@ -31,40 +32,21 @@ test('tree shaking is disabled by the user, bar and baz should be kept', async (
3132

3233
test('minify is enabled by default in mf format, bar and baz should be minified', async () => {
3334
const fixturePath = join(__dirname, 'mf/default');
35+
const nodeEnv = process.env.NODE_ENV;
36+
process.env.NODE_ENV = 'production';
3437
const { mfExposeEntry } = await buildAndGetResults({ fixturePath });
38+
process.env.NODE_ENV = nodeEnv;
3539
// biome-ignore format: snapshot
36-
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=()=>{}}}]);"`);
40+
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=()=>{}}}]);"`);
3741
});
3842

3943
test('minify is disabled by the user, bar and baz should not be minified', async () => {
4044
const fixturePath = join(__dirname, 'mf/config');
41-
const { mfExposeEntry } = await buildAndGetResults({ fixturePath });
42-
expect(mfExposeEntry).toMatchInlineSnapshot(`
43-
""use strict";
44-
(globalThis['disable_minify'] = globalThis['disable_minify'] || []).push([["249"], {
45-
"../../__fixtures__/src/index.ts": (function (__unused_webpack_module, __webpack_exports__, __webpack_require__) {
46-
__webpack_require__.r(__webpack_exports__);
47-
__webpack_require__.d(__webpack_exports__, {
48-
foo: function() { return foo; }
49-
});
50-
const foo = ()=>{};
51-
const bar = ()=>{};
52-
const baz = ()=>{
53-
return bar();
54-
};
55-
56-
57-
}),
58-
59-
}]);"
60-
`);
61-
});
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;
45+
const nodeEnv = process.env.NODE_ENV;
6646
process.env.NODE_ENV = 'production';
6747
const { mfExposeEntry } = await buildAndGetResults({ fixturePath });
48+
process.env.NODE_ENV = nodeEnv;
49+
6850
expect(mfExposeEntry).toMatchInlineSnapshot(`
6951
""use strict";
7052
(globalThis['disable_minify'] = globalThis['disable_minify'] || []).push([["249"], {
@@ -84,6 +66,4 @@ test('enable minify and set NODE_ENV to production, the moduleIds should be the
8466
8567
}]);"
8668
`);
87-
88-
process.env.NODE_ENV = prevNodeEnv;
8969
});

0 commit comments

Comments
 (0)