Skip to content
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

fix(@angular-devkit/build-angular): prefer ES2015 entrypoints when application targets ES2019 or lower #22305

Merged
merged 1 commit into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ describe('Browser Builder lazy modules', () => {
const { files } = await browserBuild(architect, host, target);
expect(files['src_one_ts.js']).not.toBeUndefined();
expect(files['src_two_ts.js']).not.toBeUndefined();
expect(files['default-node_modules_angular_common_fesm2020_http_mjs.js']).toBeDefined();
expect(files['default-node_modules_angular_common_fesm2015_http_mjs.js']).toBeDefined();
});

it(`supports disabling the common bundle`, async () => {
Expand All @@ -165,6 +165,6 @@ describe('Browser Builder lazy modules', () => {
const { files } = await browserBuild(architect, host, target, { commonChunk: false });
expect(files['src_one_ts.js']).not.toBeUndefined();
expect(files['src_two_ts.js']).not.toBeUndefined();
expect(files['default-node_modules_angular_common_fesm2020_http_mjs.js']).toBeUndefined();
expect(files['default-node_modules_angular_common_fesm2015_http_mjs.js']).toBeUndefined();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,24 @@ import {
externalizePackages,
getCacheSettings,
getInstrumentationExcludedPaths,
getMainFieldsAndConditionNames,
getOutputHashFormat,
getStatsOptions,
globalScriptsByBundleName,
} from '../utils/helpers';

// eslint-disable-next-line max-lines-per-function
export async function getCommonConfig(wco: WebpackConfigOptions): Promise<Configuration> {
const { root, projectRoot, buildOptions, tsConfig, projectName, sourceRoot, tsConfigPath } = wco;
const {
root,
projectRoot,
buildOptions,
tsConfig,
projectName,
sourceRoot,
tsConfigPath,
scriptTarget,
} = wco;
const {
cache,
codeCoverage,
Expand Down Expand Up @@ -283,7 +293,7 @@ export async function getCommonConfig(wco: WebpackConfigOptions): Promise<Config
new JavaScriptOptimizerPlugin({
define: buildOptions.aot ? GLOBAL_DEFS_FOR_TERSER_WITH_AOT : GLOBAL_DEFS_FOR_TERSER,
sourcemap: scriptsSourceMap,
target: wco.scriptTarget,
target: scriptTarget,
keepNames: !allowMangle || isPlatformServer,
removeLicenses: buildOptions.extractLicenses,
advanced: buildOptions.buildOptimizer,
Expand Down Expand Up @@ -314,18 +324,15 @@ export async function getCommonConfig(wco: WebpackConfigOptions): Promise<Config
devtool: false,
target: [
isPlatformServer ? 'node' : 'web',
tsConfig.options.target === ScriptTarget.ES5 ? 'es5' : 'es2015',
scriptTarget === ScriptTarget.ES5 ? 'es5' : 'es2015',
],
profile: buildOptions.statsJson,
resolve: {
roots: [projectRoot],
extensions: ['.ts', '.tsx', '.mjs', '.js'],
symlinks: !buildOptions.preserveSymlinks,
modules: [tsConfig.options.baseUrl || projectRoot, 'node_modules'],
mainFields: isPlatformServer
? ['es2015', 'module', 'main']
: ['es2020', 'es2015', 'browser', 'module', 'main'],
conditionNames: isPlatformServer ? ['es2015', '...'] : ['es2020', 'es2015', '...'],
...getMainFieldsAndConditionNames(scriptTarget, isPlatformServer),
},
resolveLoader: {
symlinks: !buildOptions.preserveSymlinks,
Expand Down Expand Up @@ -394,7 +401,7 @@ export async function getCommonConfig(wco: WebpackConfigOptions): Promise<Config
loader: require.resolve('../../babel/webpack-loader'),
options: {
cacheDirectory: (cache.enabled && path.join(cache.path, 'babel-webpack')) || false,
scriptTarget: wco.scriptTarget,
scriptTarget,
aot: buildOptions.aot,
optimize: buildOptions.buildOptimizer,
instrumentCode: codeCoverage
Expand Down
21 changes: 21 additions & 0 deletions packages/angular_devkit/build_angular/src/webpack/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { createHash } from 'crypto';
import { existsSync } from 'fs';
import glob from 'glob';
import * as path from 'path';
import { ScriptTarget } from 'typescript';
import type { Configuration, WebpackOptionsNormalized } from 'webpack';
import {
AssetPatternClass,
Expand Down Expand Up @@ -298,3 +299,23 @@ export function getStatsOptions(verbose = false): WebpackStatsOptions {
? { ...webpackOutputOptions, ...verboseWebpackOutputOptions }
: webpackOutputOptions;
}

export function getMainFieldsAndConditionNames(
target: ScriptTarget,
platformServer: boolean,
): Pick<WebpackOptionsNormalized['resolve'], 'mainFields' | 'conditionNames'> {
const mainFields = platformServer
? ['es2015', 'module', 'main']
: ['es2015', 'browser', 'module', 'main'];
const conditionNames = ['es2015', '...'];

if (target >= ScriptTarget.ES2020) {
mainFields.unshift('es2020');
conditionNames.unshift('es2020');
}

return {
mainFields,
conditionNames,
};
}