[英]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.使用 babel 以 Node v4 为目标,生成的代码似乎依赖于未包含在输出中的regeneratorRuntime
模块。
From what I understand it seems that one must require the 'regenerator-runtime/runtime' module at runtime before executing the generated code.据我所知,在执行生成的代码之前,似乎必须在运行时需要“regenerator-runtime/runtime”模块。 (via https://babeljs.io/docs/en/usage#polyfill ) (通过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?有什么方法可以指示 Babel 在生成的代码中包含 regeneratorRuntime 模块吗?
In short, I would like the generated output to be self contained without any runtime requirements.简而言之,我希望生成的输出是自包含的,没有任何运行时要求。
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.您可以通过将 @babel/plugin-transform-runtime 安装到您的项目并在 babel 配置的“插件”部分中引用它来添加该功能。
This is an example of working .babelrc
(babel conf file):这是一个工作.babelrc
(babel conf 文件)的例子:
{
"presets": [
[
"@babel/preset-env",
{
"debug": false,
"module": false,
"useBuiltIns": "usage",
"corejs": 3
}
]
],
"plugins": [
"@babel/plugin-transform-runtime"
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.