Skip to content

Commit 7d786aa

Browse files
committed
fixup! perf(@angular/build): cache transformed configuration in TypeScript compilation
1 parent 40ede4f commit 7d786aa

4 files changed

Lines changed: 55 additions & 15 deletions

File tree

‎packages/angular/build/src/tools/angular/compilation/angular-compilation_spec.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,41 @@ describe('AngularCompilation', () => {
165165
expect(result.compilerOptions.target).toBe(ts.ScriptTarget.ES2022);
166166
expect(result.compilerOptions.inlineSources).toBe(true);
167167
expect(result.warnings.length).toBeGreaterThan(0);
168+
expect(result.tsConfigFiles).toEqual(['tsconfig.json']);
169+
});
170+
171+
it('caches transformed configuration when rootFiles are provided and invalidates on config change', async () => {
172+
const compilation = new MockTypeScriptCompilation();
173+
const mockReadConfig = jasmine.createSpy('readConfiguration').and.returnValue({
174+
options: { target: ts.ScriptTarget.ES2020 },
175+
rootNames: ['/src/main.ts'],
176+
errors: [],
177+
});
178+
spyOn(TypeScriptCompilation, 'loadCompilerCli').and.resolveTo({
179+
readConfiguration: mockReadConfig,
180+
} as unknown as typeof import('@angular/compiler-cli'));
181+
182+
const overrides: CompilerOptionOverrides = { rootFiles: ['/src/main.ts'] };
183+
184+
const result1 = await compilation.testLoadConfiguration('tsconfig.json', overrides);
185+
expect(mockReadConfig).toHaveBeenCalledTimes(1);
186+
187+
// Re-loading with same rootFiles should return cached configuration without calling readConfiguration
188+
const result2 = await compilation.testLoadConfiguration('tsconfig.json', overrides);
189+
expect(mockReadConfig).toHaveBeenCalledTimes(1);
190+
expect(result2).toBe(result1);
191+
192+
// Invalidation of non-config file should keep cache intact
193+
await compilation.update?.(new Set(['/src/main.ts']));
194+
const result3 = await compilation.testLoadConfiguration('tsconfig.json', overrides);
195+
expect(mockReadConfig).toHaveBeenCalledTimes(1);
196+
expect(result3).toBe(result1);
197+
198+
// Invalidation of tsconfig file should clear cache
199+
await compilation.update?.(new Set(['tsconfig.json']));
200+
const result4 = await compilation.testLoadConfiguration('tsconfig.json', overrides);
201+
expect(mockReadConfig).toHaveBeenCalledTimes(2);
202+
expect(result4).toBeDefined();
168203
});
169204
});
170205

‎packages/angular/build/src/tools/angular/compilation/aot-compilation.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export class AotCompilation extends TypeScriptCompilation {
7777
rootNames,
7878
errors: configurationDiagnostics,
7979
warnings,
80-
extendedConfigFiles,
80+
tsConfigFiles,
8181
} = await this.loadConfiguration(tsconfig, compilerOptionOverrides, buildType);
8282

8383
const useTypeScriptTranspilation =
@@ -211,7 +211,7 @@ export class AotCompilation extends TypeScriptCompilation {
211211

212212
// Get all files referenced in the TypeScript/Angular program including component resources
213213
const referencedFiles = [
214-
...(extendedConfigFiles ?? []),
214+
...tsConfigFiles,
215215
...typeScriptProgram
216216
.getSourceFiles()
217217
.filter((sourceFile) => !angularCompiler.ignoreForEmit.has(sourceFile))

‎packages/angular/build/src/tools/angular/compilation/jit-compilation.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class JitCompilation extends TypeScriptCompilation {
5555
rootNames,
5656
errors: configurationDiagnostics,
5757
warnings,
58-
extendedConfigFiles,
58+
tsConfigFiles,
5959
} = await this.loadConfiguration(tsconfig, compilerOptionOverrides, buildType);
6060

6161
if (hostOptions.modifiedFiles) {
@@ -91,7 +91,7 @@ export class JitCompilation extends TypeScriptCompilation {
9191
);
9292

9393
const referencedFiles = [
94-
...(extendedConfigFiles ?? []),
94+
...tsConfigFiles,
9595
...typeScriptProgram.getSourceFiles().map((sourceFile) => sourceFile.fileName),
9696
];
9797

‎packages/angular/build/src/tools/angular/compilation/typescript-compilation.ts‎

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ export interface TransformedConfiguration {
2020
rootNames: string[];
2121
errors: ts.Diagnostic[];
2222
warnings: PartialMessage[];
23-
extendedConfigFiles?: readonly string[];
23+
tsConfigFiles: readonly string[];
2424
}
2525

2626
export abstract class TypeScriptCompilation extends AngularCompilation {
2727
static #angularCompilerCliModule?: typeof ng;
2828
#cachedConfiguration?: TransformedConfiguration;
2929
#cachedRootFiles?: readonly string[];
30+
#tsConfigFiles?: Set<string>;
3031
readonly #extendedConfigCache = new Map<string, ts.ExtendedConfigCacheEntry>();
3132

3233
static async loadCompilerCli(): Promise<typeof ng> {
@@ -91,6 +92,8 @@ export abstract class TypeScriptCompilation extends AngularCompilation {
9192
),
9293
);
9394

95+
this.#tsConfigFiles = new Set([toPosixPath(tsconfig), ...this.#extendedConfigCache.keys()]);
96+
9497
let rootNames = originalRootNames;
9598
if (compilerOptionOverrides?.rootFiles?.length) {
9699
const rootFilesSet = new Set(
@@ -117,10 +120,10 @@ export abstract class TypeScriptCompilation extends AngularCompilation {
117120
rootNames,
118121
errors,
119122
warnings,
120-
extendedConfigFiles: [...this.#extendedConfigCache.keys()],
123+
tsConfigFiles: Array.from(this.#tsConfigFiles),
121124
};
122125

123-
if (config.extendedConfigFiles?.length && currentRootFiles?.length) {
126+
if (currentRootFiles?.length) {
124127
this.#cachedConfiguration = config;
125128
this.#cachedRootFiles = currentRootFiles;
126129
}
@@ -131,34 +134,36 @@ export abstract class TypeScriptCompilation extends AngularCompilation {
131134
protected readonly sourceFiles = new Map<string, ts.SourceFile>();
132135

133136
protected invalidateFiles(files: Iterable<string>): void {
137+
const tsConfigFiles = this.#tsConfigFiles;
138+
134139
for (const file of files) {
135140
const posixFile = toPosixPath(file);
136141
this.sourceFiles.delete(posixFile);
137142

138-
if (!this.#extendedConfigCache.size) {
143+
if (!tsConfigFiles) {
139144
continue;
140145
}
141146

142-
let cacheKey: string | undefined;
143-
if (this.#extendedConfigCache.has(posixFile)) {
144-
cacheKey = posixFile;
147+
let tsConfigCachedPath: string | undefined;
148+
if (tsConfigFiles.has(posixFile)) {
149+
tsConfigCachedPath = posixFile;
145150
} else {
146151
// Check with lowercased key because TypeScript lowercases the keys
147152
// of the extended config cache on case-insensitive operating systems.
148153
const lowerCasedPosixFile = posixFile.toLowerCase();
149-
if (this.#extendedConfigCache.has(lowerCasedPosixFile)) {
150-
cacheKey = lowerCasedPosixFile;
154+
if (tsConfigFiles.has(lowerCasedPosixFile)) {
155+
tsConfigCachedPath = lowerCasedPosixFile;
151156
}
152157
}
153158

154-
if (!cacheKey) {
159+
if (!tsConfigCachedPath) {
155160
continue;
156161
}
157162

158163
// If a tsconfig changes, we need to re-read the configuration.
159164
this.#cachedConfiguration = undefined;
160165
this.#cachedRootFiles = undefined;
161-
this.#extendedConfigCache.delete(cacheKey);
166+
this.#extendedConfigCache.delete(tsConfigCachedPath);
162167
}
163168
}
164169

0 commit comments

Comments
 (0)