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