Replace Object.assign with an inline helper
npx @tessl/cli install tessl/npm-babel-plugin-transform-object-assign@6.22.0A Babel plugin that replaces Object.assign calls with an inline helper function for improved compatibility with older JavaScript environments. This plugin performs build-time transformations, eliminating the need for runtime polyfills while maintaining the same API.
npm install --save-dev babel-plugin-transform-object-assignThis is a Babel plugin, so it's not imported directly in your code. Instead, it's configured in your Babel setup:
Via .babelrc (Recommended):
{
"plugins": ["transform-object-assign"]
}Via Node API:
require("babel-core").transform("code", {
plugins: ["transform-object-assign"]
});The plugin automatically transforms Object.assign calls during compilation:
Input:
Object.assign(a, b);
Object.assign({}, defaults, options);
Object['assign'](target, source);Output:
var _extends = /* inline helper function */;
_extends(a, b);
_extends({}, defaults, options);
_extends(target, source);Configure the plugin in your .babelrc file:
{
"plugins": ["transform-object-assign"]
}Use the plugin via the Babel CLI:
babel --plugins transform-object-assign script.jsUse the plugin programmatically with babel-core:
const babel = require("babel-core");
const result = babel.transform("Object.assign(a, b);", {
plugins: ["transform-object-assign"]
});The main export function that creates the Babel plugin configuration.
/**
* Creates a Babel plugin that transforms Object.assign calls
* @returns {Object} Babel plugin configuration object
*/
export default function(): BabelPlugin;
interface BabelPlugin {
visitor: {
CallExpression: (path: BabelPath, file: BabelFile) => void;
};
}The plugin uses Babel's AST visitor pattern to transform Object.assign calls:
/**
* Visitor method that processes CallExpression nodes
* @param {BabelPath} path - Babel AST path object for the CallExpression
* @param {BabelFile} file - Babel file object with helper methods
*/
CallExpression(path: BabelPath, file: BabelFile): void;Transformation Logic:
Object.assign or Object['assign'] patternsextends helper functionThe plugin transforms these patterns:
// Direct property access
Object.assign(target, source);
// Bracket notation
Object['assign'](target, source);
// Multiple arguments
Object.assign({}, defaults, options, overrides);These patterns are not transformed:
// Destructured assignment
const { assign } = Object;
assign(target, source);
// Variable assignment
const assign = Object.assign;
assign(target, source);
// Method call on variable
const obj = Object;
obj.assign(target, source);interface Dependencies {
"babel-runtime": "^6.22.0";
}The plugin requires babel-runtime for the injected helper functions.
CallExpression nodes in the ASTpath.get("callee").matchesPattern("Object.assign") to identify target callsfile.addHelper("extends")The plugin injects an _extends helper function that provides the same functionality as Object.assign:
var _extends = Object.assign || function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};The plugin operates at build-time and doesn't throw runtime errors. Potential issues:
babel-runtime will cause build failuresObject.assign patterns are left untransformedIdeal for libraries that need to support older browsers:
// Source code
function mergeOptions(defaults, options) {
return Object.assign({}, defaults, options);
}
// Transformed output (compatible with older browsers)
function mergeOptions(defaults, options) {
return _extends({}, defaults, options);
}Enables use of Object.assign syntax while maintaining IE compatibility:
// Write modern code
const config = Object.assign({
timeout: 5000,
retries: 3
}, userOptions);
// Automatically transformed for legacy support
const config = _extends({
timeout: 5000,
retries: 3
}, userOptions);interface BabelPath {
get(property: string): BabelPath;
matchesPattern(pattern: string): boolean;
node: ASTNode;
}
interface BabelFile {
addHelper(name: string): ASTNode;
}
interface ASTNode {
callee: ASTNode;
type: string;
}