mirror of
https://github.com/alex8088/electron-vite.git
synced 2025-11-10 06:24:33 +08:00
refactor: replace JSON.parse/stringify with manual deep clone
This commit is contained in:
parent
eb0a7e3ffe
commit
56fb519092
@ -28,7 +28,7 @@ import importMetaPlugin from './plugins/importMeta'
|
||||
import esmShimPlugin from './plugins/esmShim'
|
||||
import modulePathPlugin from './plugins/modulePath'
|
||||
import isolateEntriesPlugin from './plugins/isolateEntries'
|
||||
import { isObject, isFilePathESM } from './utils'
|
||||
import { isObject, isFilePathESM, deepClone } from './utils'
|
||||
|
||||
export { defineConfig as defineViteConfig } from 'vite'
|
||||
|
||||
@ -266,10 +266,6 @@ export async function resolveConfig(
|
||||
return resolved
|
||||
}
|
||||
|
||||
function deepClone<T>(data: T): T {
|
||||
return JSON.parse(JSON.stringify(data))
|
||||
}
|
||||
|
||||
function resetOutDir(config: ViteConfig, outDir: string, subOutDir: string): void {
|
||||
let userOutDir = config.build?.outDir
|
||||
if (outDir === userOutDir) {
|
||||
|
||||
33
src/utils.ts
33
src/utils.ts
@ -1,3 +1,4 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-function-type */
|
||||
import path from 'node:path'
|
||||
import fs from 'node:fs'
|
||||
import { createHash } from 'node:crypto'
|
||||
@ -78,3 +79,35 @@ export function isFilePathESM(filePath: string): boolean {
|
||||
return pkg?.type === 'module'
|
||||
}
|
||||
}
|
||||
|
||||
type DeepWritable<T> =
|
||||
T extends ReadonlyArray<unknown>
|
||||
? { -readonly [P in keyof T]: DeepWritable<T[P]> }
|
||||
: T extends RegExp
|
||||
? RegExp
|
||||
: T[keyof T] extends Function
|
||||
? T
|
||||
: { -readonly [P in keyof T]: DeepWritable<T[P]> }
|
||||
|
||||
export function deepClone<T>(value: T): DeepWritable<T> {
|
||||
if (Array.isArray(value)) {
|
||||
return value.map(v => deepClone(v)) as DeepWritable<T>
|
||||
}
|
||||
if (isObject(value)) {
|
||||
const cloned: Record<string, any> = {}
|
||||
for (const key in value) {
|
||||
cloned[key] = deepClone(value[key])
|
||||
}
|
||||
return cloned as DeepWritable<T>
|
||||
}
|
||||
if (typeof value === 'function') {
|
||||
return value as DeepWritable<T>
|
||||
}
|
||||
if (value instanceof RegExp) {
|
||||
return new RegExp(value) as DeepWritable<T>
|
||||
}
|
||||
if (typeof value === 'object' && value != null) {
|
||||
throw new Error('Cannot deep clone non-plain object')
|
||||
}
|
||||
return value as DeepWritable<T>
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user