Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/core/src/defaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,11 @@ export const normalizeConfig = (
config.server.publicDir,
);

const defaultConfig = createDefaultConfig();
defaultConfig.mode = getMode();

const mergedConfig = mergeRsbuildConfig(
{
...createDefaultConfig(),
mode: getMode(),
},
defaultConfig,
config,
) as NormalizedConfig;

Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ export const isObject = (obj: unknown): obj is Record<string, any> =>

// Cache Object.prototype reference for better performance in hot paths
const objectPrototype = Object.prototype;
const getProto = Object.getPrototypeOf;

export const isPlainObject = (obj: unknown): obj is Record<string, any> => {
return (
obj !== null &&
typeof obj === 'object' &&
Object.getPrototypeOf(obj) === objectPrototype
obj !== null && typeof obj === 'object' && getProto(obj) === objectPrototype
);
};

Expand Down
31 changes: 19 additions & 12 deletions packages/core/src/mergeConfig.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { castArray, cloneDeep, isFunction, isPlainObject } from './helpers';
import { cloneDeep, isPlainObject } from './helpers';
import type { RsbuildConfig } from './types';

const OVERRIDE_PATHS = [
const OVERRIDE_PATHS = new Set([
'performance.removeConsole',
'output.inlineScripts',
'output.inlineStyles',
Expand All @@ -13,7 +13,7 @@ const OVERRIDE_PATHS = [
'resolve.conditionNames',
'resolve.mainFields',
'provider',
];
]);

/**
* When merging configs, some properties prefer `override` over `merge to array`
Expand All @@ -22,10 +22,10 @@ const isOverridePath = (key: string) => {
// ignore environments name prefix, such as `environments.web`
if (key.startsWith('environments.')) {
const realKey = key.split('.').slice(2).join('.');
return OVERRIDE_PATHS.includes(realKey);
return OVERRIDE_PATHS.has(realKey);
}
return (
OVERRIDE_PATHS.includes(key) ||
OVERRIDE_PATHS.has(key) ||
// output.filename.* supports function but we should not merge them as array
key.startsWith('output.filename.')
);
Expand All @@ -45,21 +45,28 @@ const merge = (x: unknown, y: unknown, path = ''): unknown => {
return isPlainObject(x) ? cloneDeep(x) : x;
}

const typeX = typeof x;
const typeY = typeof y;

// no need to merge boolean with object or function, such as `tools.htmlPlugin`
if (typeof x === 'boolean' || typeof y === 'boolean') {
if (typeX === 'boolean' || typeY === 'boolean') {
return y;
}

const pair = [x, y];

// combine array
if (pair.some(Array.isArray)) {
return [...castArray(x), ...castArray(y)];
const isArrayX = Array.isArray(x);
const isArrayY = Array.isArray(y);
if (isArrayX && isArrayY) {
return x.concat(y);
} else if (isArrayX) {
return [...x, y];
} else if (isArrayY) {
return [x, ...y];
}

// convert function to chained function
if (pair.some(isFunction)) {
return pair;
if (typeX === 'function' || typeY === 'function') {
return [x, y];
}

if (!isPlainObject(x) || !isPlainObject(y)) {
Expand Down
Loading