babel-plugin-transform-object-spread-inline
raw JSON → 0.0.3 verified Fri May 01 auth: no javascript
A Babel plugin that transpiles object spread syntax into inline assignments and for loops using Object.keys, avoiding the slower for...in with hasOwnProperty check. Version 0.0.3 appears to be the latest, with no recent updates. It provides a performance alternative to the default babel-plugin-transform-object-rest-spread by generating faster code for object spreads, especially when only one spread is used. Note that it does not handle object rest syntax and may not be suitable for multiple spreads per object.
Common errors
error Error: Cannot find module 'babel-plugin-transform-object-spread-inline' ↓
cause Plugin not installed or not in node_modules.
fix
Run: npm install --save-dev babel-plugin-transform-object-spread-inline
error SyntaxError: Unexpected token ... ↓
cause Babel is not configured to use the plugin, or the source contains rest syntax not spread.
fix
Ensure the plugin is in .babelrc 'plugins' array and check for rest patterns; use separate plugin for rest.
Warnings
gotcha This plugin does not transpile object rest syntax (e.g., const { a, ...rest } = obj). ↓
fix Use a separate plugin like @babel/plugin-proposal-object-rest-spread for rest syntax.
gotcha Using multiple spread operators in a single object literal will generate multiple loops, potentially negating performance benefits. ↓
fix Minimize the number of spreads per object, or avoid this plugin if many spreads are used.
deprecated The plugin has not been updated recently and may not be compatible with newer Babel versions (e.g., Babel 7+). ↓
fix Consider using @babel/plugin-proposal-object-rest-spread with built-in optimization or use modern Babel presets.
gotcha The plugin's transformation uses Object.keys, which does not include inherited enumerable properties, unlike for...in. ↓
fix If you need inherited properties, do not use this plugin and stick with the standard plugin.
Install
npm install babel-plugin-transform-object-spread-inline yarn add babel-plugin-transform-object-spread-inline pnpm add babel-plugin-transform-object-spread-inline Imports
- default wrong
require('babel-plugin-transform-object-spread-inline')correct// Add to .babelrc plugins array: "transform-object-spread-inline" - default (from npm) wrong
npm install babel-plugin-transform-object-spread-inlinecorrectnpm install --save-dev babel-plugin-transform-object-spread-inline
Quickstart
// Install plugin
npm install --save-dev babel-plugin-transform-object-spread-inline
// .babelrc configuration
{
"plugins": ["transform-object-spread-inline"]
}
// Input source
const a = { b, c, ...d, e, f: 42 };
// Output (compiled)
"use strict";
var _keys, _l, _i, _source, _key, _result = {};
_result.b = b;
_result.c = c;
for (_source = d, _keys = Object.keys(_source), _l = _keys.length, _i = 0; _i < _l; _i++) {
_key = _keys[_i];
_result[_key] = _source[_key];
}
_result.e = e;
_result.f = 42;
a = _result;