Skip to content

Commit 73dcd06

Browse files
authored
Remove some lodash/cloneDeep calls (#13032)
1 parent 0233e3c commit 73dcd06

9 files changed

Lines changed: 35 additions & 15 deletions

File tree

packages/babel-core/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
"./lib/transform-file.js": "./lib/transform-file-browser.js",
4444
"./src/config/files/index.js": "./src/config/files/index-browser.js",
4545
"./src/config/resolve-targets.js": "./src/config/resolve-targets-browser.js",
46-
"./src/transform-file.js": "./src/transform-file-browser.js"
46+
"./src/transform-file.js": "./src/transform-file-browser.js",
47+
"./src/transformation/util/clone-deep.js": "./src/transformation/util/clone-deep-browser.js"
4748
},
4849
"dependencies": {
4950
"@babel/code-frame": "workspace:^7.12.13",

packages/babel-core/src/transformation/normalize-file.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
import fs from "fs";
44
import path from "path";
55
import buildDebug from "debug";
6-
import cloneDeep from "lodash/cloneDeep";
76
import type { Handler } from "gensync";
87
import * as t from "@babel/types";
98
import type { PluginPasses } from "../config";
109
import convertSourceMap, { typeof Converter } from "convert-source-map";
1110
import File from "./file/file";
1211
import parser from "../parser";
12+
import cloneDeep from "./util/clone-deep";
1313

1414
const debug = buildDebug("babel:transform:file");
1515
const LARGE_INPUT_SOURCEMAP_THRESHOLD = 1_000_000;
@@ -35,8 +35,7 @@ export default function* normalizeFile(
3535
throw new Error("AST root must be a Program or File node");
3636
}
3737

38-
const { cloneInputAst } = options;
39-
if (cloneInputAst) {
38+
if (options.cloneInputAst) {
4039
ast = cloneDeep(ast);
4140
}
4241
} else {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const serialized = "$$ babel internal serialized type" + Math.random();
2+
3+
function serialize(key, value) {
4+
if (typeof value !== "bigint") return value;
5+
return {
6+
[serialized]: "BigInt",
7+
value: value.toString(),
8+
};
9+
}
10+
11+
function revive(key, value) {
12+
if (!value || typeof value !== "object") return value;
13+
if (value[serialized] !== "BigInt") return value;
14+
return BigInt(value.value);
15+
}
16+
17+
export default function (value) {
18+
return JSON.parse(JSON.stringify(value, serialize), revive);
19+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import v8 from "v8";
2+
import cloneDeep from "./clone-deep-browser";
3+
4+
export default function (value) {
5+
if (v8.deserialize && v8.serialize) {
6+
return v8.deserialize(v8.serialize(value));
7+
}
8+
return cloneDeep(value);
9+
}

packages/babel-helper-fixtures/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
"homepage": "https://babel.dev/docs/en/next/babel-helper-fixtures",
1616
"main": "lib/index.js",
1717
"dependencies": {
18-
"lodash": "^4.17.19",
1918
"semver": "condition:BABEL_8_BREAKING ? ^7.3.4 : ^6.3.0"
2019
},
2120
"devDependencies": {
22-
"@types/lodash": "^4.14.150",
2321
"@types/semver": "^7.3.4"
2422
}
2523
}

packages/babel-helper-fixtures/src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import cloneDeep from "lodash/cloneDeep";
21
import semver from "semver";
32
import path from "path";
43
import fs from "fs";
@@ -121,7 +120,7 @@ function pushTask(taskName, taskDir, suite, suiteName) {
121120
execLocAlias = suiteName + "/" + taskName;
122121
}
123122

124-
const taskOpts = cloneDeep(suite.options);
123+
const taskOpts = JSON.parse(JSON.stringify(suite.options));
125124

126125
const taskOptsLoc = tryResolve(taskDir + "/options");
127126
if (taskOptsLoc) Object.assign(taskOpts, require(taskOptsLoc));

packages/babel-traverse/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
"@babel/parser": "workspace:^7.13.0",
2424
"@babel/types": "workspace:^7.13.0",
2525
"debug": "^4.1.0",
26-
"globals": "condition:BABEL_8_BREAKING ? ^13.5.0 : ^11.1.0",
27-
"lodash": "^4.17.19"
26+
"globals": "condition:BABEL_8_BREAKING ? ^13.5.0 : ^11.1.0"
2827
},
2928
"devDependencies": {
3029
"@babel/helper-plugin-test-runner": "workspace:*"

packages/babel-traverse/test/traverse.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import cloneDeep from "lodash/cloneDeep";
21
import traverse from "../lib";
32
import { parse } from "@babel/parser";
43
import * as t from "@babel/types";
@@ -17,7 +16,7 @@ describe("traverse", function () {
1716
type: "StringLiteral",
1817
value: "foo",
1918
};
20-
const ast2 = cloneDeep(program);
19+
const ast2 = JSON.parse(JSON.stringify(program));
2120

2221
traverse(ast2, {
2322
enter: function (path) {

yarn.lock

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -542,9 +542,7 @@ __metadata:
542542
version: 0.0.0-use.local
543543
resolution: "@babel/helper-fixtures@workspace:packages/babel-helper-fixtures"
544544
dependencies:
545-
"@types/lodash": ^4.14.150
546545
"@types/semver": ^7.3.4
547-
lodash: ^4.17.19
548546
semver: "condition:BABEL_8_BREAKING ? ^7.3.4 : ^6.3.0"
549547
languageName: unknown
550548
linkType: soft
@@ -3470,7 +3468,6 @@ __metadata:
34703468
"@babel/types": "workspace:^7.13.0"
34713469
debug: ^4.1.0
34723470
globals: "condition:BABEL_8_BREAKING ? ^13.5.0 : ^11.1.0"
3473-
lodash: ^4.17.19
34743471
languageName: unknown
34753472
linkType: soft
34763473

0 commit comments

Comments
 (0)