4 * Compile *.es6.js files to ES5.
6 * @internal This file is part of the core javascript build process and is only
7 * meant to be used in that context.
12 const fs = require('fs');
13 const path = require('path');
14 const babel = require('babel-core');
15 const glob = require('glob');
17 // Logging human-readable timestamp.
18 const log = function (message) {
19 // eslint-disable-next-line no-console
20 console.log(`[${new Date().toTimeString().slice(0, 8)}] ${message}`);
23 function addSourceMappingUrl(code, loc) {
24 return code + '\n\n//# sourceMappingURL=' + path.basename(loc);
27 const changedOrAdded = (filePath) => {
28 babel.transformFile(filePath, {
31 }, function (err, result) {
32 const fileName = filePath.slice(0, -7);
33 // we've requested for a sourcemap to be written to disk
34 let mapLoc = `${fileName}.js.map`;
36 fs.writeFile(mapLoc, JSON.stringify(result.map));
37 fs.writeFile(`${fileName}.js`, addSourceMappingUrl(result.code, mapLoc));
39 log(`'${filePath}' is being processed.`);
43 const fileMatch = './**/*.es6.js';
45 ignore: 'node_modules/**'
47 const processFiles = (error, filePaths) => {
51 filePaths.forEach(changedOrAdded);
53 glob(fileMatch, globOptions, processFiles);