babel-plugin-transform-proto-to-assign

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

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.

error Cannot find module 'babel-plugin-transform-proto-to-assign'
cause Plugin not installed or not in node_modules.
fix
Run 'npm install --save-dev babel-plugin-transform-proto-to-assign' and ensure node_modules is present.
error Error: Plugin .__proto__ is not a function
cause Attempting to use the plugin with Babel 7 where the package name has changed.
fix
Use '@babel/plugin-transform-proto-to-assign' instead, or downgrade to Babel 6.
error TypeError: Cannot set property __proto__ of undefined
cause Assigning to __proto__ on a non-object value (null/undefined).
fix
Ensure the left-hand side of __proto__ assignment is an object.
deprecated This plugin is part of Babel 6 and has no active development. For Babel 7+, use '@babel/plugin-transform-proto-to-assign' instead.
fix Upgrade to Babel 7+ and install @babel/plugin-transform-proto-to-assign.
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.
fix Use Object.setPrototypeOf or Object.create for proper prototype chain if needed.
gotcha Modifying Object.prototype can cause performance issues and side effects in other code. Avoid if possible.
fix Use explicit property assignment or Object.assign instead of __proto__ manipulation.
breaking Only transforms assignments like `obj.__proto__ = value`. Does not handle Object.setPrototypeOf or Reflect.setPrototypeOf.
fix Add a separate plugin or polyfill if supporting Object.setPrototypeOf is required.
npm install babel-plugin-transform-proto-to-assign
yarn add babel-plugin-transform-proto-to-assign
pnpm add babel-plugin-transform-proto-to-assign

Shows installation, configuration, source code with __proto__ assignment, and compilation output.

// 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.