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
5 changes: 5 additions & 0 deletions .changeset/mean-frogs-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lynx-js/rspeedy": patch
---

Add `mergeRspeedyConfig` function for merging multiple Rspeedy configuration object.
3 changes: 3 additions & 0 deletions packages/rspeedy/core/etc/rspeedy.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ export interface LoadConfigResult {

export { logger }

// @public
export function mergeRspeedyConfig(...configs: Config[]): Config;

// @public
export interface Minify {
css?: boolean | undefined;
Expand Down
47 changes: 47 additions & 0 deletions packages/rspeedy/core/src/config/mergeRspeedyConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2024 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.

import { mergeRsbuildConfig } from '@rsbuild/core'

import type { Config } from './index.js'

/**
* Merge multiple Rspeedy configuration objects.
*
* @param configs - The Rspeedy configuration objects to merge.
*
* @returns The merged Rspeedy configuration object.
*
* @example
*
* ```ts
* import { mergeRspeedyConfig } from '@lynx-js/rspeedy';
*
* const config1 = {
* dev: {
* writeToDisk: false,
* },
* };
* const config2 = {
* dev: {
* writeToDisk: true,
* },
* };
*
* const mergedConfig = mergeRspeedyConfig(config1, config2);
*
* console.log(mergedConfig); // { dev: { writeToDisk: true } }
* ```
*
* @remarks
*
* This is actually an alias of {@link https://rsbuild.dev/api/javascript-api/core#mergersbuildconfig | mergeRsbuildConfig}.
*
* @public
*/
export function mergeRspeedyConfig(
...configs: Config[]
): Config {
return mergeRsbuildConfig(...configs)
}
1 change: 1 addition & 0 deletions packages/rspeedy/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export {
type CreateRspeedyOptions,
} from './create-rspeedy.js'
export { logger } from '@rsbuild/core'
export { mergeRspeedyConfig } from './config/mergeRspeedyConfig.js'

// Config
export { defineConfig } from './config/defineConfig.js'
Expand Down
78 changes: 78 additions & 0 deletions packages/rspeedy/core/test/config/mergeRspeedyConfig.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2024 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.
import { describe, expect, it } from 'vitest'

import { mergeRspeedyConfig } from '../../src/config/mergeRspeedyConfig.js'

describe('mergeRspeedyConfig', () => {
it('should override Rsbuild config', () => {
const config = mergeRspeedyConfig({
source: { entry: './src/index.tsx' },
}, {
source: { entry: './src/index2.tsx' },
})

expect(config).toEqual({
source: { entry: './src/index2.tsx' },
})
})

it('should merge Rsbuild config array', () => {
const config = mergeRspeedyConfig({
source: { entry: ['./src/index.tsx'] },
}, {
source: { entry: ['./src/index2.tsx'] },
})

expect(config).toEqual({
source: { entry: ['./src/index.tsx', './src/index2.tsx'] },
})
})

it('should merge Rsbuild config object', () => {
const config = mergeRspeedyConfig({
source: { define: { FOO: 'true' } },
}, {
source: { define: { BAR: 'false' } },
})

expect(config).toEqual({
source: { define: { FOO: 'true', BAR: 'false' } },
})
})

it('should merge Rsbuild config object and array', () => {
const config = mergeRspeedyConfig({
source: { entry: ['./src/index.tsx'] },
}, {
source: { define: { BAR: 'false' } },
})

expect(config).toEqual({
source: { entry: ['./src/index.tsx'], define: { BAR: 'false' } },
})
})

it('should merge Rspeedy custom config', () => {
const config = mergeRspeedyConfig({
output: {
filename: 'foo.bundle',
},
}, {
output: {
filename: {
bundle: 'bundle2.js',
},
},
})

expect(config).toEqual({
output: {
filename: {
bundle: 'bundle2.js',
},
},
})
})
})
Loading