Skip to content

Commit d009aa1

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. (cherry picked from commit 4643a8a)
1 parent f1ee15d commit d009aa1

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');
@@ -751,3 +752,18 @@ function getEsBuildCommonPolyfillsOptions(
751752
function entryFileToWorkspaceRelative(workspaceRoot: string, entryFile: string): string {
752753
return './' + toPosixPath(relative(workspaceRoot, entryFile).replace(/.[mc]?ts$/, ''));
753754
}
755+
756+
/**
757+
* Determines if a polyfill path is a local file.
758+
* A local file is defined as a path starting with a `.` or having a TypeScript/JavaScript extension.
759+
* `zone.js` and its subpaths are specifically excluded and treated as packages.
760+
* @param path The polyfill path to check.
761+
* @returns true if the path is a local file; false otherwise.
762+
*/
763+
function isLocalFile(path: string): boolean {
764+
if (path.startsWith('zone.js')) {
765+
return false;
766+
}
767+
768+
return path[0] === '.' || /\.[mc]?[jt]sx?$/.test(path);
769+
}

0 commit comments

Comments
 (0)