Skip to content

Commit

Permalink
refactor(@angular-devkit/build-angular): cleanup/fix linting of webpa…
Browse files Browse the repository at this point in the history
…ck configs
  • Loading branch information
clydin authored and kyliau committed Oct 23, 2018
1 parent e08a6f4 commit 9f114ae
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 71 deletions.
Expand Up @@ -5,27 +5,19 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
// tslint:disable
// TODO: cleanup this file, it's copied as is from Angular CLI.

import * as path from 'path';
const SubresourceIntegrityPlugin = require('webpack-subresource-integrity');
import { LicenseWebpackPlugin } from 'license-webpack-plugin';
import { generateEntryPoints } from '../../utilities/package-chunk-sort';
import * as path from 'path';
import { IndexHtmlWebpackPlugin } from '../../plugins/index-html-webpack-plugin';
import { generateEntryPoints } from '../../utilities/package-chunk-sort';
import { WebpackConfigOptions } from '../build-options';
import { normalizeExtraEntryPoints } from './utils';

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


export function getBrowserConfig(wco: WebpackConfigOptions) {
const { root, buildOptions } = wco;
let extraPlugins: any[] = [];
const extraPlugins = [];

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

if (buildOptions.subresourceIntegrity) {
extraPlugins.push(new SubresourceIntegrityPlugin({
hashFuncNames: ['sha384']
hashFuncNames: ['sha384'],
}));
}

if (buildOptions.extractLicenses) {
extraPlugins.push(new LicenseWebpackPlugin({
stats: {
warnings: false,
errors: false
errors: false,
},
perChunkOutput: false,
outputFilename: `3rdpartylicenses.txt`
outputFilename: `3rdpartylicenses.txt`,
}));
}

Expand All @@ -75,11 +67,11 @@ export function getBrowserConfig(wco: WebpackConfigOptions) {
resolve: {
mainFields: [
...(wco.supportES2015 ? ['es2015'] : []),
'browser', 'module', 'main'
]
'browser', 'module', 'main',
],
},
output: {
crossOriginLoading: buildOptions.subresourceIntegrity ? 'anonymous' : false
crossOriginLoading: buildOptions.subresourceIntegrity ? 'anonymous' : false,
},
optimization: {
runtimeChunk: 'single',
Expand All @@ -103,15 +95,16 @@ export function getBrowserConfig(wco: WebpackConfigOptions) {
name: 'vendor',
chunks: 'initial',
enforce: true,
test: (module: any, chunks: Array<{ name: string }>) => {
test: (module: { nameForCondition?: Function }, chunks: Array<{ name: string }>) => {
const moduleName = module.nameForCondition ? module.nameForCondition() : '';

return /[\\/]node_modules[\\/]/.test(moduleName)
&& !chunks.some(({ name }) => name === 'polyfills'
|| globalStylesBundleNames.includes(name));
},
},
}
}
},
},
},
plugins: extraPlugins,
node: false,
Expand Down
Expand Up @@ -5,54 +5,43 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
// tslint:disable
// TODO: cleanup this file, it's copied as is from Angular CLI.

import * as CopyWebpackPlugin from 'copy-webpack-plugin';
import * as path from 'path';
import { HashedModuleIdsPlugin, debug } from 'webpack';
import * as CopyWebpackPlugin from 'copy-webpack-plugin';
import { getOutputHashFormat } from './utils';
import { isDirectory } from '../../utilities/is-directory';
import { requireProjectModule } from '../../utilities/require-project-module';
import { WebpackConfigOptions } from '../build-options';
import { AssetPatternObject } from '../../../browser/schema';
import { BundleBudgetPlugin } from '../../plugins/bundle-budget';
import { CleanCssWebpackPlugin } from '../../plugins/cleancss-webpack-plugin';
import { ScriptsWebpackPlugin } from '../../plugins/scripts-webpack-plugin';
import { findUp } from '../../utilities/find-up';
import { AssetPatternObject } from '../../../browser/schema';
import { normalizeExtraEntryPoints } from './utils';
import { isDirectory } from '../../utilities/is-directory';
import { requireProjectModule } from '../../utilities/require-project-module';
import { WebpackConfigOptions } from '../build-options';
import { getOutputHashFormat, normalizeExtraEntryPoints } from './utils';

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

/**
* Enumerate loaders and their dependencies from this file to let the dependency validator
* know they are used.
*
* require('source-map-loader')
* require('raw-loader')
* require('url-loader')
* require('file-loader')
* require('@angular-devkit/build-optimizer')
*/

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

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

const nodeModules = findUp('node_modules', projectRoot);
if (!nodeModules) {
throw new Error('Cannot locate node_modules directory.')
throw new Error('Cannot locate node_modules directory.');
}

let extraPlugins: any[] = [];
let entryPoints: { [key: string]: string[] } = {};
// tslint:disable-next-line:no-any
const extraPlugins: any[] = [];
const entryPoints: { [key: string]: string[] } = {};

if (buildOptions.main) {
entryPoints['main'] = [path.resolve(root, buildOptions.main)];
Expand All @@ -72,19 +61,19 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
if (buildOptions.profile) {
extraPlugins.push(new debug.ProfilingPlugin({
outputPath: path.resolve(root, 'chrome-profiler-events.json'),
}))
}));
}

// determine hashing format
const hashFormat = getOutputHashFormat(buildOptions.outputHashing as any);
const hashFormat = getOutputHashFormat(buildOptions.outputHashing || 'none');

// process global scripts
if (buildOptions.scripts.length > 0) {
const globalScriptsByBundleName = normalizeExtraEntryPoints(buildOptions.scripts, 'scripts')
.reduce((prev: { bundleName: string, paths: string[], lazy: boolean }[], curr) => {
const bundleName = curr.bundleName;
const resolvedPath = path.resolve(root, curr.input);
let existingEntry = prev.find((el) => el.bundleName === bundleName);
const existingEntry = prev.find((el) => el.bundleName === bundleName);
if (existingEntry) {
if (existingEntry.lazy && !curr.lazy) {
// All entries have to be lazy for the bundle to be lazy.
Expand All @@ -97,9 +86,10 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
prev.push({
bundleName,
paths: [resolvedPath],
lazy: curr.lazy
lazy: curr.lazy,
});
}

return prev;
}, []);

Expand Down Expand Up @@ -141,8 +131,8 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
ignore: asset.ignore,
from: {
glob: asset.glob,
dot: true
}
dot: true,
},
};
});

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

if (buildOptions.showCircularDependencies) {
extraPlugins.push(new CircularDependencyPlugin({
exclude: /[\\\/]node_modules[\\\/]/
exclude: /[\\\/]node_modules[\\\/]/,
}));
}

Expand All @@ -172,10 +162,10 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
sourceMapUseRule = {
use: [
{
loader: 'source-map-loader'
}
]
}
loader: 'source-map-loader',
},
],
};
}

let buildOptimizerUseRule;
Expand All @@ -184,7 +174,7 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
use: [
{
loader: buildOptimizerLoader,
options: { sourceMap: buildOptions.sourceMap }
options: { sourceMap: buildOptions.sourceMap },
},
],
};
Expand Down Expand Up @@ -253,10 +243,10 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
wco.tsConfig.options.baseUrl || projectRoot,
'node_modules',
],
alias
alias,
},
resolveLoader: {
modules: loaderNodeModules
modules: loaderNodeModules,
},
context: projectRoot,
entry: entryPoints,
Expand All @@ -267,7 +257,7 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
},
watch: buildOptions.watch,
watchOptions: {
poll: buildOptions.poll
poll: buildOptions.poll,
},
performance: {
hints: false,
Expand All @@ -280,7 +270,7 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
loader: 'file-loader',
options: {
name: `[name]${hashFormat.file}.[ext]`,
}
},
},
{
// Mark files inside `@angular/core` as using SystemJS style dynamic imports.
Expand All @@ -298,7 +288,7 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
enforce: 'pre',
...sourceMapUseRule,
},
]
],
},
optimization: {
noEmitOnErrors: true,
Expand Down
Expand Up @@ -5,9 +5,6 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
// tslint:disable
// TODO: cleanup this file, it's copied as is from Angular CLI.

export * from './browser';
export * from './common';
export * from './server';
Expand Down
Expand Up @@ -5,18 +5,17 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
// tslint:disable
// TODO: cleanup this file, it's copied as is from Angular CLI.

import { Configuration } from 'webpack';
import { WebpackConfigOptions } from '../build-options';


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

const config: any = {
const config: Configuration = {
devtool: wco.buildOptions.sourceMap ? 'source-map' : false,
resolve: {
mainFields: [
Expand All @@ -26,14 +25,15 @@ export function getServerConfig(wco: WebpackConfigOptions) {
},
target: 'node',
output: {
libraryTarget: 'commonjs'
libraryTarget: 'commonjs',
},
node: false,
};

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

Expand Down

0 comments on commit 9f114ae

Please sign in to comment.