Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(@angular-devkit/build-angular): reduce promise creation during estimated transfer size calculation #26701

Merged
merged 1 commit into from Dec 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 36 additions & 23 deletions packages/angular_devkit/build_angular/src/tools/esbuild/utils.ts
Expand Up @@ -12,7 +12,6 @@ import { createHash } from 'node:crypto';
import { constants as fsConstants } from 'node:fs';
import fs from 'node:fs/promises';
import path from 'node:path';
import { promisify } from 'node:util';
import { brotliCompress } from 'node:zlib';
import { coerce } from 'semver';
import { NormalizedOutputOptions } from '../../builders/application/options';
Expand All @@ -22,8 +21,6 @@ import { BundleStats, generateBuildStatsTable } from '../webpack/utils/stats';
import { BuildOutputFile, BuildOutputFileType, InitialFileRecord } from './bundler-context';
import { BuildOutputAsset } from './bundler-execution-result';

const compressAsync = promisify(brotliCompress);

export function logBuildStats(
context: BuilderContext,
metafile: Metafile,
Expand Down Expand Up @@ -87,31 +84,47 @@ export async function calculateEstimatedTransferSizes(
outputFiles: OutputFile[],
): Promise<Map<string, number>> {
const sizes = new Map<string, number>();
const pendingCompression = [];
if (outputFiles.length <= 0) {
return sizes;
}

for (const outputFile of outputFiles) {
// Only calculate JavaScript and CSS files
if (!outputFile.path.endsWith('.js') && !outputFile.path.endsWith('.css')) {
continue;
}
return new Promise((resolve, reject) => {
let completeCount = 0;
for (const outputFile of outputFiles) {
// Only calculate JavaScript and CSS files
if (!outputFile.path.endsWith('.js') && !outputFile.path.endsWith('.css')) {
++completeCount;
continue;
}

// Skip compressing small files which may end being larger once compressed and will most likely not be
// compressed in actual transit.
if (outputFile.contents.byteLength < 1024) {
sizes.set(outputFile.path, outputFile.contents.byteLength);
continue;
}
// Skip compressing small files which may end being larger once compressed and will most likely not be
// compressed in actual transit.
if (outputFile.contents.byteLength < 1024) {
sizes.set(outputFile.path, outputFile.contents.byteLength);
++completeCount;
continue;
}

pendingCompression.push(
compressAsync(outputFile.contents).then((result) =>
sizes.set(outputFile.path, result.byteLength),
),
);
}
// Directly use the async callback function to minimize the number of Promises that need to be created.
brotliCompress(outputFile.contents, (error, result) => {
if (error) {
reject(error);

await Promise.all(pendingCompression);
return;
}

return sizes;
sizes.set(outputFile.path, result.byteLength);
if (++completeCount >= outputFiles.length) {
resolve(sizes);
}
});
}

// Covers the case where no files need to be compressed
if (completeCount >= outputFiles.length) {
resolve(sizes);
}
});
}

export async function withSpinner<T>(text: string, action: () => T | Promise<T>): Promise<T> {
Expand Down