@babel/plugin-transform-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 _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
- Bun
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
bun add --dev @babel/plugin-transform-explicit-resource-management
用法
¥Usage
使用配置文件(推荐)
¥With a configuration file (Recommended)
babel.config.json
{
"plugins": ["@babel/plugin-transform-explicit-resource-management"]
}
通过 CLI
¥Via CLI
Shell
babel --plugins @babel/plugin-transform-explicit-resource-management script.js
通过 Node API
¥Via Node API
JavaScript
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-transform-explicit-resource-management"]
});
参考
¥References