@babel/plugin-proposal-explicit-resource-management
此插件使 Babel 能够使用声明进行转换 using handler = await read();
。
示例
input.js
using handlerSync = openSync();
await using handlerAsync = await openAsync();
将被转换为
output.js
try {
var _stack = [];
var handlerSync = babelHelpers.using(_stack, openSync());
var handlerAsync = babelHelpers.using(_stack, await openAsync(), true);
} catch (_) {
var _error = _;
var _hasError = true;
} finally {
await babelHelpers.dispose(_stack, _error, _hasError);
}
安装
- npm
- Yarn
- pnpm
npm install --save-dev @babel/plugin-proposal-explicit-resource-management
yarn add --dev @babel/plugin-proposal-explicit-resource-management
pnpm add --save-dev @babel/plugin-proposal-explicit-resource-management
用法
使用配置文件(推荐)
babel.config.json
{
"plugins": ["@babel/plugin-proposal-explicit-resource-management"]
}
通过 CLI
Shell
babel --plugins @babel/plugin-proposal-explicit-resource-management script.js
通过 Node API
JavaScript
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-proposal-explicit-resource-management"]
});