Skip to content

Commit

Permalink
fix(@angular/build): correctly name entry points to match budgets
Browse files Browse the repository at this point in the history
This commit addresses an issue where some lazy entry points were not name correctly to align with specified budgets.

Closes: #27936
(cherry picked from commit f3ba208)
  • Loading branch information
alan-agius4 committed Jun 27, 2024
1 parent 6aff0f8 commit fc928f6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
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, '-');
}

0 comments on commit fc928f6

Please sign in to comment.