Skip to content

Commit 0b8a4c4

Browse files
committed
refactor(@angular/build): Expose codeBundleCache
Exposes the codeBundle cache used in executeBuild to allow for performant multi-builder setups.
1 parent b0f49bb commit 0b8a4c4

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

packages/angular/build/src/builders/application/execute-build.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import { BuilderContext } from '@angular-devkit/architect';
1010
import { createAngularCompilation } from '../../tools/angular/compilation';
11-
import { SourceFileCache } from '../../tools/esbuild/angular/source-file-cache';
1211
import { generateBudgetStats } from '../../tools/esbuild/budget-stats';
1312
import {
1413
BuildOutputFileType,
@@ -51,6 +50,7 @@ export async function executeBuild(
5150
assets,
5251
cacheOptions,
5352
serverEntryPoint,
53+
codeBundleCache,
5454
baseHref,
5555
ssrOptions,
5656
verbose,
@@ -70,13 +70,11 @@ export async function executeBuild(
7070
// Reuse rebuild state or create new bundle contexts for code and global stylesheets
7171
let bundlerContexts;
7272
let componentStyleBundler;
73-
let codeBundleCache;
7473
let bundlingResult: BundleContextResult;
7574
let templateUpdates: Map<string, string> | undefined;
7675
if (rebuildState) {
7776
bundlerContexts = rebuildState.rebuildContexts;
7877
componentStyleBundler = rebuildState.componentStyleBundler;
79-
codeBundleCache = rebuildState.codeBundleCache;
8078
templateUpdates = rebuildState.templateUpdates;
8179
// Reset template updates for new rebuild
8280
templateUpdates?.clear();
@@ -99,7 +97,6 @@ export async function executeBuild(
9997
bundlingResult = BundlerContext.mergeResults([bundlingResult, ...typescriptResults]);
10098
} else {
10199
const target = transformSupportedBrowsersToTargets(browsers);
102-
codeBundleCache = new SourceFileCache(cacheOptions.enabled ? cacheOptions.path : undefined);
103100
componentStyleBundler = createComponentStyleBundler(options, target);
104101
if (options.templateUpdates) {
105102
templateUpdates = new Map<string, string>();

packages/angular/build/src/builders/application/options.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { realpathSync } from 'node:fs';
1212
import { access, constants, readFile } from 'node:fs/promises';
1313
import { createRequire } from 'node:module';
1414
import path from 'node:path';
15+
import { SourceFileCache } from '../../tools/esbuild/angular/source-file-cache';
1516
import { normalizeAssetPatterns, normalizeOptimization, normalizeSourceMaps } from '../../utils';
1617
import { supportColor } from '../../utils/color';
1718
import { useJSONBuildLogs, usePartialSsrBuild } from '../../utils/environment-options';
@@ -70,6 +71,9 @@ interface InternalOptions {
7071
*/
7172
entryPoints?: Set<string> | Map<string, string>;
7273

74+
/** Allow to programatically provide a shared cache */
75+
codeBundleCache?: SourceFileCache;
76+
7377
/** File extension to use for the generated output files. */
7478
outExtension?: 'js' | 'mjs';
7579

@@ -164,8 +168,11 @@ export async function normalizeOptions(
164168
const { projectRoot, projectSourceRoot } = getProjectRootPaths(workspaceRoot, projectMetadata);
165169

166170
// Gather persistent caching option and provide a project specific cache location
167-
const cacheOptions = normalizeCacheOptions(projectMetadata, workspaceRoot);
168-
cacheOptions.path = path.join(cacheOptions.path, projectName);
171+
const cacheOptions = normalizeCacheOptions(projectMetadata, workspaceRoot, projectName);
172+
173+
const codeBundleCache =
174+
options.codeBundleCache ??
175+
new SourceFileCache(cacheOptions.enabled ? cacheOptions.path : undefined);
169176

170177
const i18nOptions: I18nOptions & {
171178
duplicateTranslationBehavior?: I18NTranslation;
@@ -446,6 +453,7 @@ export async function normalizeOptions(
446453
allowedCommonJsDependencies,
447454
baseHref,
448455
cacheOptions,
456+
codeBundleCache,
449457
crossOrigin,
450458
externalDependencies: normalizeExternals(externalDependencies),
451459
externalPackages:

packages/angular/build/src/builders/unit-test/options.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ export async function normalizeOptions(
4747
const { projectRoot, projectSourceRoot } = getProjectRootPaths(workspaceRoot, projectMetadata);
4848

4949
// Gather persistent caching option and provide a project specific cache location
50-
const cacheOptions = normalizeCacheOptions(projectMetadata, workspaceRoot);
51-
cacheOptions.path = path.join(cacheOptions.path, projectName);
50+
const cacheOptions = normalizeCacheOptions(projectMetadata, workspaceRoot, projectName);
5251

5352
// Target specifier defaults to the current project's build target using a development configuration
5453
const buildTargetSpecifier = options.buildTarget ?? `::development`;

packages/angular/build/src/utils/normalize-cache.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ function hasCacheMetadata(value: unknown): value is { cli: { cache: CacheMetadat
4242
export function normalizeCacheOptions(
4343
projectMetadata: unknown,
4444
worspaceRoot: string,
45+
projectName?: string,
4546
): NormalizedCachedOptions {
4647
const cacheMetadata = hasCacheMetadata(projectMetadata) ? projectMetadata.cli.cache : {};
4748

@@ -67,9 +68,13 @@ export function normalizeCacheOptions(
6768

6869
const cacheBasePath = resolve(worspaceRoot, path);
6970

71+
const cachePath = projectName
72+
? join(cacheBasePath, VERSION, projectName)
73+
: join(cacheBasePath, VERSION);
74+
7075
return {
7176
enabled: cacheEnabled,
7277
basePath: cacheBasePath,
73-
path: join(cacheBasePath, VERSION),
78+
path: cachePath,
7479
};
7580
}

0 commit comments

Comments
 (0)