babel-es6-build.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. 'use strict';
  10. const fs = require('fs');
  11. const path = require('path');
  12. const babel = require('babel-core');
  13. const glob = require('glob');
  14. // Logging human-readable timestamp.
  15. const log = function (message) {
  16. // eslint-disable-next-line no-console
  17. console.log(`[${new Date().toTimeString().slice(0, 8)}] ${message}`);
  18. };
  19. function addSourceMappingUrl(code, loc) {
  20. return code + '\n\n//# sourceMappingURL=' + path.basename(loc);
  21. }
  22. const changedOrAdded = (filePath) => {
  23. babel.transformFile(filePath, {
  24. sourceMaps: true,
  25. comments: false
  26. }, function (err, result) {
  27. const fileName = filePath.slice(0, -7);
  28. // we've requested for a sourcemap to be written to disk
  29. let mapLoc = `${fileName}.js.map`;
  30. fs.writeFile(mapLoc, JSON.stringify(result.map));
  31. fs.writeFile(`${fileName}.js`, addSourceMappingUrl(result.code, mapLoc));
  32. log(`'${filePath}' is being processed.`);
  33. });
  34. };
  35. const fileMatch = './**/*.es6.js';
  36. const globOptions = {
  37. ignore: 'node_modules/**'
  38. };
  39. const processFiles = (error, filePaths) => {
  40. if (error) {
  41. process.exitCode = 1;
  42. }
  43. filePaths.forEach(changedOrAdded);
  44. };
  45. glob(fileMatch, globOptions, processFiles);
  46. process.exitCode = 0;