From 35b1b5634eececa914f977c31ae182ed88f24987 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20V=C3=A1zquez=20P=C3=BAa?= Date: Fri, 21 Jun 2019 10:45:22 +0200 Subject: [PATCH] Fix: Increase the instance version when new root files are found (#955) * Fix: Increase the instance version when new root files are found Fixes #943 * Bump version and update CHANGELOG --- CHANGELOG.md | 3 +++ package.json | 2 +- src/index.ts | 20 +++++++++++++++++++- src/instances.ts | 4 ++++ src/interfaces.ts | 1 + 5 files changed, 28 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed284eb34..65363ff15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## v6.0.4 +* [Fix issue when handling files not included in tsconfig.json](https://github.com/TypeStrong/ts-loader/issues/943) (#934) - thanks @davazp! + ## v6.0.3 * [Upgrade typescript version to 3.5.2](https://github.com/TypeStrong/ts-loader/pull/954) (#954) - thanks @fa93hws diff --git a/package.json b/package.json index 69a57801a..20fc6b098 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ts-loader", - "version": "6.0.3", + "version": "6.0.4", "description": "TypeScript loader for webpack", "main": "index.js", "types": "dist/types/index.d.ts", diff --git a/src/index.ts b/src/index.ts index 1d5351b5f..8818205b3 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,22 @@ 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) && + // however, be careful not to add files from node_modules unless + // it is allowed by the options. + (options.allowTsInNodeModules || filePath.indexOf('node_modules') === -1) + ) { + instance.version!++; + instance.rootFileNames.add(filePath); + } + if (file.text !== contents) { file.version++; file.text = contents; @@ -381,6 +398,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 a0118172e..8531e0675 100644 --- a/src/instances.ts +++ b/src/instances.ts @@ -125,6 +125,7 @@ function successfulTypeScriptInstance( } const compilerOptions = getCompilerOptions(configParseResult); + const rootFileNames = new Set(); const files: TSFiles = new Map(); const otherFiles: TSFiles = new Map(); @@ -200,6 +201,7 @@ function successfulTypeScriptInstance( compilerOptions, appendTsTsxSuffixesIfRequired, loaderOptions, + rootFileNames, files, otherFiles, program, @@ -226,6 +228,7 @@ function successfulTypeScriptInstance( text: fs.readFileSync(normalizedFilePath, 'utf-8'), version: 0 }); + rootFileNames.add(normalizedFilePath); }); } catch (exc) { return { @@ -249,6 +252,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 */