Skip to content

Commit 31fc634

Browse files
author
Markus Wolf
committed
feat: add depending .d.ts files as changed when watch is triggered
If a project depends on custom definition files, meaning not the ones in node_modules, then it should consider them changed when webpack triggers a watch-run. Since webpack doesn't know about definition files, we use the one detected by the languageService which should be more accurate anyway. Closes TypeStrong#697
1 parent c633381 commit 31fc634

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

src/constants.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ export const dtsTsTsxRegex = /(\.d)?\.ts(x?)$/i;
1818
export const dtsTsTsxJsJsxRegex = /((\.d)?\.ts(x?)|js(x?))$/i;
1919
export const tsTsxJsJsxRegex = /\.tsx?$|\.jsx?$/i;
2020
export const jsJsx = /\.js(x?)$/i;
21-
export const jsJsxMap = /\.js(x?)\.map$/i;
21+
export const jsJsxMap = /\.js(x?)\.map$/i;
22+
export const nodeModules = /node_modules/i;

src/watch-run.ts

+21-8
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,28 @@ export function makeWatchRun(
2828
)
2929
.forEach(filePath => {
3030
lastTimes[filePath] = times[filePath];
31-
filePath = path.normalize(filePath);
32-
const file = instance.files[filePath];
33-
if (file !== undefined) {
34-
file.text = readFile(filePath) || '';
35-
file.version++;
36-
instance.version!++;
37-
instance.modifiedFiles![filePath] = file;
38-
}
31+
updateFile(instance, filePath);
32+
});
33+
// On watch update add all known dts files expect the ones in node_modules
34+
// (skip @types/* and modules with typings)
35+
Object.keys(instance.files)
36+
.filter(filePath =>
37+
filePath.match(constants.dtsDtsxRegex) && !filePath.match(constants.nodeModules)
38+
)
39+
.forEach(filePath => {
40+
updateFile(instance, filePath);
3941
});
4042
cb();
4143
};
4244
}
45+
46+
function updateFile(instance: TSInstance, filePath: string) {
47+
filePath = path.normalize(filePath);
48+
const file = instance.files[filePath];
49+
if (file !== undefined) {
50+
file.text = readFile(filePath) || '';
51+
file.version++;
52+
instance.version!++;
53+
instance.modifiedFiles![filePath] = file;
54+
}
55+
}

0 commit comments

Comments
 (0)