Skip to content

Commit f53003b

Browse files
committed
Fix: Increase the instance version when new root files are found
Fixes #943
1 parent adf6b3c commit f53003b

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

src/index.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function successLoader(
6868
)
6969
: rawFilePath;
7070

71-
const fileVersion = updateFileInCache(filePath, contents, instance);
71+
const fileVersion = updateFileInCache(options, filePath, contents, instance);
7272
const referencedProject = getAndCacheProjectReference(filePath, instance);
7373
if (referencedProject !== undefined) {
7474
const [relativeProjectConfigPath, relativeFilePath] = [
@@ -332,6 +332,7 @@ function makeLoaderOptions(instanceName: string, loaderOptions: LoaderOptions) {
332332
* Also add the file to the modified files
333333
*/
334334
function updateFileInCache(
335+
options: LoaderOptions,
335336
filePath: string,
336337
contents: string,
337338
instance: TSInstance
@@ -358,6 +359,20 @@ function updateFileInCache(
358359
fileWatcherEventKind = instance.compiler.FileWatcherEventKind.Deleted;
359360
}
360361

362+
// filePath is a root file as it was passed to the loader. But it
363+
// could have been found earlier as a dependency of another file. If
364+
// that is the case, compiling this file changes the structure of
365+
// the program and we need to increase the instance version.
366+
//
367+
// See https://github.com/TypeStrong/ts-loader/issues/943
368+
if (
369+
!instance.rootFileNames.has(filePath) &&
370+
(options.allowTsInNodeModules || filePath.indexOf('node_modules') !== -1)
371+
) {
372+
instance.version!++;
373+
instance.rootFileNames.add(filePath);
374+
}
375+
361376
if (file.text !== contents) {
362377
file.version++;
363378
file.text = contents;
@@ -381,6 +396,7 @@ function updateFileInCache(
381396
instance.modifiedFiles = new Map<string, TSFile>();
382397
}
383398
instance.modifiedFiles.set(filePath, file);
399+
384400
return file.version;
385401
}
386402

src/instances.ts

+4
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ function successfulTypeScriptInstance(
124124
}
125125

126126
const compilerOptions = getCompilerOptions(configParseResult);
127+
const rootFileNames = new Set<string>();
127128
const files: TSFiles = new Map<string, TSFile>();
128129
const otherFiles: TSFiles = new Map<string, TSFile>();
129130

@@ -199,6 +200,7 @@ function successfulTypeScriptInstance(
199200
compilerOptions,
200201
appendTsTsxSuffixesIfRequired,
201202
loaderOptions,
203+
rootFileNames,
202204
files,
203205
otherFiles,
204206
program,
@@ -225,6 +227,7 @@ function successfulTypeScriptInstance(
225227
text: fs.readFileSync(normalizedFilePath, 'utf-8'),
226228
version: 0
227229
});
230+
rootFileNames.add(normalizedFilePath);
228231
});
229232
} catch (exc) {
230233
return {
@@ -248,6 +251,7 @@ function successfulTypeScriptInstance(
248251
compilerOptions,
249252
appendTsTsxSuffixesIfRequired,
250253
loaderOptions,
254+
rootFileNames,
251255
files,
252256
otherFiles,
253257
languageService: null,

src/interfaces.ts

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export interface TSInstance {
5555
/** Used for Vue for the most part */
5656
appendTsTsxSuffixesIfRequired: (filePath: string) => string;
5757
loaderOptions: LoaderOptions;
58+
rootFileNames: Set<string>;
5859
/**
5960
* a cache of all the files
6061
*/

0 commit comments

Comments
 (0)