Skip to content

Commit

Permalink
Add Babel Builtin Constructor extension plugin.
Browse files Browse the repository at this point in the history
This is a Babel 6 plugin to enable extending builtin types like "Error" and "Array" and such, which require special treatment and require static analysis to detect.
@ref: https://github.com/loganfsmyth/babel-plugin-transform-builtin-extend
  • Loading branch information
vash15 committed May 19, 2017
1 parent 0077d3d commit e2109c2
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 8 deletions.
8 changes: 4 additions & 4 deletions example/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"description": "",
"author": "",
"license": "",
"env": "production",
"build-date": "2017-05-11",
"API_KEY": "KEY_FOR_PRODUCTION",
"build-date-time": "2017-05-11 11:32:31"
"env": "developer",
"build-date": "2017-05-19",
"API_KEY": "PERSONAL_KEY_FOR_DEV",
"build-date-time": "2017-05-19 17:26:30"
}
30 changes: 30 additions & 0 deletions example/lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,33 @@ console.log("LoooL");
console.log(a, _.isArray(a) );

$('body').css({ "background": "red", "height":"350px"});


export class WrongCategoryError extends Error {};
export class WrongCategoryError2 extends Error {};



window.err1 = new WrongCategoryError({message: "Error 1"});
window.err2 = new WrongCategoryError2({message: "Error 2"});

console.log("%cTest extend Error class:", "color: #FFF; background: #000");


console.log("%cDeclaration:", "color: #FFF; background: #000");
console.log("export class WrongCategoryError extends Error {};");
console.log("export class WrongCategoryError2 extends Error {};");

console.log('window.err1 = new WrongCategoryError({message: "Error 1"});');
console.log('window.err2 = new WrongCategoryError2({message: "Error 2"});');

console.log("%cTest:", "color: #FFF; background: #000");
console.log( "`err1 instanceof WrongCategoryError` [TRUE]: %s", err1 instanceof WrongCategoryError ); // True
console.log( "`err1 instanceof Error` [TRUE]: %s", err1 instanceof Error ); // True
console.log( "`err1 instanceof WrongCategoryError2` [FALSE]: %s", err1 instanceof WrongCategoryError2 ); // False

console.log("`err1 == err2` [FALSE]: %s",err1 == err2 ); //False

console.log( "`err2 instanceof WrongCategoryError` [FALSE]: %s",err2 instanceof WrongCategoryError ); // False
console.log( "`err2 instanceof Error` [TRUE]: %s", err2 instanceof Error ); // True
console.log( "`err2 instanceof WrongCategoryError2` [TRUE]: %s", err2 instanceof WrongCategoryError2 ); // True
8 changes: 7 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,11 @@
"eve-build-date": "2017-4-26",
"eve-configs-development": {},
"eve-configs-production": {},
"eve-configs": {}
"eve-configs": {},
"eve-babel-plugins": {
"builtin-extend": {
"globals": ["Error"],
"approximate": false
}
}
}
9 changes: 9 additions & 0 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const UglifyJS = require('uglify-js');
const jscrambler = require('jscrambler').default;
const unzip = require('unzip');
const es2015 = require.resolve('babel-preset-es2015');
const builtinExtend = require.resolve('babel-plugin-transform-builtin-extend'); // @ref: https://www.npmjs.com/package/babel-plugin-transform-builtin-extend
const bowerrcFile = path.resolve('.bowerrc');
const factor = require('factor-bundle');
const waterfall = require('async').waterfall;
Expand Down Expand Up @@ -79,6 +80,8 @@ module.exports = function(type, unit, watcher, options, done) {
+ ("0" + now.getMinutes().toString() ).slice(-2)+':'
+ ("0" + now.getSeconds().toString() ).slice(-2);
var mapFiles = [ `${outputCommonsJsFile}.map` ];
var pkgBabelPlugins = pkg["eve-babel-plugins"] || {};


// Check if exists configs file
try{
Expand Down Expand Up @@ -201,9 +204,15 @@ module.exports = function(type, unit, watcher, options, done) {
searchES6IntoComponents(babelifyDir);

if ( babelifyDir.length > 0 ){
var babelPlugins = [];
if ( pkgBabelPlugins["builtin-extend"] ){
babelPlugins.push( [ builtinExtend, pkgBabelPlugins["builtin-extend"] ] );
}

b.transform('babelify', {
basedir: path.resolve(__dirname, "node_modules"),
presets: [ es2015 ], // @ref: https://github.com/babel/babel-loader/issues/149#issuecomment-191991686
plugins: babelPlugins,
sourceMapsAbsolute: true,
only: babelifyDir
});
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-eve",
"version": "2.3.3",
"version": "2.3.4",
"description": "Eve command line tools for browserify projects",
"bin": {
"eve": "./lib/eve.js"
Expand Down Expand Up @@ -30,6 +30,7 @@
"license": "MIT",
"dependencies": {
"async": "^2.3.0",
"babel-plugin-transform-builtin-extend": "^1.1.2",
"babel-polyfill": "^6.23.0",
"babel-preset-es2015": "^6.24.0",
"babel-preset-es2016": "^6.22.0",
Expand Down
10 changes: 8 additions & 2 deletions resources/app-template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@
},
"dependencies": {},
"devDependencies": {
"node-eve": "^2.3.3"
"node-eve": "^2.3.4"
},
"browserify-shim": {},
"eve-language": "es6",
"eve-version": "2.3.3"
"eve-version": "2.3.4",
"eve-babel-plugins": {
"builtin-extend": {
"globals": ["Error"],
"approximate": false
}
}
}

0 comments on commit e2109c2

Please sign in to comment.