babel-plugin-transform-replace-object-assign
raw JSON →This Babel plugin replaces all `Object.assign` calls with a custom implementation specified via the `moduleSpecifier` option, importing the shim from an npm package. At version 2.0.0 (latest release, last updated in 2018), the plugin requires `@babel/core@^7.0.0` as a peer dependency. It is similar to `babel-plugin-transform-object-assign` but allows using an external package (e.g., `object-assign`) instead of Babel's internal `_extends` helper, reducing bundle size by avoiding per-file function declarations. However, the plugin is explicitly deprecated by the author: the original Chrome bug it addressed (V8 issue #4118) has been fixed since Chrome 49. The plugin remains functional but is discouraged for new projects; developers should consider native `Object.assign` or `babel-plugin-transform-object-assign` instead.
Common errors
error Error: Could not find plugin "transform-replace-object-assign". Ensure there is an entry in ./node_modules/babel-plugin-transform-replace-object-assign ↓
error Error: Cannot find module 'object-assign' ↓
error TypeError: Object(...) is not a function (or similar at runtime if object-assign not imported properly) ↓
Warnings
deprecated Plugin explicitly deprecated by author: original Chrome bug (V8 #4118) fixed in Chrome 49, making plugin unnecessary for its original purpose. Author advises against use unless you have a specific need. ↓
gotcha The moduleSpecifier option must be a package available at runtime; if omitted, defaults to 'object-assign', which must be installed as a dependency. ↓
gotcha Plugin only replaces direct calls to Object.assign (e.g., Object.assign(a, b)) — not polyfills or re-assignments. Code that uses Object.assign as a value (e.g., var assign = Object.assign) will not be transformed. ↓
breaking Major version 2.0.0 requires @babel/core ^7.0.0 (Babel 7) and is not compatible with Babel 6. ↓
deprecated Plugin has not been updated since 2018 — no fix for potential future Babel core changes. ↓
Install
npm install babel-plugin-transform-replace-object-assign yarn add babel-plugin-transform-replace-object-assign pnpm add babel-plugin-transform-replace-object-assign Imports
- transform-replace-object-assign wrong
plugins: ["transform-replace-object-assign"] (will use default moduleSpecifier, but may not be intended)correctplugins: [["transform-replace-object-assign", { "moduleSpecifier": "object-assign" }]] - object-assign (runtime package) wrong
npm install --save-dev object-assign (should be regular dependency if used at runtime)correctnpm install --save object-assign - object-assign (import in output) wrong
import _objectAssign from 'object-assign'; (no common mistake, but output is auto-generated)correctimport _objectAssign from 'object-assign';
Quickstart
// Install
npm install --save-dev @babel/core babel-plugin-transform-replace-object-assign
npm install --save object-assign
// .babelrc
{
"plugins": [
["transform-replace-object-assign", { "moduleSpecifier": "object-assign" }]
]
}
// Input: src/index.js
Object.assign({}, { foo: 'bar' });
// Build output (after Babel transform):
import _objectAssign from 'object-assign';
_objectAssign({}, { foo: 'bar' });
// Note: Default option is { "moduleSpecifier": "object-assign" } so config can be simplified to:
{
"plugins": ["transform-replace-object-assign"]
}