From b584fbab5b403e945562d9a6e9271b0b4901166e Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Thu, 11 Jul 2024 14:28:41 +0800 Subject: [PATCH 1/2] fix: rspack plugin apply error --- packages/core/src/helpers.ts | 29 +++++++++++++++++++--- packages/core/tests/mergeConfig.test.ts | 33 +++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/packages/core/src/helpers.ts b/packages/core/src/helpers.ts index b857cc0d25..1eecf3d4f4 100644 --- a/packages/core/src/helpers.ts +++ b/packages/core/src/helpers.ts @@ -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 => - obj !== null && typeof obj === 'object'; + Object.prototype.toString.call(obj) === '[object Object]'; -export const isPlainObject = (obj: unknown): obj is Record => - isObject(obj) && Object.prototype.toString.call(obj) === '[object Object]'; +export const isPlainObject = (o: unknown): o is Record => { + if (isObject(o) === false) return false; + + // If has modified constructor + const ctor = (o as Record).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: + if (prot.hasOwnProperty('isPrototypeOf') === false) { + return false; + } + + // Most likely a plain Object + return true; +}; export const castArray = (arr?: T | T[]): T[] => { if (arr === undefined) { @@ -55,7 +74,9 @@ export const cloneDeep = (value: T): T => { if (value === null || value === undefined) { return value; } - return deepmerge({}, value); + return deepmerge({}, value, { + isMergeableObject: isPlainObject, + }); }; const compareSemver = (version1: string, version2: string) => { diff --git a/packages/core/tests/mergeConfig.test.ts b/packages/core/tests/mergeConfig.test.ts index 9833cc12e8..8abcbcc972 100644 --- a/packages/core/tests/mergeConfig.test.ts +++ b/packages/core/tests/mergeConfig.test.ts @@ -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( From 0537e04603b2088d45658f1193e813dfa34092b9 Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Thu, 11 Jul 2024 14:43:22 +0800 Subject: [PATCH 2/2] fix: dictionary --- scripts/dictionary.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/dictionary.txt b/scripts/dictionary.txt index 4ccc989ab5..9baf431ac6 100644 --- a/scripts/dictionary.txt +++ b/scripts/dictionary.txt @@ -118,3 +118,4 @@ vnode watchpack webm webp +Mergeable