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
54 changes: 54 additions & 0 deletions e2e/cases/output/copy/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'node:fs';
import { join } from 'node:path';
import { build } from '@e2e/helper';
import { expect, test } from '@playwright/test';
import type { RsbuildPlugin } from '@rsbuild/core';

test('should copy asset to dist folder correctly', async () => {
await build({
Expand Down Expand Up @@ -49,3 +50,56 @@ test('should copy asset to dist sub-folder correctly', async () => {

expect(fs.existsSync(join(__dirname, 'dist-1/foo/icon.png'))).toBeTruthy();
});

test('should merge copy config correctly', async () => {
const rsbuildPlugin = (): RsbuildPlugin => ({
name: 'example',
setup(api) {
api.modifyRsbuildConfig((config, { mergeRsbuildConfig }) => {
return mergeRsbuildConfig(config, {
output: {
copy: {
patterns: [
{
from: '../../../assets/icon.png',
},
],
},
},
});
});
},
});

const rsbuildPlugin2 = (): RsbuildPlugin => ({
name: 'example2',
setup(api) {
api.modifyRsbuildConfig((config, { mergeRsbuildConfig }) => {
return mergeRsbuildConfig(config, {
output: {
copy: [
{
from: '../../../assets/image.png',
},
],
},
});
});
},
});

await build({
cwd: __dirname,
rsbuildConfig: {
output: {
distPath: {
root: 'dist-4',
},
},
plugins: [rsbuildPlugin(), rsbuildPlugin2()],
},
});

expect(fs.existsSync(join(__dirname, 'dist-4/icon.png'))).toBeTruthy();
expect(fs.existsSync(join(__dirname, 'dist-4/image.png'))).toBeTruthy();
});
8 changes: 7 additions & 1 deletion packages/core/src/mergeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const isOverridePath = (key: string) => {
return OVERRIDE_PATHS.includes(key);
};

const merge = (x: unknown, y: unknown, path = '') => {
const merge = (x: unknown, y: unknown, path = ''): unknown => {
// force some keys to override
if (isOverridePath(path)) {
return y ?? x;
Expand All @@ -48,6 +48,12 @@ const merge = (x: unknown, y: unknown, path = '') => {

// combine array
if (pair.some(Array.isArray)) {
if (path === 'output.copy' && !pair.every(Array.isArray)) {
// x: { patterns: [A] }、y: [B, C] => { patterns: [A,B,C] }
return Array.isArray(x)
? merge({ patterns: x }, y, path)
: merge(x, { patterns: y }, path);
}
return [...castArray(x), ...castArray(y)];
}

Expand Down
Loading