6

Using babel to target Node v4, it seems the generated code depends on a regeneratorRuntime module which is not included in the output.

From what I understand it seems that one must require the 'regenerator-runtime/runtime' module at runtime before executing the generated code. (via https://babeljs.io/docs/en/usage#polyfill)

Generator Script:

const babel = require('@babel/core');
const out = babel.transformSync(`
  (async () => {

  })();
`, {
  presets: [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "4"
        }
      }
    ]
  ]
});

Output:

"use strict";

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
  return regeneratorRuntime.wrap(function _callee$(_context) {
    while (1) {
      switch (_context.prev = _context.next) {
        case 0:
        case "end":
          return _context.stop();
      }
    }
  }, _callee);
}))();

Is there any way to instruct Babel to include the regeneratorRuntime module within the generated code?

In short, I would like the generated output to be self contained without any runtime requirements.

lostsource
  • 21,070
  • 8
  • 66
  • 88
  • Did you tried to vendorize the regenerator runtime on your code? You could add it on your output and import: https://unpkg.com/regenerator-runtime@0.13.7/runtime.js – macabeus Nov 17 '20 at 23:57

1 Answers1

0

You can add the feature by installing @babel/plugin-transform-runtime to your project and referring to it among "plugins" section of your babel configuration.

This is an example of working .babelrc (babel conf file):

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "debug": false,
        "module": false,
        "useBuiltIns": "usage",
        "corejs": 3
      }
    ]
  ],
  "plugins": [
    "@babel/plugin-transform-runtime"
  ]
}

Enrico Deleo
  • 388
  • 2
  • 15
  • I tried this configuration however the output still did not contain any polyfills required at runtime. (it only added multiple `import` statements at the top of the file..) .. The goal (if possible) is to have babel include the required polyfills within the generated code itself. – lostsource Nov 23 '20 at 10:10
  • I can absolutely confirm this configuration works cause I personally use it, probably you need more polyfills since you are on an old nodejs version. Try adding `"targets": { "node": "v4.0.0" }` within the configuration object of preset-env. – Enrico Deleo Nov 23 '20 at 16:52