Skip to content

Commit be9fe38

Browse files
committed
fix(@angular-devkit/build-angular): downlevelled libraries based on the browserlist configurations
There is no standard for library authors to ship their library in different ES versions, which can result in vendor libraries to ship ES features which are not supported by one or more browsers that the user's application supports. With this change, we will be downlevelling libraries based on the list of supported browsers which is configured in the browserslist configuration. Previously, we only downlevelled libraries when targetting ES 5. The TypeScript target option will not effect how the libraries get downlevelled. Closes angular#23126
1 parent c71832f commit be9fe38

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

packages/angular_devkit/build_angular/src/babel/presets/application.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export interface ApplicationPresetOptions {
4747
linkerPluginCreator: typeof import('@angular/compiler-cli/linker/babel').createEs2015LinkerPlugin;
4848
};
4949

50-
forceES5?: boolean;
50+
forcePresetEnv?: boolean;
5151
forceAsyncTransformation?: boolean;
5252
instrumentCode?: {
5353
includedBasePath: string;
@@ -59,6 +59,7 @@ export interface ApplicationPresetOptions {
5959
wrapDecorators: boolean;
6060
};
6161

62+
supportedBrowsers?: string[];
6263
diagnosticReporter?: DiagnosticReporter;
6364
}
6465

@@ -178,14 +179,13 @@ export default function (api: unknown, options: ApplicationPresetOptions) {
178179
);
179180
}
180181

181-
if (options.forceES5) {
182+
if (options.forcePresetEnv) {
182183
presets.push([
183184
require('@babel/preset-env').default,
184185
{
185186
bugfixes: true,
186187
modules: false,
187-
// Comparable behavior to tsconfig target of ES5
188-
targets: { ie: 9 },
188+
targets: options.supportedBrowsers,
189189
exclude: ['transform-typeof-symbol'],
190190
},
191191
]);

packages/angular_devkit/build_angular/src/babel/webpack-loader.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,26 @@ export default custom<ApplicationPresetOptions>(() => {
7272

7373
return {
7474
async customOptions(options, { source, map }) {
75-
const { i18n, scriptTarget, aot, optimize, instrumentCode, ...rawOptions } =
76-
options as AngularBabelLoaderOptions;
75+
const {
76+
i18n,
77+
scriptTarget,
78+
aot,
79+
optimize,
80+
instrumentCode,
81+
supportedBrowsers,
82+
...rawOptions
83+
} = options as AngularBabelLoaderOptions;
7784

7885
// Must process file if plugins are added
7986
let shouldProcess = Array.isArray(rawOptions.plugins) && rawOptions.plugins.length > 0;
8087

8188
const customOptions: ApplicationPresetOptions = {
8289
forceAsyncTransformation: false,
83-
forceES5: false,
90+
forcePresetEnv: false,
8491
angularLinker: undefined,
8592
i18n: undefined,
8693
instrumentCode: undefined,
94+
supportedBrowsers,
8795
};
8896

8997
// Analyze file for linking
@@ -105,20 +113,25 @@ export default custom<ApplicationPresetOptions>(() => {
105113
shouldProcess = true;
106114
}
107115

108-
// Analyze for ES target processing
109-
const esTarget = scriptTarget as ScriptTarget | undefined;
110-
if (esTarget !== undefined) {
111-
if (esTarget < ScriptTarget.ES2015) {
112-
customOptions.forceES5 = true;
113-
} else if (esTarget >= ScriptTarget.ES2017 || /\.[cm]?js$/.test(this.resourcePath)) {
116+
if (/\.[cm]?js$/.test(this.resourcePath)) {
117+
// Applications code ES version can be controlled using TypeScript's `target` option.
118+
// However, this doesn't effect libraries and hence we use preset-env to downlevel ES fetaures
119+
// based on the supported browsers in browserlist.
120+
customOptions.forcePresetEnv = true;
121+
122+
// Analyze for ES target processing
123+
const esTarget = scriptTarget as ScriptTarget | undefined;
124+
125+
if (esTarget !== undefined && esTarget >= ScriptTarget.ES2017) {
114126
// Application code (TS files) will only contain native async if target is ES2017+.
115127
// However, third-party libraries can regardless of the target option.
116128
// APF packages with code in [f]esm2015 directories is downlevelled to ES2015 and
117129
// will not have native async.
118130
customOptions.forceAsyncTransformation =
119131
!/[\\/][_f]?esm2015[\\/]/.test(this.resourcePath) && source.includes('async');
120132
}
121-
shouldProcess ||= customOptions.forceAsyncTransformation || customOptions.forceES5 || false;
133+
134+
shouldProcess = true;
122135
}
123136

124137
// Analyze for i18n inlining

packages/angular_devkit/build_angular/src/webpack/configs/common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ export async function getCommonConfig(wco: WebpackConfigOptions): Promise<Config
378378
scriptTarget,
379379
aot: buildOptions.aot,
380380
optimize: buildOptions.buildOptimizer,
381+
supportedBrowsers: buildOptions.supportedBrowsers,
381382
instrumentCode: codeCoverage
382383
? {
383384
includedBasePath: sourceRoot,

0 commit comments

Comments
 (0)