generate-regenerator-runtime.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* eslint-disable import/no-extraneous-dependencies */
  2. import fs from "fs";
  3. import { createRequire } from "module";
  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. const REGENERATOR_RUNTIME_IN_FILE = fs.readFileSync(
  15. createRequire(import.meta.url).resolve("regenerator-runtime"),
  16. "utf8"
  17. );
  18. const MIN_VERSION = "7.18.0";
  19. const HEADER = `/* @minVersion ${MIN_VERSION} */
  20. /*
  21. * This file is auto-generated! Do not modify it directly.
  22. * To re-generate, update the regenerator-runtime dependency of
  23. * @babel/helpers and run 'yarn gulp generate-runtime-helpers'.
  24. */
  25. /* eslint-disable */
  26. `;
  27. const COPYRIGHT = `/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */`;
  28. export default function generateRegeneratorRuntimeHelper() {
  29. const ast = parse(REGENERATOR_RUNTIME_IN_FILE, { sourceType: "script" });
  30. const factoryFunction = ast.program.body[0].declarations[0].init.callee;
  31. factoryFunction.type = "FunctionDeclaration";
  32. factoryFunction.id = { type: "Identifier", name: "_regeneratorRuntime" };
  33. factoryFunction.params = [];
  34. factoryFunction.body.body.unshift(
  35. ...stmts(`
  36. ${COPYRIGHT}
  37. _regeneratorRuntime = function () { return exports; };
  38. var exports = {};
  39. `)
  40. );
  41. const { code } = generate({
  42. type: "ExportDefaultDeclaration",
  43. declaration: factoryFunction,
  44. });
  45. return HEADER + code;
  46. }
  47. function stmts(code) {
  48. return parse(`function _() { ${code} }`, {
  49. sourceType: "script",
  50. }).program.body[0].body.body;
  51. }