Skip to content

Commit

Permalink
Remove some lodash/cloneDeep calls (#13032)
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell committed Mar 25, 2021
1 parent 0233e3c commit 73dcd06
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 15 deletions.
3 changes: 2 additions & 1 deletion packages/babel-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"./lib/transform-file.js": "./lib/transform-file-browser.js",
"./src/config/files/index.js": "./src/config/files/index-browser.js",
"./src/config/resolve-targets.js": "./src/config/resolve-targets-browser.js",
"./src/transform-file.js": "./src/transform-file-browser.js"
"./src/transform-file.js": "./src/transform-file-browser.js",
"./src/transformation/util/clone-deep.js": "./src/transformation/util/clone-deep-browser.js"
},
"dependencies": {
"@babel/code-frame": "workspace:^7.12.13",
Expand Down
5 changes: 2 additions & 3 deletions packages/babel-core/src/transformation/normalize-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import fs from "fs";
import path from "path";
import buildDebug from "debug";
import cloneDeep from "lodash/cloneDeep";
import type { Handler } from "gensync";
import * as t from "@babel/types";
import type { PluginPasses } from "../config";
import convertSourceMap, { typeof Converter } from "convert-source-map";
import File from "./file/file";
import parser from "../parser";
import cloneDeep from "./util/clone-deep";

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

const { cloneInputAst } = options;
if (cloneInputAst) {
if (options.cloneInputAst) {
ast = cloneDeep(ast);
}
} else {
Expand Down
19 changes: 19 additions & 0 deletions packages/babel-core/src/transformation/util/clone-deep-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const serialized = "$$ babel internal serialized type" + Math.random();

function serialize(key, value) {
if (typeof value !== "bigint") return value;
return {
[serialized]: "BigInt",
value: value.toString(),
};
}

function revive(key, value) {
if (!value || typeof value !== "object") return value;
if (value[serialized] !== "BigInt") return value;
return BigInt(value.value);
}

export default function (value) {
return JSON.parse(JSON.stringify(value, serialize), revive);
}
9 changes: 9 additions & 0 deletions packages/babel-core/src/transformation/util/clone-deep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import v8 from "v8";
import cloneDeep from "./clone-deep-browser";

export default function (value) {
if (v8.deserialize && v8.serialize) {
return v8.deserialize(v8.serialize(value));
}
return cloneDeep(value);
}
2 changes: 0 additions & 2 deletions packages/babel-helper-fixtures/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
"homepage": "https://babel.dev/docs/en/next/babel-helper-fixtures",
"main": "lib/index.js",
"dependencies": {
"lodash": "^4.17.19",
"semver": "condition:BABEL_8_BREAKING ? ^7.3.4 : ^6.3.0"
},
"devDependencies": {
"@types/lodash": "^4.14.150",
"@types/semver": "^7.3.4"
}
}
3 changes: 1 addition & 2 deletions packages/babel-helper-fixtures/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import cloneDeep from "lodash/cloneDeep";
import semver from "semver";
import path from "path";
import fs from "fs";
Expand Down Expand Up @@ -121,7 +120,7 @@ function pushTask(taskName, taskDir, suite, suiteName) {
execLocAlias = suiteName + "/" + taskName;
}

const taskOpts = cloneDeep(suite.options);
const taskOpts = JSON.parse(JSON.stringify(suite.options));

const taskOptsLoc = tryResolve(taskDir + "/options");
if (taskOptsLoc) Object.assign(taskOpts, require(taskOptsLoc));
Expand Down
3 changes: 1 addition & 2 deletions packages/babel-traverse/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
"@babel/parser": "workspace:^7.13.0",
"@babel/types": "workspace:^7.13.0",
"debug": "^4.1.0",
"globals": "condition:BABEL_8_BREAKING ? ^13.5.0 : ^11.1.0",
"lodash": "^4.17.19"
"globals": "condition:BABEL_8_BREAKING ? ^13.5.0 : ^11.1.0"
},
"devDependencies": {
"@babel/helper-plugin-test-runner": "workspace:*"
Expand Down
3 changes: 1 addition & 2 deletions packages/babel-traverse/test/traverse.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import cloneDeep from "lodash/cloneDeep";
import traverse from "../lib";
import { parse } from "@babel/parser";
import * as t from "@babel/types";
Expand All @@ -17,7 +16,7 @@ describe("traverse", function () {
type: "StringLiteral",
value: "foo",
};
const ast2 = cloneDeep(program);
const ast2 = JSON.parse(JSON.stringify(program));

traverse(ast2, {
enter: function (path) {
Expand Down
3 changes: 0 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -542,9 +542,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@babel/helper-fixtures@workspace:packages/babel-helper-fixtures"
dependencies:
"@types/lodash": ^4.14.150
"@types/semver": ^7.3.4
lodash: ^4.17.19
semver: "condition:BABEL_8_BREAKING ? ^7.3.4 : ^6.3.0"
languageName: unknown
linkType: soft
Expand Down Expand Up @@ -3470,7 +3468,6 @@ __metadata:
"@babel/types": "workspace:^7.13.0"
debug: ^4.1.0
globals: "condition:BABEL_8_BREAKING ? ^13.5.0 : ^11.1.0"
lodash: ^4.17.19
languageName: unknown
linkType: soft

Expand Down

0 comments on commit 73dcd06

Please sign in to comment.