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

fix: rspack plugin apply error after merging config #2877

Merged
merged 2 commits into from
Jul 11, 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
29 changes: 25 additions & 4 deletions packages/core/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,29 @@ export const isFunction = (func: unknown): func is (...args: any[]) => any =>
typeof func === 'function';

export const isObject = (obj: unknown): obj is Record<string, any> =>
obj !== null && typeof obj === 'object';
Object.prototype.toString.call(obj) === '[object Object]';

export const isPlainObject = (obj: unknown): obj is Record<string, any> =>
isObject(obj) && Object.prototype.toString.call(obj) === '[object Object]';
export const isPlainObject = (o: unknown): o is Record<string, any> => {
if (isObject(o) === false) return false;

// If has modified constructor
const ctor = (o as Record<string, any>).constructor;
if (ctor === undefined) return true;

// If has modified prototype
const prot = ctor.prototype;
if (isObject(prot) === false) return false;

// If constructor does not have an Object-specific method

// biome-ignore lint/suspicious/noPrototypeBuiltins: <explanation>
if (prot.hasOwnProperty('isPrototypeOf') === false) {
return false;
}

// Most likely a plain Object
return true;
};

export const castArray = <T>(arr?: T | T[]): T[] => {
if (arr === undefined) {
Expand All @@ -55,7 +74,9 @@ export const cloneDeep = <T>(value: T): T => {
if (value === null || value === undefined) {
return value;
}
return deepmerge<T>({}, value);
return deepmerge<T>({}, value, {
isMergeableObject: isPlainObject,
});
};

const compareSemver = (version1: string, version2: string) => {
Expand Down
33 changes: 33 additions & 0 deletions packages/core/tests/mergeConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,39 @@ describe('mergeRsbuildConfig', () => {
});
});

it('should merge rspack plugins as expected', () => {
class A {
a = 1;

apply() {
return this.a;
}
}

const pluginA = new A();

const mergedConfig = mergeRsbuildConfig(
{
tools: {
rspack: {
plugins: [pluginA],
},
},
},
{},
);

expect(mergedConfig).toEqual({
tools: {
rspack: {
plugins: [pluginA],
},
},
});

expect(mergedConfig.tools!.rspack.plugins[0] instanceof A).toBeTruthy();
});

test('should merge overrideBrowserslist in environments as expected', async () => {
expect(
mergeRsbuildConfig(
Expand Down
1 change: 1 addition & 0 deletions scripts/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,4 @@ vnode
watchpack
webm
webp
Mergeable
Loading