Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support for merging environments config #2700

Merged
merged 2 commits into from
Jun 26, 2024
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
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'],
},
},
},
});
});
});
Loading