Skip to content

Commit

Permalink
refactor(@angular-devkit/build-angular): cleanup Webpack rule generation
Browse files Browse the repository at this point in the history
This change reduces the number of variables needed as well as reduces type casting.
  • Loading branch information
clydin committed Aug 4, 2020
1 parent 520459e commit 1a63293
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -327,15 +327,13 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
extraPlugins.push(new BundleBudgetPlugin({ budgets: buildOptions.budgets }));
}

let sourceMapUseRule;
if ((scriptsSourceMap || stylesSourceMap) && vendorSourceMap) {
sourceMapUseRule = {
use: [
{
loader: require.resolve('source-map-loader'),
},
],
};
extraRules.push({
test: /\.m?js$/,
exclude: /(ngfactory|ngstyle)\.js$/,
enforce: 'pre',
loader: require.resolve('source-map-loader'),
});
}

let buildOptimizerUseRule: RuleSetLoader[] = [];
Expand Down Expand Up @@ -583,12 +581,6 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
...buildOptimizerUseRule,
],
},
{
test: /\.m?js$/,
exclude: /(ngfactory|ngstyle)\.js$/,
enforce: 'pre',
...sourceMapUseRule,
},
...extraRules,
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
}

// set base rules to derive final rules from
const baseRules: webpack.RuleSetRule[] = [
const baseRules: { test: RegExp, use: webpack.RuleSetLoader[] }[] = [
{ test: /\.css$/, use: [] },
{
test: /\.scss$|\.sass$/,
Expand Down Expand Up @@ -206,7 +206,7 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
&& !buildOptions.sourceMap.hidden ? 'inline' : false,
},
},
...(use as webpack.Loader[]),
...use,
],
}));

Expand Down Expand Up @@ -237,7 +237,7 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
: cssSourceMap,
},
},
...(use as webpack.Loader[]),
...use,
],
};
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,27 @@ import { getSourceMapDevTool, isPolyfillsEntry } from './utils';
export function getTestConfig(
wco: WebpackConfigOptions<WebpackTestOptions>,
): webpack.Configuration {
const { root, buildOptions, sourceRoot: include } = wco;
const {
buildOptions: { codeCoverage, codeCoverageExclude, main, sourceMap },
root,
sourceRoot,
} = wco;

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

if (buildOptions.codeCoverage) {
const codeCoverageExclude = buildOptions.codeCoverageExclude;
if (codeCoverage) {
const exclude: (string | RegExp)[] = [
/\.(e2e|spec)\.tsx?$/,
/node_modules/,
];

if (codeCoverageExclude) {
codeCoverageExclude.forEach((excludeGlob: string) => {
const excludeFiles = glob
for (const excludeGlob of codeCoverageExclude) {
glob
.sync(path.join(root, excludeGlob), { nodir: true })
.map(file => path.normalize(file));
exclude.push(...excludeFiles);
});
.forEach((file) => exclude.push(path.normalize(file)));
}
}

extraRules.push({
Expand All @@ -42,31 +44,27 @@ export function getTestConfig(
options: { esModules: true },
enforce: 'post',
exclude,
include,
include: sourceRoot,
});
}

if (wco.buildOptions.sourceMap) {
const { styles, scripts } = wco.buildOptions.sourceMap;

if (styles || scripts) {
extraPlugins.push(getSourceMapDevTool(
scripts,
styles,
false,
true,
));
}
if (sourceMap.scripts || sourceMap.styles) {
extraPlugins.push(getSourceMapDevTool(
sourceMap.scripts,
sourceMap.styles,
false,
true,
));
}

return {
mode: 'development',
resolve: {
mainFields: ['es2015', 'browser', 'module', 'main'],
},
devtool: buildOptions.sourceMap ? false : 'eval',
devtool: false,
entry: {
main: path.resolve(root, buildOptions.main),
main: path.resolve(root, main),
},
module: {
rules: extraRules,
Expand Down
20 changes: 6 additions & 14 deletions packages/angular_devkit/build_angular/src/dev-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,16 +306,6 @@ async function setupLocalize(
}
}

// Get the insertion point for the i18n babel loader rule
// This is currently dependent on the rule order/construction in common.ts
// A future refactor of the webpack configuration definition will improve this situation
// tslint:disable-next-line: no-non-null-assertion
const rules = webpackConfig.module!.rules;
const index = rules.findIndex(r => r.enforce === 'pre');
if (index === -1) {
throw new Error('Invalid internal webpack configuration');
}

const i18nRule: webpack.Rule = {
test: /\.(?:m?js|ts)$/,
enforce: 'post',
Expand All @@ -334,15 +324,17 @@ async function setupLocalize(
translationIntegrity: localeDescription && localeDescription.integrity,
}),
plugins,
parserOpts: {
plugins: ['dynamicImport'],
},
},
},
],
};

rules.splice(index, 0, i18nRule);
if (!webpackConfig.module) {
webpackConfig.module = { rules: [] };
} else if (!webpackConfig.module.rules) {
webpackConfig.module.rules = [];
}
webpackConfig.module.rules.push(i18nRule);

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

0 comments on commit 1a63293

Please sign in to comment.