summaryrefslogtreecommitdiffstats
path: root/core/scripts
diff options
context:
space:
mode:
authorAlex Pott2016-10-13 23:53:15 (GMT)
committerAlex Pott2016-10-13 23:53:15 (GMT)
commit46c6327f4b6b76ba147635c7c38a6e114858ffa8 (patch)
tree551d79ae78a6069ed2d3bff0efcf85dfed6aa36b /core/scripts
parent3e2e6e9c520691da62499152cc26d19486ba914a (diff)
Issue #2809477 by nod_, droplet, drpal: Add ES6 to ES5 build process
Diffstat (limited to 'core/scripts')
-rw-r--r--core/scripts/js/babel-es6-compile.sh11
-rw-r--r--core/scripts/js/babel-es6-watch.js71
-rw-r--r--core/scripts/js/rename-js-files-to-es6.sh12
3 files changed, 94 insertions, 0 deletions
diff --git a/core/scripts/js/babel-es6-compile.sh b/core/scripts/js/babel-es6-compile.sh
new file mode 100644
index 0000000..174b337
--- /dev/null
+++ b/core/scripts/js/babel-es6-compile.sh
@@ -0,0 +1,11 @@
+#!/bin/bash
+
+# Compile *.es6.js files to ES5.
+#
+# @internal This file is part of the core javascript build process and is only
+# meant to be used in that context.
+
+for js in `find ./{misc,modules,themes} -name '*.es6.js'`;
+do
+ ./node_modules/.bin/babel ${js} --out-file ${js%???????}.js --source-maps --no-comments
+done
diff --git a/core/scripts/js/babel-es6-watch.js b/core/scripts/js/babel-es6-watch.js
new file mode 100644
index 0000000..9262e9e
--- /dev/null
+++ b/core/scripts/js/babel-es6-watch.js
@@ -0,0 +1,71 @@
+/**
+ * @file
+ *
+ * Watch changes to *.es6.js files and compile them to ES5 during development.
+ *
+ * @internal This file is part of the core javascript build process and is only
+ * meant to be used in that context.
+ */
+
+'use strict';
+
+const fs = require('fs');
+const path = require('path');
+const babel = require('babel-core');
+const chokidar = require('chokidar');
+
+// 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 fileMatch = './**/*.es6.js';
+const watcher = chokidar.watch(fileMatch, {
+ ignoreInitial: true,
+ ignored: 'node_modules/**'
+});
+
+const babelOptions = {
+ sourceMaps: true,
+ comments: false
+};
+
+const changedOrAdded = (filePath) => {
+ babel.transformFile(filePath, babelOptions, 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`;
+ result.code = addSourceMappingUrl(result.code, mapLoc);
+ fs.writeFileSync(mapLoc, JSON.stringify(result.map));
+
+ fs.writeFileSync(`${fileName}.js`, result.code);
+
+ log(`'${filePath}' has been changed.`);
+ });
+};
+
+const unlinkHandler = (err) => {
+ if (err) {
+ return log(err);
+ }
+};
+
+watcher
+ .on('add', filePath => changedOrAdded(filePath))
+ .on('change', filePath => changedOrAdded(filePath))
+ .on('unlink', filePath => {
+ const fileName = filePath.slice(0, -7);
+ fs.stat(`${fileName}.js`, function () {
+ fs.unlink(`${fileName}.js`, unlinkHandler);
+ });
+ fs.stat(`${fileName}.js.map`, function () {
+ fs.unlink(`${fileName}.js.map`, unlinkHandler);
+ });
+ })
+ .on('ready', () => log(`Watching '${fileMatch}' for changes.`));
diff --git a/core/scripts/js/rename-js-files-to-es6.sh b/core/scripts/js/rename-js-files-to-es6.sh
new file mode 100644
index 0000000..5ae2a89
--- /dev/null
+++ b/core/scripts/js/rename-js-files-to-es6.sh
@@ -0,0 +1,12 @@
+#!/bin/bash
+
+# Rename *.js files in *.es6.js. Only need to be run once.
+# Should be removed after *.es6.js files are committed to core.
+#
+# @internal This file is part of the core javascript build process and is only
+# meant to be used in that context.
+
+for js in `find ./{misc,modules,themes} -name '*.js'`;
+do
+ mv ${js} ${js%???}.es6.js;
+done