@babel/plugin-transform-duplicate-keys

duplicate-keys - 图1info

This plugin is included in @babel/preset-env

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

In

JavaScript

  1. var x = { a: 5, a: 6 };
  2. var y = {
  3. get a() {},
  4. set a(x) {},
  5. a: 3,
  6. };

Out

JavaScript

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

Installation

  • npm
  • Yarn
  • pnpm
  1. npm install --save-dev @babel/plugin-transform-duplicate-keys
  1. yarn add --dev @babel/plugin-transform-duplicate-keys
  1. pnpm add --save-dev @babel/plugin-transform-duplicate-keys

Usage

babel.config.json

  1. {
  2. "plugins": ["@babel/plugin-transform-duplicate-keys"]
  3. }

Via CLI

Shell

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

Via Node API

JavaScript

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