loganfsmyth / babel-plugin-transform-builtin-extend Goto Github PK
View Code? Open in Web Editor NEWA plugin for Babel 6 supports extending from builtin types based on static analysis.
License: MIT License
A plugin for Babel 6 supports extending from builtin types based on static analysis.
License: MIT License
npm release is 1.1.2 but corresponding tag is missing from repo. Please add the tag.
Tried using the plugin here: https://github.com/pureprofile-warp/babel-preset-pp-node4/blob/master/index.js but on start I get an error:
C:\dev\pureprofile\ah-api-web\node_modules\babel-core\lib\transformation\plugin.js:125
throw new Error(messages.get("pluginInvalidProperty", loc, i, key));
^
Error: Plugin 0 specified in "C:\\dev\\pureprofile\\ah-api-web\\node_modules\\babel-preset-pp-node4\\index.js" provided an invalid property of "default"
at Plugin.init (C:\dev\pureprofile\ah-api-web\node_modules\babel-core\lib\transformation\plugin.js:125:13)
at Function.normalisePlugin (C:\dev\pureprofile\ah-api-web\node_modules\babel-core\lib\transformation\file\options\option-manager.js:167:12)
at C:\dev\pureprofile\ah-api-web\node_modules\babel-core\lib\transformation\file\options\option-manager.js:197:30
at Array.map (native)
at Function.normalisePlugins (C:\dev\pureprofile\ah-api-web\node_modules\babel-core\lib\transformation\file\options\option-manager.js:173:20)
at OptionManager.mergeOptions (C:\dev\pureprofile\ah-api-web\node_modules\babel-core\lib\transformation\file\options\option-manager.js:271:36)
at OptionManager.mergePresets (C:\dev\pureprofile\ah-api-web\node_modules\babel-core\lib\transformation\file\options\option-manager.js:325:16)
at OptionManager.mergeOptions (C:\dev\pureprofile\ah-api-web\node_modules\babel-core\lib\transformation\file\options\option-manager.js:287:12)
at OptionManager.addConfig (C:\dev\pureprofile\ah-api-web\node_modules\babel-core\lib\transformation\file\options\option-manager.js:221:10)
at OptionManager.findConfigs (C:\dev\pureprofile\ah-api-web\node_modules\babel-core\lib\transformation\file\options\option-manager.js:364:16)
[nodemon] app crashed - waiting for file changes before starting...
After adding the plugin I have the error in terminal
ReferenceError: Reflect is not defined
....
I'm using this plugin for correct extending Error class, and when I try to throw an error I have error "Reflect is not defined"
After a quick investigation I've noticed that you are using Reflect
any ideas how to fix it?
At https://gist.github.com/glasserc/be6ffc197fc961bd7f0ea3e9fee3cfd5 I have posted a stripped-down example of what I'm facing in a project at work. I have a src file which has some subclasses of Error
:
class Error1 extends Error {}
class Error2 extends Error {}
I'm using transform-builtin-extend
as well as transform-runtime
(because this is a library that uses async/await).
{
"env": {
"development": {
"plugins": [
["transform-builtin-extend", {
"globals": ["Error"]
}],
"transform-runtime",
"transform-es2015-classes",
"transform-es2015-modules-commonjs"
]
}
}
}
(Here I have removed the es2015 plugins that don't appear to be relevant to the problem.)
The compiled output for the two Error subclasses is strange in that builtins are transformed inconsistently, with the first one referring to _create
, _from
, _getPrototypeOf
, etc.:
function _extendableBuiltin(cls) {
function ExtendableBuiltin() {
var instance = (0, _construct2.default)(cls, (0, _from2.default)(arguments));
(0, _setPrototypeOf2.default)(instance, (0, _getPrototypeOf2.default)(this));
return instance;
}
ExtendableBuiltin.prototype = (0, _create2.default)(cls.prototype, {
constructor: {
value: cls,
enumerable: false,
writable: true,
configurable: true
}
});
if (_setPrototypeOf2.default) {
(0, _setPrototypeOf2.default)(ExtendableBuiltin, cls);
} else {
ExtendableBuiltin.__proto__ = cls;
}
return ExtendableBuiltin;
}
However, the second error is transformed differently, referring to _Object$create
, _Array$from
, _Object$getPrototypeOf
, etc:
function _extendableBuiltin3(cls) {
function ExtendableBuiltin() {
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;
}
This causes errors when I use this library in other projects, because _Object$create
is not defined anywhere.
Changing the order of transform-builtin-extend
and transform-runtime
in .babelrc does not appear to change the compiled output. Indeed, moving transform-runtime
to the end of the list also doesn't seem to change anything.
Removing transform-runtime
from .babelrc
(leaving transform-builtin-extend
and the es2015
plugins) generates _extendableBuiltin3
and _extendableBuiltin
which are identical, and use Object.create
, Array.from
, Object.getPrototypeOf
, etc.
function _extendableBuiltin(cls) {
function ExtendableBuiltin() {
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;
}
Taking this compiled output and saving it in index.js and running it through babel without transform-builtin-extend
(but leaving transform-runtime
and the es2015
plugins) seems to generate sensible output (with _create2.default
, _from2.default
, _getPrototypeOf2.default
used in call sites in both _extendableBuiltin
and _extendableBuiltin3
).
For completeness, rerunning the original index.js through Babel without transform-es2015-modules-commonjs
(but leaving transform-runtime
and transform-builtin-extend
as well as transform-es2015-classes
) also seems to generate sensible output (with _Object$create
, _Array$from
, and _Object$getPrototypeOf
being used in both functions after importing them using e.g. import _Object$create from "babel-runtime/core-js/object/create";
).
In this experiment, the _Object$create
behavior only seems to happen to the second exception subclass (even if you add other subclasses afterwards), but in my real project, it happened to both the second and the fourth.
Based on all of this, I conclude that there's some bug that arises from an interaction between transform-builtin-extend
, transform-runtime
, and the es2015
plugins, but I'm not sure how to characterize that interaction and I'm not really sure how to debug further, though. Do you have any idea what's going on?
var instance = Reflect.construct(cls, Array.from(arguments));
^ReferenceError: Reflect is not defined
I've discovered that subclasses of Error
work significantly better (i.e. better stack traces; more like regular Error
behavior) in Chrome and Firefox when using native classes. Below is some example code of how I would approach this in native JS (without Babel).
I'm wondering how to best incorporate this into a Babel workflow. Would it make sense to add this as an option for this plugin? (I could submit a pull request.) Or should it be its own separate plugin? It occurred to me that I could simply tell Babel to ignore the files with my custom error classes, but then I'd no longer be able to easily include those files with the rest of my source code in a single bundle (or at least I don't think so).
var supportsClasses = false;
try {
eval('class X{}');
supportsClasses = true;
} catch (e) {}
var CustomError;
if (supportsClasses) {
CustomError = class CustomError extends Error {
constructor(message) {
super(message);
this.name = 'CustomError';
}
}
}
else {
CustomError = function CustomError(message) {
"use strict";
this.message = message;
if (Error.captureStackTrace) {
Error.captureStackTrace(this, CustomError);
}
else {
this.stack = (new Error).stack;
}
}
CustomError.prototype = Object.create(Error.prototype);
CustomError.prototype.name = 'CustomError';
}
I try to extend the Date object and get following error:
ReferenceError: Reflect is not defined
and it highlights following line:
var instance = Reflect.construct(cls, Array.from(arguments));
^
The file:
export default class Instant extends Date {
constructor () {
super()
}
}
This plugin will only reles on assigning
__proto__
...
I'm unsure what the meaning of "reles" should be.
Thanks.
>
> npm test
> [email protected] test /mnt/sda5/Resources/temp/babel-plugin-transform-builtin-extend
> babel-node node_modules/.bin/_mocha --recursive test
/mnt/sda5/Resources/temp/babel-plugin-transform-builtin-extend/node_modules/babel-core/lib/transformation/file/options/option-manager.js:176
throw new ReferenceError(messages.get("pluginUnknown", plugin, loc, i, dirname));
^
ReferenceError: Unknown plugin "../" specified in "/mnt/sda5/Resources/temp/babel-plugin-transform-builtin-extend/test/approximate/.babelrc" at 0, attempted to resolve relative to "/mnt/sda5/Resources/temp/babel-plugin-transform-builtin-extend/test/approximate"
at /mnt/sda5/Resources/temp/babel-plugin-transform-builtin-extend/node_modules/babel-core/lib/transformation/file/options/option-manager.js:176:17
at Array.map (native)
at Function.normalisePlugins (/mnt/sda5/Resources/temp/babel-plugin-transform-builtin-extend/node_modules/babel-core/lib/transformation/file/options/option-manager.js:154:20)
at OptionManager.mergeOptions (/mnt/sda5/Resources/temp/babel-plugin-transform-builtin-extend/node_modules/babel-core/lib/transformation/file/options/option-manager.js:229:36)
at OptionManager.init (/mnt/sda5/Resources/temp/babel-plugin-transform-builtin-extend/node_modules/babel-core/lib/transformation/file/options/option-manager.js:374:12)
at compile (/mnt/sda5/Resources/temp/babel-plugin-transform-builtin-extend/node_modules/babel-register/lib/node.js:103:45)
at loader (/mnt/sda5/Resources/temp/babel-plugin-transform-builtin-extend/node_modules/babel-register/lib/node.js:144:14)
at Object.require.extensions.(anonymous function) [as .js] (/mnt/sda5/Resources/temp/babel-plugin-transform-builtin-extend/node_modules/babel-register/lib/node.js:154:7)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at /mnt/sda5/Resources/temp/babel-plugin-transform-builtin-extend/node_modules/mocha/lib/mocha.js:220:27
at Array.forEach (native)
at Mocha.loadFiles (/mnt/sda5/Resources/temp/babel-plugin-transform-builtin-extend/node_modules/mocha/lib/mocha.js:217:14)
at Mocha.run (/mnt/sda5/Resources/temp/babel-plugin-transform-builtin-extend/node_modules/mocha/lib/mocha.js:469:10)
at Object.<anonymous> (/mnt/sda5/Resources/temp/babel-plugin-transform-builtin-extend/node_modules/mocha/bin/_mocha:404:18)
at Module._compile (module.js:409:26)
at Module._extensions..js (module.js:416:10)
at Object.require.extensions.(anonymous function) [as .js] (/mnt/sda5/Resources/temp/babel-plugin-transform-builtin-extend/node_modules/babel-register/lib/node.js:152:7)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at /mnt/sda5/Resources/temp/babel-plugin-transform-builtin-extend/node_modules/babel-cli/lib/_babel-node.js:159:24
at Object.<anonymous> (/mnt/sda5/Resources/temp/babel-plugin-transform-builtin-extend/node_modules/babel-cli/lib/_babel-node.js:160:7)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:974:3
npm ERR! Test failed. See above for more details.
This should be mentioned under "Limitations" in the readme. The work-around is to compare with the name
property instead.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.