Skip to main content

@babel/plugin-transform-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 _usingCtx = babelHelpers.usingCtx();
var handlerSync = _usingCtx.u(openSync());
var handlerAsync = _usingCtx.a(await openAsync());
} catch (_) {
_usingCtx.e = _;
} finally {
await _usingCtx.d();
}

Try it on the REPL.

Installation

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

Usage

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

Via CLI

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

Via Node API

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

References