Skip to content

fix(@angular-devkit/build-angular): display server bundles in build stats #26988

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

Merged
merged 2 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export async function executeBuild(
assets,
cacheOptions,
prerenderOptions,
ssrOptions,
verbose,
} = options;

// TODO: Consider integrating into watch mode. Would require full rebuild on target changes.
Expand Down Expand Up @@ -188,6 +190,8 @@ export async function executeBuild(
budgetFailures,
changedFiles,
estimatedTransferSizes,
!!ssrOptions,
verbose,
);

// Write metafile if stats option is enabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ export function setupBundlerContexts(
nodeTargets,
codeBundleCache,
),
() => false,
),
);

Expand All @@ -116,12 +115,7 @@ export function setupBundlerContexts(

if (serverPolyfillBundleOptions) {
bundlerContexts.push(
new BundlerContext(
workspaceRoot,
!!options.watch,
serverPolyfillBundleOptions,
() => false,
),
new BundlerContext(workspaceRoot, !!options.watch, serverPolyfillBundleOptions),
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,6 @@ describe('Browser Builder scripts array', () => {
expect(joinedLogs).toMatch(/lazy-script.+\d+ bytes/);
expect(joinedLogs).toMatch(/renamed-script.+\d+ bytes/);
expect(joinedLogs).toMatch(/renamed-lazy-script.+\d+ bytes/);
expect(joinedLogs).not.toContain('Lazy Chunks');
expect(joinedLogs).not.toContain('Lazy chunks');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ export function generateBudgetStats(
continue;
}

// Exclude server bundles
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if ((entry as any)['ng-platform-server']) {
continue;
}

const initialRecord = initialFiles.get(file);
let name = initialRecord?.name;
if (name === undefined && entry.entryPoint) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,13 @@ export class BundlerContext {
// For non-incremental builds, perform a single build
result = await build(this.#esbuildOptions);
}

if (this.#esbuildOptions?.platform === 'node') {
for (const entry of Object.values(result.metafile.outputs)) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(entry as any)['ng-platform-server'] = true;
}
}
} catch (failure) {
// Build failures will throw an exception which contains errors/warnings
if (isEsBuildFailure(failure)) {
Expand Down
37 changes: 28 additions & 9 deletions packages/angular_devkit/build_angular/src/tools/esbuild/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { coerce } from 'semver';
import { NormalizedOutputOptions } from '../../builders/application/options';
import { BudgetCalculatorResult } from '../../utils/bundle-calculator';
import { Spinner } from '../../utils/spinner';
import { BundleStats, generateBuildStatsTable } from '../webpack/utils/stats';
import { BundleStats, generateEsbuildBuildStatsTable } from '../webpack/utils/stats';
import { BuildOutputFile, BuildOutputFileType, InitialFileRecord } from './bundler-context';
import { BuildOutputAsset } from './bundler-execution-result';

Expand All @@ -28,14 +28,19 @@ export function logBuildStats(
budgetFailures: BudgetCalculatorResult[] | undefined,
changedFiles?: Set<string>,
estimatedTransferSizes?: Map<string, number>,
ssrOutputEnabled?: boolean,
verbose?: boolean,
): void {
const stats: BundleStats[] = [];
const browserStats: BundleStats[] = [];
const serverStats: BundleStats[] = [];
let unchangedCount = 0;

for (const [file, output] of Object.entries(metafile.outputs)) {
// Only display JavaScript and CSS files
if (!file.endsWith('.js') && !file.endsWith('.css')) {
if (!/\.(?:css|m?js)$/.test(file)) {
continue;
}

// Skip internal component resources
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if ((output as any)['ng-component']) {
Expand All @@ -48,6 +53,13 @@ export function logBuildStats(
continue;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const isPlatformServer = (output as any)['ng-platform-server'];
if (isPlatformServer && !ssrOutputEnabled) {
// Only log server build stats when SSR is enabled.
continue;
}

let name = initial.get(file)?.name;
if (name === undefined && output.entryPoint) {
name = path
Expand All @@ -56,22 +68,29 @@ export function logBuildStats(
.replace(/[\\/.]/g, '-');
}

stats.push({
const stat: BundleStats = {
initial: initial.has(file),
stats: [file, name ?? '-', output.bytes, estimatedTransferSizes?.get(file) ?? '-'],
});
};

if (isPlatformServer) {
serverStats.push(stat);
} else {
browserStats.push(stat);
}
}

if (stats.length > 0) {
const tableText = generateBuildStatsTable(
stats,
if (browserStats.length > 0 || serverStats.length > 0) {
const tableText = generateEsbuildBuildStatsTable(
[browserStats, serverStats],
true,
unchangedCount === 0,
!!estimatedTransferSizes,
budgetFailures,
verbose,
);

logger.info('\n' + tableText + '\n');
logger.info(tableText + '\n');
} else if (changedFiles !== undefined) {
logger.info('\nNo output file changes.\n');
}
Expand Down
Loading