diff --git a/core/package.json b/core/package.json index b20516d..120eba4 100644 --- a/core/package.json +++ b/core/package.json @@ -9,19 +9,38 @@ "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-plugin-external-helpers": "^6.22.0", + "babel-preset-env": "^1.3.3", "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", "eslint-plugin-react": "6.10.3", - "glob": "7.1.1" + "glob": "7.1.1", + "rollup": "^0.41.6", + "rollup-plugin-babel": "^2.7.1", + "rollup-plugin-babili": "^2.0.0", + "rollup-plugin-commonjs": "^8.0.2", + "rollup-plugin-node-resolve": "^3.0.0" }, "babel": { "presets": [ - "es2015" + [ + "env", + { + "modules": false, + "targets": { + "browsers": [ + "last 2 versions", + "ie >= 11" + ] + } + } + ] + ], + "plugins": [ + "external-helpers" ] } } diff --git a/core/scripts/js/babel-es6-build.js b/core/scripts/js/babel-es6-build.js index e073f6a..8e4ec74 100644 --- a/core/scripts/js/babel-es6-build.js +++ b/core/scripts/js/babel-es6-build.js @@ -11,44 +11,20 @@ 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) => { 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 d614774..00b0c2c 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 0000000..e0400b7 --- /dev/null +++ b/core/scripts/js/changeOrAdded.js @@ -0,0 +1,33 @@ +const rollup = require('rollup'); +const resolve = require('rollup-plugin-node-resolve'); +const commonjs = require('rollup-plugin-commonjs'); +const babel = require('rollup-plugin-babel'); +const babili = require('rollup-plugin-babili'); + +const log = require('./log'); + +module.exports = (filePath) => { + const moduleName = filePath.slice(0, -7); + log(`'${filePath}' is being processed.`); + rollup.rollup({ + entry: filePath, + format: 'iife', + plugins: [ + resolve({ jsnext: true, main: true }), + commonjs(), + babel({ + exclude: 'node_modules/**' + }), + babili() + ] + }) + .then(bundle => bundle.write({ + format: 'iife', + moduleName: moduleName, + dest: `${moduleName}.js`, + sourceMap: true + })) + .then(() => { + log(`'${filePath}' is finished.`); + }); +} diff --git a/core/scripts/js/log.js b/core/scripts/js/log.js new file mode 100644 index 0000000..08349b3 --- /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}`); +}