Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Increase the instance version when new root files are found #955

Merged
merged 2 commits into from Jun 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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