Skip to main content

@babel/plugin-transform-duplicate-keys

info

这个插件包含在 @babel/preset-env 中

这个插件实际上会把对象中重复的键转换为计算属性,然后必须由 @babel/plugin-transform-computed-properties 插件来处理。最终结果中不会包含任何有重复键的对象字面量。

🌐 This plugin actually converts duplicate keys in objects to be computed properties, which then must be handled by the @babel/plugin-transform-computed-properties plugin. The final result won't contain any object literals with duplicate keys.

示例​

🌐 Example

在

JavaScript
var x = { a: 5, a: 6 };
var y = {
get a() {},
set a(x) {},
a: 3,
};

出

JavaScript
var x = { a: 5, ["a"]: 6 };
var y = {
get a() {},
set a(x) {},
["a"]: 3,
};

安装​

🌐 Installation

npm install --save-dev @babel/plugin-transform-duplicate-keys

用法​

🌐 Usage

🌐 With a configuration file (Recommended)

babel.config.json
{
"plugins": ["@babel/plugin-transform-duplicate-keys"]
}

通过 CLI​

🌐 Via CLI

Shell
babel --plugins @babel/plugin-transform-duplicate-keys script.js

通过 Node API​

🌐 Via Node API

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