babel-plugin-transform-proto-to-assign
raw JSON →This Babel plugin transforms assignments to __proto__ into a shallow property copy, enabling prototype-like inheritance in environments that do not support __proto__ or Object.setPrototypeOf. The latest stable version is 6.26.0 (2017-01-30), part of Babel 6.x. It is maintained as part of the Babel monorepo but is considered legacy; Babel 7+ discourages use of this plugin and instead recommends using Object.setPrototypeOf or Object.create for performance reasons. The plugin does a shallow clone of all enumerable own properties from the target prototype. It works only with assignments like `obj.__proto__ = proto`, not `Object.setPrototypeOf`. It modifies Object.prototype, which can cause side effects. It is a single-purpose transform, not part of any preset-env. Alternative: use `@babel/plugin-transform-proto-to-assign` for Babel 7. The plugin is deprecated in favor of modern Object.setPrototypeOf.
Common errors
error Cannot find module 'babel-plugin-transform-proto-to-assign' ↓
error Error: Plugin .__proto__ is not a function ↓
error TypeError: Cannot set property __proto__ of undefined ↓
Warnings
deprecated This plugin is part of Babel 6 and has no active development. For Babel 7+, use '@babel/plugin-transform-proto-to-assign' instead. ↓
gotcha The transform does a shallow copy of properties at the moment of assignment, not a live prototype chain. Changing the prototype later does not affect already-assigned properties. ↓
gotcha Modifying Object.prototype can cause performance issues and side effects in other code. Avoid if possible. ↓
breaking Only transforms assignments like `obj.__proto__ = value`. Does not handle Object.setPrototypeOf or Reflect.setPrototypeOf. ↓
Install
npm install babel-plugin-transform-proto-to-assign yarn add babel-plugin-transform-proto-to-assign pnpm add babel-plugin-transform-proto-to-assign Imports
- default (plugin) wrong
import p from 'babel-plugin-transform-proto-to-assign';correctmodule.exports = require('babel-plugin-transform-proto-to-assign'); - babel-core transform with plugin wrong
require('babel-core').transform(code, { plugins: [require('babel-plugin-transform-proto-to-assign')] }); // may work but string name is preferredcorrectrequire('babel-core').transform(code, { plugins: ['transform-proto-to-assign'] }); - .babelrc configuration wrong
{ "plugins": ["babel-plugin-transform-proto-to-assign"] } // also valid, but shorter name without prefix is typicalcorrect{ "plugins": ["transform-proto-to-assign"] }
Quickstart
// Step 1: Install the plugin
// npm install --save-dev babel-plugin-transform-proto-to-assign
// Step 2: Create .babelrc with the plugin
// {
// "plugins": ["transform-proto-to-assign"]
// }
// Step 3: Write source with __proto__ assignment
var foo = { a: 1 };
var bar = { b: 2 };
bar.__proto__ = foo;
// Step 4: Compile with Babel
// npx babel script.js --out-file compiled.js
// compiled.js output (simplified):
// var _defaults = ...;
// _defaults(bar, foo);
// Note: Does not support dynamic prototype chain; only shallow copy of properties at assignment time.