@babel/plugin-proposal-function-sent
示例
JavaScript
function* generator() {
console.log("Sent", function.sent);
console.log("Yield", yield);
}
const iterator = generator();
iterator.next(1); // Logs "Sent 1"
iterator.next(2); // Logs "Yield 2"
大致编译为
JavaScript
let generator = _skipFirstGeneratorNext(function*() {
const _functionSent = yield;
console.log("Sent", _functionSent);
console.log("Yield", yield);
});
const iterator = generator();
iterator.next(1); // Logs "Sent 1"
iterator.next(2); // Logs "Yield 2"
安装
- npm
- Yarn
- pnpm
npm install --save-dev @babel/plugin-proposal-function-sent
yarn add --dev @babel/plugin-proposal-function-sent
pnpm add --save-dev @babel/plugin-proposal-function-sent
用法
使用配置文件(推荐)
babel.config.json
{
"plugins": ["@babel/plugin-proposal-function-sent"]
}
通过 CLI
Shell
babel --plugins @babel/plugin-proposal-function-sent script.js
通过 Node API
JavaScript
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-proposal-function-sent"],
});