watch-run.js 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.makeWatchRun = void 0;
  4. const path = require("path");
  5. const constants = require("./constants");
  6. const servicesHost_1 = require("./servicesHost");
  7. const utils_1 = require("./utils");
  8. /**
  9. * Make function which will manually update changed files
  10. */
  11. function makeWatchRun(instance, loader) {
  12. // Called Before starting compilation after watch
  13. const lastTimes = new Map();
  14. const startTime = 0;
  15. // Save the loader index.
  16. const loaderIndex = loader.loaderIndex;
  17. return (compiler, callback) => {
  18. var _a, _b, _c, _d, _e, _f;
  19. (_b = (_a = instance.servicesHost) === null || _a === void 0 ? void 0 : _a.clearCache) === null || _b === void 0 ? void 0 : _b.call(_a);
  20. (_d = (_c = instance.watchHost) === null || _c === void 0 ? void 0 : _c.clearCache) === null || _d === void 0 ? void 0 : _d.call(_c);
  21. (_e = instance.moduleResolutionCache) === null || _e === void 0 ? void 0 : _e.clear();
  22. (_f = instance.typeReferenceResolutionCache) === null || _f === void 0 ? void 0 : _f.clear();
  23. const promises = [];
  24. if (instance.loaderOptions.transpileOnly) {
  25. instance.reportTranspileErrors = true;
  26. }
  27. else {
  28. const times = compiler.fileTimestamps;
  29. if (times) {
  30. for (const [filePath, date] of times) {
  31. const key = instance.filePathKeyMapper(filePath);
  32. const lastTime = lastTimes.get(key) || startTime;
  33. if (!date ||
  34. date === 'ignore' ||
  35. (date.timestamp || date.safeTime) <= lastTime) {
  36. continue;
  37. }
  38. lastTimes.set(key, date.timestamp || date.safeTime);
  39. promises.push(updateFile(instance, key, filePath, loader, loaderIndex));
  40. }
  41. // On watch update add all known dts files expect the ones in node_modules
  42. // (skip @types/* and modules with typings)
  43. for (const [key, { fileName }] of instance.files.entries()) {
  44. if (fileName.match(constants.dtsDtsxOrDtsDtsxMapRegex) !== null &&
  45. fileName.match(constants.nodeModules) === null) {
  46. promises.push(updateFile(instance, key, fileName, loader, loaderIndex));
  47. }
  48. }
  49. }
  50. }
  51. // Update all the watched files from solution builder
  52. if (instance.solutionBuilderHost) {
  53. for (const { fileName, } of instance.solutionBuilderHost.watchedFiles.values()) {
  54. instance.solutionBuilderHost.updateSolutionBuilderInputFile(fileName);
  55. }
  56. instance.solutionBuilderHost.clearCache();
  57. }
  58. Promise.all(promises)
  59. .then(() => callback())
  60. .catch(err => callback(err));
  61. };
  62. }
  63. exports.makeWatchRun = makeWatchRun;
  64. function updateFile(instance, key, filePath, loader, loaderIndex) {
  65. return new Promise((resolve, reject) => {
  66. // When other loaders are specified after ts-loader
  67. // (e.g. `{ test: /\.ts$/, use: ['ts-loader', 'other-loader'] }`),
  68. // manually apply them to TypeScript files.
  69. // Otherwise, files not 'preprocessed' by them may cause complication errors (#1111).
  70. if (loaderIndex + 1 < loader.loaders.length &&
  71. instance.rootFileNames.has(path.normalize(filePath))) {
  72. let request = `!!${path.resolve(__dirname, 'stringify-loader.js')}!`;
  73. for (let i = loaderIndex + 1; i < loader.loaders.length; ++i) {
  74. request += loader.loaders[i].request + '!';
  75. }
  76. request += filePath;
  77. loader.loadModule(request, (err, source) => {
  78. if (err) {
  79. reject(err);
  80. }
  81. else {
  82. const text = JSON.parse(source);
  83. (0, servicesHost_1.updateFileWithText)(instance, key, filePath, () => text);
  84. resolve();
  85. }
  86. });
  87. }
  88. else {
  89. (0, servicesHost_1.updateFileWithText)(instance, key, filePath, nFilePath => (0, utils_1.fsReadFile)(nFilePath) || '');
  90. resolve();
  91. }
  92. });
  93. }
  94. //# sourceMappingURL=watch-run.js.map