From f53003b70b4173f0aa7e906e4872ffff29be0f76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20V=C3=A1zquez=20P=C3=BAa?= Date: Wed, 19 Jun 2019 22:20:54 +0200 Subject: [PATCH] Fix: Increase the instance version when new root files are found Fixes #943 --- src/index.ts | 18 +++++++++++++++++- src/instances.ts | 4 ++++ src/interfaces.ts | 1 + 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 1d5351b5f..33bda4817 100644 --- a/src/index.ts +++ b/src/index.ts @@ -68,7 +68,7 @@ function successLoader( ) : rawFilePath; - const fileVersion = updateFileInCache(filePath, contents, instance); + const fileVersion = updateFileInCache(options, filePath, contents, instance); const referencedProject = getAndCacheProjectReference(filePath, instance); if (referencedProject !== undefined) { const [relativeProjectConfigPath, relativeFilePath] = [ @@ -332,6 +332,7 @@ function makeLoaderOptions(instanceName: string, loaderOptions: LoaderOptions) { * Also add the file to the modified files */ function updateFileInCache( + options: LoaderOptions, filePath: string, contents: string, instance: TSInstance @@ -358,6 +359,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) && + (options.allowTsInNodeModules || filePath.indexOf('node_modules') !== -1) + ) { + instance.version!++; + instance.rootFileNames.add(filePath); + } + if (file.text !== contents) { file.version++; file.text = contents; @@ -381,6 +396,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 */