Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce3785f
commit f3d49e0
Showing
4 changed files
with
69 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 0 additions & 5 deletions
5
packages/babel-core/src/transformation/util/clone-deep-browser.ts
This file was deleted.
Oops, something went wrong.
42 changes: 29 additions & 13 deletions
42
packages/babel-core/src/transformation/util/clone-deep.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,32 @@ | ||
import v8 from "v8"; | ||
import cloneDeep from "clone-deep"; | ||
import semver from "semver"; | ||
//https://github.com/babel/babel/pull/14583#discussion_r882828856 | ||
function deepClone(value, cache) { | ||
if (value !== null) { | ||
if (cache.has(value)) return cache.get(value); | ||
let cloned; | ||
if (Array.isArray(value)) { | ||
cloned = new Array(value.length); | ||
for (let i = 0; i < value.length; i++) { | ||
cloned[i] = | ||
typeof value[i] !== "object" ? value[i] : deepClone(value[i], cache); | ||
} | ||
} else { | ||
cloned = {}; | ||
const keys = Object.keys(value); | ||
for (let i = 0; i < keys.length; i++) { | ||
const key = keys[i]; | ||
cloned[key] = | ||
typeof value[key] !== "object" | ||
? value[key] | ||
: deepClone(value[key], cache); | ||
} | ||
} | ||
cache.set(value, cloned); | ||
return cloned; | ||
} | ||
return value; | ||
} | ||
|
||
export default function (value) { | ||
// There was a memory leak prior to node 18.2.0 | ||
// https://github.com/nodejs/node/issues/40828 | ||
if ( | ||
v8.deserialize && | ||
v8.serialize && | ||
semver.satisfies(process.versions.node, ">=18.2.0") | ||
) { | ||
return v8.deserialize(v8.serialize(value)); | ||
} | ||
return cloneDeep(value); | ||
if (typeof value !== "object") return value; | ||
return deepClone(value, new Map()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters