@babel/plugin-transform-regenerator
資訊
此外掛程式包含在 @babel/preset-env
中
範例
在
JavaScript
function* a() {
yield 1;
}
輸出
JavaScript
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
- Yarn
- pnpm
npm install --save-dev @babel/plugin-transform-regenerator
yarn add --dev @babel/plugin-transform-regenerator
pnpm add --save-dev @babel/plugin-transform-regenerator
用法
使用設定檔(建議)
無選項
babel.config.json
{
"plugins": ["@babel/plugin-transform-regenerator"]
}
有選項
名稱 | 預設值 |
---|---|
asyncGenerators | true |
generators | true |
async | true |
babel.config.json
{
"plugins": [
[
"@babel/plugin-transform-regenerator",
{
"asyncGenerators": false,
"generators": false,
"async": false
}
]
]
}
透過 CLI
Shell
babel --plugins @babel/plugin-transform-regenerator script.js
透過 Node API
JavaScript
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-transform-regenerator"],
});