babel-plugin-transform-replace-object-assign

raw JSON →
2.0.0 verified Sat Apr 25 auth: no javascript deprecated

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.

error Error: Could not find plugin "transform-replace-object-assign". Ensure there is an entry in ./node_modules/babel-plugin-transform-replace-object-assign
cause Plugin not installed or not listed correctly in Babel config.
fix
Run: npm install --save-dev babel-plugin-transform-replace-object-assign
error Error: Cannot find module 'object-assign'
cause Default moduleSpecifier 'object-assign' not installed as a runtime dependency.
fix
Run: npm install --save object-assign
error TypeError: Object(...) is not a function (or similar at runtime if object-assign not imported properly)
cause Plugin transforms code but the custom assign package is not available at runtime because it's only in devDependencies or missing.
fix
Ensure the moduleSpecifier package is in dependencies, not devDependencies.
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.
fix Use native Object.assign (ES6) or babel-plugin-transform-object-assign if you need runtime shimming.
gotcha The moduleSpecifier option must be a package available at runtime; if omitted, defaults to 'object-assign', which must be installed as a dependency.
fix Ensure the specified package is in your dependencies and correct.
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.
fix If you need to replace all uses, consider using a different approach like babel-plugin-transform-object-assign which uses the _extends helper.
breaking Major version 2.0.0 requires @babel/core ^7.0.0 (Babel 7) and is not compatible with Babel 6.
fix If using Babel 6, stick with version 1.x of this plugin.
deprecated Plugin has not been updated since 2018 — no fix for potential future Babel core changes.
fix Consider alternatives like babel-plugin-transform-object-assign or @babel/plugin-transform-object-assign (Babel 7 core).
npm install babel-plugin-transform-replace-object-assign
yarn add babel-plugin-transform-replace-object-assign
pnpm add babel-plugin-transform-replace-object-assign

Install the plugin and a custom Object.assign implementation, configure Babel, and see how Object.assign is replaced with an import of the custom package.

// 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"]
}