What is @babel/plugin-transform-object-assign?
@babel/plugin-transform-object-assign is a Babel plugin that transforms ES6 Object.assign() calls into a more compatible form for older environments that do not support Object.assign(). This is particularly useful for ensuring code compatibility across different JavaScript environments.
What are @babel/plugin-transform-object-assign's main functionalities?
Transform Object.assign()
This feature transforms the ES6 Object.assign() method into a compatible form for older JavaScript environments. The code sample demonstrates how Object.assign() is used to merge properties from a source object into a target object.
const target = { a: 1 };
const source = { b: 2 };
const result = Object.assign(target, source);
console.log(result); // { a: 1, b: 2 }
Other packages similar to @babel/plugin-transform-object-assign
core-js
core-js is a modular standard library for JavaScript that includes polyfills for many ECMAScript features, including Object.assign(). It provides a more comprehensive solution compared to @babel/plugin-transform-object-assign, as it covers a wider range of polyfills.
lodash.assign
lodash.assign is a method from the Lodash library that performs a similar function to Object.assign(). It is part of the larger Lodash utility library, which offers a wide range of utility functions for JavaScript. Unlike @babel/plugin-transform-object-assign, lodash.assign is not a Babel plugin but a standalone utility function.
object-assign
object-assign is a standalone npm package that provides a polyfill for Object.assign(). It is a lightweight alternative to @babel/plugin-transform-object-assign and is useful for projects that need a simple polyfill without the overhead of a full Babel plugin.
@babel/plugin-transform-object-assign
Replace Object.assign
with an inline helper. If you are authoring an application, rather than a library, it is recommended that you use the Object.assign
polyfill instead.
Example
In
Object.assign(a, b);
Out
var _extends = ...;
_extends(a, b);
Caveats
Installation
npm install --save-dev @babel/plugin-transform-object-assign
Usage
Via .babelrc
(Recommended)
.babelrc
{
"plugins": ["@babel/transform-object-assign"]
}
Via CLI
babel --plugins @babel/transform-object-assign script.js
Via Node API
require("@babel/core").transform("code", {
plugins: ["@babel/transform-object-assign"]
});