Skip to content

Commit 2f6b810

Browse files
committed
refactor(@angular-devkit/build-angular): cleanup Webpack rule generation
This change reduces the number of variables needed as well as reduces type casting.
1 parent f5a2e41 commit 2f6b810

File tree

5 files changed

+46
-66
lines changed

5 files changed

+46
-66
lines changed

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

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -327,15 +327,13 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
327327
extraPlugins.push(new BundleBudgetPlugin({ budgets: buildOptions.budgets }));
328328
}
329329

330-
let sourceMapUseRule;
331330
if ((scriptsSourceMap || stylesSourceMap) && vendorSourceMap) {
332-
sourceMapUseRule = {
333-
use: [
334-
{
335-
loader: require.resolve('source-map-loader'),
336-
},
337-
],
338-
};
331+
extraRules.push({
332+
test: /\.m?js$/,
333+
exclude: /(ngfactory|ngstyle)\.js$/,
334+
enforce: 'pre',
335+
loader: require.resolve('source-map-loader'),
336+
});
339337
}
340338

341339
let buildOptimizerUseRule: RuleSetLoader[] = [];
@@ -583,12 +581,6 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
583581
...buildOptimizerUseRule,
584582
],
585583
},
586-
{
587-
test: /\.m?js$/,
588-
exclude: /(ngfactory|ngstyle)\.js$/,
589-
enforce: 'pre',
590-
...sourceMapUseRule,
591-
},
592584
...extraRules,
593585
],
594586
},

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
121121
}
122122

123123
// set base rules to derive final rules from
124-
const baseRules: webpack.RuleSetRule[] = [
124+
const baseRules: { test: RegExp, use: webpack.RuleSetLoader[] }[] = [
125125
{ test: /\.css$/, use: [] },
126126
{
127127
test: /\.scss$|\.sass$/,
@@ -206,7 +206,7 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
206206
&& !buildOptions.sourceMap.hidden ? 'inline' : false,
207207
},
208208
},
209-
...(use as webpack.Loader[]),
209+
...use,
210210
],
211211
}));
212212

@@ -237,7 +237,7 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
237237
: cssSourceMap,
238238
},
239239
},
240-
...(use as webpack.Loader[]),
240+
...use,
241241
],
242242
};
243243
}),

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

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,27 @@ import { getSourceMapDevTool, isPolyfillsEntry } from './utils';
1515
export function getTestConfig(
1616
wco: WebpackConfigOptions<WebpackTestOptions>,
1717
): webpack.Configuration {
18-
const { root, buildOptions, sourceRoot: include } = wco;
18+
const {
19+
buildOptions: { codeCoverage, codeCoverageExclude, main, sourceMap },
20+
root,
21+
sourceRoot,
22+
} = wco;
1923

2024
const extraRules: webpack.RuleSetRule[] = [];
2125
const extraPlugins: { apply(compiler: webpack.Compiler): void }[] = [];
2226

23-
if (buildOptions.codeCoverage) {
24-
const codeCoverageExclude = buildOptions.codeCoverageExclude;
27+
if (codeCoverage) {
2528
const exclude: (string | RegExp)[] = [
2629
/\.(e2e|spec)\.tsx?$/,
2730
/node_modules/,
2831
];
2932

3033
if (codeCoverageExclude) {
31-
codeCoverageExclude.forEach((excludeGlob: string) => {
32-
const excludeFiles = glob
34+
for (const excludeGlob of codeCoverageExclude) {
35+
glob
3336
.sync(path.join(root, excludeGlob), { nodir: true })
34-
.map(file => path.normalize(file));
35-
exclude.push(...excludeFiles);
36-
});
37+
.forEach((file) => exclude.push(path.normalize(file)));
38+
}
3739
}
3840

3941
extraRules.push({
@@ -42,31 +44,27 @@ export function getTestConfig(
4244
options: { esModules: true },
4345
enforce: 'post',
4446
exclude,
45-
include,
47+
include: sourceRoot,
4648
});
4749
}
4850

49-
if (wco.buildOptions.sourceMap) {
50-
const { styles, scripts } = wco.buildOptions.sourceMap;
51-
52-
if (styles || scripts) {
53-
extraPlugins.push(getSourceMapDevTool(
54-
scripts,
55-
styles,
56-
false,
57-
true,
58-
));
59-
}
51+
if (sourceMap.scripts || sourceMap.styles) {
52+
extraPlugins.push(getSourceMapDevTool(
53+
sourceMap.scripts,
54+
sourceMap.styles,
55+
false,
56+
true,
57+
));
6058
}
6159

6260
return {
6361
mode: 'development',
6462
resolve: {
6563
mainFields: ['es2015', 'browser', 'module', 'main'],
6664
},
67-
devtool: buildOptions.sourceMap ? false : 'eval',
65+
devtool: false,
6866
entry: {
69-
main: path.resolve(root, buildOptions.main),
67+
main: path.resolve(root, main),
7068
},
7169
module: {
7270
rules: extraRules,

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

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -306,17 +306,7 @@ async function setupLocalize(
306306
}
307307
}
308308

309-
// Get the insertion point for the i18n babel loader rule
310-
// This is currently dependent on the rule order/construction in common.ts
311-
// A future refactor of the webpack configuration definition will improve this situation
312-
// tslint:disable-next-line: no-non-null-assertion
313-
const rules = webpackConfig.module!.rules;
314-
const index = rules.findIndex(r => r.enforce === 'pre');
315-
if (index === -1) {
316-
throw new Error('Invalid internal webpack configuration');
317-
}
318-
319-
const i18nRule: webpack.Rule = {
309+
const i18nRule: webpack.RuleSetRule = {
320310
test: /\.(?:m?js|ts)$/,
321311
enforce: 'post',
322312
use: [
@@ -334,15 +324,17 @@ async function setupLocalize(
334324
translationIntegrity: localeDescription && localeDescription.integrity,
335325
}),
336326
plugins,
337-
parserOpts: {
338-
plugins: ['dynamicImport'],
339-
},
340327
},
341328
},
342329
],
343330
};
344331

345-
rules.splice(index, 0, i18nRule);
332+
if (!webpackConfig.module) {
333+
webpackConfig.module = { rules: [] };
334+
} else if (!webpackConfig.module.rules) {
335+
webpackConfig.module.rules = [];
336+
}
337+
webpackConfig.module.rules.push(i18nRule);
346338

347339
// Add a plugin to inject the i18n diagnostics
348340
// tslint:disable-next-line: no-non-null-assertion

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ import {
1818
getTestConfig,
1919
getWorkerConfig,
2020
} from '../angular-cli-files/models/webpack-configs';
21-
import {
22-
SingleTestTransformLoader,
23-
SingleTestTransformLoaderOptions,
24-
} from '../angular-cli-files/plugins/single-test-transform';
21+
import { SingleTestTransformLoader } from '../angular-cli-files/plugins/single-test-transform';
2522
import { findTests } from '../angular-cli-files/utilities/find-tests';
2623
import { Schema as BrowserBuilderOptions } from '../browser/schema';
2724
import { ExecutionTransformer } from '../transforms';
@@ -104,12 +101,7 @@ export function execute(
104101
}
105102

106103
// prepend special webpack loader that will transform test.ts
107-
if (
108-
webpackConfig &&
109-
webpackConfig.module &&
110-
options.include &&
111-
options.include.length > 0
112-
) {
104+
if (options.include && options.include.length > 0) {
113105
const mainFilePath = getSystemPath(
114106
join(normalize(context.workspaceRoot), options.main),
115107
);
@@ -123,15 +115,21 @@ export function execute(
123115
return;
124116
}
125117

118+
if (!webpackConfig.module) {
119+
webpackConfig.module = { rules: [] };
120+
} else if (!webpackConfig.module.rules) {
121+
webpackConfig.module.rules = [];
122+
}
123+
126124
webpackConfig.module.rules.unshift({
127-
test: path => path === mainFilePath,
125+
test: mainFilePath,
128126
use: {
129127
// cannot be a simple path as it differs between environments
130128
loader: SingleTestTransformLoader,
131129
options: {
132130
files,
133131
logger: context.logger,
134-
} as SingleTestTransformLoaderOptions,
132+
},
135133
},
136134
});
137135
}

0 commit comments

Comments
 (0)