Skip to content

Commit 2b5a9a6

Browse files
committed
perf(@angular/build): enable TypeScript incremental build cache in library builder
Pass the normalized `cacheOptions.path` to `compilationInstance.initialize` when persistent disk caching is enabled so TypeScript writes and reuses `.tsbuildinfo` across builds.
1 parent 3515b05 commit 2b5a9a6

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

‎packages/angular/build/src/builders/library/pipeline/compilation.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ export async function compileLibrary(
8383
cached?: SingleProgramCache,
8484
modifiedFiles?: Set<string>,
8585
): Promise<LibraryCompilationOutput> {
86-
const { tsConfigPath, compilationMode, preserveSymlinks, colors, declarationMap } = options;
86+
const { tsConfigPath, compilationMode, preserveSymlinks, colors, declarationMap, cacheOptions } =
87+
options;
8788

8889
const entryPathsMap: Record<string, string[]> = {};
8990
const rootFiles: string[] = [];
@@ -155,6 +156,7 @@ export async function compileLibrary(
155156
declarationMap,
156157
compilationMode,
157158
paths: entryPathsMap,
159+
cachePath: cacheOptions.enabled ? cacheOptions.path : undefined,
158160
},
159161
'library',
160162
);

‎packages/angular/build/src/builders/library/tests/behavior/build_spec.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,5 +236,47 @@ describeLibraryBuilder(executeLibraryBuilder, LIBRARY_BUILDER_INFO, (harness) =>
236236
}),
237237
);
238238
});
239+
240+
it('should write and reuse .tsbuildinfo in the persistent cache directory when cache is enabled', async () => {
241+
harness.useProject('lib', {
242+
root: 'projects/lib',
243+
sourceRoot: 'projects/lib/src',
244+
cli: {
245+
cache: {
246+
enabled: true,
247+
environment: 'all',
248+
path: '.angular/cache',
249+
},
250+
},
251+
});
252+
253+
harness.useTarget('build', {
254+
...BASE_OPTIONS,
255+
});
256+
257+
const firstBuild = await harness.executeOnce();
258+
expect(firstBuild.result?.success).toBeTrue();
259+
harness
260+
.expectFile('.angular/cache/0.0.0-PLACEHOLDER/lib/angular-build-library/.tsbuildinfo')
261+
.toExist();
262+
263+
await harness.writeFile(
264+
'projects/lib/src/public-api.ts',
265+
`
266+
export * from './lib/lib.service';
267+
export * from './lib/lib.component';
268+
export const CACHED_BUILD = true;
269+
`,
270+
);
271+
272+
const secondBuild = await harness.executeOnce();
273+
expect(secondBuild.result?.success).toBeTrue();
274+
const fesmContent = harness.readFile('dist/lib/fesm2022/lib.mjs');
275+
expect(fesmContent).toContain('LibComponent');
276+
expect(fesmContent).toContain('CACHED_BUILD');
277+
const dtsContent = harness.readFile('dist/lib/types/lib.d.ts');
278+
expect(dtsContent).toContain('LibComponent');
279+
expect(dtsContent).toContain('CACHED_BUILD');
280+
});
239281
});
240282
});

0 commit comments

Comments
 (0)