Skip to content

Commit e0a2a71

Browse files
Inline regeneratorRuntime as a normal helper (#14538)
Co-authored-by: Babel Bot <[email protected]>
1 parent e57d081 commit e0a2a71

88 files changed

Lines changed: 1243 additions & 173 deletions

File tree

Some content is hidden

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

Gulpfile.mjs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,14 @@ function generateTraverseHelpers(helperKind) {
145145
);
146146
}
147147

148-
function generateRuntimeHelpers() {
148+
async function generateRuntimeHelpers() {
149+
await generateHelpers(
150+
`./packages/babel-helpers/scripts/generate-regenerator-runtime.js`,
151+
`./packages/babel-helpers/src/helpers`,
152+
"regeneratorRuntime.js",
153+
"@babel/helpers -> regeneratorRuntime"
154+
);
155+
149156
return generateHelpers(
150157
`./packages/babel-helpers/scripts/generate-helpers.js`,
151158
`./packages/babel-helpers/src/`,
@@ -340,6 +347,7 @@ function buildRollup(packages, targetBrowsers) {
340347
include: [
341348
/node_modules/,
342349
"packages/babel-runtime/regenerator/**",
350+
"packages/babel-runtime/helpers/*.js",
343351
"packages/babel-preset-env/data/*.js",
344352
// Rollup doesn't read export maps, so it loads the cjs fallback
345353
"packages/babel-compat-data/*.js",
@@ -365,6 +373,7 @@ function buildRollup(packages, targetBrowsers) {
365373
babelHelpers: "bundled",
366374
extends: "./babel.config.js",
367375
extensions: [".ts", ".js", ".mjs", ".cjs"],
376+
ignore: ["packages/babel-runtime/helpers/*.js"],
368377
}),
369378
rollupNodeResolve({
370379
extensions: [".ts", ".js", ".mjs", ".cjs", ".json"],
@@ -575,15 +584,12 @@ gulp.task(
575584
"build",
576585
gulp.series(
577586
"build-vendor",
578-
gulp.parallel("build-rollup", "build-babel", "generate-runtime-helpers"),
579-
gulp.parallel(
580-
"generate-standalone",
581-
gulp.series(
582-
"generate-type-helpers",
583-
// rebuild @babel/types since type-helpers may be changed
584-
"build-babel"
585-
)
586-
)
587+
gulp.parallel("build-rollup", "build-babel"),
588+
gulp.parallel("generate-type-helpers", "generate-runtime-helpers"),
589+
// rebuild @babel/types and @babel/helpers since
590+
// type-helpers and generated helpers may be changed
591+
"build-babel",
592+
"generate-standalone"
587593
)
588594
);
589595

Makefile

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,16 @@ prepublish:
188188
IS_PUBLISH=true $(MAKE) test
189189

190190
new-version-checklist:
191-
# @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
192-
# @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
193-
# @echo "!!!!!! !!!!!!"
194-
# @echo "!!!!!! Write any message that should !!!!!!"
195-
# @echo "!!!!!! block the release here !!!!!!"
196-
# @echo "!!!!!! !!!!!!"
197-
# @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
198-
# @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
199-
# @exit 1
191+
@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
192+
@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
193+
@echo "!!!!!! !!!!!!"
194+
@echo "!!!!!! Update the regeneratorRuntime helper version !!!!!!"
195+
@echo "!!!!!! in generate-regenerator-runtime.js !!!!!!"
196+
@echo "!!!!!! and re-build the runtime helpers !!!!!!"
197+
@echo "!!!!!! !!!!!!"
198+
@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
199+
@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
200+
@exit 1
200201

201202
new-version:
202203
$(MAKE) new-version-checklist

packages/babel-core/test/fixtures/transformation/misc/regression-2892/output.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ var Foo = /*#__PURE__*/function () {
1313
babelHelpers.createClass(Foo, [{
1414
key: "bar",
1515
value: function () {
16-
var _bar = babelHelpers.asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
16+
var _bar = babelHelpers.asyncToGenerator( /*#__PURE__*/babelHelpers.regeneratorRuntime().mark(function _callee() {
1717
var baz;
18-
return regeneratorRuntime.wrap(function _callee$(_context) {
18+
return babelHelpers.regeneratorRuntime().wrap(function _callee$(_context) {
1919
while (1) {
2020
switch (_context.prev = _context.next) {
2121
case 0:
@@ -46,17 +46,17 @@ function foo() {
4646
}
4747

4848
function _foo() {
49-
_foo = babelHelpers.asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
49+
_foo = babelHelpers.asyncToGenerator( /*#__PURE__*/babelHelpers.regeneratorRuntime().mark(function _callee3() {
5050
var bar, _bar2;
5151

52-
return regeneratorRuntime.wrap(function _callee3$(_context3) {
52+
return babelHelpers.regeneratorRuntime().wrap(function _callee3$(_context3) {
5353
while (1) {
5454
switch (_context3.prev = _context3.next) {
5555
case 0:
5656
_bar2 = function _bar4() {
57-
_bar2 = babelHelpers.asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
57+
_bar2 = babelHelpers.asyncToGenerator( /*#__PURE__*/babelHelpers.regeneratorRuntime().mark(function _callee2() {
5858
var baz;
59-
return regeneratorRuntime.wrap(function _callee2$(_context2) {
59+
return babelHelpers.regeneratorRuntime().wrap(function _callee2$(_context2) {
6060
while (1) {
6161
switch (_context2.prev = _context2.next) {
6262
case 0:

packages/babel-helper-transform-fixture-test-runner/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
"@babel/helper-check-duplicate-nodes": "workspace:^",
2121
"@babel/helper-fixtures": "workspace:^",
2222
"@jridgewell/trace-mapping": "^0.3.8",
23-
"quick-lru": "5.1.0",
24-
"regenerator-runtime": "^0.13.7"
23+
"quick-lru": "5.1.0"
2524
},
2625
"devDependencies": {
2726
"@types/jest": "^27.4.1"

packages/babel-helper-transform-fixture-test-runner/src/index.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,6 @@ function createContext() {
5555
const moduleCache = Object.create(null);
5656
contextModuleCache.set(context, moduleCache);
5757

58-
// Initialize the test context with the polyfill, and then freeze the global to prevent implicit
59-
// global creation in tests, which could cause things to bleed between tests.
60-
runModuleInTestContext(
61-
"regenerator-runtime",
62-
fileURLToPath(import.meta.url),
63-
context,
64-
moduleCache,
65-
);
66-
6758
// Populate the "babelHelpers" global with Babel's helper utilities.
6859
runCacheableScriptInTestContext(
6960
path.join(

packages/babel-helpers/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
"@babel/types": "workspace:^"
2121
},
2222
"devDependencies": {
23+
"@babel/generator": "workspace:^",
2324
"@babel/helper-plugin-test-runner": "workspace:^",
25+
"@babel/parser": "workspace:^",
26+
"regenerator-runtime": "^0.13.9",
2427
"terser": "^5.9.0"
2528
},
2629
"engines": {

packages/babel-helpers/scripts/generate-helpers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from "fs";
22
import { join } from "path";
33
import { URL, fileURLToPath } from "url";
4-
import { minify } from "terser"; // eslint-disable-line
4+
import { minify } from "terser"; // eslint-disable-line import/no-extraneous-dependencies
55

66
const HELPERS_FOLDER = new URL("../src/helpers", import.meta.url);
77
const IGNORED_FILES = new Set(["package.json"]);
@@ -17,7 +17,7 @@ import template from "@babel/template";
1717
function helper(minVersion, source) {
1818
return Object.freeze({
1919
minVersion,
20-
ast: () => template.program.ast(source),
20+
ast: () => template.program.ast(source, { preserveComments: true }),
2121
})
2222
}
2323
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* eslint-disable import/no-extraneous-dependencies */
2+
3+
import fs from "fs";
4+
import { createRequire } from "module";
5+
6+
const [parse, generate] = await Promise.all([
7+
import("@babel/parser").then(ns => ns.parse),
8+
import("@babel/generator").then(ns => ns.default.default),
9+
]).catch(error =>
10+
Promise.reject(
11+
new Error(
12+
"Before running generate-helpers.js you must compile @babel/parser and @babel/generator.",
13+
{ cause: error }
14+
)
15+
)
16+
);
17+
18+
const REGENERATOR_RUNTIME_IN_FILE = fs.readFileSync(
19+
createRequire(import.meta.url).resolve("regenerator-runtime"),
20+
"utf8"
21+
);
22+
23+
const MIN_VERSION = "7.17.9";
24+
25+
const HEADER = `/* @minVersion ${MIN_VERSION} */
26+
/*
27+
* This file is auto-generated! Do not modify it directly.
28+
* To re-generate, update the regenerator-runtime dependency of
29+
* @babel/helpers and run 'yarn gulp generate-runtime-helpers'.
30+
*/
31+
32+
/* eslint-disable */
33+
`;
34+
35+
const COPYRIGHT = `/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */`;
36+
37+
export default function generateRegeneratorRuntimeHelper() {
38+
const ast = parse(REGENERATOR_RUNTIME_IN_FILE, { sourceType: "script" });
39+
40+
const factoryFunction = ast.program.body[0].declarations[0].init.callee;
41+
factoryFunction.type = "FunctionDeclaration";
42+
factoryFunction.id = { type: "Identifier", name: "_regeneratorRuntime" };
43+
factoryFunction.params = [];
44+
factoryFunction.body.body.unshift(
45+
...stmts(`
46+
${COPYRIGHT}
47+
_regeneratorRuntime = function () { return exports; };
48+
var exports = {};
49+
`)
50+
);
51+
52+
const { code } = generate({
53+
type: "ExportDefaultDeclaration",
54+
declaration: factoryFunction,
55+
});
56+
57+
return HEADER + code;
58+
}
59+
60+
function stmts(code) {
61+
return parse(`function _() { ${code} }`, {
62+
sourceType: "script",
63+
}).program.body[0].body.body;
64+
}

0 commit comments

Comments
 (0)