Repository navigation
perf(@angular/build): bundle a single copy of domino for the server - #34516
ChristopherPHolder wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a mechanism to share a single bundled copy of Domino (from @angular/platform-server/init in the server polyfills bundle) across other server bundles, rather than bundling it multiple times. This is achieved by exporting Domino from the server polyfills bundle and redirecting imports in other server bundles to this exported copy using a new esbuild plugin (platform-server-domino-plugin). A comprehensive test suite has also been added to verify this behavior under various configurations. I have no further feedback to provide as there are no review comments.
`@angular/platform-server/init` (bundled into the server polyfills) and `@angular/platform-server` (bundled into the main server bundle) both import the domino DOM implementation shipped with the package. As the two bundles are separate esbuild contexts, each output contained its own copy, and both were evaluated at runtime. Besides the size and memory cost, the two copies are distinct class hierarchies: DOM globals captured at module evaluation time (the scenario `init` exists for) come from the polyfills copy, while the platform renders with the other, so `instanceof` checks between them fail. The server polyfills bundle now re-exports the domino module it already contains, and the main server bundle redirects the `@angular/platform-server` import of domino to that export. The polyfills bundle is already imported first and marked external by every server bundle, which keeps the evaluation order unchanged. Fixes angular#34514
6a6708e to
9f1c146
Compare
|
Thank you @ChristopherPHolder for identifying this issue, investigating the duplicate Domino bundling in server builds, and providing the initial implementation! Closing in favor of #34527, which implements a streamlined approach avoiding additional content threading in polyfill options and filesystem probing. |
PR Checklist
Please check to confirm your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Issue Number: #34514
The server build emits two complete copies of domino:
polyfills.server.mjsbundles it via@angular/platform-server/init, andmain.server.mjsbundles it again via@angular/platform-server. The two bundles are separate esbuild contexts, so the sharedthird_party/domino/bundled-domino.mjsmodule is inlined into each, and both are evaluated at runtime (main.server.mjsimports./polyfills.server.mjsfirst).Because the two copies are distinct class hierarchies, DOM globals captured at module-evaluation time (the scenario
initexists for) do not match the classes the platform renders with:What is the new behavior?
The server polyfills bundle re-exports the domino module it already contains, and the main server bundle redirects the
@angular/platform-serverimport of../third_party/domino/bundled-domino.mjsto that export../polyfills.server.mjsis already inSERVER_GENERATED_EXTERNALSand imported first by every server bundle, so the evaluation order is unchanged.The polyfills bundle has to stay a separate, externally imported file: with code splitting, a side-effect import at the top of the
main.serverentry is inlined into the entry while shared chunks are hoisted above it, so moving theinitimport there would run it after shared application chunks. Sharing the copy avoids that.On the default
ng new --ssrapp (Angular CLI 22.2.4, Node 26.8.1):bundled-domino.mjsinmain.server.mjsmain.server.mjs(production)main.server.mjsbody instanceof capturedHTMLElementCovered:
outputMode: 'server', the legacyssr+prerenderpath (same esbuild context and banner),platform: 'neutral', prerendering, and the dev server when bundling. When packages are external (ng serveprebundling) nothing changes; Node's module cache already shares one copy there. If@angular/platform-serveris not installed, is external, or has an unexpected layout, both sides fall back to today's behavior.Reproduction with the measurement harness and the
instanceofprobe: https://github.com/ChristopherPHolder/ng-double-dominoDoes this PR introduce a breaking change?
Other information
@angular/platform-server/initis still added to every server build. Making that optional (and the server polyfills list user-controllable in general) is a separate request: #34515.