UNPKG

1.47 kBMarkdownView Raw
1# babel-plugin-transform-object-rest-spread
2
3> This plugin allows Babel to transform rest properties for object destructuring assignment and spread properties for object literals.
4
5## Example
6
7### Rest Properties
8
9```js
10let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
11console.log(x); // 1
12console.log(y); // 2
13console.log(z); // { a: 3, b: 4 }
14```
15
16### Spread Properties
17
18```js
19let n = { x, y, ...z };
20console.log(n); // { x: 1, y: 2, a: 3, b: 4 }
21```
22
23## Installation
24
25```sh
26npm install --save-dev babel-plugin-transform-object-rest-spread
27```
28
29## Usage
30
31### Via `.babelrc` (Recommended)
32
33**.babelrc**
34
35```json
36{
37 "plugins": ["transform-object-rest-spread"]
38}
39```
40
41### Via CLI
42
43```sh
44babel --plugins transform-object-rest-spread script.js
45```
46
47### Via Node API
48
49```javascript
50require("babel-core").transform("code", {
51 plugins: ["transform-object-rest-spread"]
52});
53```
54
55## Options
56
57### `useBuiltIns`
58
59`boolean`, defaults to `false`.
60
61By default, this plugin uses Babel's `extends` helper which polyfills `Object.assign`. Enabling this option will use `Object.assign` directly.
62
63**.babelrc**
64
65```json
66{
67 "plugins": [
68 ["transform-object-rest-spread", { "useBuiltIns": true }]
69 ]
70}
71```
72
73**In**
74
75```js
76z = { x, ...y };
77```
78
79**Out**
80
81```js
82z = Object.assign({ x }, y);
83```
84
85## References
86
87* [Proposal: Object Rest/Spread Properties for ECMAScript](https://github.com/sebmarkbage/ecmascript-rest-spread)
88* [Spec](http://sebmarkbage.github.io/ecmascript-rest-spread)