@@ -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 */
334334function 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
0 commit comments