-
Notifications
You must be signed in to change notification settings - Fork 11.9k
fix(@angular/build): return only lowest version per target engine #33800
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alan-agius4
merged 1 commit into
angular:main
from
alan-agius4:fix/lowest-target-versions
Aug 7, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| import { coerce, compare, minVersion } from 'semver'; | ||
|
|
||
| /** | ||
| * Compares two target version strings. | ||
| * | ||
| * This function is used to determine the lowest version for a given browser target. | ||
| * | ||
| * @param a The first version string. | ||
| * @param b The second version string. | ||
| * @returns A negative value if `a` is lower than `b`, a positive value if `a` is higher than `b`, and 0 if they are equal. | ||
| */ | ||
| function compareTargetVersions(a: string, b: string): number { | ||
| const aVersion = coerce(a); | ||
| const bVersion = coerce(b); | ||
|
|
||
| if (!aVersion || !bVersion) { | ||
| return aVersion ? -1 : bVersion ? 1 : 0; | ||
| } | ||
|
|
||
| return compare(aVersion, bVersion); | ||
| } | ||
|
|
||
| // https://esbuild.github.io/api/#target | ||
| const ESBUILD_SUPPORTED_BROWSERS: ReadonlySet<string> = new Set([ | ||
| 'chrome', | ||
| 'edge', | ||
| 'firefox', | ||
| 'ie', | ||
| 'ios', | ||
| 'node', | ||
| 'opera', | ||
| 'safari', | ||
| ]); | ||
|
|
||
| /** | ||
| * Transform browserlists result to esbuild target. | ||
| * | ||
| * Only the lowest version for each browser is returned to avoid issues with esbuild and rolldown | ||
| * when multiple versions of the same target engine are specified. | ||
| * | ||
| * @see https://esbuild.github.io/api/#target | ||
| * @see https://github.com/evanw/esbuild/issues/4509 | ||
| * @see https://github.com/rolldown/rolldown/issues/10633 | ||
| */ | ||
| export function transformSupportedBrowsersToTargets(supportedBrowsers: string[]): string[] { | ||
| const browsers = new Map<string, string>(); | ||
|
|
||
| for (const browser of supportedBrowsers) { | ||
| let [browserName, version] = browser.toLowerCase().split(' '); | ||
| if (!browserName || !version) { | ||
| continue; | ||
| } | ||
|
|
||
| // browserslist uses the name `ios_saf` for iOS Safari whereas esbuild uses `ios` | ||
| if (browserName === 'ios_saf') { | ||
| browserName = 'ios'; | ||
| } | ||
|
|
||
| if (!ESBUILD_SUPPORTED_BROWSERS.has(browserName)) { | ||
| continue; | ||
| } | ||
|
|
||
| // browserslist uses ranges `15.2-15.3` versions but only the lowest is required | ||
| // to perform minimum supported feature checks. esbuild also expects a single version. | ||
| [version] = version.split('-'); | ||
|
alan-agius4 marked this conversation as resolved.
|
||
|
|
||
| if (browserName === 'safari' && version === 'tp') { | ||
| // esbuild only supports numeric versions so `TP` is converted to a high number (999) since | ||
| // a Technology Preview (TP) of Safari is assumed to support all currently known features. | ||
| version = '999'; | ||
| } else if (!version.includes('.')) { | ||
| // A lone major version is considered by esbuild to include all minor versions. However, | ||
| // browserslist does not and is also inconsistent in its `.0` version naming. For example, | ||
| // Safari 15.0 is named `safari 15` but Safari 16.0 is named `safari 16.0`. | ||
| version += '.0'; | ||
| } | ||
|
|
||
| const current = browsers.get(browserName); | ||
| if (!current || compareTargetVersions(version, current) < 0) { | ||
| browsers.set(browserName, version); | ||
| } | ||
| } | ||
|
|
||
| return Array.from(browsers, ([browserName, version]) => browserName + version); | ||
| } | ||
|
|
||
| const SUPPORTED_NODE_VERSIONS = '0.0.0-ENGINES-NODE'; | ||
|
|
||
| /** | ||
| * Transform supported Node.js versions to esbuild target. | ||
| * | ||
| * Only the lowest Node.js version is returned to avoid issues with esbuild and rolldown | ||
| * when multiple versions of the same target engine are specified. | ||
| * | ||
| * @see https://esbuild.github.io/api/#target | ||
| * @see https://github.com/evanw/esbuild/issues/4509 | ||
| * @see https://github.com/rolldown/rolldown/issues/10633 | ||
| */ | ||
| export function getSupportedNodeTargets(): string[] { | ||
| if (SUPPORTED_NODE_VERSIONS.charAt(0) === '0') { | ||
| // Unlike `pkg_npm`, `ts_library` which is used to run unit tests does not support substitutions. | ||
| return []; | ||
| } | ||
|
|
||
| const parsed = minVersion(SUPPORTED_NODE_VERSIONS); | ||
|
|
||
| return parsed ? ['node' + parsed.version] : []; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| import { getSupportedNodeTargets, transformSupportedBrowsersToTargets } from './target'; | ||
|
|
||
| describe('esbuild target', () => { | ||
| describe('transformSupportedBrowsersToTargets', () => { | ||
| it('should return the smallest version for each browser', () => { | ||
| const targets = transformSupportedBrowsersToTargets([ | ||
| 'chrome 122', | ||
| 'chrome 120', | ||
| 'chrome 121', | ||
| 'firefox 116', | ||
| 'firefox 115', | ||
| 'safari 17.0', | ||
| 'safari 16.4', | ||
| ]); | ||
|
|
||
| expect(targets).toEqual(['chrome120.0', 'firefox115.0', 'safari16.4']); | ||
| }); | ||
|
|
||
| it('should handle version ranges and pick the lowest version', () => { | ||
| const targets = transformSupportedBrowsersToTargets([ | ||
| 'ios_saf 15.4', | ||
| 'ios_saf 15.2-15.3', | ||
| 'ios_saf 16.0', | ||
| ]); | ||
|
|
||
| expect(targets).toEqual(['ios15.2']); | ||
| }); | ||
|
|
||
| it('should handle Safari TP (Technology Preview)', () => { | ||
| const targetsWithOlderSafari = transformSupportedBrowsersToTargets([ | ||
| 'safari TP', | ||
| 'safari 16.4', | ||
| ]); | ||
| expect(targetsWithOlderSafari).toEqual(['safari16.4']); | ||
|
|
||
| const targetsWithOnlyTP = transformSupportedBrowsersToTargets(['safari TP']); | ||
| expect(targetsWithOnlyTP).toEqual(['safari999']); | ||
| }); | ||
|
|
||
| it('should ignore browsers not supported by esbuild', () => { | ||
| const targets = transformSupportedBrowsersToTargets([ | ||
| 'android 4.4', | ||
| 'samsung 22', | ||
| 'kaios 2.5', | ||
| 'chrome 115', | ||
| ]); | ||
|
|
||
| expect(targets).toEqual(['chrome115.0']); | ||
| }); | ||
|
|
||
| it('should return empty array for empty supportedBrowsers', () => { | ||
| const targets = transformSupportedBrowsersToTargets([]); | ||
| expect(targets).toEqual([]); | ||
| }); | ||
|
alan-agius4 marked this conversation as resolved.
|
||
|
|
||
| it('should handle malformed or incomplete browser strings gracefully', () => { | ||
| const targets = transformSupportedBrowsersToTargets(['chrome', 'firefox ', '']); | ||
| expect(targets).toEqual([]); | ||
| }); | ||
|
|
||
| it('should handle single major versions by appending .0', () => { | ||
| const targets = transformSupportedBrowsersToTargets(['chrome 120', 'edge 120']); | ||
| expect(targets).toEqual(['chrome120.0', 'edge120.0']); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getSupportedNodeTargets', () => { | ||
| it('should return empty array when node versions are not stamped', () => { | ||
| expect(getSupportedNodeTargets()).toEqual([]); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.