Tuesday, 17 November 2020

Can Babel include regeneratorRuntime Dependency within the Generated Code?

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.



from Can Babel include regeneratorRuntime Dependency within the Generated Code?

No comments:

Post a Comment