Skip to content

Commit c656a57

Browse files
committed
fixup! perf(@angular/build): overlap library asset resolution and deduplicate output mkdir calls
1 parent 4965b1d commit c656a57

3 files changed

Lines changed: 26 additions & 25 deletions

File tree

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

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,8 @@ 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-
74+
let collectedAssetsPromise: CollectedAssets | Promise<CollectedAssets> | undefined =
75+
actionContext.collectedAssets;
8176
const filesToEmit: OutputFile[] = [];
8277

8378
if (shouldCompileEntryPoints) {
@@ -106,6 +101,13 @@ export async function buildAction(actionContext: BuildActionContext): Promise<Ex
106101
return result;
107102
}
108103

104+
if (!collectedAssetsPromise) {
105+
const promise = buildState.collectAssets(options, modifiedFiles);
106+
// Prevent unhandled promise rejections if bundling returns early.
107+
promise.catch(() => {});
108+
collectedAssetsPromise = promise;
109+
}
110+
109111
const itemsToBundle = collectEntryPointsToBundle(options, buildState);
110112
let bundleOutput: BundleEntryPointsOutput;
111113
try {
@@ -158,17 +160,26 @@ export async function buildAction(actionContext: BuildActionContext): Promise<Ex
158160
filesToEmit.push(...manifestFiles);
159161
}
160162

161-
const collectedAssets = await collectedAssetsPromise;
163+
const collectedAssets = await (collectedAssetsPromise ??
164+
buildState.collectAssets(options, modifiedFiles));
162165
filesToEmit.push(...collectedAssets.filesToEmit);
163166

167+
const emitPromise = emitFilesToDisk(filesToEmit, (file) =>
168+
path.posix.join(options.outputPath, file.path),
169+
);
170+
164171
const filesToDelete = buildState.recordEmittedOutputs(collectedAssets, manifestFiles);
165172

166-
await Promise.all([
167-
deleteFilesFromDisk(
168-
Array.from(filesToDelete, (file) => path.posix.join(options.outputPath, file)),
169-
),
170-
emitFilesToDisk(filesToEmit, (file) => path.posix.join(options.outputPath, file.path)),
171-
]);
173+
if (filesToDelete.size) {
174+
await Promise.all([
175+
emitPromise,
176+
deleteFilesFromDisk(
177+
Array.from(filesToDelete, (file) => path.posix.join(options.outputPath, file)),
178+
),
179+
]);
180+
} else {
181+
await emitPromise;
182+
}
172183
} catch (error) {
173184
assertIsError(error);
174185
result.addError(error.message);

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

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

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ 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' },
4442
{ origin: 'disk', path: 'nested/b/disk1.txt', source: sourceFile },
4543
{ origin: 'disk', path: 'nested/c/disk2.txt', source: sourceFile },
4644
{ origin: 'memory', path: 'skipped/file.txt', contents: 'should-not-exist' },
@@ -53,8 +51,6 @@ describe('disk-files', () => {
5351

5452
expect(await readFile(join(outDir, 'nested/a/mem1.txt'), 'utf-8')).toBe('mem-1');
5553
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');
5854
expect(await readFile(join(outDir, 'nested/b/disk1.txt'), 'utf-8')).toBe('from-disk');
5955
expect(await readFile(join(outDir, 'nested/c/disk2.txt'), 'utf-8')).toBe('from-disk');
6056
expect(existsSync(join(outDir, 'skipped/file.txt'))).toBeFalse();

0 commit comments

Comments
 (0)