babel-es6-watch.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /**
  2. * @file
  3. *
  4. * Watch changes to *.es6.js files and compile them to ES5 during development.
  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 chokidar = require('chokidar');
  13. const changeOrAdded = require('./changeOrAdded');
  14. const log = require('./log');
  15. // Match only on .es6.js files.
  16. const fileMatch = './**/*.es6.js';
  17. // Ignore everything in node_modules
  18. const watcher = chokidar.watch(fileMatch, {
  19. ignoreInitial: true,
  20. ignored: './node_modules/**'
  21. });
  22. const unlinkHandler = (err) => {
  23. if (err) {
  24. log(err);
  25. }
  26. };
  27. // Watch for filesystem changes.
  28. watcher
  29. .on('add', changeOrAdded)
  30. .on('change', changeOrAdded)
  31. .on('unlink', (filePath) => {
  32. const fileName = filePath.slice(0, -7);
  33. fs.stat(`${fileName}.js`, () => {
  34. fs.unlink(`${fileName}.js`, unlinkHandler);
  35. });
  36. fs.stat(`${fileName}.js.map`, () => {
  37. fs.unlink(`${fileName}.js.map`, unlinkHandler);
  38. });
  39. })
  40. .on('ready', () => log(`Watching '${fileMatch}' for changes.`));