babel-plugin-extensible-destructuring
raw JSON → 4.3.1 verified Sat Apr 25 auth: no javascript
Babel plugin that transforms destructuring assignments to calls to a runtime function, enabling custom behavior like Immutable.js support or safe access (avoiding undefined). Version 4.3.1 works with Babel 6 and 7 (using full plugin name for Babel 7). It requires the separate runtime package `extensible-runtime` and offers three implementations: normal (ES6 compatible), immutable (for Immutable.js Maps/Lists), and safe (default, prevents undefined returns). Provides opt-in/opt-out per-file modes for gradual adoption.
Common errors
error Error: Cannot find module 'extensible-runtime' ↓
cause Runtime package not installed.
fix
npm install --save extensible-runtime
error ReferenceError: __extensible_get__ is not defined ↓
cause Missing runtime import or Babel not transforming correctly.
fix
Ensure both plugin and runtime are installed and Babel is configured correctly.
error TypeError: Cannot destructure property 'x' of 'undefined' or 'null' ↓
cause Using safe mode but the value is null, not undefined.
fix
Safe mode only prevents undefined; for null, use a default value or check beforehand.
Warnings
breaking Babel 7 requires full plugin name 'babel-plugin-extensible-destructuring' instead of 'extensible-destructuring'. ↓
fix Use 'babel-plugin-extensible-destructuring' in plugins array for Babel 7.
gotcha You must install both the plugin and runtime package: 'extensible-runtime' is required at runtime. ↓
fix Run npm install --save extensible-runtime.
gotcha Opt-in mode is default; destructuring will not be transformed unless 'use extensible' directive is at top of file. ↓
fix Add 'use extensible'; at the beginning of each file, or set mode to 'optout' in options.
Install
npm install babel-plugin-extensible-destructuring yarn add babel-plugin-extensible-destructuring pnpm add babel-plugin-extensible-destructuring Imports
- plugin wrong
// Wrong: using 'babel-plugin-extensible-destructuring' with Babel 6 (works but not short name) { "plugins": ["babel-plugin-extensible-destructuring"] }correct// .babelrc { "plugins": ["extensible-destructuring"] } - plugin with options wrong
// Wrong: omitting double array brackets { "plugins": ["extensible-destructuring", { "mode": "optin" }] }correct// .babelrc { "plugins": [["extensible-destructuring", { "mode": "optin", "impl": "safe" }]] } - use extensible directive wrong
// Wrong: misspelling 'use extansible'; var {a} = obj;correct// top of file 'use extensible'; var {a} = obj;
Quickstart
// Install dependencies
// npm install --save-dev babel-plugin-extensible-destructuring
// npm install --save extensible-runtime
// .babelrc
{
"presets": ["@babel/preset-env"],
"plugins": ["babel-plugin-extensible-destructuring"]
}
// src/index.js (opt-in mode by default)
'use extensible';
import { fromJS } from 'immutable';
const map = fromJS({ author: { name: { first: "John", last: "Doe" }, birthdate: "10-10-2010" } });
const { author: { name: { first, last }, birthdate } } = map;
console.log(first, last, birthdate); // John Doe 10-10-2010