Skip to content

Commit 3e3f1ef

Browse files
committed
fixup! fix(@angular/build): invalidate inline stylesheet load cache and support persistent cache
1 parent df3016c commit 3e3f1ef

5 files changed

Lines changed: 85 additions & 236 deletions

File tree

‎packages/angular/build/src/builders/library/pipeline/stylesheet-bundler.ts‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
*/
88

99
import { ComponentStylesheetBundler } from '../../../tools/esbuild/angular/component-stylesheets';
10-
import { PersistentLoadResultCache } from '../../../tools/esbuild/persistent-load-result-cache';
1110
import type { BundleStylesheetOptions } from '../../../tools/esbuild/stylesheets/bundle-options';
12-
import { calculateGlobalStylesheetConfigHash } from '../../../tools/esbuild/stylesheets/stylesheet-cache-key';
1311
import type { NormalizedLibraryOptions } from '../types';
1412

1513
/**
@@ -67,13 +65,5 @@ export function createComponentStylesheetBundlerForLibrary(
6765
tailwindConfiguration,
6866
};
6967

70-
const loadCache =
71-
cacheOptions.enabled && cacheOptions.path
72-
? new PersistentLoadResultCache(
73-
cacheOptions.path,
74-
calculateGlobalStylesheetConfigHash(bundleOptions),
75-
)
76-
: undefined;
77-
78-
return new ComponentStylesheetBundler(bundleOptions, inlineStyleLanguage, incremental, loadCache);
68+
return new ComponentStylesheetBundler(bundleOptions, inlineStyleLanguage, incremental);
7969
}

‎packages/angular/build/src/tools/esbuild/angular/component-stylesheets.ts‎

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ import { MemoryCache } from '../../../utils/cache';
1212
import { createContentHash } from '../../../utils/hash';
1313
import { BundleContextResult, BundlerContext } from '../bundler-context';
1414
import { type BuildOutputFile, BuildOutputFileType } from '../bundler-files';
15-
import { type LoadResultCache, MemoryLoadResultCache } from '../load-result-cache';
16-
import { PersistentLoadResultCache } from '../persistent-load-result-cache';
15+
import { MemoryLoadResultCache } from '../load-result-cache';
1716
import {
1817
BundleStylesheetOptions,
1918
createStylesheetBundleOptions,
2019
} from '../stylesheets/bundle-options';
21-
import { calculateGlobalStylesheetConfigHash } from '../stylesheets/stylesheet-cache-key';
2220

2321
export type ComponentStylesheetResult = BundleContextResult & {
2422
contents: string;
@@ -33,29 +31,18 @@ export type ComponentStylesheetResult = BundleContextResult & {
3331
export class ComponentStylesheetBundler {
3432
readonly #fileContexts = new MemoryCache<BundlerContext>();
3533
readonly #inlineContexts = new MemoryCache<BundlerContext>();
36-
readonly #loadCache: LoadResultCache;
34+
readonly #loadCache = new MemoryLoadResultCache();
3735

3836
/**
3937
* @param options An object containing the stylesheet bundling options.
4038
* @param defaultInlineLanguage The default language to use for inline component styles.
4139
* @param incremental True if incremental watch mode is enabled.
42-
* @param loadCache Optional cache instance to use for stylesheet load results.
4340
*/
4441
constructor(
4542
private readonly options: BundleStylesheetOptions,
4643
private readonly defaultInlineLanguage: string,
4744
private readonly incremental: boolean,
48-
loadCache?: LoadResultCache,
49-
) {
50-
this.#loadCache =
51-
loadCache ??
52-
(options.cacheOptions.enabled && options.cacheOptions.path
53-
? new PersistentLoadResultCache(
54-
options.cacheOptions.path,
55-
calculateGlobalStylesheetConfigHash(options),
56-
)
57-
: new MemoryLoadResultCache());
58-
}
45+
) {}
5946

6047
/**
6148
* Bundle a file-based component stylesheet for use within an AOT compiled Angular application.
@@ -270,12 +257,9 @@ export class ComponentStylesheetBundler {
270257
const contexts = [...this.#fileContexts.values(), ...this.#inlineContexts.values()];
271258
this.#fileContexts.clear();
272259
this.#inlineContexts.clear();
273-
this.#loadCache.clear?.();
260+
this.#loadCache.clear();
274261

275-
await Promise.allSettled([
276-
...contexts.map((context) => context.dispose()),
277-
this.#loadCache.dispose?.(),
278-
]);
262+
await Promise.allSettled(contexts.map((context) => context.dispose()));
279263
}
280264

281265
private extractResult(

‎packages/angular/build/src/tools/esbuild/angular/component-stylesheets_spec.ts‎

Lines changed: 13 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
*/
88

99
import assert from 'node:assert';
10-
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
10+
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
1111
import { join } from 'node:path';
12-
import { createComponentStylesheetBundlerForLibrary } from '../../../builders/library/pipeline/stylesheet-bundler';
1312
import { initializeHash } from '../../../utils/hash';
1413
import { MemoryLoadResultCache } from '../load-result-cache';
1514
import { resetSassWorkerPoolCaches, shutdownSassWorkerPool } from '../stylesheets/sass-language';
@@ -34,7 +33,6 @@ describe('ComponentStylesheetBundler', () => {
3433
const testDir = await mkdtemp(join(tempRoot, 'inline-error-recovery-'));
3534
const componentFile = join(testDir, 'comp.ts');
3635
const partialFile = join(testDir, '_missing.scss');
37-
const loadCache = new MemoryLoadResultCache();
3836

3937
const bundler = new ComponentStylesheetBundler(
4038
{
@@ -48,7 +46,6 @@ describe('ComponentStylesheetBundler', () => {
4846
},
4947
'scss',
5048
true,
51-
loadCache,
5249
);
5350

5451
try {
@@ -77,11 +74,11 @@ describe('ComponentStylesheetBundler', () => {
7774
}
7875
});
7976

80-
it('evicts angular:styles/component loadCache entries when the containing component .ts file is invalidated', async () => {
77+
it('should evict #loadCache entries when invalidating inline component stylesheets', async () => {
8178
const testDir = await mkdtemp(join(tempRoot, 'inline-evict-'));
8279
const componentFile = join(testDir, 'comp.ts');
83-
const loadCache = new MemoryLoadResultCache();
84-
const putSpy = spyOn(loadCache, 'put').and.callThrough();
80+
const putSpy = spyOn(MemoryLoadResultCache.prototype, 'put').and.callThrough();
81+
const invalidateSpy = spyOn(MemoryLoadResultCache.prototype, 'invalidate').and.callThrough();
8582

8683
const bundler = new ComponentStylesheetBundler(
8784
{
@@ -95,7 +92,6 @@ describe('ComponentStylesheetBundler', () => {
9592
},
9693
'scss',
9794
true,
98-
loadCache,
9995
);
10096

10197
try {
@@ -105,54 +101,21 @@ describe('ComponentStylesheetBundler', () => {
105101

106102
const cacheKey = putSpy.calls.mostRecent().args[0];
107103
expect(cacheKey).toMatch(/^angular:styles\/component:scss;/);
108-
expect(loadCache.get(cacheKey)).toBeDefined();
109104

110105
bundler.invalidate([componentFile]);
111106

112-
expect(loadCache.get(cacheKey)).toBeUndefined();
113-
} finally {
114-
await bundler.dispose();
115-
}
116-
});
107+
expect(invalidateSpy).toHaveBeenCalledWith(cacheKey);
108+
expect(invalidateSpy).toHaveBeenCalledWith(componentFile);
117109

118-
it('persists and reuses stylesheet load results across builds when persistent cache is enabled', async () => {
119-
const testDir = await mkdtemp(join(tempRoot, 'persistent-cache-'));
120-
const cacheDir = join(testDir, '.angular', 'cache');
121-
await mkdir(testDir, { recursive: true });
122-
const styleFile = join(testDir, 'comp.scss');
123-
await writeFile(styleFile, '$primary: blue;\n.card { color: $primary; }\n');
124-
125-
const options = {
126-
workspaceRoot: testDir,
127-
preserveSymlinks: false,
128-
styleIncludePaths: [],
129-
sass: undefined,
130-
cacheOptions: {
131-
enabled: true,
132-
path: cacheDir,
133-
basePath: cacheDir,
134-
},
135-
inlineStyleLanguage: 'scss' as const,
136-
postcssConfiguration: undefined,
137-
tailwindConfiguration: undefined,
138-
};
139-
140-
const firstBundler = createComponentStylesheetBundlerForLibrary(options, false, ['es2022']);
141-
try {
142-
const firstResult = await firstBundler.bundleFile(styleFile);
143-
expect(firstResult.errors).toBeUndefined();
144-
expect(firstResult.contents).toContain('.card{color:#00f}');
145-
} finally {
146-
await firstBundler.dispose();
147-
}
148-
149-
const secondBundler = createComponentStylesheetBundlerForLibrary(options, false, ['es2022']);
150-
try {
151-
const secondResult = await secondBundler.bundleFile(styleFile);
110+
const secondResult = await bundler.bundleInline(
111+
'.btn { color: red; }',
112+
componentFile,
113+
'scss',
114+
);
152115
expect(secondResult.errors).toBeUndefined();
153-
expect(secondResult.contents).toContain('.card{color:#00f}');
116+
expect(putSpy).toHaveBeenCalledTimes(2);
154117
} finally {
155-
await secondBundler.dispose();
118+
await bundler.dispose();
156119
}
157120
});
158121
});

‎packages/angular/build/src/tools/esbuild/load-result-cache.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ export interface LoadResultCache {
1313
get(path: string): OnLoadResult | Promise<OnLoadResult | undefined> | undefined;
1414
put(path: string, result: OnLoadResult): Promise<void>;
1515
invalidate(path: string): boolean;
16-
clear?(): void;
17-
dispose?(): Promise<void> | void;
1816
readonly watchFiles: ReadonlyArray<string>;
1917
}
2018

0 commit comments

Comments
 (0)