Skip to content

Commit 6ad8608

Browse files
committed
fix(@angular/build): bundle polyfills to preserve execution order in dev server
When using the development server with ESM, polyfills were built as a mix of bundled local files and external package imports. Due to ESM hoisting behavior, all top-level import statements are executed before any other code in the module. This caused polyfills that modify global state (like zone-flags.js) to run after the polyfills they were intended to configure (like zone.js), rendering the flags ineffective. This change ensures that all polyfills are bundled together into the single polyfills.js file when using the development server, matching the behavior of the production build and preserving the intended execution order. Closes angular#32632 (cherry picked from commit ec289c3)
1 parent 43a9dfa commit 6ad8608

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

packages/angular/build/src/tools/esbuild/application-code-bundle.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -654,9 +654,13 @@ function getEsBuildCommonPolyfillsOptions(
654654
tryToResolvePolyfillsAsRelative: boolean,
655655
loadResultCache: LoadResultCache | undefined,
656656
): BuildOptions | undefined {
657-
const { jit, workspaceRoot, i18nOptions, externalPackages } = options;
657+
const { jit, workspaceRoot, i18nOptions } = options;
658658

659-
const buildOptions = getEsBuildCommonOptions(options);
659+
const buildOptions = getEsBuildCommonOptions({
660+
...options,
661+
externalPackages: false,
662+
});
663+
buildOptions.packages = 'bundle';
660664
buildOptions.splitting = false;
661665
buildOptions.plugins ??= [];
662666

@@ -671,10 +675,8 @@ function getEsBuildCommonPolyfillsOptions(
671675
// Locale data should go first so that project provided polyfill code can augment if needed.
672676
let needLocaleDataPlugin = false;
673677
if (i18nOptions.shouldInline) {
674-
if (!externalPackages) {
675-
// Remove localize polyfill when i18n inline transformation have been applied to all the packages.
676-
polyfills = polyfills.filter((path) => !path.startsWith('@angular/localize'));
677-
}
678+
// Remove localize polyfill when i18n inline transformation have been applied to all the packages.
679+
polyfills = polyfills.filter((path) => !path.startsWith('@angular/localize'));
678680

679681
// Add locale data for all active locales
680682
// TODO: Inject each individually within the inlining process itself

0 commit comments

Comments
 (0)