Skip to content

Commit eb4b4fc

Browse files
committed
Read and write output files from referenced projects to disk
1 parent 0561a9f commit eb4b4fc

File tree

87 files changed

+719
-395
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+719
-395
lines changed

src/after-compile.ts

+14-17
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ export function makeAfterCompile(
3939
return;
4040
}
4141

42+
if (instance.loaderOptions.transpileOnly) {
43+
provideAssetsFromSolutionBuilderHost(instance, compilation);
44+
callback();
45+
return;
46+
}
47+
4248
removeTSLoaderErrors(compilation.errors);
4349

4450
provideCompilerOptionDiagnosticErrorsToWebpack(
@@ -418,23 +424,14 @@ function provideAssetsFromSolutionBuilderHost(
418424
) {
419425
if (instance.solutionBuilderHost) {
420426
// written files
421-
addDeclarationFilesAsAsset(
422-
instance.solutionBuilderHost.outputFiles.values(),
423-
compilation,
424-
outputFile => !outputFile.isNew
425-
);
426-
// tsbuild infos
427-
outputFilesToAsset(
428-
instance.solutionBuilderHost.tsbuildinfos.values(),
429-
compilation,
430-
outputFile => !outputFile.isNew
431-
);
432-
instance.solutionBuilderHost.outputFiles.forEach(
433-
outputFile => (outputFile.isNew = false)
434-
);
435-
instance.solutionBuilderHost.tsbuildinfos.forEach(
436-
outputFile => (outputFile.isNew = false)
437-
);
427+
for (const outputFile of instance.solutionBuilderHost.outputFiles.values()) {
428+
if (outputFile) {
429+
if (outputFile.isNew) {
430+
outputFileToAsset(outputFile, compilation);
431+
}
432+
outputFile.isNew = false;
433+
}
434+
}
438435
}
439436
}
440437

src/index.ts

+64-29
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,26 @@ function addDependenciesFromSolutionBuilder(
563563
if (!instance.solutionBuilderHost) {
564564
return;
565565
}
566+
566567
// Add all the input files from the references as
567568
const resolvedFilePath = path.resolve(filePath);
569+
if (!isReferencedFile(instance, filePath)) {
570+
if (
571+
instance.configParseResult.fileNames.some(
572+
f => path.resolve(f) === resolvedFilePath
573+
)
574+
) {
575+
addDependenciesFromProjectReferences(
576+
instance,
577+
path.resolve(instance.configFilePath!),
578+
instance.configParseResult.projectReferences,
579+
addDependency
580+
);
581+
}
582+
return;
583+
}
584+
585+
// Referenced file find the config for it
568586
for (const [
569587
configFile,
570588
configInfo
@@ -588,40 +606,57 @@ function addDependenciesFromSolutionBuilder(
588606
continue;
589607
}
590608

591-
// This is the config for the input file
592-
const seenMap = new Map<string, true>();
593-
seenMap.set(configFile, true);
594-
595609
// Depend on all the dts files from the program
596610
if (configInfo.dtsFiles) {
597611
configInfo.dtsFiles.forEach(addDependency);
598612
}
613+
addDependenciesFromProjectReferences(
614+
instance,
615+
configFile,
616+
configInfo.config.projectReferences,
617+
addDependency
618+
);
619+
break;
620+
}
621+
}
599622

600-
// Add dependencies to all the input files from the project reference files since building them
601-
const queue = configInfo.config.projectReferences.slice();
602-
while (true) {
603-
const currentRef = queue.pop();
604-
if (!currentRef) {
605-
break;
606-
}
607-
const refConfigFile = path.resolve(
608-
instance.compiler.resolveProjectReferencePath(currentRef)
609-
);
610-
if (seenMap.has(refConfigFile)) {
611-
continue;
612-
}
613-
const refConfigInfo = instance.solutionBuilderHost.configFileInfo.get(
614-
refConfigFile
615-
);
616-
if (!refConfigInfo) {
617-
continue;
618-
}
619-
seenMap.set(refConfigFile, true);
620-
if (refConfigInfo.config) {
621-
refConfigInfo.config.fileNames.forEach(addDependency);
622-
if (refConfigInfo.config.projectReferences) {
623-
queue.push(...refConfigInfo.config.projectReferences);
624-
}
623+
function addDependenciesFromProjectReferences(
624+
instance: TSInstance,
625+
configFile: string,
626+
projectReferences: readonly typescript.ProjectReference[] | undefined,
627+
addDependency: (file: string) => void
628+
) {
629+
if (!projectReferences || !projectReferences.length) {
630+
return;
631+
}
632+
// This is the config for the input file
633+
const seenMap = new Map<string, true>();
634+
seenMap.set(configFile, true);
635+
636+
// Add dependencies to all the input files from the project reference files since building them
637+
const queue = projectReferences.slice();
638+
while (true) {
639+
const currentRef = queue.pop();
640+
if (!currentRef) {
641+
break;
642+
}
643+
const refConfigFile = path.resolve(
644+
instance.compiler.resolveProjectReferencePath(currentRef)
645+
);
646+
if (seenMap.has(refConfigFile)) {
647+
continue;
648+
}
649+
const refConfigInfo = instance.solutionBuilderHost!.configFileInfo.get(
650+
refConfigFile
651+
);
652+
if (!refConfigInfo) {
653+
continue;
654+
}
655+
seenMap.set(refConfigFile, true);
656+
if (refConfigInfo.config) {
657+
refConfigInfo.config.fileNames.forEach(addDependency);
658+
if (refConfigInfo.config.projectReferences) {
659+
queue.push(...refConfigInfo.config.projectReferences);
625660
}
626661
}
627662
}

src/instances.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,10 @@ export function initializeInstance(
277277
instance.transformers = getCustomTransformers(program);
278278
// Setup watch run for solution building
279279
if (instance.solutionBuilderHost) {
280+
loader._compiler.hooks.afterCompile.tapAsync(
281+
'ts-loader',
282+
makeAfterCompile(instance, instance.configFilePath)
283+
);
280284
loader._compiler.hooks.watchRun.tapAsync(
281285
'ts-loader',
282286
makeWatchRun(instance)
@@ -398,10 +402,10 @@ export function buildSolutionReferences(
398402
);
399403
instance.solutionBuilder = instance.compiler.createSolutionBuilderWithWatch(
400404
instance.solutionBuilderHost,
401-
[instance.configFilePath!],
405+
instance.configParseResult.projectReferences!.map(ref => ref.path),
402406
{ verbose: true }
403407
);
404-
instance.solutionBuilder!.buildReferences(instance.configFilePath!);
408+
instance.solutionBuilder!.build();
405409
ensureAllReferences(instance);
406410
} else {
407411
instance.solutionBuilderHost.buildReferences();
@@ -410,11 +414,8 @@ export function buildSolutionReferences(
410414

411415
function ensureAllReferences(instance: TSInstance) {
412416
// Return result from the json without errors so that the extra errors from config are digested here
413-
const rootConfigInfo = instance.solutionBuilderHost!.configFileInfo.get(
414-
instance.configFilePath!
415-
);
416417
for (const configInfo of instance.solutionBuilderHost!.configFileInfo.values()) {
417-
if (configInfo === rootConfigInfo || !configInfo.config) {
418+
if (!configInfo.config) {
418419
continue;
419420
}
420421
// Load all the input files

src/interfaces.ts

+2-7
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ export interface SolutionBuilderWithWatchHost
8989
>,
9090
WatchFactory {
9191
diagnostics: SolutionDiagnostics;
92-
outputFiles: Map<string, OutputFile>;
93-
tsbuildinfos: Map<string, OutputFile>;
92+
outputFiles: Map<string, false | OutputFile>;
9493
configFileInfo: Map<string, ConfigFileInfo>;
9594
outputAffectingInstanceVersion: Map<string, true>;
9695
getOutputFileFromReferencedProject(
@@ -162,11 +161,7 @@ export interface TSInstance {
162161
configFilePath: string | undefined;
163162

164163
initialSetupPending: boolean;
165-
configParseResult: {
166-
options: typescript.CompilerOptions;
167-
fileNames: string[];
168-
projectReferences?: ReadonlyArray<typescript.ProjectReference>;
169-
};
164+
configParseResult: typescript.ParsedCommandLine;
170165
log: logger.Logger;
171166
}
172167

0 commit comments

Comments
 (0)