Skip to content

Commit 528b7f1

Browse files
alan-agius4clydin
authored andcommitted
refactor(@angular-devkit/build-angular): extract webpack configurations from browser builder
1 parent f90a832 commit 528b7f1

File tree

5 files changed

+68
-57
lines changed

5 files changed

+68
-57
lines changed

packages/angular_devkit/build_angular/src/browser/index.ts

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import {
2525
urlJoin,
2626
} from '../utils';
2727
import { BundleActionExecutor } from '../utils/action-executor';
28-
import { WebpackConfigOptions } from '../utils/build-options';
2928
import { ThresholdSeverity, checkBudgets } from '../utils/bundle-calculator';
3029
import { findCachePath } from '../utils/cache-path';
3130
import { colors } from '../utils/color';
@@ -53,14 +52,14 @@ import {
5352
getIndexOutputFile,
5453
} from '../utils/webpack-browser-config';
5554
import {
55+
getAnalyticsConfig,
5656
getBrowserConfig,
5757
getCommonConfig,
5858
getStatsConfig,
5959
getStylesConfig,
6060
getTypeScriptConfig,
6161
getWorkerConfig,
6262
} from '../webpack/configs';
63-
import { NgBuildAnalyticsPlugin } from '../webpack/plugins/analytics';
6463
import { markAsyncChunksNonInitial } from '../webpack/utils/async-chunks';
6564
import { normalizeExtraEntryPoints } from '../webpack/utils/helpers';
6665
import {
@@ -90,43 +89,6 @@ export type BrowserBuilderOutput = json.JsonObject &
9089
outputPath: string;
9190
};
9291

93-
export function getAnalyticsConfig(
94-
wco: WebpackConfigOptions,
95-
context: BuilderContext,
96-
): webpack.Configuration {
97-
if (context.analytics) {
98-
// If there's analytics, add our plugin. Otherwise no need to slow down the build.
99-
let category = 'build';
100-
if (context.builder) {
101-
// We already vetted that this is a "safe" package, otherwise the analytics would be noop.
102-
category =
103-
context.builder.builderName.split(':')[1] || context.builder.builderName || 'build';
104-
}
105-
106-
// The category is the builder name if it's an angular builder.
107-
return {
108-
plugins: [
109-
new NgBuildAnalyticsPlugin(
110-
wco.projectRoot,
111-
context.analytics,
112-
category,
113-
!!wco.tsConfig.options.enableIvy,
114-
),
115-
],
116-
};
117-
}
118-
119-
return {};
120-
}
121-
122-
export function getCompilerConfig(wco: WebpackConfigOptions): webpack.Configuration {
123-
if (wco.buildOptions.main || wco.buildOptions.polyfills) {
124-
return getTypeScriptConfig(wco);
125-
}
126-
127-
return {};
128-
}
129-
13092
async function initialize(
13193
options: BrowserBuilderSchema,
13294
context: BuilderContext,
@@ -153,7 +115,7 @@ async function initialize(
153115
getStylesConfig(wco),
154116
getStatsConfig(wco),
155117
getAnalyticsConfig(wco, context),
156-
getCompilerConfig(wco),
118+
getTypeScriptConfig(wco),
157119
wco.buildOptions.webWorkerTsConfig ? getWorkerConfig(wco) : {},
158120
],
159121
{ differentialLoadingNeeded },

packages/angular_devkit/build_angular/src/dev-server/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import * as ts from 'typescript';
2020
import * as url from 'url';
2121
import * as webpack from 'webpack';
2222
import * as webpackDevServer from 'webpack-dev-server';
23-
import { getAnalyticsConfig, getCompilerConfig } from '../browser';
2423
import { Schema as BrowserBuilderSchema, OutputHashing } from '../browser/schema';
2524
import { ExecutionTransformer } from '../transforms';
2625
import { BuildBrowserFeatures, normalizeOptimization } from '../utils';
@@ -38,11 +37,13 @@ import {
3837
getIndexOutputFile,
3938
} from '../utils/webpack-browser-config';
4039
import {
40+
getAnalyticsConfig,
4141
getBrowserConfig,
4242
getCommonConfig,
4343
getDevServerConfig,
4444
getStatsConfig,
4545
getStylesConfig,
46+
getTypeScriptConfig,
4647
getWorkerConfig,
4748
} from '../webpack/configs';
4849
import { IndexHtmlWebpackPlugin } from '../webpack/plugins/index-html-webpack-plugin';
@@ -219,7 +220,7 @@ export function serveWebpackBrowser(
219220
getStylesConfig(wco),
220221
getStatsConfig(wco),
221222
getAnalyticsConfig(wco, context),
222-
getCompilerConfig(wco),
223+
getTypeScriptConfig(wco),
223224
browserOptions.webWorkerTsConfig ? getWorkerConfig(wco) : {},
224225
],
225226
devServerOptions,
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import { BuilderContext } from '@angular-devkit/architect';
10+
import { Configuration } from 'webpack';
11+
import { WebpackConfigOptions } from '../../utils/build-options';
12+
import { NgBuildAnalyticsPlugin } from '../plugins/analytics';
13+
14+
export function getAnalyticsConfig(
15+
wco: WebpackConfigOptions,
16+
context: BuilderContext,
17+
): Configuration {
18+
if (!context.analytics) {
19+
return {};
20+
}
21+
22+
// If there's analytics, add our plugin. Otherwise no need to slow down the build.
23+
let category = 'build';
24+
if (context.builder) {
25+
// We already vetted that this is a "safe" package, otherwise the analytics would be noop.
26+
category = context.builder.builderName.split(':')[1] || context.builder.builderName || 'build';
27+
}
28+
29+
// The category is the builder name if it's an angular builder.
30+
return {
31+
plugins: [
32+
new NgBuildAnalyticsPlugin(
33+
wco.projectRoot,
34+
context.analytics,
35+
category,
36+
!!wco.tsConfig.options.enableIvy,
37+
),
38+
],
39+
};
40+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
export * from './analytics';
910
export * from './browser';
1011
export * from './common';
1112
export * from './dev-server';

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

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import { getSystemPath } from '@angular-devkit/core';
1010
import { CompilerOptions } from '@angular/compiler-cli';
1111
import { AngularWebpackLoaderPath, AngularWebpackPlugin } from '@ngtools/webpack';
12+
import { Configuration } from 'webpack';
1213
import { WebpackConfigOptions } from '../../utils/build-options';
1314

1415
function ensureIvy(wco: WebpackConfigOptions): void {
@@ -78,23 +79,29 @@ function createIvyPlugin(
7879
});
7980
}
8081

81-
export function getTypeScriptConfig(wco: WebpackConfigOptions) {
82-
const { buildOptions, tsConfigPath } = wco;
83-
const aot = !!buildOptions.aot;
82+
export function getTypeScriptConfig(wco: WebpackConfigOptions): Configuration {
83+
const {
84+
buildOptions: { aot = false, main, polyfills },
85+
tsConfigPath,
86+
} = wco;
8487

85-
ensureIvy(wco);
88+
if (main || polyfills) {
89+
ensureIvy(wco);
8690

87-
return {
88-
module: {
89-
rules: [
90-
{
91-
test: /\.[jt]sx?$/,
92-
loader: AngularWebpackLoaderPath,
93-
},
94-
],
95-
},
96-
plugins: [createIvyPlugin(wco, aot, tsConfigPath)],
97-
};
91+
return {
92+
module: {
93+
rules: [
94+
{
95+
test: /\.[jt]sx?$/,
96+
loader: AngularWebpackLoaderPath,
97+
},
98+
],
99+
},
100+
plugins: [createIvyPlugin(wco, aot, tsConfigPath)],
101+
};
102+
}
103+
104+
return {};
98105
}
99106

100107
export function getTypescriptWorkerPlugin(wco: WebpackConfigOptions, workerTsConfigPath: string) {

0 commit comments

Comments
 (0)