跳到主要内容

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

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

Try it on the REPL.

Installation

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

Usage

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

Via CLI

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

Via Node API

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

References