@storm-stack/core
Version:
A build toolkit and runtime used by Storm Software in TypeScript applications
90 lines (86 loc) • 2.82 kB
JavaScript
import { declare } from '@babel/helper-plugin-utils';
import template from '@babel/template';
import * as t from '@babel/types';
import { toArray } from '@stryke/convert';
// src/lib/babel/plugins/builtin-extend.ts
var buildHelper = template(`
function HELPER(cls){
function ExtendableBuiltin(){
// Not passing "newTarget" because core-js would fall back to non-exotic
// object creation.
var instance = Reflect.construct(cls, Array.from(arguments));
Object.setPrototypeOf(instance, Object.getPrototypeOf(this));
return instance;
}
ExtendableBuiltin.prototype = Object.create(cls.prototype, {
constructor: {
value: cls,
enumerable: false,
writable: true,
configurable: true,
},
});
if (Object.setPrototypeOf){
Object.setPrototypeOf(ExtendableBuiltin, cls);
} else {
ExtendableBuiltin.__proto__ = cls;
}
return ExtendableBuiltin;
}
`);
var buildHelperApproximate = template(`
function HELPER(cls){
function ExtendableBuiltin(){
cls.apply(this, arguments);
}
ExtendableBuiltin.prototype = Object.create(cls.prototype, {
constructor: {
value: cls,
enumerable: false,
writable: true,
configurable: true,
},
});
if (Object.setPrototypeOf){
Object.setPrototypeOf(ExtendableBuiltin, cls);
} else {
ExtendableBuiltin.__proto__ = cls;
}
return ExtendableBuiltin;
}
`);
var BuiltinExtendPlugin = declare((api, options) => {
return {
name: "storm-stack:builtin-extend",
visitor: {
Class(path) {
const globals = toArray(options.globals);
if (!globals.includes("Error")) {
globals.push("Error");
}
const superClass = path.get("superClass");
if (!superClass.node || !globals.some((name2) => superClass.isIdentifier({
name: name2
}))) {
return;
}
if (path.scope.hasBinding(superClass.node?.name, {
noGlobals: true
})) {
return;
}
const name = path.scope.generateUidIdentifier("stormExtendableBuiltin");
const helper = (this.opts.approximate ? buildHelperApproximate : buildHelper)({
HELPER: name
});
path.scope.getProgramParent().path.unshiftContainer("body", helper);
superClass.replaceWith(t.callExpression(name, [
superClass.node
]));
}
}
};
});
export { BuiltinExtendPlugin };
//# sourceMappingURL=chunk-7CO6HHJD.js.map
//# sourceMappingURL=chunk-7CO6HHJD.js.map