Skip to content

Commit 961ee0c

Browse files
committed
fixup! fix(@angular/build): improve library watch mode state invalidation and lazy asset matchers
1 parent cae948d commit 961ee0c

3 files changed

Lines changed: 12 additions & 197 deletions

File tree

‎packages/angular/build/src/builders/library/builder-state.ts‎

Lines changed: 6 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ export class BuilderState {
4444

4545
#singleProgramCache?: SingleProgramCache;
4646
#compilationMode?: 'partial' | 'full';
47-
#declarationMap?: boolean;
4847
#hasEmittedTslib = false;
4948

5049
/** Map of entry point names to their previous bundle results. */
@@ -67,7 +66,7 @@ export class BuilderState {
6766

6867
constructor(
6968
options?: Partial<
70-
Pick<NormalizedLibraryOptions, 'tsConfigPath' | 'packageJsonPath' | 'entryPoints' | 'assets'>
69+
Pick<NormalizedLibraryOptions, 'tsConfigPath' | 'packageJsonPath' | 'entryPoints'>
7170
>,
7271
) {
7372
if (options?.tsConfigPath) {
@@ -93,11 +92,6 @@ export class BuilderState {
9392
return this.#compilationMode;
9493
}
9594

96-
/** Effective declarationMap setting resolved from builder options and tsconfig. */
97-
get declarationMap(): boolean | undefined {
98-
return this.#declarationMap;
99-
}
100-
10195
/** Whether the last emitted root package.json included a tslib dependency. */
10296
get hasEmittedTslib(): boolean {
10397
return this.#hasEmittedTslib;
@@ -302,49 +296,10 @@ export class BuilderState {
302296
*/
303297
recordCompilationOutput(output: LibraryCompilationOutput): void {
304298
this.#pendingCompilationFiles.clear();
305-
const lastCache = this.#singleProgramCache;
306-
const lastCompilationMode = this.#compilationMode;
307-
const lastDeclarationMap = this.#declarationMap;
308299
this.#singleProgramCache = output.cache;
309-
this.#compilationMode = output.compilationMode;
310-
this.#declarationMap = output.declarationMap;
311-
312-
if (lastCompilationMode !== output.compilationMode) {
300+
if (this.#compilationMode !== output.compilationMode) {
301+
this.#compilationMode = output.compilationMode;
313302
this.hasEmittedManifests = false;
314-
if (lastCompilationMode !== undefined) {
315-
if (lastCache && lastCache.esmFiles !== output.esmFiles) {
316-
lastCache.esmFiles.clear();
317-
}
318-
if (lastCache && lastCache.dtsFiles !== output.dtsFiles) {
319-
lastCache.dtsFiles.clear();
320-
}
321-
for (const prev of this.previousBundleResults.values()) {
322-
for (const file of prev.esmModuleIds) {
323-
this.pendingChangedEsmFiles.add(file);
324-
}
325-
for (const file of prev.dtsModuleIds) {
326-
this.pendingChangedDtsFiles.add(file);
327-
}
328-
}
329-
this.previousBundleResults.clear();
330-
for (const file of output.esmFiles.keys()) {
331-
this.pendingChangedEsmFiles.add(file.endsWith('.map') ? file.slice(0, -4) : file);
332-
}
333-
for (const file of output.dtsFiles.keys()) {
334-
this.pendingChangedDtsFiles.add(file.endsWith('.map') ? file.slice(0, -4) : file);
335-
}
336-
}
337-
}
338-
339-
if (lastDeclarationMap !== undefined && lastDeclarationMap !== output.declarationMap) {
340-
for (const prev of this.previousBundleResults.values()) {
341-
for (const file of prev.dtsModuleIds) {
342-
this.pendingChangedDtsFiles.add(file);
343-
}
344-
}
345-
for (const file of output.dtsFiles.keys()) {
346-
this.pendingChangedDtsFiles.add(file.endsWith('.map') ? file.slice(0, -4) : file);
347-
}
348303
}
349304

350305
for (const file of output.referencedFiles) {
@@ -387,7 +342,7 @@ export class BuilderState {
387342
* @param modifiedFiles Optional set of modified POSIX file paths in watch mode.
388343
* @returns The collected asset files to emit and delete.
389344
*/
390-
async collectAssets(
345+
collectAssets(
391346
options: Pick<NormalizedLibraryOptions, 'assets' | 'workspaceRoot'>,
392347
modifiedFiles?: ReadonlySet<string>,
393348
): Promise<CollectedAssets> {
@@ -399,29 +354,17 @@ export class BuilderState {
399354
effectiveModifiedFiles = this.#pendingAssetFiles;
400355
}
401356

402-
if (effectiveModifiedFiles && effectiveModifiedFiles.size > 0 && options.assets.length > 0) {
357+
if (effectiveModifiedFiles?.size && options.assets.length > 0) {
403358
this.#assetMatchers ??= createAssetMatchers(options.assets);
404359
}
405360

406-
const result = await collectAssetsToEmit(
361+
return collectAssetsToEmit(
407362
options.assets,
408363
options.workspaceRoot,
409364
this.#emittedAssets,
410365
effectiveModifiedFiles,
411366
this.#assetMatchers,
412367
);
413-
414-
if (effectiveModifiedFiles) {
415-
this.#pendingAssetFiles.clear();
416-
for (const file of result.filesToEmit) {
417-
this.#pendingAssetFiles.add(file.source);
418-
}
419-
for (const file of result.filesToDelete) {
420-
this.#pendingAssetFiles.add(file);
421-
}
422-
}
423-
424-
return result;
425368
}
426369

427370
/**
@@ -519,7 +462,6 @@ export class BuilderState {
519462
await this.#singleProgramCache?.compilationInstance.close?.();
520463
this.#singleProgramCache = undefined;
521464
this.#compilationMode = undefined;
522-
this.#declarationMap = undefined;
523465
this.#assetMatchers = undefined;
524466
this.#hasEmittedTslib = false;
525467
this.#hasEmittedAssets = false;

‎packages/angular/build/src/builders/library/builder-state_spec.ts‎

Lines changed: 6 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -172,106 +172,18 @@ describe('BuilderState', () => {
172172
).toBeFalse();
173173
});
174174

175-
it('should invalidate hasEmittedManifests and compiled bundle state when compilationMode changes', () => {
175+
it('should invalidate hasEmittedManifests when compilationMode changes', () => {
176176
const state = new BuilderState();
177177
state.hasEmittedManifests = true;
178-
const firstCache = createMockCache();
179-
firstCache.esmFiles.set('/src/index.js', 'export const a = 1;');
180-
firstCache.dtsFiles.set('/src/index.d.ts', 'export declare const a: number;');
181-
182-
state.recordCompilationOutput(
183-
createMockCompilationOutput({
184-
compilationMode: 'partial',
185-
cache: firstCache,
186-
esmFiles: firstCache.esmFiles,
187-
dtsFiles: firstCache.dtsFiles,
188-
}),
189-
);
178+
state.recordCompilationOutput(createMockCompilationOutput({ compilationMode: 'partial' }));
190179
expect(state.hasEmittedManifests).toBeFalse();
191180

192-
state.recordBundleOutput(
193-
new Map([
194-
[
195-
'.',
196-
{
197-
esmModuleIds: new Set(['/src/index.js']),
198-
dtsModuleIds: new Set(['/src/index.d.ts']),
199-
esmOutputFiles: new Set(['fesm2022/my-lib.mjs']),
200-
dtsOutputFiles: new Set(['types/my-lib.d.ts']),
201-
hasTslibImport: false,
202-
},
203-
],
204-
]),
205-
);
206181
state.hasEmittedManifests = true;
207-
208-
state.recordCompilationOutput(
209-
createMockCompilationOutput({
210-
compilationMode: 'partial',
211-
cache: firstCache,
212-
esmFiles: firstCache.esmFiles,
213-
dtsFiles: firstCache.dtsFiles,
214-
}),
215-
);
182+
state.recordCompilationOutput(createMockCompilationOutput({ compilationMode: 'partial' }));
216183
expect(state.hasEmittedManifests).toBeTrue();
217-
expect(state.previousBundleResults.size).toBe(1);
218-
219-
const secondCache = createMockCache();
220-
secondCache.esmFiles.set('/src/index.js', 'export const a = 2;');
221-
secondCache.dtsFiles.set('/src/index.d.ts', 'export declare const a: number;');
222184

223-
state.recordCompilationOutput(
224-
createMockCompilationOutput({
225-
compilationMode: 'full',
226-
cache: secondCache,
227-
esmFiles: secondCache.esmFiles,
228-
dtsFiles: secondCache.dtsFiles,
229-
}),
230-
);
185+
state.recordCompilationOutput(createMockCompilationOutput({ compilationMode: 'full' }));
231186
expect(state.hasEmittedManifests).toBeFalse();
232-
expect(state.previousBundleResults.size).toBe(0);
233-
expect(firstCache.esmFiles.size).toBe(0);
234-
expect(firstCache.dtsFiles.size).toBe(0);
235-
expect(state.pendingChangedEsmFiles).toEqual(new Set(['/src/index.js']));
236-
expect(state.pendingChangedDtsFiles).toEqual(new Set(['/src/index.d.ts']));
237-
});
238-
239-
it('should mark entry point DTS files dirty when declarationMap changes', () => {
240-
const state = new BuilderState();
241-
state.recordCompilationOutput(
242-
createMockCompilationOutput({
243-
declarationMap: false,
244-
dtsFiles: new Map([['/src/index.d.ts', 'export declare const a: number;']]),
245-
}),
246-
);
247-
state.recordBundleOutput(
248-
new Map([
249-
[
250-
'.',
251-
{
252-
esmModuleIds: new Set(['/src/index.js']),
253-
dtsModuleIds: new Set(['/src/index.d.ts']),
254-
esmOutputFiles: new Set(['fesm2022/my-lib.mjs']),
255-
dtsOutputFiles: new Set(['types/my-lib.d.ts']),
256-
hasTslibImport: false,
257-
},
258-
],
259-
]),
260-
);
261-
expect(state.pendingChangedDtsFiles.size).toBe(0);
262-
263-
state.recordCompilationOutput(
264-
createMockCompilationOutput({
265-
declarationMap: true,
266-
dtsFiles: new Map([
267-
['/src/index.d.ts', 'export declare const a: number;'],
268-
['/src/index.d.ts.map', '{}'],
269-
]),
270-
}),
271-
);
272-
expect(state.declarationMap).toBeTrue();
273-
expect(state.pendingChangedDtsFiles).toEqual(new Set(['/src/index.d.ts']));
274-
expect(state.pendingChangedEsmFiles.size).toBe(0);
275187
});
276188
});
277189

@@ -393,7 +305,7 @@ describe('BuilderState', () => {
393305
workspaceRoot: tempRoot,
394306
assets: [{ glob: 'README.md', input: tempRoot, output: '.' }],
395307
};
396-
const state = new BuilderState(options);
308+
const state = new BuilderState();
397309

398310
// Initial build emits README.md
399311
const initialAssets = await state.collectAssets(options);
@@ -445,7 +357,7 @@ describe('BuilderState', () => {
445357
};
446358

447359
// Constructing BuilderState should not compile asset matchers
448-
const state = new BuilderState(options);
360+
const state = new BuilderState();
449361
expect(globAccessCount).toBe(0);
450362

451363
state.recordEmittedOutputs({ filesToEmit: [], filesToDelete: [] });
@@ -465,35 +377,6 @@ describe('BuilderState', () => {
465377
}
466378
});
467379

468-
it('should prune non-asset paths from pendingAssetFiles after collectAssets', async () => {
469-
const baseTmpDir = process.env['TEST_TMPDIR'];
470-
assert(baseTmpDir, 'TEST_TMPDIR is not set');
471-
const tempRoot = await mkdtemp(join(baseTmpDir, 'angular-cli-builder-state-prune-'));
472-
473-
try {
474-
const options = {
475-
workspaceRoot: tempRoot,
476-
assets: [{ glob: '**/*.txt', input: tempRoot, output: '.' }],
477-
};
478-
const state = new BuilderState(options);
479-
state.recordEmittedOutputs({ filesToEmit: [], filesToDelete: [] });
480-
481-
// Pass a path that does not exist on disk yet, so it produces no asset output on iteration 1
482-
const transientPath = join(tempRoot, 'transient.txt');
483-
const firstCollect = await state.collectAssets(options, new Set([transientPath]));
484-
expect(firstCollect.filesToEmit).toHaveSize(0);
485-
486-
// Now create transientPath on disk, but trigger collectAssets with only a non-asset .ts file.
487-
// Because transientPath was pruned after the first collectAssets call, it should not be emitted.
488-
await writeFile(transientPath, 'created later');
489-
const nonAssetFile = join(tempRoot, 'src/index.ts');
490-
const secondCollect = await state.collectAssets(options, new Set([nonAssetFile]));
491-
expect(secondCollect.filesToEmit).toHaveSize(0);
492-
} finally {
493-
await rm(tempRoot, { recursive: true, force: true });
494-
}
495-
});
496-
497380
it('should close compilationInstance and clear cached state on dispose', async () => {
498381
const closeSpy = jasmine.createSpy('close').and.resolveTo();
499382
const state = new BuilderState({ packageJsonPath });

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,6 @@ export async function compileLibrary(
170170
posixReferencedFiles,
171171
effectiveModifiedFiles,
172172
hasCompilationModeChanged,
173-
buildState.declarationMap !== undefined &&
174-
buildState.declarationMap !== effectiveDeclarationMap &&
175-
!effectiveDeclarationMap,
176173
);
177174

178175
return {
@@ -209,7 +206,6 @@ function updateEmittedFileMaps(
209206
posixReferencedFiles: ReadonlySet<string>,
210207
effectiveModifiedFiles: ReadonlySet<string> | undefined,
211208
hasCompilationModeChanged: boolean,
212-
removeDtsMaps: boolean,
213209
): {
214210
esmFiles: Map<string, string>;
215211
dtsFiles: Map<string, string>;
@@ -221,12 +217,6 @@ function updateEmittedFileMaps(
221217
if (hasCompilationModeChanged) {
222218
esmFiles.clear();
223219
dtsFiles.clear();
224-
} else if (removeDtsMaps) {
225-
for (const key of dtsFiles.keys()) {
226-
if (key.endsWith('.map')) {
227-
dtsFiles.delete(key);
228-
}
229-
}
230220
}
231221

232222
const changedEsmFiles = new Set<string>();

0 commit comments

Comments
 (0)