Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): avoid double build optimizer proc…
Browse files Browse the repository at this point in the history
…essing

TypeScript files had the potential to be processed twice by the build optimizer. This did not affect the output code but could lead to longer production build times. The build optimizer is now configured in one centralized location for both TypeScript and JavaScript files. The Webpack configuration partial for TypeScript support is also reduced to one common function for both AOT and JIT as a result.
  • Loading branch information
clydin authored and alan-agius4 committed Apr 19, 2021
1 parent 71717cf commit e559236
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 50 deletions.
5 changes: 2 additions & 3 deletions packages/angular_devkit/build_angular/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,11 @@ import {
getIndexOutputFile,
} from '../utils/webpack-browser-config';
import {
getAotConfig,
getBrowserConfig,
getCommonConfig,
getNonAotConfig,
getStatsConfig,
getStylesConfig,
getTypeScriptConfig,
getWorkerConfig,
} from '../webpack/configs';
import { NgBuildAnalyticsPlugin } from '../webpack/plugins/analytics';
Expand Down Expand Up @@ -121,7 +120,7 @@ export function getAnalyticsConfig(

export function getCompilerConfig(wco: WebpackConfigOptions): webpack.Configuration {
if (wco.buildOptions.main || wco.buildOptions.polyfills) {
return wco.buildOptions.aot ? getAotConfig(wco) : getNonAotConfig(wco);
return getTypeScriptConfig(wco);
}

return {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ import { ExecutionTransformer } from '../transforms';
import { createI18nOptions } from '../utils/i18n-options';
import { assertCompatibleAngularVersion } from '../utils/version';
import { generateBrowserWebpackConfigFromContext } from '../utils/webpack-browser-config';
import { getAotConfig, getBrowserConfig, getCommonConfig, getStatsConfig } from '../webpack/configs';
import {
getBrowserConfig,
getCommonConfig,
getStatsConfig,
getTypeScriptConfig,
} from '../webpack/configs';
import { createWebpackLoggingCallback } from '../webpack/utils/stats';
import { Format, Schema } from './schema';

Expand Down Expand Up @@ -194,7 +199,7 @@ export async function execute(
{ plugins: [new NoEmitPlugin()] },
getCommonConfig(wco),
getBrowserConfig(wco),
getAotConfig(wco),
getTypeScriptConfig(wco),
getStatsConfig(wco),
];

Expand Down
4 changes: 2 additions & 2 deletions packages/angular_devkit/build_angular/src/karma/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import { assertCompatibleAngularVersion } from '../utils/version';
import { generateBrowserWebpackConfigFromContext } from '../utils/webpack-browser-config';
import {
getCommonConfig,
getNonAotConfig,
getStylesConfig,
getTestConfig,
getTypeScriptConfig,
getWorkerConfig,
} from '../webpack/configs';
import { SingleTestTransformLoader } from '../webpack/plugins/single-test-transform';
Expand All @@ -46,7 +46,7 @@ async function initialize(
wco => [
getCommonConfig(wco),
getStylesConfig(wco),
getNonAotConfig(wco),
getTypeScriptConfig(wco),
getTestConfig(wco),
getWorkerConfig(wco),
],
Expand Down
4 changes: 2 additions & 2 deletions packages/angular_devkit/build_angular/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ import { readTsconfig } from '../utils/read-tsconfig';
import { assertCompatibleAngularVersion } from '../utils/version';
import { generateI18nBrowserWebpackConfigFromContext } from '../utils/webpack-browser-config';
import {
getAotConfig,
getCommonConfig,
getServerConfig,
getStatsConfig,
getStylesConfig,
getTypeScriptConfig,
} from '../webpack/configs';
import { JsonCompilationStats, webpackStatsLogger } from '../webpack/utils/stats';
import { Schema as ServerBuilderOptions } from './schema';
Expand Down Expand Up @@ -166,7 +166,7 @@ async function initialize(
getServerConfig(wco),
getStylesConfig(wco),
getStatsConfig(wco),
getAotConfig(wco),
getTypeScriptConfig(wco),
],
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { buildOptimizerLoaderPath } from '@angular-devkit/build-optimizer';
import { getSystemPath } from '@angular-devkit/core';
import { CompilerOptions } from '@angular/compiler-cli';
import { AngularWebpackLoaderPath, AngularWebpackPlugin } from '@ngtools/webpack';
Expand Down Expand Up @@ -78,55 +77,23 @@ function createIvyPlugin(
});
}

export function getNonAotConfig(wco: WebpackConfigOptions) {
const { tsConfigPath } = wco;

return {
module: {
rules: [
{
test: /\.[jt]sx?$/,
loader: AngularWebpackLoaderPath,
},
],
},
plugins: [
createIvyPlugin(wco, false, tsConfigPath),
],
};
}

export function getAotConfig(wco: WebpackConfigOptions) {
const { tsConfigPath, buildOptions } = wco;
export function getTypeScriptConfig(wco: WebpackConfigOptions) {
const { buildOptions, tsConfigPath } = wco;
const aot = !!buildOptions.aot;

ensureIvy(wco);

return {
module: {
rules: [
{
test: /\.tsx?$/,
use: [
...(buildOptions.buildOptimizer
? [
{
loader: buildOptimizerLoaderPath,
options: { sourceMap: buildOptions.sourceMap.scripts },
},
]
: []),
AngularWebpackLoaderPath,
],
},
// "allowJs" support with ivy plugin - ensures build optimizer is not run twice
{
test: /\.jsx?$/,
use: [AngularWebpackLoaderPath],
test: /\.[jt]sx?$/,
loader: AngularWebpackLoaderPath,
},
],
},
plugins: [
createIvyPlugin(wco, true, tsConfigPath),
createIvyPlugin(wco, aot, tsConfigPath),
],
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default async function () {
'--output-path=dist/second',
);

const chunkId = '730';
const chunkId = '265';
const codeHashES5 = createHash('sha384')
.update(await readFile(`dist/first/${chunkId}-es5.js`))
.digest('base64');
Expand Down
2 changes: 1 addition & 1 deletion tests/legacy-cli/e2e/tests/build/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default async function () {
await expectFileToMatch('dist/test-project/main-es2017.js', 'src_app_app_worker_ts');

await ng('build', '--output-hashing=none');
const chunkId = '137';
const chunkId = '954';
await expectFileToExist(`dist/test-project/${chunkId}-es5.js`);
await expectFileToMatch('dist/test-project/main-es5.js', chunkId);
await expectFileToExist(`dist/test-project/${chunkId}-es2017.js`);
Expand Down

0 comments on commit e559236

Please sign in to comment.