A Babel plugin that transforms ES2015 (ES6) spread syntax into ES5-compatible code. It handles three main transformation scenarios: array expressions with spread elements, function calls with spread arguments, and constructor calls with spread arguments.
babel-plugin-transform-es2015-spreadnpm install --save-dev babel-plugin-transform-es2015-spreadThe plugin is used as a Babel plugin configuration rather than being imported directly:
// Via .babelrc configuration
{
"plugins": ["transform-es2015-spread"]
}
// Via .babelrc with options
{
"plugins": [
["transform-es2015-spread", {
"loose": true
}]
]
}
// Via Node API
require("babel-core").transform("code", {
plugins: ["transform-es2015-spread"]
});This plugin automatically transforms spread syntax during Babel compilation:
// Input ES2015 code with spread syntax
const arr1 = [1, 2, 3];
const arr2 = [0, ...arr1, 4]; // Array spread
function fn(a, b, c) {
return a + b + c;
}
fn(...arr1); // Function call spread
class MyClass {
constructor(a, b, c) {
this.values = [a, b, c];
}
}
new MyClass(...arr1); // Constructor spread// Output ES5 compatible code (generated by plugin)
var arr1 = [1, 2, 3];
var arr2 = [0].concat(babelHelpers.toConsumableArray(arr1), [4]);
function fn(a, b, c) {
return a + b + c;
}
fn.apply(undefined, babelHelpers.toConsumableArray(arr1));
var MyClass = function MyClass(a, b, c) {
this.values = [a, b, c];
};
new (Function.prototype.bind.apply(MyClass, [null].concat(babelHelpers.toConsumableArray(arr1))))();function pluginFactory({ types: t });The main export is a plugin factory function that accepts Babel's types object and returns a plugin configuration.
Parameters:
{ types: t } - Babel types utility object for AST manipulationReturns: Plugin configuration object with visitor methods
interface PluginOptions {
loose?: boolean;
}booleanfalsetrue, assumes all iterables are arrays for more efficient transformations. In loose mode, the plugin generates direct array access instead of using the toConsumableArray helper, resulting in smaller and faster code but breaking ES6 semantics for non-array iterables.Usage:
{
"plugins": [
["transform-es2015-spread", { "loose": true }]
]
}Loose Mode Output Comparison:
// Input
[...items]
// Strict mode (default)
[].concat(babelHelpers.toConsumableArray(items))
// Loose mode
[].concat(items)This plugin uses Babel's helper system to generate robust spread transformations. The key helper used is toConsumableArray:
// The toConsumableArray helper provides proper ES6 spread semantics
function toConsumableArray(arr) {
if (Array.isArray(arr)) {
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) {
arr2[i] = arr[i];
}
return arr2;
} else {
return Array.from(arr);
}
}Helper Output Modes:
babel-plugin-external-helpers: Uses babelHelpers.toConsumableArray()_toConsumableArrayThe helper ensures:
Transforms array expressions containing spread elements into ES5 concat operations with helper functions:
// Input
[a, ...b, c]
// Output
[a].concat(babelHelpers.toConsumableArray(b), [c])Transforms function calls with spread arguments into ES5 apply calls with helper functions:
// Input
fn(...args)
// Output
fn.apply(undefined, babelHelpers.toConsumableArray(args))
// Input (method call)
obj.method(...args)
// Output (preserves this context)
obj.method.apply(obj, babelHelpers.toConsumableArray(args))Transforms constructor calls with spread arguments using Function.prototype.bind.apply with helper functions:
// Input
new Constructor(...args)
// Output
new (Function.prototype.bind.apply(Constructor, [null].concat(babelHelpers.toConsumableArray(args))))()The plugin returns a Babel plugin configuration with the following visitor methods:
// Plugin returns an object with visitor methods
{
visitor: {
ArrayExpression(path, state) { /* transforms array expressions */ },
CallExpression(path, state) { /* transforms function calls */ },
NewExpression(path, state) { /* transforms constructor calls */ }
}
}
// Plugin state object structure
{
opts: {
loose?: boolean
}
}Handles transformation of array expressions containing spread elements.
Handles transformation of function calls containing spread arguments. Skips super() calls which require special handling.
Handles transformation of constructor calls containing spread arguments.
The plugin safely handles edge cases:
super() calls appropriately by skipping themthis contextarguments object efficiently (uses direct arguments instead of toConsumableArray(arguments))This plugin is part of Babel 6.x and transforms ES2015 spread syntax to work in ES5 environments. It's designed for maximum compatibility when transpiling modern JavaScript to run in older environments that don't support native spread syntax.