Skip to content

Commit

Permalink
Fix: Increase the instance version when new root files are found (#955)
Browse files Browse the repository at this point in the history
* Fix: Increase the instance version when new root files are found

Fixes #943

* Bump version and update CHANGELOG
  • Loading branch information
davazp authored and johnnyreilly committed Jun 21, 2019
1 parent 6e95586 commit 35b1b56
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
3 changes: 3 additions & 0 deletions 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

Expand Down
2 changes: 1 addition & 1 deletion 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",
Expand Down
20 changes: 19 additions & 1 deletion src/index.ts
Expand Up @@ -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] = [
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -381,6 +398,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
Expand Up @@ -125,6 +125,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 @@ -200,6 +201,7 @@ function successfulTypeScriptInstance(
compilerOptions,
appendTsTsxSuffixesIfRequired,
loaderOptions,
rootFileNames,
files,
otherFiles,
program,
Expand All @@ -226,6 +228,7 @@ function successfulTypeScriptInstance(
text: fs.readFileSync(normalizedFilePath, 'utf-8'),
version: 0
});
rootFileNames.add(normalizedFilePath);
});
} catch (exc) {
return {
Expand All @@ -249,6 +252,7 @@ function successfulTypeScriptInstance(
compilerOptions,
appendTsTsxSuffixesIfRequired,
loaderOptions,
rootFileNames,
files,
otherFiles,
languageService: null,
Expand Down
1 change: 1 addition & 0 deletions src/interfaces.ts
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 35b1b56

Please sign in to comment.