Skip to content

Commit 7aa477c

Browse files
committed
fix(@angular/build): only use external packages for polyfills when no local files are present
The polyfills bundle now conditionally employs external package resolution only if no local files are detected in the polyfills array. If local files are present, all polyfills are bundled together to ensure the import execution order is correctly preserved between local and package-based entries.
1 parent cb57740 commit 7aa477c

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -654,18 +654,19 @@ function getEsBuildCommonPolyfillsOptions(
654654
tryToResolvePolyfillsAsRelative: boolean,
655655
loadResultCache: LoadResultCache | undefined,
656656
): BuildOptions | undefined {
657-
const { jit, workspaceRoot, i18nOptions } = options;
657+
const { jit, workspaceRoot, i18nOptions, externalPackages } = options;
658+
659+
let polyfills = options.polyfills ? [...options.polyfills] : [];
658660

659661
const buildOptions = getEsBuildCommonOptions({
660662
...options,
661-
externalPackages: false,
663+
// If any polyfills are local files, disable external packages for the polyfills build.
664+
// This ensures that local files are properly bundled.
665+
externalPackages: polyfills.some(isLocalFile) ? false : externalPackages,
662666
});
663-
buildOptions.packages = 'bundle';
664667
buildOptions.splitting = false;
665668
buildOptions.plugins ??= [];
666669

667-
let polyfills = options.polyfills ? [...options.polyfills] : [];
668-
669670
// Angular JIT mode requires the runtime compiler
670671
if (jit) {
671672
polyfills.unshift('@angular/compiler');
@@ -749,3 +750,18 @@ function getEsBuildCommonPolyfillsOptions(
749750
function entryFileToWorkspaceRelative(workspaceRoot: string, entryFile: string): string {
750751
return './' + toPosixPath(relative(workspaceRoot, entryFile).replace(/.[mc]?ts$/, ''));
751752
}
753+
754+
/**
755+
* Determines if a polyfill path is a local file.
756+
* A local file is defined as a path starting with a `.` or having a TypeScript/JavaScript extension.
757+
* `zone.js` and its subpaths are specifically excluded and treated as packages.
758+
* @param path The polyfill path to check.
759+
* @returns true if the path is a local file; false otherwise.
760+
*/
761+
function isLocalFile(path: string): boolean {
762+
if (path === 'zone.js' || path.startsWith('zone.js/')) {
763+
return false;
764+
}
765+
766+
return path.startsWith('.') || /\.[mc]?[jt]sx?$/.test(path);
767+
}

0 commit comments

Comments
 (0)