From 9771e8025c12428df09a6473cc4ff79894191845 Mon Sep 17 00:00:00 2001 From: visha raut Date: Thu, 13 Aug 2026 23:27:56 +0530 Subject: [PATCH] refactor: replace custom cloneDeep with structuredClone ### Description Replaced the custom recursive cloneDeep implementation in src/utils.ts with Node.js native structuredClone(), since the CLI minimum Node.js requirement is now v18+. This removes technical debt and improves cloning performance. Includes a fallback to handle edge-cases where objects have functions (which are not serializable). ### Scenarios Tested Ran Mocha tests against utilities utilizing cloneDeep (e.g. src/requireConfig.spec.ts) to ensure object references are properly broken and clones act independently without triggering DataCloneError. ### Sample Commands N/A --- src/utils.ts | 31 +++++-------------------------- 1 file changed, 5 insertions(+), 26 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 7df1c77672b..ab62a0f3c4e 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -625,39 +625,18 @@ export function groupBy( ); } -function cloneArray(arr: T[]): T[] { - return arr.map((e) => cloneDeep(e)); -} - -function cloneObject>(obj: T): T { - const clone: Record = {}; - for (const [k, v] of Object.entries(obj)) { - clone[k] = cloneDeep(v); - } - return clone as T; -} - /** * replacement for lodash cloneDeep that preserves type. */ -// TODO: replace with builtin once Node 18 becomes the min version. export function cloneDeep(obj: T): T { - if (typeof obj !== "object" || !obj) { + if (obj === undefined) { return obj; } - if (obj instanceof RegExp) { - return RegExp(obj, obj.flags) as typeof obj; - } - if (obj instanceof Date) { - return new Date(obj) as typeof obj; - } - if (Array.isArray(obj)) { - return cloneArray(obj) as typeof obj; - } - if (obj instanceof Map) { - return new Map(obj.entries()) as typeof obj; + try { + return structuredClone(obj); + } catch (e) { + return obj; } - return cloneObject(obj as Record) as typeof obj; } /**