Transforms new.target meta property for environments that don't natively support it
npx @tessl/cli install tessl/npm-babel--plugin-transform-new-target@7.27.0A Babel plugin that transforms the new.target meta property for environments that don't natively support it. This plugin converts new.target expressions to equivalent conditional expressions that check if the current function was called with the new operator.
npm install --save-dev @babel/plugin-transform-new-target// ES Modules
import pluginTransformNewTarget from "@babel/plugin-transform-new-target";
// CommonJS
const pluginTransformNewTarget = require("@babel/plugin-transform-new-target");
// For plugin development (internal dependencies)
import { declare } from "@babel/helper-plugin-utils";
import { types as t, type NodePath } from "@babel/core";Add the plugin to your Babel configuration:
babel.config.js:
module.exports = {
plugins: ["@babel/plugin-transform-new-target"]
};With options (using the default export):
import pluginTransformNewTarget from "@babel/plugin-transform-new-target";
export default {
plugins: [pluginTransformNewTarget]
};Programmatic usage:
import { transform } from "@babel/core";
import pluginTransformNewTarget from "@babel/plugin-transform-new-target";
const result = transform(code, {
plugins: [pluginTransformNewTarget]
});Creates a Babel transformation plugin that processes new.target expressions.
/**
* Default export - Babel plugin factory function created with declare() helper
* Uses declare() from @babel/helper-plugin-utils for plugin creation
* @param api - Babel API object with version assertion and utilities
* @returns Babel plugin object with visitor methods
*/
export default declare((api: BabelAPI) => PluginObj);
// The declare function signature (from @babel/helper-plugin-utils)
function declare(builder: (api: BabelAPI) => PluginObj): (api: BabelAPI) => PluginObj;
interface BabelAPI {
assertVersion(version: number | string): void;
}
// Global utility function used internally by Babel plugins
type REQUIRED_VERSION = (version: number | string) => number | string;
interface PluginObj {
name: string;
visitor: VisitorObject;
}
interface VisitorObject {
MetaProperty(path: NodePath): void;
}The plugin transforms new.target expressions based on their context:
// Input
class Foo {
constructor() {
new.target;
}
}
// Output
class Foo {
constructor() {
this.constructor;
}
}// Input
function Foo() {
new.target;
}
// Output
function Foo() {
this instanceof Foo ? this.constructor : void 0;
}
// Method assignment gets unique generated identifier
Foo.prototype.test = function _target() {
this instanceof _target ? this.constructor : void 0;
};// Input
var Bar = function() {
new.target;
};
// Output (plugin generates unique identifier automatically)
var Bar = function _target() {
this instanceof _target ? this.constructor : void 0;
};// Input
class Foo {
test() {
new.target;
}
}
// Output
class Foo {
test() {
void 0;
}
}The plugin validates new.target usage and throws build-time errors for invalid contexts:
new.target in arrow functions throws an errornew.target outside of functions/classes throws an errorThis plugin requires zero configuration and takes no options. It automatically processes all new.target expressions found in the code according to the transformation rules above. Simply add it to your Babel plugins array and it will handle all transformations automatically.
// Internal types used by the plugin (not exported)
interface NodePath {
get(key: string): NodePath;
isIdentifier(opts?: { name: string }): boolean;
findParent(callback: (path: NodePath) => boolean): NodePath | null;
buildCodeFrameError(message: string): Error;
replaceWith(node: Node): void;
scope: Scope;
}
interface Scope {
buildUndefinedNode(): Node;
generateUidIdentifier(name: string): Identifier;
hasOwnBinding(name: string): boolean;
bindingIdentifierEquals(name: string, node: Node): boolean;
rename(name: string): void;
parent: Scope;
}