Commit 46c6327f authored by alexpott's avatar alexpott

Issue #2809477 by nod_, droplet, drpal: Add ES6 to ES5 build process

parent 3e2e6e9c
{
"extends": "eslint:recommended",
"env": {
"browser": true
"browser": true,
"es6": true,
"node": true
},
"globals": {
"Drupal": true,
......
{
"name": "Drupal",
"description": "Drupal is an open source content management platform powering millions of websites and applications.",
"license": "GPL-2.0",
"private": true,
"scripts": {
"build:js": "./scripts/js/babel-es6-compile.sh",
"watch:js": "node ./scripts/js/babel-es6-watch.js"
},
"devDependencies": {
"babel-cli": "6.16.0",
"babel-preset-es2015": "6.16.0",
"chokidar": "1.6.0"
},
"babel": {
"presets": [
"es2015"
]
}
}
#!/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
/**
* @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.`));
#!/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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment