UNPKG

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