This plugin uses the regenerator module to transform async and generator functions. regeneratorRuntime
is not included.
Runtime required
You need to use either the Babel polyfill or the regenerator runtime so that
regeneratorRuntime
will be defined.
Async functions
These are only usable if you enable their syntax plugin. See syntax-async-functions for information.
输入
function* a() {
yield 1;
}
输出
var _marked = [a].map(regeneratorRuntime.mark);
function a() {
return regeneratorRuntime.wrap(function a$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return 1;
case 2:
case "end":
return _context.stop();
}
}
}, _marked[0], this);
}
npm install --save-dev babel-plugin-transform-regenerator
.babelrc
(推荐)未包含选项:
{
"plugins": ["transform-regenerator"]
}
包含选项:
name | default value |
---|---|
asyncGenerators | true |
generators | true |
async | true |
{
"plugins": [
["transform-regenerator", {
"asyncGenerators": false,
"generators": false,
"async": false
}]
]
}
babel --plugins transform-regenerator script.js
require("babel-core").transform("code", {
plugins: ["transform-regenerator"]
});