Skip to content

Commit ea923d0

Browse files
committed
fix(@angular-devkit/build-angular): prefer ES2015 entrypoints when application targets ES2019 or lower
Previously, we always consumed the ES2020 entrypoints, which caused issues in environments where the application compilation target is ES2019 or lower and ES2020 is not supported. This is because we only downlevel code when we target ES5 or below. - ES5 or below compilations, ES2015 entrypoints are used and their code is downlevelled to ES5. - ES2019 or below, ES2015 entrypoints are used and no downlevelling is involved. - ES2020 or later, ES2020 entrypoints are used. Closes #22270
1 parent 09efafe commit ea923d0

5 files changed

Lines changed: 29 additions & 12 deletions

File tree

packages/angular_devkit/build_angular/src/builders/browser/specs/lazy-module_spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ describe('Browser Builder lazy modules', () => {
152152
const { files } = await browserBuild(architect, host, target);
153153
expect(files['src_one_ts.js']).not.toBeUndefined();
154154
expect(files['src_two_ts.js']).not.toBeUndefined();
155-
expect(files['default-node_modules_angular_common_fesm2020_http_mjs.js']).toBeDefined();
155+
expect(files['default-node_modules_angular_common_fesm2015_http_mjs.js']).toBeDefined();
156156
});
157157

158158
it(`supports disabling the common bundle`, async () => {
@@ -165,6 +165,6 @@ describe('Browser Builder lazy modules', () => {
165165
const { files } = await browserBuild(architect, host, target, { commonChunk: false });
166166
expect(files['src_one_ts.js']).not.toBeUndefined();
167167
expect(files['src_two_ts.js']).not.toBeUndefined();
168-
expect(files['default-node_modules_angular_common_fesm2020_http_mjs.js']).toBeUndefined();
168+
expect(files['default-node_modules_angular_common_fesm2015_http_mjs.js']).toBeUndefined();
169169
});
170170
});

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ export function getBrowserConfig(wco: WebpackConfigOptions): webpack.Configurati
5353

5454
return {
5555
devtool: false,
56-
resolve: {
57-
mainFields: ['es2020', 'es2015', 'browser', 'module', 'main'],
58-
conditionNames: ['es2020', 'es2015', '...'],
59-
},
6056
output: {
6157
crossOriginLoading,
6258
trustedTypes: 'angular#bundler',

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { DedupeModuleResolvePlugin, ScriptsWebpackPlugin } from '../plugins';
3131
import { JavaScriptOptimizerPlugin } from '../plugins/javascript-optimizer-plugin';
3232
import {
3333
getInstrumentationExcludedPaths,
34+
getMainFieldsAndConditionNames,
3435
getOutputHashFormat,
3536
getWatchOptions,
3637
normalizeExtraEntryPoints,
@@ -326,11 +327,13 @@ export async function getCommonConfig(
326327
);
327328
}
328329

330+
const isPlatformServer = platform === 'server';
331+
329332
return {
330333
mode: scriptsOptimization || stylesOptimization.minify ? 'production' : 'development',
331334
devtool: false,
332335
target: [
333-
platform === 'server' ? 'node' : 'web',
336+
isPlatformServer ? 'node' : 'web',
334337
tsConfig.options.target === ScriptTarget.ES5 ? 'es5' : 'es2015',
335338
],
336339
profile: buildOptions.statsJson,
@@ -339,6 +342,7 @@ export async function getCommonConfig(
339342
extensions: ['.ts', '.tsx', '.mjs', '.js'],
340343
symlinks: !buildOptions.preserveSymlinks,
341344
modules: [tsConfig.options.baseUrl || projectRoot, 'node_modules'],
345+
...getMainFieldsAndConditionNames(wco.scriptTarget, isPlatformServer),
342346
},
343347
resolveLoader: {
344348
symlinks: !buildOptions.preserveSymlinks,

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ export function getServerConfig(wco: WebpackConfigOptions): Configuration {
3232
}
3333

3434
return {
35-
resolve: {
36-
mainFields: ['es2015', 'main', 'module'],
37-
conditionNames: ['es2015', '...'],
38-
},
3935
output: {
4036
libraryTarget: 'commonjs',
4137
},

packages/angular_devkit/build_angular/src/webpack/utils/helpers.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
import glob from 'glob';
1010
import * as path from 'path';
11-
import { Configuration, SourceMapDevToolPlugin } from 'webpack';
11+
import { ScriptTarget } from 'typescript';
12+
import { Configuration, SourceMapDevToolPlugin, WebpackOptionsNormalized } from 'webpack';
1213
import { ExtraEntryPoint, ExtraEntryPointClass } from '../../builders/browser/schema';
1314

1415
export interface HashFormat {
@@ -147,3 +148,23 @@ export function getInstrumentationExcludedPaths(
147148

148149
return excluded;
149150
}
151+
152+
export function getMainFieldsAndConditionNames(
153+
target: ScriptTarget,
154+
platformServer: boolean,
155+
): Pick<WebpackOptionsNormalized['resolve'], 'mainFields' | 'conditionNames'> {
156+
const mainFields = platformServer
157+
? ['es2015', 'module', 'main']
158+
: ['es2015', 'browser', 'module', 'main'];
159+
const conditionNames = ['es2015', '...'];
160+
161+
if (target >= ScriptTarget.ES2020) {
162+
mainFields.unshift('es2020');
163+
conditionNames.unshift('es2020');
164+
}
165+
166+
return {
167+
mainFields,
168+
conditionNames,
169+
};
170+
}

0 commit comments

Comments
 (0)