Skip to content

Commit

Permalink
feat: support for merging environments config (#2700)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Jun 26, 2024
1 parent e334255 commit fdad2b6
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 8 deletions.
11 changes: 9 additions & 2 deletions packages/core/src/mergeConfig.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type RsbuildConfig, castArray } from '@rsbuild/shared';
import { isFunction, isPlainObject } from './helpers';

const OVERRIDE_PATH = [
const OVERRIDE_PATHS = [
'performance.removeConsole',
'output.inlineScripts',
'output.inlineStyles',
Expand All @@ -15,7 +15,14 @@ const OVERRIDE_PATH = [
/**
* When merging configs, some properties prefer `override` over `merge to array`
*/
const isOverridePath = (key: string) => OVERRIDE_PATH.includes(key);
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.includes(key);
};

const merge = (x: unknown, y: unknown, path = '') => {
// force some keys to override
Expand Down
71 changes: 65 additions & 6 deletions packages/core/tests/mergeConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,25 @@ describe('mergeRsbuildConfig', () => {
const config = mergeRsbuildConfig(
{ source: { alias: {} } },
{ source: { alias: undefined } },
{ tools: { webpack: noop } },
{ tools: { webpack: undefined } },
{ tools: { rspack: noop } },
{ tools: { rspack: undefined } },
);
expect(config).toEqual({
source: {
alias: {},
},
tools: {
webpack: noop,
rspack: noop,
},
});
});

test('should keep single function value', () => {
const config = mergeRsbuildConfig(
{ tools: { webpack: undefined } },
{ tools: { webpack: () => ({}) } },
{ tools: { rspack: undefined } },
{ tools: { rspack: () => ({}) } },
);
expect(typeof config.tools?.webpack).toEqual('function');
expect(typeof config.tools?.rspack).toEqual('function');
});

test('should merge string and string[] correctly', async () => {
Expand Down Expand Up @@ -222,4 +222,63 @@ describe('mergeRsbuildConfig', () => {
},
});
});

test('should merge overrideBrowserslist in environments as expected', async () => {
expect(
mergeRsbuildConfig(
{
output: {
overrideBrowserslist: ['chrome 50'],
},
environments: {
web: {
output: {
overrideBrowserslist: ['edge 10'],
},
},
node: {
output: {
overrideBrowserslist: ['node 14'],
},
},
},
},
{
output: {
overrideBrowserslist: ['chrome 100'],
},
},
{
environments: {
web: {
output: {
overrideBrowserslist: ['edge 11'],
},
},
node: {
output: {
overrideBrowserslist: ['node 16'],
},
},
},
},
),
).toEqual({
output: {
overrideBrowserslist: ['chrome 100'],
},
environments: {
web: {
output: {
overrideBrowserslist: ['edge 11'],
},
},
node: {
output: {
overrideBrowserslist: ['node 16'],
},
},
},
});
});
});

0 comments on commit fdad2b6

Please sign in to comment.