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
|
10 | let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
|
11 | console.log(x); // 1
|
12 | console.log(y); // 2
|
13 | console.log(z); // { a: 3, b: 4 }
|
14 | ```
|
15 |
|
16 | ### Spread Properties
|
17 |
|
18 | ```js
|
19 | let n = { x, y, ...z };
|
20 | console.log(n); // { x: 1, y: 2, a: 3, b: 4 }
|
21 | ```
|
22 |
|
23 | ## Installation
|
24 |
|
25 | ```sh
|
26 | npm 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
|
44 | babel --plugins transform-object-rest-spread script.js
|
45 | ```
|
46 |
|
47 | ### Via Node API
|
48 |
|
49 | ```javascript
|
50 | require("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 |
|
61 | By 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
|
76 | z = { x, ...y };
|
77 | ```
|
78 |
|
79 | **Out**
|
80 |
|
81 | ```js
|
82 | z = 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)
|