angular / angular-cli

CLI tool for Angular
https://cli.angular.dev
MIT License
26.78k stars 11.98k forks source link

Zoneless SSR doen't build when using file polyfill #28898

Open martin-yumsto opened 2 days ago

martin-yumsto commented 2 days ago

Which @angular/* package(s) are the source of the bug?

compiler-cli

Is this a regression?

No

Description

I'm trying to go zoneless for my SSR angular app with hydration.

This condition seems to cause difficulties to migrate to zoneless for any SSR app that uses file based polyfills: https://github.com/angular/angular-cli/blob/d622e594878611c4a00aab94e15ce2dcf108b088/packages/angular/build/src/tools/esbuild/application-code-bundle.ts#L175

I guess you are aware of the issue, since there is a todo on the condition that feels too generic: https://github.com/angular/angular-cli/blob/d622e594878611c4a00aab94e15ce2dcf108b088/packages/angular/build/src/tools/esbuild/utils.ts#L470

Basically it adds zone.js/node import for all the builds that have file polyfill.

Minimal repro would be:

angular.json: "polyfills": ["@angular/localize/init", "polyfills.ts"]

polyfills.ts (window as any).global = window; // Amplify

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

Please provide the environment you discovered this bug in (run ng version)

ngular CLI: 19.0.0
Node: 20.17.0
Package Manager: pnpm 8.14.1
OS: darwin arm64

Angular: 19.0.0
... animations, cdk, cli, common, compiler, compiler-cli, core
... forms, localize, platform-browser, platform-browser-dynamic
... platform-server, router, service-worker, ssr

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1900.0
@angular-devkit/build-angular   19.0.0
@angular-devkit/core            19.0.0
@angular-devkit/schematics      19.0.0
@schematics/angular             19.0.0
rxjs                            7.5.7
typescript                      5.6.3

Anything else?

No response

e-oz commented 2 days ago

I didn't get it. My zoneless SSR app builds just fine with v19.

martin-yumsto commented 2 days ago

@e-oz do you use file based polyfill ?

I just noticed I forgot to include the error log so it's here:

✘ [ERROR] Could not resolve "zone.js/node"

    angular:polyfills-server:angular:polyfills-server:2:7:
      2 │ import 'zone.js/node';

Basically for the server side package build the condition that detects whether it should build zone-based or zoneless app is very generic and it matches "zone.js" or any other js/ts file inside the polyfills array. So I suppose even empty file specified in that array inside angular.json build will cause the error.

e-oz commented 2 days ago

polyfills.ts (window as any).global = window; // Amplify

I guess that this thing alone will throw an exception on the server side.

In my polyfills I have only link to localize.