diff --git a/core/package.json b/core/package.json index b20516db94..11529d563c 100644 --- a/core/package.json +++ b/core/package.json @@ -4,15 +4,15 @@ "license": "GPL-2.0", "private": true, "scripts": { - "build:js": "node ./scripts/js/babel-es6-build.js", + "build:js": "BABEL_ENV=production node ./scripts/js/babel-es6-build.js", "watch:js": "node ./scripts/js/babel-es6-watch.js", "lint:core-js": "node ./node_modules/eslint/bin/eslint.js --ext=.es6.js . --fix || exit 0" }, "devDependencies": { - "babel-core": "6.24.0", - "babel-preset-es2015": "6.16.0", + "babel-preset-babili": "0.0.12", + "babel-preset-env": "1.4.0", "chokidar": "1.6.1", - "eslint": "3.18.0", + "eslint": "3.19.0", "eslint-config-airbnb": "14.1.0", "eslint-plugin-import": "2.2.0", "eslint-plugin-jsx-a11y": "4.0.0", @@ -21,7 +21,23 @@ }, "babel": { "presets": [ - "es2015" - ] + [ + "env", + { + "modules": false, + "targets": { + "browsers": [ + "last 2 versions", + "ie >= 11" + ] + } + } + ] + ], + "env": { + "production": { + "presets": ["babili"] + } + } } } diff --git a/core/scripts/js/babel-es6-build.js b/core/scripts/js/babel-es6-build.js index e073f6a431..bc7b4a2d08 100644 --- a/core/scripts/js/babel-es6-build.js +++ b/core/scripts/js/babel-es6-build.js @@ -11,44 +11,21 @@ const fs = require('fs'); const path = require('path'); -const babel = require('babel-core'); const glob = require('glob'); -// Logging human-readable timestamp. -const log = function (message) { - // eslint-disable-next-line no-console - console.log(`[${new Date().toTimeString().slice(0, 8)}] ${message}`); -}; - -function addSourceMappingUrl(code, loc) { - return code + '\n\n//# sourceMappingURL=' + path.basename(loc); -} - -const changedOrAdded = (filePath) => { - babel.transformFile(filePath, { - sourceMaps: true, - comments: false - }, function (err, result) { - const fileName = filePath.slice(0, -7); - // we've requested for a sourcemap to be written to disk - let mapLoc = `${fileName}.js.map`; - - fs.writeFile(mapLoc, JSON.stringify(result.map)); - fs.writeFile(`${fileName}.js`, addSourceMappingUrl(result.code, mapLoc)); - - log(`'${filePath}' is being processed.`); - }); -}; +const changeOrAdded = require('./changeOrAdded'); +const log = require('./log'); const fileMatch = './**/*.es6.js'; const globOptions = { - ignore: 'node_modules/**' + ignore: './node_modules/**' }; const processFiles = (error, filePaths) => { + console.log(filePaths); if (error) { process.exitCode = 1; } - filePaths.forEach(changedOrAdded); + filePaths.forEach(changeOrAdded); }; glob(fileMatch, globOptions, processFiles); process.exitCode = 0; diff --git a/core/scripts/js/babel-es6-watch.js b/core/scripts/js/babel-es6-watch.js index d614774f29..00b0c2c7be 100644 --- a/core/scripts/js/babel-es6-watch.js +++ b/core/scripts/js/babel-es6-watch.js @@ -11,41 +11,17 @@ const fs = require('fs'); const path = require('path'); -const babel = require('babel-core'); const chokidar = require('chokidar'); -// Logging human-readable timestamp. -const log = function log(message) { - // eslint-disable-next-line no-console - console.log(`[${new Date().toTimeString().slice(0, 8)}] ${message}`); -}; - -function addSourceMappingUrl(code, loc) { - return `${code}\n\n//# sourceMappingURL=${path.basename(loc)}`; -} +const changeOrAdded = require('./changeOrAdded'); +const log = require('./log'); const fileMatch = './**/*.es6.js'; const watcher = chokidar.watch(fileMatch, { ignoreInitial: true, - ignored: 'node_modules/**' + ignored: './node_modules/**' }); -const changedOrAdded = (filePath) => { - babel.transformFile(filePath, { - sourceMaps: true, - comments: false - }, (err, result) => { - const fileName = filePath.slice(0, -7); - // we've requested for a sourcemap to be written to disk - const mapLoc = `${fileName}.js.map`; - - fs.writeFileSync(mapLoc, JSON.stringify(result.map)); - fs.writeFileSync(`${fileName}.js`, addSourceMappingUrl(result.code, mapLoc)); - - log(`'${filePath}' has been changed.`); - }); -}; - const unlinkHandler = (err) => { if (err) { log(err); @@ -53,8 +29,8 @@ const unlinkHandler = (err) => { }; watcher - .on('add', filePath => changedOrAdded(filePath)) - .on('change', filePath => changedOrAdded(filePath)) + .on('add', changeOrAdded) + .on('change', changeOrAdded) .on('unlink', (filePath) => { const fileName = filePath.slice(0, -7); fs.stat(`${fileName}.js`, () => { diff --git a/core/scripts/js/changeOrAdded.js b/core/scripts/js/changeOrAdded.js new file mode 100644 index 0000000000..d9be402907 --- /dev/null +++ b/core/scripts/js/changeOrAdded.js @@ -0,0 +1,22 @@ +const fs = require('fs'); +const babel = require('babel-core'); + +const log = require('./log'); + +module.exports = (filePath) => { + const moduleName = filePath.slice(0, -7); + log(`'${filePath}' is being processed.`); + babel.transformFile( + filePath, + { + sourceMaps: process.env.BABEL_ENV ? false : 'inline', + comments: false + }, + (err, result) => { + const fileName = filePath.slice(0, -7); + fs.writeFile(`${fileName}.js`, result.code, () => { + log(`'${filePath}' is finished.`); + }); + } + ); +} diff --git a/core/scripts/js/log.js b/core/scripts/js/log.js new file mode 100644 index 0000000000..08349b3ae3 --- /dev/null +++ b/core/scripts/js/log.js @@ -0,0 +1,4 @@ +module.exports = (message) => { + // Logging human-readable timestamp. + console.log(`[${new Date().toTimeString().slice(0, 8)}] ${message}`); +}