Skip to content

Commit c5a2e68

Browse files
committed
perf(@angular/build): overlap library asset resolution and deduplicate output mkdir calls
- Start `buildState.collectAssets(options, modifiedFiles)` before `compileLibrary` so asset globbing overlaps with TypeScript/Angular compilation, Rolldown bundling, and manifest generation. - Execute `deleteFilesFromDisk` and `emitFilesToDisk` concurrently via `Promise.all` since deleted and emitted paths are disjoint. - Cache ancestor directory promises created by `mkdir(..., { recursive: true })` in `emitFilesToDisk` so parent directories (such as `outputPath`) do not incur redundant `mkdir` syscalls when subdirectories (`fesm2022`, `types`) are created first. - Return early when `result.hasErrors` is true before adding `bundleOutput.warnings` so secondary bundler warnings are not logged alongside compilation errors. - Include `options.packageJsonPath` in the error message when `verifyAllowedDependencies` fails.
1 parent 193abc0 commit c5a2e68

5 files changed

Lines changed: 37 additions & 16 deletions

File tree

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ export async function buildAction(actionContext: BuildActionContext): Promise<Ex
7171
return result;
7272
}
7373

74+
const collectedAssetsPromise =
75+
actionContext.collectedAssets ?? buildState.collectAssets(options, modifiedFiles);
76+
if (collectedAssetsPromise instanceof Promise) {
77+
// Prevent unhandled promise rejections if compilation or bundling returns early.
78+
collectedAssetsPromise.catch(() => {});
79+
}
80+
7481
const filesToEmit: OutputFile[] = [];
7582

7683
if (shouldCompileEntryPoints) {
@@ -121,10 +128,10 @@ export async function buildAction(actionContext: BuildActionContext): Promise<Ex
121128
return result;
122129
}
123130

124-
result.addWarnings(bundleOutput.warnings);
125131
if (result.hasErrors) {
126132
return result;
127133
}
134+
result.addWarnings(bundleOutput.warnings);
128135

129136
filesToEmit.push(...bundleOutput.filesToEmit);
130137
buildState.recordBundleOutput(bundleOutput.bundleResults);
@@ -145,19 +152,17 @@ export async function buildAction(actionContext: BuildActionContext): Promise<Ex
145152
filesToEmit.push(...manifestFiles);
146153
}
147154

148-
const collectedAssets =
149-
actionContext.collectedAssets ?? (await buildState.collectAssets(options, modifiedFiles));
155+
const collectedAssets = await collectedAssetsPromise;
150156
filesToEmit.push(...collectedAssets.filesToEmit);
151157

152-
await emitFilesToDisk(filesToEmit, (file) => path.posix.join(options.outputPath, file.path));
153-
154158
const filesToDelete = buildState.recordEmittedOutputs(collectedAssets, manifestFiles);
155159

156-
if (filesToDelete.size) {
157-
await deleteFilesFromDisk(
160+
await Promise.all([
161+
deleteFilesFromDisk(
158162
Array.from(filesToDelete, (file) => path.posix.join(options.outputPath, file)),
159-
);
160-
}
163+
),
164+
emitFilesToDisk(filesToEmit, (file) => path.posix.join(options.outputPath, file.path)),
165+
]);
161166
} catch (error) {
162167
assertIsError(error);
163168
result.addError(error.message);
@@ -178,7 +183,7 @@ function verifyAllowedDependencies(
178183
options: NormalizedLibraryOptions,
179184
result: ExecutionResult,
180185
): boolean {
181-
const { packageJson, allowedNonPeerDependencies } = options;
186+
const { packageJson, packageJsonPath, allowedNonPeerDependencies } = options;
182187
const dependencies = {
183188
...(packageJson.dependencies ?? {}),
184189
...(packageJson.optionalDependencies ?? {}),
@@ -189,7 +194,7 @@ function verifyAllowedDependencies(
189194
if (!allowedNonPeerDependencies.some((regex) => regex.test(dep))) {
190195
result.addError(
191196
`Dependency '${dep}' must be explicitly allowed using the 'allowedNonPeerDependencies' option, ` +
192-
`or moved to 'peerDependencies' in 'package.json'.`,
197+
`or moved to 'peerDependencies' in '${packageJsonPath}'.`,
193198
);
194199
isValid = false;
195200
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ describeLibraryBuilder(executeLibraryBuilder, LIBRARY_BUILDER_INFO, (harness) =>
8686

8787
const { result, logs } = await harness.executeOnce();
8888
expect(result?.success).toBeTrue();
89-
expect(logs.some((l) => l.level === 'warn')).toBeFalse();
89+
expect(logs).not.toContain(jasmine.objectContaining({ level: 'warn' }));
9090
});
9191

9292
it('should preserve JSDoc comments in .d.ts and annotation comments in .mjs bundles', async () => {
@@ -204,10 +204,13 @@ describeLibraryBuilder(executeLibraryBuilder, LIBRARY_BUILDER_INFO, (harness) =>
204204
);
205205
});
206206

207-
it('should log failure on build error', async () => {
207+
it('should log failure on build error without secondary bundler warnings', async () => {
208208
await harness.writeFile(
209209
'projects/lib/src/public-api.ts',
210-
`export const title: number = 'invalid type';`,
210+
`
211+
export const title: number = 'invalid type';
212+
export function runDynamic(code: string) { return eval(code); }
213+
`,
211214
);
212215

213216
harness.useTarget('build', {
@@ -230,6 +233,7 @@ describeLibraryBuilder(executeLibraryBuilder, LIBRARY_BUILDER_INFO, (harness) =>
230233
),
231234
}),
232235
);
236+
expect(logs).not.toContain(jasmine.objectContaining({ level: 'warn' }));
233237
expect(logs).not.toContain(
234238
jasmine.objectContaining({
235239
message: jasmine.stringMatching(/Output location:/),

‎packages/angular/build/src/builders/library/tests/options/allowed-non-peer-dependencies_spec.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ describeLibraryBuilder(executeLibraryBuilder, LIBRARY_BUILDER_INFO, (harness) =>
2929
expect(logs).toContain(
3030
jasmine.objectContaining({
3131
level: 'error',
32-
message: jasmine.stringMatching(/lodash-es.*allowedNonPeerDependencies/),
32+
message: jasmine.stringMatching(
33+
/lodash-es.*allowedNonPeerDependencies.*projects\/lib\/package\.json/,
34+
),
3335
}),
3436
);
3537
});

‎packages/angular/build/src/utils/disk-files.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,13 @@ export async function emitFilesToDisk<T extends EmittableFile = EmittableFile>(
5050
let mkdirPromise = directoryCreationPromises.get(fileBasePath);
5151
if (!mkdirPromise) {
5252
mkdirPromise = mkdir(fileBasePath, { recursive: true });
53-
directoryCreationPromises.set(fileBasePath, mkdirPromise);
53+
for (
54+
let currentDir = fileBasePath;
55+
!directoryCreationPromises.has(currentDir);
56+
currentDir = dirname(currentDir)
57+
) {
58+
directoryCreationPromises.set(currentDir, mkdirPromise);
59+
}
5460
}
5561
await mkdirPromise;
5662
}

‎packages/angular/build/src/utils/disk-files_spec.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ describe('disk-files', () => {
3939
contents: new TextEncoder().encode('mem-1'),
4040
},
4141
{ origin: 'memory', path: 'nested/a/mem2.txt', contents: 'mem-2' },
42+
{ origin: 'memory', path: 'nested/parent.txt', contents: 'parent-mem' },
43+
{ origin: 'memory', path: 'root.txt', contents: 'root-mem' },
4244
{ origin: 'disk', path: 'nested/b/disk1.txt', source: sourceFile },
4345
{ origin: 'disk', path: 'nested/c/disk2.txt', source: sourceFile },
4446
{ origin: 'memory', path: 'skipped/file.txt', contents: 'should-not-exist' },
@@ -51,6 +53,8 @@ describe('disk-files', () => {
5153

5254
expect(await readFile(join(outDir, 'nested/a/mem1.txt'), 'utf-8')).toBe('mem-1');
5355
expect(await readFile(join(outDir, 'nested/a/mem2.txt'), 'utf-8')).toBe('mem-2');
56+
expect(await readFile(join(outDir, 'nested/parent.txt'), 'utf-8')).toBe('parent-mem');
57+
expect(await readFile(join(outDir, 'root.txt'), 'utf-8')).toBe('root-mem');
5458
expect(await readFile(join(outDir, 'nested/b/disk1.txt'), 'utf-8')).toBe('from-disk');
5559
expect(await readFile(join(outDir, 'nested/c/disk2.txt'), 'utf-8')).toBe('from-disk');
5660
expect(existsSync(join(outDir, 'skipped/file.txt'))).toBeFalse();

0 commit comments

Comments
 (0)