/
watch-run.ts
58 lines (50 loc) · 1.61 KB
/
watch-run.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import * as webpack from 'webpack';
import * as constants from './constants';
import { TSInstance } from './interfaces';
import { updateFileWithText } from './servicesHost';
import { readFile } from './utils';
/**
* Make function which will manually update changed files
*/
export function makeWatchRun(instance: TSInstance) {
// Called Before starting compilation after watch
const lastTimes = new Map<string, number>();
const startTime = 0;
return (compiler: webpack.Compiler, callback: () => void) => {
const times = compiler.fileTimestamps;
for (const [filePath, date] of times) {
if (
date > (lastTimes.get(filePath) || startTime) &&
filePath.match(constants.tsTsxJsJsxRegex) !== null
) {
continue;
}
lastTimes.set(filePath, date);
updateFile(instance, filePath);
}
// On watch update add all known dts files expect the ones in node_modules
// (skip @types/* and modules with typings)
for (const filePath of instance.files.keys()) {
if (
filePath.match(constants.dtsDtsxOrDtsDtsxMapRegex) !== null &&
filePath.match(constants.nodeModules) === null
) {
updateFile(instance, filePath);
}
}
// Update all the watched files from solution builder
if (instance.solutionBuilderHost) {
for (const filePath of instance.solutionBuilderHost.watchedFiles.keys()) {
updateFile(instance, filePath);
}
}
callback();
};
}
function updateFile(instance: TSInstance, filePath: string) {
updateFileWithText(
instance,
filePath,
nFilePath => readFile(nFilePath) || ''
);
}