Skip to content

Commit f418f66

Browse files
committed
fixup! fix(@angular/build): improve library compilation diagnostics and skip redundant d.ts rebundling
1 parent f234c92 commit f418f66

2 files changed

Lines changed: 78 additions & 45 deletions

File tree

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

Lines changed: 77 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { existsSync } from 'node:fs';
1111
import {
1212
type AngularCompilation,
1313
DiagnosticModes,
14+
type EmitFileResult,
1415
createAngularCompilation,
1516
} from '../../../tools/angular/compilation';
1617
import type { ComponentStylesheetBundler } from '../../../tools/esbuild/angular/component-stylesheets';
@@ -79,6 +80,9 @@ export async function compileLibrary(
7980
const failedFiles = new Set<string>();
8081
const clearedReferenceFiles = new Set<string>();
8182
const clearPreviousReferencesOnce = (targetFile: string): void => {
83+
if (!cached) {
84+
return;
85+
}
8286
const posixTarget = toPosixPath(targetFile);
8387
if (!clearedReferenceFiles.has(posixTarget)) {
8488
clearedReferenceFiles.add(posixTarget);
@@ -167,51 +171,14 @@ export async function compileLibrary(
167171

168172
const esmFiles = cached?.esmFiles ?? new Map<string, string>();
169173
const dtsFiles = cached?.dtsFiles ?? new Map<string, string>();
170-
const changedEsmFiles = new Set<string>();
171-
const changedDtsFiles = new Set<string>();
172174
const posixReferencedFiles = new Set(referencedFiles.map(toPosixPath));
173-
174-
if (cached && effectiveModifiedFiles) {
175-
for (const modifiedFile of effectiveModifiedFiles) {
176-
if (posixReferencedFiles.has(modifiedFile)) {
177-
continue;
178-
}
179-
180-
const basePathWithoutExt = modifiedFile.replace(/(?:\.d\.[cm]?ts|\.[cm]?[jt]sx?)$/i, '');
181-
if (basePathWithoutExt === modifiedFile || existsSync(modifiedFile)) {
182-
continue;
183-
}
184-
185-
for (const ext of EMITTED_EXTENSIONS) {
186-
const outputPath = `${basePathWithoutExt}${ext}`;
187-
const mapPath = `${outputPath}.map`;
188-
if (esmFiles.delete(outputPath)) {
189-
changedEsmFiles.add(outputPath);
190-
}
191-
if (dtsFiles.delete(outputPath)) {
192-
changedDtsFiles.add(outputPath);
193-
}
194-
esmFiles.delete(mapPath);
195-
dtsFiles.delete(mapPath);
196-
}
197-
}
198-
}
199-
200-
for (const { filename, contents } of emittedFiles) {
201-
const normalized = toPosixPath(filename);
202-
const isMap = normalized.endsWith('.map');
203-
const isDts = isMap ? isDeclarationSourceMapFile(normalized) : isDeclarationFile(normalized);
204-
const targetFiles = isDts ? dtsFiles : esmFiles;
205-
206-
if (targetFiles.get(normalized) !== contents) {
207-
targetFiles.set(normalized, contents);
208-
if (!isDts) {
209-
changedEsmFiles.add(isMap ? normalized.slice(0, -4) : normalized);
210-
} else if (!isMap) {
211-
changedDtsFiles.add(normalized);
212-
}
213-
}
214-
}
175+
const { changedEsmFiles, changedDtsFiles } = updateEmittedFileCaches(
176+
emittedFiles,
177+
esmFiles,
178+
dtsFiles,
179+
posixReferencedFiles,
180+
cached ? effectiveModifiedFiles : undefined,
181+
);
215182

216183
const diagnosePromise = runDiagnosticsAndFormat(
217184
compilationInstance,
@@ -246,6 +213,72 @@ export async function compileLibrary(
246213
}
247214
}
248215

216+
/**
217+
* Updates the in-memory ESM and DTS file maps with newly emitted files and prunes deleted
218+
* source files, returning the sets of changed ESM and DTS file paths.
219+
*
220+
* @param emittedFiles Newly emitted files from the Angular compilation.
221+
* @param esmFiles In-memory map of POSIX ESM file paths to contents.
222+
* @param dtsFiles In-memory map of POSIX declaration file paths to contents.
223+
* @param posixReferencedFiles Set of POSIX file paths currently referenced by the program.
224+
* @param modifiedFiles Optional set of modified POSIX file paths in incremental builds.
225+
* @returns Sets of changed ESM and DTS file paths.
226+
*/
227+
function updateEmittedFileCaches(
228+
emittedFiles: Iterable<EmitFileResult>,
229+
esmFiles: Map<string, string>,
230+
dtsFiles: Map<string, string>,
231+
posixReferencedFiles: ReadonlySet<string>,
232+
modifiedFiles?: ReadonlySet<string>,
233+
): { changedEsmFiles: Set<string>; changedDtsFiles: Set<string> } {
234+
const changedEsmFiles = new Set<string>();
235+
const changedDtsFiles = new Set<string>();
236+
237+
if (modifiedFiles) {
238+
for (const modifiedFile of modifiedFiles) {
239+
if (posixReferencedFiles.has(modifiedFile)) {
240+
continue;
241+
}
242+
243+
const basePathWithoutExt = modifiedFile.replace(/(?:\.d\.[cm]?ts|\.[cm]?[jt]sx?)$/i, '');
244+
if (basePathWithoutExt === modifiedFile || existsSync(modifiedFile)) {
245+
continue;
246+
}
247+
248+
for (const ext of EMITTED_EXTENSIONS) {
249+
const outputPath = `${basePathWithoutExt}${ext}`;
250+
const mapPath = `${outputPath}.map`;
251+
if (esmFiles.delete(outputPath)) {
252+
changedEsmFiles.add(outputPath);
253+
}
254+
if (dtsFiles.delete(outputPath)) {
255+
changedDtsFiles.add(outputPath);
256+
}
257+
esmFiles.delete(mapPath);
258+
dtsFiles.delete(mapPath);
259+
}
260+
}
261+
}
262+
263+
for (const { filename, contents } of emittedFiles) {
264+
const normalized = toPosixPath(filename);
265+
const isMap = normalized.endsWith('.map');
266+
const isDts = isMap ? isDeclarationSourceMapFile(normalized) : isDeclarationFile(normalized);
267+
const targetFiles = isDts ? dtsFiles : esmFiles;
268+
269+
if (targetFiles.get(normalized) !== contents) {
270+
targetFiles.set(normalized, contents);
271+
if (!isDts) {
272+
changedEsmFiles.add(isMap ? normalized.slice(0, -4) : normalized);
273+
} else if (!isMap) {
274+
changedDtsFiles.add(normalized);
275+
}
276+
}
277+
}
278+
279+
return { changedEsmFiles, changedDtsFiles };
280+
}
281+
249282
/**
250283
* Formats stylesheet bundling diagnostics and TypeScript/Angular diagnostics
251284
* and records them on the build state's execution result.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ describe('compileLibrary pipeline', () => {
169169
expect(warningsText).toContain('NG8107: Optional chain warning');
170170
});
171171

172-
it('should still run diagnoseFiles when stylesheetErrors are present and handle diagnoseFiles rejection without unhandled rejection', async () => {
172+
it('should run diagnoseFiles when stylesheetErrors are present and catch diagnoseFiles rejections', async () => {
173173
const compilationInstance: AngularCompilation = {
174174
initialize: jasmine
175175
.createSpy('initialize')

0 commit comments

Comments
 (0)