Skip to content

Commit

Permalink
perf: Improve module transforms (#16882)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu authored Oct 23, 2024
1 parent 5155711 commit 422c6d0
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 37 deletions.
1 change: 0 additions & 1 deletion packages/babel-helper-module-transforms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"main": "./lib/index.js",
"dependencies": {
"@babel/helper-module-imports": "workspace:^",
"@babel/helper-simple-access": "workspace:^",
"@babel/helper-validator-identifier": "workspace:^",
"@babel/traverse": "workspace:^"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import assert from "assert";
import { template, types as t } from "@babel/core";
import type { NodePath, Visitor, Scope } from "@babel/core";
import simplifyAccess from "@babel/helper-simple-access";

import type { ModuleMetadata } from "./normalize-and-load-metadata.ts";

Expand Down Expand Up @@ -97,18 +95,6 @@ export default function rewriteLiveReferences(
rewriteBindingInitVisitorState,
);

// NOTE(logan): The 'Array.from' calls are to make this code with in loose mode.
const bindingNames = new Set([
...Array.from(imported.keys()),
...Array.from(exported.keys()),
]);
if (process.env.BABEL_8_BREAKING) {
simplifyAccess(programPath, bindingNames);
} else {
// @ts-ignore(Babel 7 vs Babel 8) The third param has been removed in Babel 8.
simplifyAccess(programPath, bindingNames, false);
}

// Rewrite reads/writes from imports and exports to have the correct behavior.
const rewriteReferencesVisitorState: RewriteReferencesVisitorState = {
seen: new WeakSet(),
Expand Down Expand Up @@ -454,8 +440,6 @@ const rewriteReferencesVisitor: Visitor<RewriteReferencesVisitorState> = {
const exportedNames = exported.get(localName);
const importData = imported.get(localName);
if (exportedNames?.length > 0 || importData) {
assert(path.node.operator === "=", "Path was not simplified");

const assignment = path.node;

if (importData) {
Expand All @@ -467,15 +451,48 @@ const rewriteReferencesVisitor: Visitor<RewriteReferencesVisitorState> = {
]);
}

const { operator } = assignment;
let newExpr;
if (operator === "=") {
newExpr = assignment;
} else if (
operator === "&&=" ||
operator === "||=" ||
operator === "??="
) {
newExpr = t.assignmentExpression(
"=",
assignment.left,
t.logicalExpression(
operator.slice(0, -1) as t.LogicalExpression["operator"],
t.cloneNode(assignment.left) as t.Expression,
assignment.right,
),
);
} else {
newExpr = t.assignmentExpression(
"=",
assignment.left,
t.binaryExpression(
operator.slice(0, -1) as t.BinaryExpression["operator"],
t.cloneNode(assignment.left) as t.Expression,
assignment.right,
),
);
}

path.replaceWith(
buildBindingExportAssignmentExpression(
this.metadata,
exportedNames,
assignment,
newExpr,
path.scope,
),
);

requeueInParent(path);

path.skip();
}
} else {
const ids = left.getOuterBindingIdentifiers();
Expand Down Expand Up @@ -523,9 +540,7 @@ const rewriteReferencesVisitor: Visitor<RewriteReferencesVisitorState> = {
}
},
},
"ForOfStatement|ForInStatement"(
path: NodePath<t.ForOfStatement | t.ForInStatement>,
) {
ForXStatement(path) {
const { scope, node } = path;
const { left } = node;
const { exported, imported, scope: programScope } = this;
Expand Down
3 changes: 0 additions & 3 deletions packages/babel-helper-module-transforms/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
{
"path": "../../packages/babel-helper-module-imports"
},
{
"path": "../../packages/babel-helper-simple-access"
},
{
"path": "../../packages/babel-helper-validator-identifier"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var a;

a += 1;
a -= 1;
a *= 1;
a /= 1;
a %= 1;
a **= 1;
a <<= 1;
a >>= 1;
a >>>= 1;
a &= 1;
a ^= 1;
a |= 1;

a &&= 1;
a ||= 1;
a ??= 1;

export { a };
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.a = void 0;
var a;
exports.a = a = a + 1;
exports.a = a = a - 1;
exports.a = a = a * 1;
exports.a = a = a / 1;
exports.a = a = a % 1;
exports.a = a = a ** 1;
exports.a = a = a << 1;
exports.a = a = a >> 1;
exports.a = a = a >>> 1;
exports.a = a = a & 1;
exports.a = a = a ^ 1;
exports.a = a = a | 1;
exports.a = a = a && 1;
exports.a = a = a || 1;
exports.a = a = a ?? 1;
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,33 @@ _baz.Baz = (44, function () {
} = ({}, function () {
throw new Error('"' + "Baz" + '" is read-only.');
}()));
_foo.default = (_foo.default + 2, function () {
_foo.default = _foo.default + (2, function () {
throw new Error('"' + "Foo" + '" is read-only.');
}());
Bar = (Bar + 2, function () {
Bar = Bar + (2, function () {
throw new Error('"' + "Bar" + '" is read-only.');
}());
_baz.Baz = (_baz.Baz + 2, function () {
_baz.Baz = _baz.Baz + (2, function () {
throw new Error('"' + "Baz" + '" is read-only.');
}());
_foo.default = (_foo.default >>> 3, function () {
_foo.default = _foo.default >>> (3, function () {
throw new Error('"' + "Foo" + '" is read-only.');
}());
Bar = (Bar >>> 3, function () {
Bar = Bar >>> (3, function () {
throw new Error('"' + "Bar" + '" is read-only.');
}());
_baz.Baz = (_baz.Baz >>> 3, function () {
_baz.Baz = _baz.Baz >>> (3, function () {
throw new Error('"' + "Baz" + '" is read-only.');
}());
_foo.default && (_foo.default = (4, function () {
_foo.default = _foo.default && (4, function () {
throw new Error('"' + "Foo" + '" is read-only.');
}()));
Bar && (Bar = (4, function () {
}());
Bar = Bar && (4, function () {
throw new Error('"' + "Bar" + '" is read-only.');
}()));
_baz.Baz && (_baz.Baz = (4, function () {
}());
_baz.Baz = _baz.Baz && (4, function () {
throw new Error('"' + "Baz" + '" is read-only.');
}()));
}());
_foo.default -= function () {
throw new Error('"' + "Foo" + '" is read-only.');
}();
Expand Down
1 change: 0 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,6 @@ __metadata:
dependencies:
"@babel/core": "workspace:^"
"@babel/helper-module-imports": "workspace:^"
"@babel/helper-simple-access": "workspace:^"
"@babel/helper-validator-identifier": "workspace:^"
"@babel/traverse": "workspace:^"
peerDependencies:
Expand Down

0 comments on commit 422c6d0

Please sign in to comment.