Skip to content

Commit 553e433

Browse files
Generate helper metadata at build time (#16501)
1 parent 125ba9a commit 553e433

File tree

147 files changed

+2693
-991
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

147 files changed

+2693
-991
lines changed

babel.config.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,16 @@ function pluginPolyfillsOldNode({ template, types: t }) {
498498
// https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V16.md#v8-93
499499
replacement: template`hasOwnProperty.call`,
500500
},
501+
{
502+
name: "Object.entries",
503+
necessary({ parent, node }) {
504+
// To avoid infinite replacement loops
505+
return !t.isLogicalExpression(parent, { operator: "||", left: node });
506+
},
507+
supported: path =>
508+
path.parentPath.isCallExpression({ callee: path.node }),
509+
replacement: template`Object.entries || (o => Object.keys(o).map(k => [k, o[k]]))`,
510+
},
501511
{
502512
name: "fs.rmSync",
503513
necessary({ node, parent }) {
@@ -543,7 +553,8 @@ function pluginPolyfillsOldNode({ template, types: t }) {
543553
if (!polyfill.necessary(path)) return;
544554
if (!polyfill.supported(path)) {
545555
throw path.buildCodeFrameError(
546-
`This '${polyfill.name}' usage is not supported by the inline polyfill.`
556+
`This '${polyfill.name}' usage is not supported by the inline polyfill.\n` +
557+
path.parentPath.toString()
547558
);
548559
}
549560

packages/babel-core/src/tools/build-external-helpers.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
variableDeclarator,
2424
} from "@babel/types";
2525
import type * as t from "@babel/types";
26-
import File from "../transformation/file/file.ts";
2726
import type { Replacements } from "@babel/template";
2827

2928
// Wrapped to avoid wasting time parsing this when almost no-one uses
@@ -174,8 +173,24 @@ function buildHelpers(
174173

175174
const ref = (refs[name] = getHelperReference(name));
176175

177-
helpers.ensure(name, File);
178-
const { nodes } = helpers.get(name, getHelperReference, ref);
176+
const { nodes } = helpers.get(
177+
name,
178+
getHelperReference,
179+
namespace ? null : `_${name}`,
180+
[],
181+
namespace
182+
? (ast, exportName, mapExportBindingAssignments) => {
183+
mapExportBindingAssignments(node =>
184+
assignmentExpression("=", ref, node),
185+
);
186+
ast.body.push(
187+
expressionStatement(
188+
assignmentExpression("=", ref, identifier(exportName)),
189+
),
190+
);
191+
}
192+
: null,
193+
);
179194

180195
body.push(...nodes);
181196
});

packages/babel-core/src/transformation/file/file.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export default class File {
163163
}
164164

165165
// make sure that the helper exists
166-
helpers.ensure(name, File);
166+
helpers.minVersion(name);
167167

168168
const uid = (this.declarations[name] =
169169
this.scope.generateUidIdentifier(name));
@@ -176,7 +176,7 @@ export default class File {
176176
const { nodes, globals } = helpers.get(
177177
name,
178178
dep => dependencies[dep],
179-
uid,
179+
uid.name,
180180
Object.keys(this.scope.getAllBindings()),
181181
);
182182

packages/babel-core/test/api.js

Lines changed: 82 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -840,24 +840,6 @@ describe("api", function () {
840840
});
841841

842842
describe("buildExternalHelpers", function () {
843-
describe("smoke tests", function () {
844-
it("builds external helpers in global output type", function () {
845-
babel.buildExternalHelpers(null, "global");
846-
});
847-
848-
it("builds external helpers in module output type", function () {
849-
babel.buildExternalHelpers(null, "module");
850-
});
851-
852-
it("builds external helpers in umd output type", function () {
853-
babel.buildExternalHelpers(null, "umd");
854-
});
855-
856-
it("builds external helpers in var output type", function () {
857-
babel.buildExternalHelpers(null, "var");
858-
});
859-
});
860-
861843
it("all", function () {
862844
const script = babel.buildExternalHelpers();
863845
expect(script).toEqual(expect.stringContaining("classCallCheck"));
@@ -880,6 +862,88 @@ describe("api", function () {
880862
const script = babel.buildExternalHelpers(["typeof"]);
881863
expect(script).toEqual(expect.stringContaining("typeof"));
882864
});
865+
866+
describe("output types", function () {
867+
it("global", function () {
868+
const script = babel.buildExternalHelpers(["get"], "global");
869+
expect(script).toMatchInlineSnapshot(`
870+
"(function (global) {
871+
var babelHelpers = global.babelHelpers = {};
872+
function _get() {
873+
return babelHelpers.get = _get = \\"undefined\\" != typeof Reflect && Reflect.get ? Reflect.get.bind() : function (e, t, r) {
874+
var p = babelHelpers.superPropBase(e, t);
875+
if (p) {
876+
var n = Object.getOwnPropertyDescriptor(p, t);
877+
return n.get ? n.get.call(arguments.length < 3 ? e : r) : n.value;
878+
}
879+
}, _get.apply(null, arguments);
880+
}
881+
babelHelpers.get = _get;
882+
})(typeof global === \\"undefined\\" ? self : global);"
883+
`);
884+
});
885+
886+
it("umd", function () {
887+
const script = babel.buildExternalHelpers(["get"], "umd");
888+
expect(script).toMatchInlineSnapshot(`
889+
"(function (root, factory) {
890+
if (typeof define === \\"function\\" && define.amd) {
891+
define([\\"exports\\"], factory);
892+
} else if (typeof exports === \\"object\\") {
893+
factory(exports);
894+
} else {
895+
factory(root.babelHelpers = {});
896+
}
897+
})(this, function (global) {
898+
var babelHelpers = global;
899+
function _get() {
900+
return babelHelpers.get = _get = \\"undefined\\" != typeof Reflect && Reflect.get ? Reflect.get.bind() : function (e, t, r) {
901+
var p = babelHelpers.superPropBase(e, t);
902+
if (p) {
903+
var n = Object.getOwnPropertyDescriptor(p, t);
904+
return n.get ? n.get.call(arguments.length < 3 ? e : r) : n.value;
905+
}
906+
}, _get.apply(null, arguments);
907+
}
908+
babelHelpers.get = _get;
909+
});"
910+
`);
911+
});
912+
913+
it("var", function () {
914+
const script = babel.buildExternalHelpers(["get"], "var");
915+
expect(script).toMatchInlineSnapshot(`
916+
"var babelHelpers = {};
917+
function _get() {
918+
return babelHelpers.get = _get = \\"undefined\\" != typeof Reflect && Reflect.get ? Reflect.get.bind() : function (e, t, r) {
919+
var p = babelHelpers.superPropBase(e, t);
920+
if (p) {
921+
var n = Object.getOwnPropertyDescriptor(p, t);
922+
return n.get ? n.get.call(arguments.length < 3 ? e : r) : n.value;
923+
}
924+
}, _get.apply(null, arguments);
925+
}
926+
babelHelpers.get = _get;
927+
babelHelpers;"
928+
`);
929+
});
930+
931+
it("module", function () {
932+
const script = babel.buildExternalHelpers(["get"], "module");
933+
expect(script).toMatchInlineSnapshot(`
934+
"export { _get as get };
935+
function _get() {
936+
return _get = \\"undefined\\" != typeof Reflect && Reflect.get ? Reflect.get.bind() : function (e, t, r) {
937+
var p = _superPropBase(e, t);
938+
if (p) {
939+
var n = Object.getOwnPropertyDescriptor(p, t);
940+
return n.get ? n.get.call(arguments.length < 3 ? e : r) : n.value;
941+
}
942+
}, _get.apply(null, arguments);
943+
}"
944+
`);
945+
});
946+
});
883947
});
884948

885949
describe("handle parsing errors", function () {

packages/babel-helpers/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@
1616
"main": "./lib/index.js",
1717
"dependencies": {
1818
"@babel/template": "workspace:^",
19-
"@babel/traverse": "workspace:^",
2019
"@babel/types": "workspace:^"
2120
},
2221
"devDependencies": {
23-
"@babel/core": "workspace:^",
2422
"@babel/generator": "workspace:^",
2523
"@babel/helper-plugin-test-runner": "workspace:^",
2624
"@babel/parser": "workspace:^",

0 commit comments

Comments
 (0)