JSPM

  • Created
  • Published
  • Downloads 19095930
  • Score
    100M100P100Q77801F
  • License MIT

Compile object rest and spread to ES5

Package Exports

  • @babel/plugin-proposal-object-rest-spread

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@babel/plugin-proposal-object-rest-spread) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@babel/plugin-proposal-object-rest-spread

This plugin allows Babel to transform rest properties for object destructuring assignment and spread properties for object literals.

Example

Rest Properties

let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
console.log(x); // 1
console.log(y); // 2
console.log(z); // { a: 3, b: 4 }

Spread Properties

let n = { x, y, ...z };
console.log(n); // { x: 1, y: 2, a: 3, b: 4 }

Installation

npm install --save-dev @babel/plugin-proposal-object-rest-spread

Usage

.babelrc

{
  "plugins": ["@babel/plugin-proposal-object-rest-spread"]
}

Via CLI

babel --plugins @babel/plugin-proposal-object-rest-spread script.js

Via Node API

require("@babel/core").transform("code", {
  plugins: ["@babel/plugin-proposal-object-rest-spread"]
});

Options

By default, this plugin will produce spec compliant code by using Babel's objectSpread helper.

loose

boolean, defaults to false.

Enabling this option will use Babel's extends helper, which is basically the same as Object.assign (see useBuiltIns below to use it directly).

⚠️ Please keep in mind that even if they're almost equivalent, there's an important difference between spread and Object.assign: spread defines new properties, while Object.assign() sets them, so using this mode might produce unexpected results in some cases.

For detailed information please check out Spread VS. Object.assign and Assigning VS. defining properties.

useBuiltIns

boolean, defaults to false.

Enabling this option will use Object.assign directly instead of the Babel's extends helper.

Example

.babelrc

{
  "plugins": [
    ["@babel/plugin-proposal-object-rest-spread", { "loose": true, "useBuiltIns": true }]
  ]
}

In

z = { x, ...y };

Out

z = Object.assign({ x }, y);

References