Skip to content

Commit 378faa7

Browse files
clydinalan-agius4
authored andcommitted
refactor(@angular-devkit/build-angular): cleanup webpack type usage
1 parent 78e8d3c commit 378faa7

File tree

8 files changed

+70
-63
lines changed

8 files changed

+70
-63
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export function getBrowserConfig(wco: WebpackConfigOptions): webpack.Configurati
6262
const globalStylesBundleNames = normalizeExtraEntryPoints(styles, 'styles')
6363
.map(style => style.bundleName);
6464

65-
let crossOriginLoading: string | false = false;
65+
let crossOriginLoading: 'anonymous' | 'use-credentials' | false = false;
6666
if (subresourceIntegrity && crossOrigin === 'none') {
6767
crossOriginLoading = 'anonymous';
6868
} else if (crossOrigin !== 'none') {

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

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@ import * as path from 'path';
1616
import { RollupOptions } from 'rollup';
1717
import { ScriptTarget } from 'typescript';
1818
import {
19-
ChunkData,
2019
Compiler,
2120
Configuration,
2221
ContextReplacementPlugin,
23-
HashedModuleIdsPlugin,
24-
Plugin,
25-
Rule,
2622
RuleSetLoader,
23+
RuleSetRule,
24+
compilation,
2725
debug,
2826
} from 'webpack';
2927
import { RawSource } from 'webpack-sources';
@@ -64,9 +62,9 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
6462
vendor: vendorSourceMap,
6563
} = buildOptions.sourceMap;
6664

67-
const extraPlugins: Plugin[] = [];
68-
const extraRules: Rule[] = [];
69-
const entryPoints: { [key: string]: string[] } = {};
65+
const extraPlugins: { apply(compiler: Compiler): void }[] = [];
66+
const extraRules: RuleSetRule[] = [];
67+
const entryPoints: { [key: string]: [string, ...string[]] } = {};
7068

7169
// determine hashing format
7270
const hashFormat = getOutputHashFormat(buildOptions.outputHashing || 'none');
@@ -150,7 +148,10 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
150148
// tslint:disable-next-line: no-any
151149
(compilation.mainTemplate.hooks as any).assetPath.tap(
152150
'build-angular',
153-
(filename: string | ((data: ChunkData) => string), data: ChunkData) => {
151+
(
152+
filename: string | ((data: { chunk: compilation.Chunk }) => string),
153+
data: { chunk: compilation.Chunk },
154+
) => {
154155
const assetName = typeof filename === 'function' ? filename(data) : filename;
155156
const isMap = assetName && assetName.endsWith('.map');
156157

@@ -177,17 +178,21 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
177178
}
178179

179180
if (buildOptions.polyfills) {
180-
entryPoints['polyfills'] = [
181-
...(entryPoints['polyfills'] || []),
182-
path.resolve(root, buildOptions.polyfills),
183-
];
181+
const projectPolyfills = path.resolve(root, buildOptions.polyfills);
182+
if (entryPoints['polyfills']) {
183+
entryPoints['polyfills'].push(projectPolyfills);
184+
} else {
185+
entryPoints['polyfills'] = [projectPolyfills];
186+
}
184187
}
185188

186189
if (!buildOptions.aot) {
187-
entryPoints['polyfills'] = [
188-
...(entryPoints['polyfills'] || []),
189-
path.join(__dirname, '..', 'jit-polyfills.js'),
190-
];
190+
const jitPolyfills = path.join(__dirname, '..', 'jit-polyfills.js');
191+
if (entryPoints['polyfills']) {
192+
entryPoints['polyfills'].push(jitPolyfills);
193+
} else {
194+
entryPoints['polyfills'] = [jitPolyfills];
195+
}
191196
}
192197
}
193198

@@ -592,8 +597,9 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
592597
],
593598
},
594599
optimization: {
600+
minimizer: extraMinimizers,
601+
moduleIds: 'hashed',
595602
noEmitOnErrors: true,
596-
minimizer: [new HashedModuleIdsPlugin(), ...extraMinimizers],
597603
},
598604
plugins: [
599605
// Always replace the context for the System.import in angular/core to prevent warnings.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ const postcssImports = require('postcss-import');
2424
// tslint:disable-next-line:no-big-function
2525
export function getStylesConfig(wco: WebpackConfigOptions) {
2626
const { root, buildOptions } = wco;
27-
const entryPoints: { [key: string]: string[] } = {};
27+
const entryPoints: { [key: string]: [string, ...string[]] } = {};
2828
const globalStylePaths: string[] = [];
29-
const extraPlugins: webpack.Plugin[] = [
30-
new AnyComponentStyleBudgetChecker(buildOptions.budgets),
31-
];
29+
const extraPlugins: { apply(compiler: webpack.Compiler): void }[] = [];
30+
31+
extraPlugins.push(new AnyComponentStyleBudgetChecker(buildOptions.budgets));
3232

3333
const cssSourceMap = buildOptions.sourceMap.styles;
3434

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ export function getTestConfig(
1717
): webpack.Configuration {
1818
const { root, buildOptions, sourceRoot: include } = wco;
1919

20-
const extraRules: webpack.Rule[] = [];
21-
const extraPlugins: webpack.Plugin[] = [];
20+
const extraRules: webpack.RuleSetRule[] = [];
21+
const extraPlugins: { apply(compiler: webpack.Compiler): void }[] = [];
2222

2323
if (buildOptions.codeCoverage) {
2424
const codeCoverageExclude = buildOptions.codeCoverageExclude;

packages/angular_devkit/build_angular/src/angular-cli-files/plugins/any-component-style-budget-checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import * as path from 'path';
10-
import { Compiler, Plugin } from 'webpack';
10+
import { Compiler } from 'webpack';
1111
import { Budget, Type } from '../../../src/browser/schema';
1212
import { ThresholdSeverity, calculateThresholds, checkThresholds } from '../utilities/bundle-calculator';
1313

@@ -17,7 +17,7 @@ const PLUGIN_NAME = 'AnyComponentStyleBudgetChecker';
1717
* Check budget sizes for component styles by emitting a warning or error if a
1818
* budget is exceeded by a particular component's styles.
1919
*/
20-
export class AnyComponentStyleBudgetChecker implements Plugin {
20+
export class AnyComponentStyleBudgetChecker {
2121
private readonly budgets: Budget[];
2222
constructor(budgets: Budget[]) {
2323
this.budgets = budgets.filter((budget) => budget.type === Type.AnyComponentStyle);

packages/angular_devkit/build_angular/src/angular-cli-files/plugins/bundle-budget.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
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, compilation } from 'webpack';
8+
import { Compiler } from 'webpack';
99
import { Budget } from '../../browser/schema';
1010
import { ProcessBundleResult } from '../../utils/process-bundle';
1111
import { ThresholdSeverity, checkBudgets } from '../utilities/bundle-calculator';
@@ -24,26 +24,22 @@ export class BundleBudgetPlugin {
2424
return;
2525
}
2626

27-
compiler.hooks.afterEmit.tap('BundleBudgetPlugin', (compilation: compilation.Compilation) => {
28-
this.runChecks(budgets, compilation);
29-
});
30-
}
31-
32-
private runChecks(budgets: Budget[], compilation: compilation.Compilation) {
33-
// No process bundle results because this plugin is only used when differential
34-
// builds are disabled.
35-
const processResults: ProcessBundleResult[] = [];
27+
compiler.hooks.afterEmit.tap('BundleBudgetPlugin', (compilation) => {
28+
// No process bundle results because this plugin is only used when differential
29+
// builds are disabled.
30+
const processResults: ProcessBundleResult[] = [];
3631

37-
const stats = compilation.getStats().toJson();
38-
for (const { severity, message } of checkBudgets(budgets, stats, processResults)) {
39-
switch (severity) {
40-
case ThresholdSeverity.Warning:
41-
compilation.warnings.push(`budgets: ${message}`);
42-
break;
43-
case ThresholdSeverity.Error:
44-
compilation.errors.push(`budgets: ${message}`);
45-
break;
32+
const stats = compilation.getStats().toJson();
33+
for (const { severity, message } of checkBudgets(budgets, stats, processResults)) {
34+
switch (severity) {
35+
case ThresholdSeverity.Warning:
36+
compilation.warnings.push(`budgets: ${message}`);
37+
break;
38+
case ThresholdSeverity.Error:
39+
compilation.errors.push(`budgets: ${message}`);
40+
break;
41+
}
4642
}
47-
}
43+
});
4844
}
4945
}

packages/angular_devkit/build_angular/src/angular-cli-files/plugins/index-html-webpack-plugin.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ export class IndexHtmlWebpackPlugin {
8888
}
8989
}
9090

91-
const loadOutputFile = (name: string) => compilation.assets[name].source();
91+
const loadOutputFile = async (name: string) => {
92+
const data = compilation.assets[name].source();
93+
94+
return typeof data === 'string' ? data : data.toString();
95+
};
9296
let indexSource = await augmentIndexHtml({
9397
input: this._options.input,
9498
inputContent,

packages/angular_devkit/build_angular/src/angular-cli-files/plugins/optimize-css-webpack-plugin.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,14 @@ function hook(
1919
compiler: Compiler,
2020
action: (
2121
compilation: compilation.Compilation,
22-
chunks: compilation.Chunk[],
23-
) => Promise<void | void[]>,
22+
chunks: Iterable<compilation.Chunk>,
23+
) => Promise<void>,
2424
) {
25-
compiler.hooks.compilation.tap(
26-
'optimize-css-webpack-plugin',
27-
(compilation: compilation.Compilation) => {
28-
compilation.hooks.optimizeChunkAssets.tapPromise('optimize-css-webpack-plugin', chunks =>
29-
action(compilation, chunks),
30-
);
31-
},
32-
);
25+
compiler.hooks.compilation.tap('optimize-css-webpack-plugin', (compilation) => {
26+
compilation.hooks.optimizeChunkAssets.tapPromise('optimize-css-webpack-plugin', (chunks) =>
27+
action(compilation, chunks),
28+
);
29+
});
3330
}
3431

3532
export class OptimizeCssWebpackPlugin {
@@ -44,14 +41,18 @@ export class OptimizeCssWebpackPlugin {
4441
}
4542

4643
apply(compiler: Compiler): void {
47-
hook(compiler, (compilation: compilation.Compilation, chunks: compilation.Chunk[]) => {
44+
hook(compiler, (compilation: compilation.Compilation, chunks: Iterable<compilation.Chunk>) => {
4845
const files: string[] = [...compilation.additionalChunkAssets];
4946

50-
chunks.forEach(chunk => {
51-
if (chunk.files && chunk.files.length > 0) {
52-
files.push(...chunk.files);
47+
for (const chunk of chunks) {
48+
if (!chunk.files) {
49+
continue;
5350
}
54-
});
51+
52+
for (const file of chunk.files) {
53+
files.push(file);
54+
}
55+
}
5556

5657
const actions = files
5758
.filter(file => this._options.test(file))
@@ -120,7 +121,7 @@ export class OptimizeCssWebpackPlugin {
120121
compilation.assets[file] = newSource;
121122
});
122123

123-
return Promise.all(actions);
124+
return Promise.all(actions).then(() => {});
124125
});
125126
}
126127
}

0 commit comments

Comments
 (0)