Skip to content

Commit

Permalink
Read and write output files from referenced projects to disk
Browse files Browse the repository at this point in the history
  • Loading branch information
sheetalkamat committed Apr 9, 2020
1 parent 0561a9f commit eb4b4fc
Show file tree
Hide file tree
Showing 87 changed files with 719 additions and 395 deletions.
31 changes: 14 additions & 17 deletions src/after-compile.ts
Expand Up @@ -39,6 +39,12 @@ export function makeAfterCompile(
return;
}

if (instance.loaderOptions.transpileOnly) {
provideAssetsFromSolutionBuilderHost(instance, compilation);
callback();
return;
}

removeTSLoaderErrors(compilation.errors);

provideCompilerOptionDiagnosticErrorsToWebpack(
Expand Down Expand Up @@ -418,23 +424,14 @@ function provideAssetsFromSolutionBuilderHost(
) {
if (instance.solutionBuilderHost) {
// written files
addDeclarationFilesAsAsset(
instance.solutionBuilderHost.outputFiles.values(),
compilation,
outputFile => !outputFile.isNew
);
// tsbuild infos
outputFilesToAsset(
instance.solutionBuilderHost.tsbuildinfos.values(),
compilation,
outputFile => !outputFile.isNew
);
instance.solutionBuilderHost.outputFiles.forEach(
outputFile => (outputFile.isNew = false)
);
instance.solutionBuilderHost.tsbuildinfos.forEach(
outputFile => (outputFile.isNew = false)
);
for (const outputFile of instance.solutionBuilderHost.outputFiles.values()) {
if (outputFile) {
if (outputFile.isNew) {
outputFileToAsset(outputFile, compilation);
}
outputFile.isNew = false;
}
}
}
}

Expand Down
93 changes: 64 additions & 29 deletions src/index.ts
Expand Up @@ -563,8 +563,26 @@ function addDependenciesFromSolutionBuilder(
if (!instance.solutionBuilderHost) {
return;
}

// Add all the input files from the references as
const resolvedFilePath = path.resolve(filePath);
if (!isReferencedFile(instance, filePath)) {
if (
instance.configParseResult.fileNames.some(
f => path.resolve(f) === resolvedFilePath
)
) {
addDependenciesFromProjectReferences(
instance,
path.resolve(instance.configFilePath!),
instance.configParseResult.projectReferences,
addDependency
);
}
return;
}

// Referenced file find the config for it
for (const [
configFile,
configInfo
Expand All @@ -588,40 +606,57 @@ function addDependenciesFromSolutionBuilder(
continue;
}

// This is the config for the input file
const seenMap = new Map<string, true>();
seenMap.set(configFile, true);

// Depend on all the dts files from the program
if (configInfo.dtsFiles) {
configInfo.dtsFiles.forEach(addDependency);
}
addDependenciesFromProjectReferences(
instance,
configFile,
configInfo.config.projectReferences,
addDependency
);
break;
}
}

// Add dependencies to all the input files from the project reference files since building them
const queue = configInfo.config.projectReferences.slice();
while (true) {
const currentRef = queue.pop();
if (!currentRef) {
break;
}
const refConfigFile = path.resolve(
instance.compiler.resolveProjectReferencePath(currentRef)
);
if (seenMap.has(refConfigFile)) {
continue;
}
const refConfigInfo = instance.solutionBuilderHost.configFileInfo.get(
refConfigFile
);
if (!refConfigInfo) {
continue;
}
seenMap.set(refConfigFile, true);
if (refConfigInfo.config) {
refConfigInfo.config.fileNames.forEach(addDependency);
if (refConfigInfo.config.projectReferences) {
queue.push(...refConfigInfo.config.projectReferences);
}
function addDependenciesFromProjectReferences(
instance: TSInstance,
configFile: string,
projectReferences: readonly typescript.ProjectReference[] | undefined,
addDependency: (file: string) => void
) {
if (!projectReferences || !projectReferences.length) {
return;
}
// This is the config for the input file
const seenMap = new Map<string, true>();
seenMap.set(configFile, true);

// Add dependencies to all the input files from the project reference files since building them
const queue = projectReferences.slice();
while (true) {
const currentRef = queue.pop();
if (!currentRef) {
break;
}
const refConfigFile = path.resolve(
instance.compiler.resolveProjectReferencePath(currentRef)
);
if (seenMap.has(refConfigFile)) {
continue;
}
const refConfigInfo = instance.solutionBuilderHost!.configFileInfo.get(
refConfigFile
);
if (!refConfigInfo) {
continue;
}
seenMap.set(refConfigFile, true);
if (refConfigInfo.config) {
refConfigInfo.config.fileNames.forEach(addDependency);
if (refConfigInfo.config.projectReferences) {
queue.push(...refConfigInfo.config.projectReferences);
}
}
}
Expand Down
13 changes: 7 additions & 6 deletions src/instances.ts
Expand Up @@ -277,6 +277,10 @@ export function initializeInstance(
instance.transformers = getCustomTransformers(program);
// Setup watch run for solution building
if (instance.solutionBuilderHost) {
loader._compiler.hooks.afterCompile.tapAsync(
'ts-loader',
makeAfterCompile(instance, instance.configFilePath)
);
loader._compiler.hooks.watchRun.tapAsync(
'ts-loader',
makeWatchRun(instance)
Expand Down Expand Up @@ -398,10 +402,10 @@ export function buildSolutionReferences(
);
instance.solutionBuilder = instance.compiler.createSolutionBuilderWithWatch(
instance.solutionBuilderHost,
[instance.configFilePath!],
instance.configParseResult.projectReferences!.map(ref => ref.path),
{ verbose: true }
);
instance.solutionBuilder!.buildReferences(instance.configFilePath!);
instance.solutionBuilder!.build();
ensureAllReferences(instance);
} else {
instance.solutionBuilderHost.buildReferences();
Expand All @@ -410,11 +414,8 @@ export function buildSolutionReferences(

function ensureAllReferences(instance: TSInstance) {
// Return result from the json without errors so that the extra errors from config are digested here
const rootConfigInfo = instance.solutionBuilderHost!.configFileInfo.get(
instance.configFilePath!
);
for (const configInfo of instance.solutionBuilderHost!.configFileInfo.values()) {
if (configInfo === rootConfigInfo || !configInfo.config) {
if (!configInfo.config) {
continue;
}
// Load all the input files
Expand Down
9 changes: 2 additions & 7 deletions src/interfaces.ts
Expand Up @@ -89,8 +89,7 @@ export interface SolutionBuilderWithWatchHost
>,
WatchFactory {
diagnostics: SolutionDiagnostics;
outputFiles: Map<string, OutputFile>;
tsbuildinfos: Map<string, OutputFile>;
outputFiles: Map<string, false | OutputFile>;
configFileInfo: Map<string, ConfigFileInfo>;
outputAffectingInstanceVersion: Map<string, true>;
getOutputFileFromReferencedProject(
Expand Down Expand Up @@ -162,11 +161,7 @@ export interface TSInstance {
configFilePath: string | undefined;

initialSetupPending: boolean;
configParseResult: {
options: typescript.CompilerOptions;
fileNames: string[];
projectReferences?: ReadonlyArray<typescript.ProjectReference>;
};
configParseResult: typescript.ParsedCommandLine;
log: logger.Logger;
}

Expand Down

0 comments on commit eb4b4fc

Please sign in to comment.