Version 1
[yaffs-website] / web / core / scripts / js / babel-es6-build.js
1 /**
2  * @file
3  *
4  * Compile *.es6.js files to ES5.
5  *
6  * @internal This file is part of the core javascript build process and is only
7  * meant to be used in that context.
8  */
9
10 'use strict';
11
12 const fs = require('fs');
13 const path = require('path');
14 const babel = require('babel-core');
15 const glob = require('glob');
16
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}`);
21 };
22
23 function addSourceMappingUrl(code, loc) {
24   return code + '\n\n//# sourceMappingURL=' + path.basename(loc);
25 }
26
27 const changedOrAdded = (filePath) => {
28   babel.transformFile(filePath, {
29     sourceMaps: true,
30     comments: false
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`;
35
36     fs.writeFile(mapLoc, JSON.stringify(result.map));
37     fs.writeFile(`${fileName}.js`, addSourceMappingUrl(result.code, mapLoc));
38
39     log(`'${filePath}' is being processed.`);
40   });
41 };
42
43 const fileMatch = './**/*.es6.js';
44 const globOptions = {
45   ignore: 'node_modules/**'
46 };
47 const processFiles = (error, filePaths) => {
48   if (error) {
49     process.exitCode = 1;
50   }
51   filePaths.forEach(changedOrAdded);
52 };
53 glob(fileMatch, globOptions, processFiles);
54 process.exitCode = 0;