@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();
}
Installation
- npm
- Yarn
- pnpm
npm install --save-dev @babel/plugin-transform-explicit-resource-management
yarn add --dev @babel/plugin-transform-explicit-resource-management
pnpm add --save-dev @babel/plugin-transform-explicit-resource-management
Usage
With a configuration file (Recommended)
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"]
});