Skip to content

Commit 0019cee

Browse files
committed
Handle --isolatedModules and d.ts emit in the builder
1 parent 2692b2e commit 0019cee

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

Diff for: src/compiler/builder.ts

+29-3
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,7 @@ namespace ts {
173173
const compilerOptions = newProgram.getCompilerOptions();
174174
state.compilerOptions = compilerOptions;
175175
// With --out or --outFile, any change affects all semantic diagnostics so no need to cache them
176-
// With --isolatedModules, emitting changed file doesnt emit dependent files so we cant know of dependent files to retrieve errors so dont cache the errors
177-
if (!compilerOptions.outFile && !compilerOptions.out && !compilerOptions.isolatedModules) {
176+
if (!compilerOptions.outFile && !compilerOptions.out) {
178177
state.semanticDiagnosticsPerFile = createMap<readonly Diagnostic[]>();
179178
}
180179
state.changedFilesSet = createMap<true>();
@@ -483,16 +482,43 @@ namespace ts {
483482
return !state.semanticDiagnosticsFromOldState.size;
484483
}
485484

485+
function isChangedSignagure(state: BuilderProgramState, path: Path) {
486+
const newSignature = Debug.assertDefined(state.currentAffectedFilesSignatures).get(path);
487+
const oldSignagure = Debug.assertDefined(state.fileInfos.get(path)).signature;
488+
return newSignature !== oldSignagure;
489+
}
490+
486491
/**
487492
* Iterate on referencing modules that export entities from affected file
488493
*/
489494
function forEachReferencingModulesOfExportOfAffectedFile(state: BuilderProgramState, affectedFile: SourceFile, fn: (state: BuilderProgramState, filePath: Path) => boolean) {
490495
// If there was change in signature (dts output) for the changed file,
491496
// then only we need to handle pending file emit
492-
if (!state.exportedModulesMap || state.affectedFiles!.length === 1 || !state.changedFilesSet.has(affectedFile.path)) {
497+
if (!state.exportedModulesMap || !state.changedFilesSet.has(affectedFile.path)) {
493498
return;
494499
}
495500

501+
if (!isChangedSignagure(state, affectedFile.path)) return;
502+
503+
// Since isolated modules dont change js files, files affected by change in signature is itself
504+
// But we need to cleanup semantic diagnostics and queue dts emit for affected files
505+
if (state.compilerOptions.isolatedModules) {
506+
const seenFileNamesMap = createMap<true>();
507+
seenFileNamesMap.set(affectedFile.path, true);
508+
const queue = BuilderState.getReferencedByPaths(state, affectedFile.resolvedPath);
509+
while (queue.length > 0) {
510+
const currentPath = queue.pop()!;
511+
if (!seenFileNamesMap.has(currentPath)) {
512+
seenFileNamesMap.set(currentPath, true);
513+
const result = fn(state, currentPath);
514+
if (result && isChangedSignagure(state, currentPath)) {
515+
const currentSourceFile = Debug.assertDefined(state.program).getSourceFileByPath(currentPath)!;
516+
queue.push(...BuilderState.getReferencedByPaths(state, currentSourceFile.resolvedPath));
517+
}
518+
}
519+
}
520+
}
521+
496522
Debug.assert(!!state.currentAffectedFilesExportedModulesMap);
497523
const seenFileAndExportsOfFile = createMap<true>();
498524
// Go through exported modules from cache first

Diff for: src/compiler/builderState.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ namespace ts.BuilderState {
466466
/**
467467
* Gets the files referenced by the the file path
468468
*/
469-
function getReferencedByPaths(state: Readonly<BuilderState>, referencedFilePath: Path) {
469+
export function getReferencedByPaths(state: Readonly<BuilderState>, referencedFilePath: Path) {
470470
return arrayFrom(mapDefinedIterator(state.referencedMap!.entries(), ([filePath, referencesInFile]) =>
471471
referencesInFile.has(referencedFilePath) ? filePath as Path : undefined
472472
));

Diff for: tests/baselines/reference/tsbuild/inferredTypeFromTransitiveModule/incremental-declaration-changes/inferred-type-from-transitive-module-with-isolatedModules.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ exports.default = foo()(function foobar() {
2121
});
2222

2323

24+
//// [/src/obj/index.d.ts]
25+
import { LazyAction } from './bundling';
26+
export declare const lazyBar: LazyAction<() => void, typeof import("./lazyIndex")>;
27+
28+
29+
//// [/src/obj/lazyIndex.d.ts] file written with same contents
2430
//// [/src/obj/tsconfig.tsbuildinfo]
2531
{
2632
"program": {
@@ -47,7 +53,7 @@ exports.default = foo()(function foobar() {
4753
},
4854
"../index.ts": {
4955
"version": "-11602502901-import { LazyAction, LazyModule } from './bundling';\r\nconst lazyModule = new LazyModule(() =>\r\n import('./lazyIndex')\r\n);\r\nexport const lazyBar = new LazyAction(lazyModule, m => m.bar);",
50-
"signature": "18468008756-import { LazyAction } from './bundling';\r\nexport declare const lazyBar: LazyAction<(param: string) => void, typeof import(\"./lazyIndex\")>;\r\n"
56+
"signature": "6256067474-import { LazyAction } from './bundling';\r\nexport declare const lazyBar: LazyAction<() => void, typeof import(\"./lazyIndex\")>;\r\n"
5157
}
5258
},
5359
"options": {
@@ -75,7 +81,15 @@ exports.default = foo()(function foobar() {
7581
"../lazyindex.ts": [
7682
"../bar.ts"
7783
]
78-
}
84+
},
85+
"semanticDiagnosticsPerFile": [
86+
"../../lib/lib.d.ts",
87+
"../bar.ts",
88+
"../bundling.ts",
89+
"../global.d.ts",
90+
"../index.ts",
91+
"../lazyindex.ts"
92+
]
7993
},
8094
"version": "FakeTSVersion"
8195
}

Diff for: tests/baselines/reference/tsbuild/inferredTypeFromTransitiveModule/initial-Build/inferred-type-from-transitive-module-with-isolatedModules.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,15 @@ exports.bar = bar_1.default;
118118
"../lazyindex.ts": [
119119
"../bar.ts"
120120
]
121-
}
121+
},
122+
"semanticDiagnosticsPerFile": [
123+
"../../lib/lib.d.ts",
124+
"../bar.ts",
125+
"../bundling.ts",
126+
"../global.d.ts",
127+
"../index.ts",
128+
"../lazyindex.ts"
129+
]
122130
},
123131
"version": "FakeTSVersion"
124132
}

0 commit comments

Comments
 (0)