Debian Codegraph @

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

raw symbols SCIP
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 || ns.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.18.0";
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 }