Skip to content

Commit

Permalink
feat: modify merge config order (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
Timeless0911 authored Aug 28, 2024
1 parent 417440e commit 4b5f674
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 59 deletions.
14 changes: 10 additions & 4 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,17 @@ export async function composeCreateRsbuildConfig(

return {
format: libConfig.format!,
// The merge order represents the priority of the configuration
// The priorities from high to low are as follows:
// 1 - userConfig: users can configure any Rsbuild and Rspack config
// 2 - libRsbuildConfig: the configuration that we compose from Rslib unique config and userConfig from 1
// 3 - internalRsbuildConfig: the built-in best practice Rsbuild configuration we provide in Rslib
// We should state in the document that the built-in configuration should not be changed optionally
// In compose process of 2, we may read some config from 1, and reassemble the related config,
// so before final mergeRsbuildConfig, we reset some specified fields
config: mergeRsbuildConfig(
internalRsbuildConfig,
libRsbuildConfig,
omitDeep(userConfig, [
'bundle',
'format',
Expand All @@ -669,10 +679,6 @@ export async function composeCreateRsbuildConfig(
'syntax',
'dts',
]),
libRsbuildConfig,
// Merge order matters, keep `internalRsbuildConfig` at the last position
// to ensure that the internal config is not overridden by user's config.
internalRsbuildConfig,
),
};
});
Expand Down
138 changes: 84 additions & 54 deletions packages/core/tests/__snapshots__/config.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,40 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
"tools": {
"htmlPlugin": false,
"rspack": [
{
"experiments": {
"rspackFuture": {
"bundlerInfo": {
"force": false,
},
},
},
"optimization": {
"moduleIds": "named",
},
"resolve": {
"extensionAlias": {
".cjs": [
".cts",
".cjs",
],
".js": [
".ts",
".tsx",
".js",
".jsx",
],
".jsx": [
".tsx",
".jsx",
],
".mjs": [
".mts",
".mjs",
],
},
},
},
{
"experiments": {
"outputModule": true,
Expand Down Expand Up @@ -89,35 +123,11 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
],
},
{
"experiments": {
"rspackFuture": {
"bundlerInfo": {
"force": false,
},
},
},
"optimization": {
"moduleIds": "named",
},
"resolve": {
"extensionAlias": {
".cjs": [
".cts",
".cjs",
],
".js": [
".ts",
".tsx",
".js",
".jsx",
],
".jsx": [
".tsx",
".jsx",
],
".mjs": [
".mts",
".mjs",
],
},
},
Expand Down Expand Up @@ -185,22 +195,6 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
"tools": {
"htmlPlugin": false,
"rspack": [
{
"externalsType": "commonjs",
"output": {
"chunkFormat": "commonjs",
"iife": false,
"library": {
"type": "commonjs",
},
},
},
[Function],
{
"target": [
"web",
],
},
{
"experiments": {
"rspackFuture": {
Expand Down Expand Up @@ -235,6 +229,32 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
},
},
},
{
"externalsType": "commonjs",
"output": {
"chunkFormat": "commonjs",
"iife": false,
"library": {
"type": "commonjs",
},
},
},
[Function],
{
"target": [
"web",
],
},
{
"resolve": {
"extensionAlias": {
".js": [
".ts",
".tsx",
],
},
},
},
],
},
},
Expand Down Expand Up @@ -294,20 +314,6 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
"tools": {
"htmlPlugin": false,
"rspack": [
{
"externalsType": "umd",
"output": {
"library": {
"type": "umd",
},
},
},
[Function],
{
"target": [
"web",
],
},
{
"experiments": {
"rspackFuture": {
Expand Down Expand Up @@ -342,6 +348,30 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
},
},
},
{
"externalsType": "umd",
"output": {
"library": {
"type": "umd",
},
},
},
[Function],
{
"target": [
"web",
],
},
{
"resolve": {
"extensionAlias": {
".js": [
".ts",
".tsx",
],
},
},
},
],
},
},
Expand Down
10 changes: 9 additions & 1 deletion packages/core/tests/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,15 @@ describe('Should compose create Rsbuild config correctly', () => {
},
output: {
filenameHash: false,
minify: true,
},
tools: {
rspack: {
resolve: {
extensionAlias: {
'.js': ['.ts', '.tsx'],
},
},
},
},
};
const composedRsbuildConfig = await composeCreateRsbuildConfig(
Expand Down

0 comments on commit 4b5f674

Please sign in to comment.