Skip to content

Commit 4b5f674

Browse files
authored
feat: modify merge config order (#135)
1 parent 417440e commit 4b5f674

File tree

3 files changed

+103
-59
lines changed

3 files changed

+103
-59
lines changed

packages/core/src/config.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,17 @@ export async function composeCreateRsbuildConfig(
660660

661661
return {
662662
format: libConfig.format!,
663+
// The merge order represents the priority of the configuration
664+
// The priorities from high to low are as follows:
665+
// 1 - userConfig: users can configure any Rsbuild and Rspack config
666+
// 2 - libRsbuildConfig: the configuration that we compose from Rslib unique config and userConfig from 1
667+
// 3 - internalRsbuildConfig: the built-in best practice Rsbuild configuration we provide in Rslib
668+
// We should state in the document that the built-in configuration should not be changed optionally
669+
// In compose process of 2, we may read some config from 1, and reassemble the related config,
670+
// so before final mergeRsbuildConfig, we reset some specified fields
663671
config: mergeRsbuildConfig(
672+
internalRsbuildConfig,
673+
libRsbuildConfig,
664674
omitDeep(userConfig, [
665675
'bundle',
666676
'format',
@@ -669,10 +679,6 @@ export async function composeCreateRsbuildConfig(
669679
'syntax',
670680
'dts',
671681
]),
672-
libRsbuildConfig,
673-
// Merge order matters, keep `internalRsbuildConfig` at the last position
674-
// to ensure that the internal config is not overridden by user's config.
675-
internalRsbuildConfig,
676682
),
677683
};
678684
});

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

Lines changed: 84 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,40 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
5959
"tools": {
6060
"htmlPlugin": false,
6161
"rspack": [
62+
{
63+
"experiments": {
64+
"rspackFuture": {
65+
"bundlerInfo": {
66+
"force": false,
67+
},
68+
},
69+
},
70+
"optimization": {
71+
"moduleIds": "named",
72+
},
73+
"resolve": {
74+
"extensionAlias": {
75+
".cjs": [
76+
".cts",
77+
".cjs",
78+
],
79+
".js": [
80+
".ts",
81+
".tsx",
82+
".js",
83+
".jsx",
84+
],
85+
".jsx": [
86+
".tsx",
87+
".jsx",
88+
],
89+
".mjs": [
90+
".mts",
91+
".mjs",
92+
],
93+
},
94+
},
95+
},
6296
{
6397
"experiments": {
6498
"outputModule": true,
@@ -89,35 +123,11 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
89123
],
90124
},
91125
{
92-
"experiments": {
93-
"rspackFuture": {
94-
"bundlerInfo": {
95-
"force": false,
96-
},
97-
},
98-
},
99-
"optimization": {
100-
"moduleIds": "named",
101-
},
102126
"resolve": {
103127
"extensionAlias": {
104-
".cjs": [
105-
".cts",
106-
".cjs",
107-
],
108128
".js": [
109129
".ts",
110130
".tsx",
111-
".js",
112-
".jsx",
113-
],
114-
".jsx": [
115-
".tsx",
116-
".jsx",
117-
],
118-
".mjs": [
119-
".mts",
120-
".mjs",
121131
],
122132
},
123133
},
@@ -185,22 +195,6 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
185195
"tools": {
186196
"htmlPlugin": false,
187197
"rspack": [
188-
{
189-
"externalsType": "commonjs",
190-
"output": {
191-
"chunkFormat": "commonjs",
192-
"iife": false,
193-
"library": {
194-
"type": "commonjs",
195-
},
196-
},
197-
},
198-
[Function],
199-
{
200-
"target": [
201-
"web",
202-
],
203-
},
204198
{
205199
"experiments": {
206200
"rspackFuture": {
@@ -235,6 +229,32 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
235229
},
236230
},
237231
},
232+
{
233+
"externalsType": "commonjs",
234+
"output": {
235+
"chunkFormat": "commonjs",
236+
"iife": false,
237+
"library": {
238+
"type": "commonjs",
239+
},
240+
},
241+
},
242+
[Function],
243+
{
244+
"target": [
245+
"web",
246+
],
247+
},
248+
{
249+
"resolve": {
250+
"extensionAlias": {
251+
".js": [
252+
".ts",
253+
".tsx",
254+
],
255+
},
256+
},
257+
},
238258
],
239259
},
240260
},
@@ -294,20 +314,6 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
294314
"tools": {
295315
"htmlPlugin": false,
296316
"rspack": [
297-
{
298-
"externalsType": "umd",
299-
"output": {
300-
"library": {
301-
"type": "umd",
302-
},
303-
},
304-
},
305-
[Function],
306-
{
307-
"target": [
308-
"web",
309-
],
310-
},
311317
{
312318
"experiments": {
313319
"rspackFuture": {
@@ -342,6 +348,30 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
342348
},
343349
},
344350
},
351+
{
352+
"externalsType": "umd",
353+
"output": {
354+
"library": {
355+
"type": "umd",
356+
},
357+
},
358+
},
359+
[Function],
360+
{
361+
"target": [
362+
"web",
363+
],
364+
},
365+
{
366+
"resolve": {
367+
"extensionAlias": {
368+
".js": [
369+
".ts",
370+
".tsx",
371+
],
372+
},
373+
},
374+
},
345375
],
346376
},
347377
},

packages/core/tests/config.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,15 @@ describe('Should compose create Rsbuild config correctly', () => {
178178
},
179179
output: {
180180
filenameHash: false,
181-
minify: true,
181+
},
182+
tools: {
183+
rspack: {
184+
resolve: {
185+
extensionAlias: {
186+
'.js': ['.ts', '.tsx'],
187+
},
188+
},
189+
},
182190
},
183191
};
184192
const composedRsbuildConfig = await composeCreateRsbuildConfig(

0 commit comments

Comments
 (0)