What is @babel/plugin-syntax-object-rest-spread?
The @babel/plugin-syntax-object-rest-spread package allows Babel to parse and handle the object rest/spread syntax. This syntax enables the extraction of properties from objects and the creation of new objects with certain properties of an existing object. It is important to note that this plugin only enables the syntax and does not transform it; the actual transformation is handled by another plugin like @babel/plugin-proposal-object-rest-spread.
What are @babel/plugin-syntax-object-rest-spread's main functionalities?
Object Rest Properties
Allows extracting the remaining own enumerable property keys that are not already picked off by the destructuring pattern.
{ ...rest } = object
Object Spread Properties
Enables the creation of a new object by copying own enumerable properties from a provided object onto the new object.
{ ...object }
Other packages similar to @babel/plugin-syntax-object-rest-spread
@babel/plugin-proposal-object-rest-spread
This package not only allows Babel to parse the object rest/spread syntax but also transforms it. It is the complement to @babel/plugin-syntax-object-rest-spread, which only enables the syntax.
babel-plugin-transform-object-rest-spread
This is a legacy plugin that was used before the proposal reached stage 4 and was included in the official ECMAScript standard. It both enabled the syntax and transformed it, similar to @babel/plugin-proposal-object-rest-spread.
@babel/plugin-syntax-object-rest-spread
Allow parsing of object rest/spread.
Installation
npm install --save-dev @babel/plugin-syntax-object-rest-spread
Usage
Via .babelrc
(Recommended)
.babelrc
{
"plugins": ["@babel/plugin-syntax-object-rest-spread"]
}
Via CLI
babel --plugins @babel/plugin-syntax-object-rest-spread script.js
Via Node API
require("@babel/core").transform("code", {
plugins: ["@babel/plugin-syntax-object-rest-spread"]
});