Skip to content
Open
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
28 changes: 28 additions & 0 deletions packages/angular/build/src/builders/application/execute-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { extractLicenses } from '../../tools/esbuild/license-extractor';
import { profileAsync } from '../../tools/esbuild/profiling';
import {
calculateEstimatedTransferSizes,
isZonelessApp,
logBuildStats,
transformSupportedBrowsersToTargets,
} from '../../tools/esbuild/utils';
Expand Down Expand Up @@ -156,6 +157,33 @@ export async function executeBuild(

// Return if the bundling has errors
if (bundlingResult.errors) {
// If Zone.js is used, augment top-level await errors with a more helpful message.
// esbuild's default error mentions "target environment" with browser versions, but
// the actual reason is that async/await is downleveled for Zone.js compatibility.
if (!isZonelessApp(options.polyfills)) {
for (const error of bundlingResult.errors) {
if (
error.text?.startsWith(
'Top-level await is not available in the configured target environment',
)
) {
error.notes = [
{
text:
'Top-level await is not supported in applications that use Zone.js. ' +
'Consider removing Zone.js or moving this code into an async function.',
location: null,
},
{
text: 'For more information about zoneless Angular applications, visit: https://angular.dev/guide/zoneless',
location: null,
},
...(error.notes ?? []),
];
}
}
}
Comment on lines +163 to +185
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve maintainability and readability, it's a good practice to extract magic strings into constants. The error message from esbuild could change in the future, and having it as a constant makes it easier to update.

    if (!isZonelessApp(options.polyfills)) {
      const TLA_ERROR_MESSAGE =
        'Top-level await is not available in the configured target environment';
      for (const error of bundlingResult.errors) {
        if (error.text?.startsWith(TLA_ERROR_MESSAGE)) {
          error.notes = [
            {
              text:
                'Top-level await is not supported in applications that use Zone.js. ' +
                'Consider removing Zone.js or moving this code into an async function.',
              location: null,
            },
            {
              text: 'For more information about zoneless Angular applications, visit: https://angular.dev/guide/zoneless',
              location: null,
            },
            ...(error.notes ?? []),
          ];
        }
      }
    }


executionResult.addErrors(bundlingResult.errors);

return executionResult;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/

import { buildApplication } from '../../index';
import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup';

describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
describe('Behavior: "Top-level await error message"', () => {
it('should show a Zone.js-specific error when top-level await is used with Zone.js', async () => {
await harness.writeFile(
'src/main.ts',
`
const value = await Promise.resolve('test');
console.log(value);
`,
);

harness.useTarget('build', {
...BASE_OPTIONS,
polyfills: ['zone.js'],
});

const { result, logs } = await harness.executeOnce({ outputLogsOnFailure: false });
expect(result?.success).toBeFalse();
expect(logs).toContain(
jasmine.objectContaining({
message: jasmine.stringMatching(
'Top-level await is not supported in applications that use Zone.js',
),
}),
);
});

it('should not show a Zone.js-specific error when top-level await is used without Zone.js', async () => {
await harness.writeFile(
'src/main.ts',
`
const value = await Promise.resolve('test');
console.log(value);
`,
);

harness.useTarget('build', {
...BASE_OPTIONS,
polyfills: [],
});

const { result, logs } = await harness.executeOnce({ outputLogsOnFailure: false });
// Without Zone.js, top-level await should be supported and the build should succeed
expect(result?.success).toBeTrue();
});
});
});
Loading