@babel/plugin-transform-destructuring

Examples

In

  1. let {x, y} = obj;
  2. let [a, b, ...rest] = arr;

Out

  1. function _toArray(arr) { ... }
  2. let _obj = obj,
  3. x = _obj.x,
  4. y = _obj.y;
  5. let _arr = arr,
  6. _arr2 = _toArray(_arr),
  7. a = _arr2[0],
  8. b = _arr2[1],
  9. rest = _arr2.slice(2);

Installation

  1. npm install --save-dev @babel/plugin-transform-destructuring

Usage

.babelrc

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

Via CLI

  1. babel --plugins @babel/plugin-transform-destructuring script.js

Via Node API

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

Options

loose

boolean, defaults to false.

Enabling this option will assume that what you want to destructure is an array and won't use Array.from on other iterables.

useBuiltIns

boolean, defaults to false.

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

Example

.babelrc

  1. {
  2. "plugins": [
  3. ["@babel/plugin-transform-destructuring", { "useBuiltIns": true }]
  4. ]
  5. }

In

  1. var { ...x } = z;

Out

  1. var _z = z,
  2. x = Object.assign({}, _z);

You can read more about configuring plugin options here

References