Skip to content

Commit 9f114ae

Browse files
clydinKeen Yee Liau
authored and
Keen Yee Liau
committed
refactor(@angular-devkit/build-angular): cleanup/fix linting of webpack configs
1 parent e08a6f4 commit 9f114ae

File tree

4 files changed

+51
-71
lines changed

4 files changed

+51
-71
lines changed

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

+15-22
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,19 @@
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-
// tslint:disable
9-
// TODO: cleanup this file, it's copied as is from Angular CLI.
10-
11-
import * as path from 'path';
12-
const SubresourceIntegrityPlugin = require('webpack-subresource-integrity');
138
import { LicenseWebpackPlugin } from 'license-webpack-plugin';
14-
import { generateEntryPoints } from '../../utilities/package-chunk-sort';
9+
import * as path from 'path';
1510
import { IndexHtmlWebpackPlugin } from '../../plugins/index-html-webpack-plugin';
11+
import { generateEntryPoints } from '../../utilities/package-chunk-sort';
1612
import { WebpackConfigOptions } from '../build-options';
1713
import { normalizeExtraEntryPoints } from './utils';
1814

19-
/**
20-
+ * license-webpack-plugin has a peer dependency on webpack-sources, list it in a comment to
21-
+ * let the dependency validator know it is used.
22-
+ *
23-
+ * require('webpack-sources')
24-
+ */
15+
const SubresourceIntegrityPlugin = require('webpack-subresource-integrity');
16+
2517

2618
export function getBrowserConfig(wco: WebpackConfigOptions) {
2719
const { root, buildOptions } = wco;
28-
let extraPlugins: any[] = [];
20+
const extraPlugins = [];
2921

3022
let sourcemaps: string | false = false;
3123
if (buildOptions.sourceMap) {
@@ -52,18 +44,18 @@ export function getBrowserConfig(wco: WebpackConfigOptions) {
5244

5345
if (buildOptions.subresourceIntegrity) {
5446
extraPlugins.push(new SubresourceIntegrityPlugin({
55-
hashFuncNames: ['sha384']
47+
hashFuncNames: ['sha384'],
5648
}));
5749
}
5850

5951
if (buildOptions.extractLicenses) {
6052
extraPlugins.push(new LicenseWebpackPlugin({
6153
stats: {
6254
warnings: false,
63-
errors: false
55+
errors: false,
6456
},
6557
perChunkOutput: false,
66-
outputFilename: `3rdpartylicenses.txt`
58+
outputFilename: `3rdpartylicenses.txt`,
6759
}));
6860
}
6961

@@ -75,11 +67,11 @@ export function getBrowserConfig(wco: WebpackConfigOptions) {
7567
resolve: {
7668
mainFields: [
7769
...(wco.supportES2015 ? ['es2015'] : []),
78-
'browser', 'module', 'main'
79-
]
70+
'browser', 'module', 'main',
71+
],
8072
},
8173
output: {
82-
crossOriginLoading: buildOptions.subresourceIntegrity ? 'anonymous' : false
74+
crossOriginLoading: buildOptions.subresourceIntegrity ? 'anonymous' : false,
8375
},
8476
optimization: {
8577
runtimeChunk: 'single',
@@ -103,15 +95,16 @@ export function getBrowserConfig(wco: WebpackConfigOptions) {
10395
name: 'vendor',
10496
chunks: 'initial',
10597
enforce: true,
106-
test: (module: any, chunks: Array<{ name: string }>) => {
98+
test: (module: { nameForCondition?: Function }, chunks: Array<{ name: string }>) => {
10799
const moduleName = module.nameForCondition ? module.nameForCondition() : '';
100+
108101
return /[\\/]node_modules[\\/]/.test(moduleName)
109102
&& !chunks.some(({ name }) => name === 'polyfills'
110103
|| globalStylesBundleNames.includes(name));
111104
},
112105
},
113-
}
114-
}
106+
},
107+
},
115108
},
116109
plugins: extraPlugins,
117110
node: false,

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

+30-40
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,43 @@
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-
// tslint:disable
9-
// TODO: cleanup this file, it's copied as is from Angular CLI.
10-
8+
import * as CopyWebpackPlugin from 'copy-webpack-plugin';
119
import * as path from 'path';
1210
import { HashedModuleIdsPlugin, debug } from 'webpack';
13-
import * as CopyWebpackPlugin from 'copy-webpack-plugin';
14-
import { getOutputHashFormat } from './utils';
15-
import { isDirectory } from '../../utilities/is-directory';
16-
import { requireProjectModule } from '../../utilities/require-project-module';
17-
import { WebpackConfigOptions } from '../build-options';
11+
import { AssetPatternObject } from '../../../browser/schema';
1812
import { BundleBudgetPlugin } from '../../plugins/bundle-budget';
1913
import { CleanCssWebpackPlugin } from '../../plugins/cleancss-webpack-plugin';
2014
import { ScriptsWebpackPlugin } from '../../plugins/scripts-webpack-plugin';
2115
import { findUp } from '../../utilities/find-up';
22-
import { AssetPatternObject } from '../../../browser/schema';
23-
import { normalizeExtraEntryPoints } from './utils';
16+
import { isDirectory } from '../../utilities/is-directory';
17+
import { requireProjectModule } from '../../utilities/require-project-module';
18+
import { WebpackConfigOptions } from '../build-options';
19+
import { getOutputHashFormat, normalizeExtraEntryPoints } from './utils';
2420

2521
const ProgressPlugin = require('webpack/lib/ProgressPlugin');
2622
const CircularDependencyPlugin = require('circular-dependency-plugin');
2723
const TerserPlugin = require('terser-webpack-plugin');
2824
const StatsPlugin = require('stats-webpack-plugin');
2925

30-
/**
31-
* Enumerate loaders and their dependencies from this file to let the dependency validator
32-
* know they are used.
33-
*
34-
* require('source-map-loader')
35-
* require('raw-loader')
36-
* require('url-loader')
37-
* require('file-loader')
38-
* require('@angular-devkit/build-optimizer')
39-
*/
4026

27+
// tslint:disable-next-line:no-any
4128
const g: any = typeof global !== 'undefined' ? global : {};
4229
export const buildOptimizerLoader: string = g['_DevKitIsLocal']
4330
? require.resolve('@angular-devkit/build-optimizer/src/build-optimizer/webpack-loader')
4431
: '@angular-devkit/build-optimizer/webpack-loader';
4532

33+
// tslint:disable-next-line:no-big-function
4634
export function getCommonConfig(wco: WebpackConfigOptions) {
4735
const { root, projectRoot, buildOptions } = wco;
4836

4937
const nodeModules = findUp('node_modules', projectRoot);
5038
if (!nodeModules) {
51-
throw new Error('Cannot locate node_modules directory.')
39+
throw new Error('Cannot locate node_modules directory.');
5240
}
5341

54-
let extraPlugins: any[] = [];
55-
let entryPoints: { [key: string]: string[] } = {};
42+
// tslint:disable-next-line:no-any
43+
const extraPlugins: any[] = [];
44+
const entryPoints: { [key: string]: string[] } = {};
5645

5746
if (buildOptions.main) {
5847
entryPoints['main'] = [path.resolve(root, buildOptions.main)];
@@ -72,19 +61,19 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
7261
if (buildOptions.profile) {
7362
extraPlugins.push(new debug.ProfilingPlugin({
7463
outputPath: path.resolve(root, 'chrome-profiler-events.json'),
75-
}))
64+
}));
7665
}
7766

7867
// determine hashing format
79-
const hashFormat = getOutputHashFormat(buildOptions.outputHashing as any);
68+
const hashFormat = getOutputHashFormat(buildOptions.outputHashing || 'none');
8069

8170
// process global scripts
8271
if (buildOptions.scripts.length > 0) {
8372
const globalScriptsByBundleName = normalizeExtraEntryPoints(buildOptions.scripts, 'scripts')
8473
.reduce((prev: { bundleName: string, paths: string[], lazy: boolean }[], curr) => {
8574
const bundleName = curr.bundleName;
8675
const resolvedPath = path.resolve(root, curr.input);
87-
let existingEntry = prev.find((el) => el.bundleName === bundleName);
76+
const existingEntry = prev.find((el) => el.bundleName === bundleName);
8877
if (existingEntry) {
8978
if (existingEntry.lazy && !curr.lazy) {
9079
// All entries have to be lazy for the bundle to be lazy.
@@ -97,9 +86,10 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
9786
prev.push({
9887
bundleName,
9988
paths: [resolvedPath],
100-
lazy: curr.lazy
89+
lazy: curr.lazy,
10190
});
10291
}
92+
10393
return prev;
10494
}, []);
10595

@@ -141,8 +131,8 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
141131
ignore: asset.ignore,
142132
from: {
143133
glob: asset.glob,
144-
dot: true
145-
}
134+
dot: true,
135+
},
146136
};
147137
});
148138

@@ -159,7 +149,7 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
159149

160150
if (buildOptions.showCircularDependencies) {
161151
extraPlugins.push(new CircularDependencyPlugin({
162-
exclude: /[\\\/]node_modules[\\\/]/
152+
exclude: /[\\\/]node_modules[\\\/]/,
163153
}));
164154
}
165155

@@ -172,10 +162,10 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
172162
sourceMapUseRule = {
173163
use: [
174164
{
175-
loader: 'source-map-loader'
176-
}
177-
]
178-
}
165+
loader: 'source-map-loader',
166+
},
167+
],
168+
};
179169
}
180170

181171
let buildOptimizerUseRule;
@@ -184,7 +174,7 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
184174
use: [
185175
{
186176
loader: buildOptimizerLoader,
187-
options: { sourceMap: buildOptions.sourceMap }
177+
options: { sourceMap: buildOptions.sourceMap },
188178
},
189179
],
190180
};
@@ -253,10 +243,10 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
253243
wco.tsConfig.options.baseUrl || projectRoot,
254244
'node_modules',
255245
],
256-
alias
246+
alias,
257247
},
258248
resolveLoader: {
259-
modules: loaderNodeModules
249+
modules: loaderNodeModules,
260250
},
261251
context: projectRoot,
262252
entry: entryPoints,
@@ -267,7 +257,7 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
267257
},
268258
watch: buildOptions.watch,
269259
watchOptions: {
270-
poll: buildOptions.poll
260+
poll: buildOptions.poll,
271261
},
272262
performance: {
273263
hints: false,
@@ -280,7 +270,7 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
280270
loader: 'file-loader',
281271
options: {
282272
name: `[name]${hashFormat.file}.[ext]`,
283-
}
273+
},
284274
},
285275
{
286276
// Mark files inside `@angular/core` as using SystemJS style dynamic imports.
@@ -298,7 +288,7 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
298288
enforce: 'pre',
299289
...sourceMapUseRule,
300290
},
301-
]
291+
],
302292
},
303293
optimization: {
304294
noEmitOnErrors: true,

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

-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
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-
// tslint:disable
9-
// TODO: cleanup this file, it's copied as is from Angular CLI.
10-
118
export * from './browser';
129
export * from './common';
1310
export * from './server';

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@
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-
// tslint:disable
9-
// TODO: cleanup this file, it's copied as is from Angular CLI.
10-
8+
import { Configuration } from 'webpack';
119
import { WebpackConfigOptions } from '../build-options';
1210

11+
1312
/**
1413
* Returns a partial specific to creating a bundle for node
1514
* @param wco Options which are include the build options and app config
1615
*/
1716
export function getServerConfig(wco: WebpackConfigOptions) {
1817

19-
const config: any = {
18+
const config: Configuration = {
2019
devtool: wco.buildOptions.sourceMap ? 'source-map' : false,
2120
resolve: {
2221
mainFields: [
@@ -26,14 +25,15 @@ export function getServerConfig(wco: WebpackConfigOptions) {
2625
},
2726
target: 'node',
2827
output: {
29-
libraryTarget: 'commonjs'
28+
libraryTarget: 'commonjs',
3029
},
3130
node: false,
3231
};
3332

3433
if (wco.buildOptions.bundleDependencies == 'none') {
3534
config.externals = [
3635
/^@angular/,
36+
// tslint:disable-next-line:no-any
3737
(_: any, request: any, callback: (error?: any, result?: any) => void) => {
3838
// Absolute & Relative paths are not externals
3939
if (request.match(/^\.{0,2}\//)) {
@@ -54,7 +54,7 @@ export function getServerConfig(wco: WebpackConfigOptions) {
5454
// Node couldn't find it, so it must be user-aliased
5555
callback();
5656
}
57-
}
57+
},
5858
];
5959
}
6060

0 commit comments

Comments
 (0)