Skip to content

Commit 1269190

Browse files
committed
refactor(@angular-devkit/build-angular): lazy load several optional webpack plugins
Multiple Webpack plugins are only used when certain options are enabled. By only requiring them when needed, startup time can be reduced by potentially eliminating large dependency hierarchies from being loaded that will then be unused. This change currently only applies to plugins that are required. This limitation is due to the current webpack configuration infrastructure being synchronous which prevents dynamic import usage.
1 parent de253fd commit 1269190

File tree

4 files changed

+13
-18
lines changed

4 files changed

+13
-18
lines changed

packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/browser.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ import { CommonJsUsageWarnPlugin } from '../../plugins/webpack';
1010
import { WebpackConfigOptions } from '../build-options';
1111
import { getSourceMapDevTool, isPolyfillsEntry, normalizeExtraEntryPoints } from './utils';
1212

13-
const SubresourceIntegrityPlugin = require('webpack-subresource-integrity');
14-
const LicenseWebpackPlugin = require('license-webpack-plugin').LicenseWebpackPlugin;
15-
16-
1713
export function getBrowserConfig(wco: WebpackConfigOptions): webpack.Configuration {
1814
const { buildOptions } = wco;
1915
const {
@@ -35,12 +31,14 @@ export function getBrowserConfig(wco: WebpackConfigOptions): webpack.Configurati
3531
} = buildOptions.sourceMap;
3632

3733
if (subresourceIntegrity) {
34+
const SubresourceIntegrityPlugin = require('webpack-subresource-integrity');
3835
extraPlugins.push(new SubresourceIntegrityPlugin({
3936
hashFuncNames: ['sha384'],
4037
}));
4138
}
4239

4340
if (extractLicenses) {
41+
const LicenseWebpackPlugin = require('license-webpack-plugin').LicenseWebpackPlugin;
4442
extraPlugins.push(new LicenseWebpackPlugin({
4543
stats: {
4644
warnings: false,

packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/common.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ import { findAllNodeModules } from '../../utilities/find-up';
4747
import { WebpackConfigOptions } from '../build-options';
4848
import { getEsVersionForFileName, getOutputHashFormat, normalizeExtraEntryPoints } from './utils';
4949

50-
const ProgressPlugin = require('webpack/lib/ProgressPlugin');
51-
const CircularDependencyPlugin = require('circular-dependency-plugin');
5250
const TerserPlugin = require('terser-webpack-plugin');
5351
const PnpWebpackPlugin = require('pnp-webpack-plugin');
5452

@@ -297,10 +295,12 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
297295
}
298296

299297
if (buildOptions.progress) {
298+
const ProgressPlugin = require('webpack/lib/ProgressPlugin');
300299
extraPlugins.push(new ProgressPlugin({ profile: buildOptions.verbose }));
301300
}
302301

303302
if (buildOptions.showCircularDependencies) {
303+
const CircularDependencyPlugin = require('circular-dependency-plugin');
304304
extraPlugins.push(
305305
new CircularDependencyPlugin({
306306
exclude: /([\\\/]node_modules[\\\/])|(ngfactory\.js$)/,

packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/worker.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ import { Configuration } from 'webpack';
1010
import { WebpackConfigOptions } from '../build-options';
1111
import { getTypescriptWorkerPlugin } from './typescript';
1212

13-
const WorkerPlugin = require('worker-plugin');
14-
15-
1613
export function getWorkerConfig(wco: WebpackConfigOptions): Configuration {
1714
const { buildOptions } = wco;
1815

@@ -25,6 +22,7 @@ export function getWorkerConfig(wco: WebpackConfigOptions): Configuration {
2522
}
2623

2724
const workerTsConfigPath = resolve(wco.root, buildOptions.webWorkerTsConfig);
25+
const WorkerPlugin = require('worker-plugin');
2826

2927
return {
3028
plugins: [new WorkerPlugin({

packages/angular_devkit/build_angular/src/angular-cli-files/plugins/named-chunks-plugin.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { Compiler } from 'webpack';
9-
// Webpack doesn't export these so the deep imports can potentially break.
10-
// There doesn't seem to exist any ergonomic way to alter chunk names for non-context lazy chunks
11-
// (https://github.com/webpack/webpack/issues/9075) so this is the best alternative for now.
12-
const ImportDependency = require('webpack/lib/dependencies/ImportDependency');
13-
const ImportDependenciesBlock = require('webpack/lib/dependencies/ImportDependenciesBlock');
14-
const Template = require('webpack/lib/Template');
15-
168
export class NamedLazyChunksPlugin {
179
constructor() { }
18-
apply(compiler: Compiler): void {
10+
apply(compiler: import('webpack').Compiler): void {
11+
// Webpack doesn't export these so the deep imports can potentially break.
12+
// There doesn't seem to exist any ergonomic way to alter chunk names for non-context lazy chunks
13+
// (https://github.com/webpack/webpack/issues/9075) so this is the best alternative for now.
14+
const ImportDependency = require('webpack/lib/dependencies/ImportDependency');
15+
const ImportDependenciesBlock = require('webpack/lib/dependencies/ImportDependenciesBlock');
16+
const Template = require('webpack/lib/Template');
17+
1918
compiler.hooks.compilation.tap('named-lazy-chunks-plugin', compilation => {
2019
// The dependencyReference hook isn't in the webpack typings so we have to type it as any.
2120
// tslint:disable-next-line: no-any

0 commit comments

Comments
 (0)