Skip to content

Commit

Permalink
Fix: Increase the instance version when new root files are found
Browse files Browse the repository at this point in the history
  • Loading branch information
davazp committed Jun 20, 2019
1 parent adf6b3c commit b82c2cc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -381,6 +395,7 @@ function updateFileInCache(
instance.modifiedFiles = new Map<string, TSFile>();
}
instance.modifiedFiles.set(filePath, file);

return file.version;
}

Expand Down
4 changes: 4 additions & 0 deletions src/instances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ function successfulTypeScriptInstance(
}

const compilerOptions = getCompilerOptions(configParseResult);
const rootFileNames = new Set<string>();
const files: TSFiles = new Map<string, TSFile>();
const otherFiles: TSFiles = new Map<string, TSFile>();

Expand Down Expand Up @@ -199,6 +200,7 @@ function successfulTypeScriptInstance(
compilerOptions,
appendTsTsxSuffixesIfRequired,
loaderOptions,
rootFileNames,
files,
otherFiles,
program,
Expand All @@ -225,6 +227,7 @@ function successfulTypeScriptInstance(
text: fs.readFileSync(normalizedFilePath, 'utf-8'),
version: 0
});
rootFileNames.add(normalizedFilePath);
});
} catch (exc) {
return {
Expand All @@ -248,6 +251,7 @@ function successfulTypeScriptInstance(
compilerOptions,
appendTsTsxSuffixesIfRequired,
loaderOptions,
rootFileNames,
files,
otherFiles,
languageService: null,
Expand Down
1 change: 1 addition & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export interface TSInstance {
/** Used for Vue for the most part */
appendTsTsxSuffixesIfRequired: (filePath: string) => string;
loaderOptions: LoaderOptions;
rootFileNames: Set<string>;
/**
* a cache of all the files
*/
Expand Down

0 comments on commit b82c2cc

Please sign in to comment.