generate-regenerator-runtime.js 1.8 KB

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