babel-es6-watch.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. console.warn('⚠️ yarn `watch:js` command is deprecated in drupal:9.4.0 and will be removed from drupal:10.0.0. This command is no longer needed in Drupal 10.0.0 once https://www.drupal.org/project/drupal/issues/3278415 is committed.️');
  16. // Match only on .es6.js files.
  17. const fileMatch = './**/*.es6.js';
  18. // Ignore everything in node_modules
  19. const watcher = chokidar.watch(fileMatch, {
  20. ignoreInitial: true,
  21. ignored: './node_modules/**'
  22. });
  23. const unlinkHandler = (err) => {
  24. if (err) {
  25. log(err);
  26. }
  27. };
  28. // Watch for filesystem changes.
  29. watcher
  30. .on('add', changeOrAdded)
  31. .on('change', changeOrAdded)
  32. .on('unlink', (filePath) => {
  33. const fileName = filePath.slice(0, -7);
  34. fs.stat(`${fileName}.js`, () => {
  35. fs.unlink(`${fileName}.js`, unlinkHandler);
  36. });
  37. })
  38. .on('ready', () => log(`Watching '${fileMatch}' for changes.`));