diff --git a/src/index.ts b/src/index.ts index 1d5351b5f..48d4c21ea 100644 --- a/src/index.ts +++ b/src/index.ts @@ -358,6 +358,20 @@ function updateFileInCache( fileWatcherEventKind = instance.compiler.FileWatcherEventKind.Deleted; } + // filePath is a root file as it was passed to the loader. But it + // could have been found earlier as a dependency of another file. If + // that is the case, compiling this file changes the structure of + // the program and we need to increase the instance version. + // + // See https://github.com/TypeStrong/ts-loader/issues/943 + if ( + !instance.rootFileNames.has(filePath) && + filePath.indexOf('node_modules') === -1 + ) { + instance.version!++; + instance.rootFileNames.add(filePath); + } + if (file.text !== contents) { file.version++; file.text = contents; @@ -381,6 +395,7 @@ function updateFileInCache( instance.modifiedFiles = new Map(); } instance.modifiedFiles.set(filePath, file); + return file.version; } diff --git a/src/instances.ts b/src/instances.ts index c745687b3..1d436f2a1 100644 --- a/src/instances.ts +++ b/src/instances.ts @@ -124,6 +124,7 @@ function successfulTypeScriptInstance( } const compilerOptions = getCompilerOptions(configParseResult); + const rootFileNames = new Set(); const files: TSFiles = new Map(); const otherFiles: TSFiles = new Map(); @@ -199,6 +200,7 @@ function successfulTypeScriptInstance( compilerOptions, appendTsTsxSuffixesIfRequired, loaderOptions, + rootFileNames, files, otherFiles, program, @@ -225,6 +227,7 @@ function successfulTypeScriptInstance( text: fs.readFileSync(normalizedFilePath, 'utf-8'), version: 0 }); + rootFileNames.add(normalizedFilePath); }); } catch (exc) { return { @@ -248,6 +251,7 @@ function successfulTypeScriptInstance( compilerOptions, appendTsTsxSuffixesIfRequired, loaderOptions, + rootFileNames, files, otherFiles, languageService: null, diff --git a/src/interfaces.ts b/src/interfaces.ts index f5de787fe..0f2deb7bd 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -55,6 +55,7 @@ export interface TSInstance { /** Used for Vue for the most part */ appendTsTsxSuffixesIfRequired: (filePath: string) => string; loaderOptions: LoaderOptions; + rootFileNames: Set; /** * a cache of all the files */