diff --git a/.babelrc b/.babelrc index 140b318..39c1fd6 100644 --- a/.babelrc +++ b/.babelrc @@ -1,17 +1,3 @@ { - "presets": [ - "es2015" - ], - "plugins": [ - "add-module-exports", - [ - "babel-plugin-transform-builtin-extend", - { - "globals": [ - "Error" - ], - "approximate": true - } - ] - ] + "presets": "./babel-config.js" } diff --git a/babel-config.js b/babel-config.js new file mode 100644 index 0000000..4281c01 --- /dev/null +++ b/babel-config.js @@ -0,0 +1,29 @@ +'use strict'; + +var plugins = [ + [ + "babel-plugin-transform-builtin-extend", + { + "globals": ["Error"], + "approximate": true + } + ] +]; + +var env = process.env.BABEL_ENV || process.env.NODE_ENV; + +var modules; + +if (env === 'es') { + modules = false; +} else { + modules = 'commonjs'; + plugins.push('add-module-exports'); +} + +module.exports = { + "presets": [ + ["es2015", { modules: modules }] + ], + "plugins": plugins +}; diff --git a/build.js b/build.js deleted file mode 100644 index a74c7c7..0000000 --- a/build.js +++ /dev/null @@ -1,33 +0,0 @@ -'use strict'; - -var babel = require('babel-core'), - fs = require('fs'); - -// es5 build -babel.transformFile('./src/index.js', function(err, result) { - if (err) throw err; - - var filename = './lib/index.js'; - fs.writeFile(filename, result.code, onWriteCompleted(filename)); -}); - -// jsnext build - -var jsnextOpts = { - presets: [ [ 'es2015', { modules: false } ] ], - babelrc: false -}; - -babel.transformFile('./src/index.js', jsnextOpts, function(err, result) { - if (err) throw err; - - var filename = './lib/index.jsnext.js'; - fs.writeFile(filename, result.code, onWriteCompleted(filename)); -}); - -function onWriteCompleted(filename) { - return function(err) { - if (err) throw err; - console.log(filename + ' written'); - } -} diff --git a/lib/index.js b/lib/index.js index 3184e58..617bef7 100644 --- a/lib/index.js +++ b/lib/index.js @@ -76,4 +76,4 @@ var ExtendableError = function (_extendableBuiltin2) { }(_extendableBuiltin(Error)); exports.default = ExtendableError; -module.exports = exports['default']; \ No newline at end of file +module.exports = exports['default']; diff --git a/lib/index.jsnext.js b/lib/index.jsnext.js index 18765e6..70fbfbc 100644 --- a/lib/index.jsnext.js +++ b/lib/index.jsnext.js @@ -4,8 +4,31 @@ function _possibleConstructorReturn(self, call) { if (!self) { throw new Referen function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } -var ExtendableError = function (_Error) { - _inherits(ExtendableError, _Error); +function _extendableBuiltin(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 ExtendableError = function (_extendableBuiltin2) { + _inherits(ExtendableError, _extendableBuiltin2); function ExtendableError() { var message = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; @@ -44,6 +67,6 @@ var ExtendableError = function (_Error) { } return ExtendableError; -}(Error); +}(_extendableBuiltin(Error)); -export default ExtendableError; \ No newline at end of file +export default ExtendableError; diff --git a/package.json b/package.json index da942c4..eefeff6 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,9 @@ "typings": "./lib/index.d.ts", "scripts": { "test": "mocha --compilers js:babel-core/register --recursive", - "build": "node build.js", + "build": "npm run build:commonjs && npm run build:es", + "build:commonjs": "cross-env BABEL_ENV=commonjs babel src/index.js --out-file lib/index.js", + "build:es": "cross-env BABEL_ENV=es babel src/index.js --out-file lib/index.jsnext.js", "prepublish": "npm run build && npm run test" }, "repository": { @@ -32,6 +34,7 @@ "babel-plugin-transform-builtin-extend": "^1.1.0", "babel-preset-es2015": "^6.6.0", "chai": "^3.2.0", + "cross-env": "^3.1.3", "mocha": "^2.4.5" }, "dependencies": {}