Skip to main content

@babel/plugin-proposal-explicit-resource-management

这个插件使 Babel 能够使用声明 using handler = await read(); 进行转换。

¥This plugin enables Babel to transform using declarations using handler = await read();.

示例

¥Example

input.js
using handlerSync = openSync();
await using handlerAsync = await openAsync();

将转变为

¥will be transformed to

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);
}

在 REPL 上试试

¥Try it on the REPL.

安装

¥Installation

npm install --save-dev @babel/plugin-proposal-explicit-resource-management

用法

¥Usage

¥With a configuration file (Recommended)

babel.config.json
{
"plugins": ["@babel/plugin-proposal-explicit-resource-management"]
}

通过 CLI

¥Via CLI

Shell
babel --plugins @babel/plugin-proposal-explicit-resource-management script.js

通过 Node API

¥Via Node API

JavaScript
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-proposal-explicit-resource-management"]
});

参考

¥References