Skip to content

fix(@angular/build): correctly name entry points to match budgets #27947

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 1 commit into from
Jun 27, 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 @@ -93,6 +93,39 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
);
});

it(`should not warn when non-injected style is not within the baseline threshold`, async () => {
harness.useTarget('build', {
...BASE_OPTIONS,
optimization: false,
styles: [
{
input: 'src/lazy-styles.css',
inject: false,
bundleName: 'lazy-styles',
},
],
budgets: [
{ type: Type.Bundle, name: 'lazy-styles', warning: '1kb', error: '1kb', baseline: '2kb' },
],
});

await harness.writeFile(
'src/lazy-styles.css',
`
.foo { color: green; padding: 1px; }
`.repeat(24),
);

const { result, logs } = await harness.executeOnce();
expect(result?.success).toBeTrue();
expect(logs).not.toContain(
jasmine.objectContaining<logging.LogEntry>({
level: 'warn',
message: jasmine.stringMatching('lazy-styles failed to meet minimum budget'),
}),
);
});

CSS_EXTENSIONS.forEach((ext) => {
it(`shows warnings for large component ${ext} when using 'anyComponentStyle' when AOT`, async () => {
const cssContent = `
Expand Down
6 changes: 2 additions & 4 deletions packages/angular/build/src/tools/esbuild/budget-stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
*/

import type { Metafile } from 'esbuild';
import { basename } from 'node:path';
import type { BudgetStats } from '../../utils/bundle-calculator';
import type { InitialFileRecord } from './bundler-context';
import { getEntryPointName } from './utils';

/**
* Generates a bundle budget calculator compatible stats object that provides
Expand Down Expand Up @@ -43,9 +43,7 @@ export function generateBudgetStats(
let name = initialRecord?.name;
if (name === undefined && entry.entryPoint) {
// For non-initial lazy modules, convert the entry point file into a Webpack compatible name
name = basename(entry.entryPoint)
.replace(/\.[cm]?[jt]s$/, '')
.replace(/[\\/.]/g, '-');
name = getEntryPointName(entry.entryPoint);
}

stats.chunks.push({
Expand Down
11 changes: 8 additions & 3 deletions packages/angular/build/src/tools/esbuild/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ export function logBuildStats(

let name = initial.get(file)?.name;
if (name === undefined && output.entryPoint) {
name = basename(output.entryPoint)
.replace(/\.[cm]?[jt]s$/, '')
.replace(/[\\/.]/g, '-');
name = getEntryPointName(output.entryPoint);
}

const stat: BundleStats = {
Expand Down Expand Up @@ -496,3 +494,10 @@ export function isZonelessApp(polyfills: string[] | undefined): boolean {
// TODO: Instead, we should rely on the presence of zone.js in the polyfills build metadata.
return !polyfills?.some((p) => p === 'zone.js' || /\.[mc]?[jt]s$/.test(p));
}

export function getEntryPointName(entryPoint: string): string {
return basename(entryPoint)
.replace(/(.*:)/, '') // global:bundle.css -> bundle.css
.replace(/\.[cm]?[jt]s$/, '')
.replace(/[\\/.]/g, '-');
}