@babel/plugin-transform-nullish-coalescing-operator
info
这个插件包含在 @babel/preset-env 中,在 ES2020 中
示例
🌐 Example
在
JavaScript
var foo = object.foo ?? "default";
出
JavaScript
var _object$foo;
var foo =
(_object$foo = object.foo) !== null && _object$foo !== void 0
? _object$foo
: "default";
note
我们不能在这里使用 != null,因为 document.all == null 和 document.all 已被认为不是“空值”。
安装
🌐 Installation
- npm
- Yarn
- pnpm
- Bun
npm install --save-dev @babel/plugin-transform-nullish-coalescing-operator
yarn add --dev @babel/plugin-transform-nullish-coalescing-operator
pnpm add --save-dev @babel/plugin-transform-nullish-coalescing-operator
bun add --dev @babel/plugin-transform-nullish-coalescing-operator
用法
🌐 Usage
使用配置文件(推荐)
🌐 With a configuration file (Recommended)
babel.config.json
{
"plugins": ["@babel/plugin-transform-nullish-coalescing-operator"]
}
通过 CLI
🌐 Via CLI
Shell
babel --plugins @babel/plugin-transform-nullish-coalescing-operator script.js
通过 Node API
🌐 Via Node API
JavaScript
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-transform-nullish-coalescing-operator"],
});
选项
🌐 Options
loose
boolean,默认值为 false。
当 true 时,这个转换会假装 document.all 不存在,并且使用 null 进行宽松的相等检查,而不是对 null 和 undefined 进行严格的相等检查。
🌐 When true, this transform will pretend document.all does not exist,
and perform loose equality checks with null instead of strict equality checks
against both null and undefined.
caution
考虑迁移到顶层 noDocumentAll 假设。
babel.config.json
{
"assumptions": {
"noDocumentAll": true
}
}
示例
🌐 Example
在
JavaScript
var foo = object.foo ?? "default";
出
JavaScript
var _object$foo;
var foo = (_object$foo = object.foo) != null ? _object$foo : "default";
tip
你可以在这里了解更多关于配置插件选项的内容
参考
🌐 References