createWatchCompiler.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _map = require('babel-runtime/core-js/map');
  6. var _map2 = _interopRequireDefault(_map);
  7. var _keys = require('babel-runtime/core-js/object/keys');
  8. var _keys2 = _interopRequireDefault(_keys);
  9. var _get2 = require('lodash/get');
  10. var _get3 = _interopRequireDefault(_get2);
  11. exports.default = createWatchCompiler;
  12. var _Watching = require('webpack/lib/Watching');
  13. var _Watching2 = _interopRequireDefault(_Watching);
  14. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  15. var noop = function noop() {
  16. return undefined;
  17. };
  18. function createWatchCompiler(compiler, watchOptions) {
  19. // this ugly statement to create a watch compiler is unfortunately necessary,
  20. // as webpack clears the file timestamps with the official compiler.watch()
  21. var createWatcher = function createWatcher() {
  22. return new _Watching2.default(compiler, watchOptions, noop);
  23. };
  24. var watchCompiler = null;
  25. return {
  26. watch: function watch() {
  27. if (watchCompiler === null) {
  28. watchCompiler = createWatcher();
  29. } else {
  30. var times = compiler.watchFileSystem.watcher.getTimes();
  31. // check if we can store some collected file timestamps
  32. // the non-empty check is necessary as the times will be reseted after .close()
  33. // and we don't want to reset already existing timestamps
  34. if ((0, _keys2.default)(times).length > 0) {
  35. var timesMap = new _map2.default((0, _keys2.default)(times).map(function (key) {
  36. return [key, times[key]];
  37. }));
  38. // set already collected file timestamps to cache compiled files
  39. // webpack will do this only after a file change, but that will not happen when we add or delete files
  40. // and this means that we have to test the whole test suite again ...
  41. compiler.fileTimestamps = timesMap; // eslint-disable-line no-param-reassign
  42. compiler.contextTimestamps = timesMap; // eslint-disable-line no-param-reassign
  43. }
  44. watchCompiler.close(function () {
  45. watchCompiler = createWatcher();
  46. });
  47. }
  48. },
  49. pause: function pause() {
  50. if (watchCompiler !== null && watchCompiler.watcher) {
  51. watchCompiler.watcher.pause();
  52. }
  53. },
  54. getWatchOptions: function getWatchOptions() {
  55. // 200 is the default value by webpack
  56. return (0, _get3.default)(watchCompiler, 'watchOptions', { aggregateTimeout: 200 });
  57. }
  58. };
  59. }