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 SolutionBuilder watches #1003

Merged
merged 19 commits into from
Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 51 additions & 6 deletions src/after-compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import * as ts from 'typescript';
import * as webpack from 'webpack';

import * as constants from './constants';
import { getEmitOutput } from './instances';
import { forEachResolvedProjectReference, getEmitOutput } from './instances';
import {
TSFile,
TSFiles,
TSInstance,
WebpackError,
WebpackModule
} from './interfaces';
import { getSolutionErrors } from './servicesHost';
import {
collectAllDependants,
ensureProgram,
Expand All @@ -20,7 +21,8 @@ import {

export function makeAfterCompile(
instance: TSInstance,
configFilePath: string | undefined
configFilePath: string | undefined,
loaderContext: webpack.loader.LoaderContext
) {
let getCompilerOptionDiagnostics = true;
let checkAllFilesForErrors = true;
Expand Down Expand Up @@ -65,9 +67,16 @@ export function makeAfterCompile(
provideDeclarationFilesToWebpack(
filesToCheckForErrors,
instance,
compilation
compilation,
loaderContext
);

provideTsBuildInfoFilesToWebpack(instance, compilation);

// append errors
compilation.errors.push(
...getSolutionErrors(instance, compilation.compiler.context)
);
instance.filesWithErrors = filesWithErrors;
instance.modifiedFiles = null;
instance.projectsMissingSourceMaps = new Set();
Expand Down Expand Up @@ -195,7 +204,6 @@ function provideErrorsToWebpack(
}

const sourceFile = program && program.getSourceFile(filePath);

// If the source file is undefined, that probably means it’s actually part of an unbuilt project reference,
// which will have already produced a more useful error than the one we would get by proceeding here.
// If it’s undefined and we’re not using project references at all, I guess carry on so the user will
Expand Down Expand Up @@ -262,14 +270,15 @@ function provideErrorsToWebpack(
function provideDeclarationFilesToWebpack(
filesToCheckForErrors: TSFiles,
instance: TSInstance,
compilation: webpack.compilation.Compilation
compilation: webpack.compilation.Compilation,
loaderContext: webpack.loader.LoaderContext
) {
for (const filePath of filesToCheckForErrors.keys()) {
if (filePath.match(constants.tsTsxRegex) === null) {
continue;
}

const outputFiles = getEmitOutput(instance, filePath);
const outputFiles = getEmitOutput(instance, filePath, loaderContext);
const declarationFiles = outputFiles.filter(outputFile =>
outputFile.name.match(constants.dtsDtsxOrDtsDtsxMapRegex)
);
Expand All @@ -287,6 +296,42 @@ function provideDeclarationFilesToWebpack(
}
}

/**
* gather all .tsbuildinfo for the project
*/
function provideTsBuildInfoFilesToWebpack(
instance: TSInstance,
compilation: webpack.compilation.Compilation
) {
if (instance.solutionBuilderHost) {
const program = ensureProgram(instance);
if (program) {
forEachResolvedProjectReference(
program.getResolvedProjectReferences(),
resolvedRef => {
// TODO:: update compiler to expose this
const buildInfoPath = (instance.compiler as any).getOutputPathForBuildInfo(
resolvedRef.commandLine.options
);
if (buildInfoPath) {
const text = instance.compiler.sys.readFile(buildInfoPath);
if (text) {
const assetPath = path.relative(
compilation.compiler.outputPath,
path.resolve(buildInfoPath)
);
compilation.assets[assetPath] = {
source: () => text,
size: () => text.length
};
}
}
}
);
}
}
}

/**
* handle all other errors. The basic approach here to get accurate error
* reporting is to start with a "blank slate" each compilation and gather
Expand Down
141 changes: 90 additions & 51 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import * as typescript from 'typescript';
import * as webpack from 'webpack';

import * as constants from './constants';
import { getEmitOutput, getTypeScriptInstance } from './instances';
import {
getEmitOutput,
getTypeScriptInstance,
isReferencedFile
} from './instances';
import {
LoaderOptions,
LoaderOptionsCache,
Expand Down Expand Up @@ -343,19 +347,33 @@ function updateFileInCache(
if (file === undefined) {
file = instance.otherFiles.get(filePath);
if (file !== undefined) {
instance.otherFiles.delete(filePath);
instance.files.set(filePath, file);
if (!isReferencedFile(instance, filePath)) {
instance.otherFiles.delete(filePath);
instance.files.set(filePath, file);
instance.changedFilesList = true;
}
} else {
if (instance.watchHost !== undefined) {
if (
instance.watchHost !== undefined ||
instance.solutionBuilderHost !== undefined
) {
fileWatcherEventKind = instance.compiler.FileWatcherEventKind.Created;
}
file = { version: 0 };
instance.files.set(filePath, file);
if (!isReferencedFile(instance, filePath)) {
instance.files.set(filePath, file);
instance.changedFilesList = true;
} else {
instance.otherFiles.set(filePath, file);
}
}
instance.changedFilesList = true;
}

if (instance.watchHost !== undefined && contents === undefined) {
if (
(instance.watchHost !== undefined ||
instance.solutionBuilderHost !== undefined) &&
contents === undefined
) {
fileWatcherEventKind = instance.compiler.FileWatcherEventKind.Deleted;
}

Expand All @@ -366,6 +384,7 @@ function updateFileInCache(
//
// See https://github.com/TypeStrong/ts-loader/issues/943
if (
!isReferencedFile(instance, filePath) &&
!instance.rootFileNames.has(filePath) &&
// however, be careful not to add files from node_modules unless
// it is allowed by the options.
Expand All @@ -380,7 +399,8 @@ function updateFileInCache(
file.text = contents;
instance.version!++;
if (
instance.watchHost !== undefined &&
(instance.watchHost !== undefined ||
instance.solutionBuilderHost !== undefined) &&
fileWatcherEventKind === undefined
) {
fileWatcherEventKind = instance.compiler.FileWatcherEventKind.Changed;
Expand All @@ -393,6 +413,20 @@ function updateFileInCache(
instance.watchHost.invokeDirectoryWatcher(path.dirname(filePath), filePath);
}

if (
instance.solutionBuilderHost !== undefined &&
fileWatcherEventKind !== undefined
) {
instance.solutionBuilderHost.invokeFileWatcher(
filePath,
fileWatcherEventKind
);
instance.solutionBuilderHost.invokeDirectoryWatcher(
path.dirname(filePath),
filePath
);
}

// push this file to modified files hash.
if (instance.modifiedFiles === null || instance.modifiedFiles === undefined) {
instance.modifiedFiles = new Map<string, TSFile>();
Expand All @@ -408,52 +442,54 @@ function getEmit(
instance: TSInstance,
loaderContext: webpack.loader.LoaderContext
) {
const outputFiles = getEmitOutput(instance, filePath);
const outputFiles = getEmitOutput(instance, filePath, loaderContext);

loaderContext.clearDependencies();
loaderContext.addDependency(rawFilePath);
if (!isReferencedFile(instance, filePath)) {
loaderContext.clearDependencies();
loaderContext.addDependency(rawFilePath);

const allDefinitionFiles = [...instance.files.keys()].filter(defFilePath =>
defFilePath.match(constants.dtsDtsxOrDtsDtsxMapRegex)
);
const allDefinitionFiles = [...instance.files.keys()].filter(defFilePath =>
defFilePath.match(constants.dtsDtsxOrDtsDtsxMapRegex)
);

// Make this file dependent on *all* definition files in the program
const addDependency = loaderContext.addDependency.bind(loaderContext);
allDefinitionFiles.forEach(addDependency);

// Additionally make this file dependent on all imported files
const fileDependencies = instance.dependencyGraph[filePath];
const additionalDependencies =
fileDependencies === undefined
? []
: fileDependencies.map(({ resolvedFileName, originalFileName }) => {
const projectReference = getAndCacheProjectReference(
resolvedFileName,
instance
);
// In the case of dependencies that are part of a project reference,
// the real dependency that webpack should watch is the JS output file.
return projectReference !== undefined
? getAndCacheOutputJSFileName(
resolvedFileName,
projectReference,
instance
)
: originalFileName;
});

if (additionalDependencies.length > 0) {
additionalDependencies.forEach(addDependency);
}
// Make this file dependent on *all* definition files in the program
const addDependency = loaderContext.addDependency.bind(loaderContext);
allDefinitionFiles.forEach(addDependency);

// Additionally make this file dependent on all imported files
const fileDependencies = instance.dependencyGraph[filePath];
const additionalDependencies =
fileDependencies === undefined
? []
: fileDependencies.map(({ resolvedFileName, originalFileName }) => {
const projectReference = getAndCacheProjectReference(
resolvedFileName,
instance
);
// In the case of dependencies that are part of a project reference,
// the real dependency that webpack should watch is the JS output file.
return projectReference !== undefined
? getAndCacheOutputJSFileName(
resolvedFileName,
projectReference,
instance
)
: originalFileName;
});

if (additionalDependencies.length > 0) {
additionalDependencies.forEach(addDependency);
}

loaderContext._module.buildMeta.tsLoaderDefinitionFileVersions = allDefinitionFiles
.concat(additionalDependencies)
.map(
defFilePath =>
defFilePath +
'@' +
(instance.files.get(defFilePath) || { version: '?' }).version
);
loaderContext._module.buildMeta.tsLoaderDefinitionFileVersions = allDefinitionFiles
.concat(additionalDependencies)
.map(
defFilePath =>
defFilePath +
'@' +
(instance.files.get(defFilePath) || { version: '?' }).version
);
}

const outputFile = outputFiles
.filter(file => file.name.match(constants.jsJsx))
Expand Down Expand Up @@ -490,7 +526,10 @@ function getTranspilationEmit(
});

// _module.errors is not available inside happypack - see https://github.com/TypeStrong/ts-loader/issues/336
if (!instance.loaderOptions.happyPackMode) {
if (
!instance.loaderOptions.happyPackMode &&
!isReferencedFile(instance, fileName)
) {
const errors = formatErrors(
diagnostics,
instance.loaderOptions,
Expand Down
Loading