Skip to content

Commit 2fa1737

Browse files
committed
refactor(@angular/cli): use shared configs for ng test
Fix #3605 Fix #4850
1 parent 224eac7 commit 2fa1737

File tree

20 files changed

+253
-221
lines changed

20 files changed

+253
-221
lines changed

packages/@angular/cli/blueprints/ng2/files/karma.conf.js

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ module.exports = function (config) {
2929
fixWebpackSourcePaths: true
3030
},
3131
angularCli: {
32-
config: './.angular-cli.json',
3332
environment: 'dev'
3433
},
3534
reporters: config.angularCli && config.angularCli.codeCoverage

packages/@angular/cli/models/webpack-config.ts

+20-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const webpackMerge = require('webpack-merge');
22
import { CliConfig } from './config';
33
import { BuildOptions } from './build-options';
44
import {
5+
getBrowserConfig,
56
getCommonConfig,
67
getDevConfig,
78
getProdConfig,
@@ -20,6 +21,7 @@ export interface WebpackConfigOptions {
2021

2122
export class NgCliWebpackConfig {
2223
public config: any;
24+
public wco: WebpackConfigOptions;
2325
constructor(buildOptions: BuildOptions) {
2426

2527
this.validateBuildOptions(buildOptions);
@@ -32,26 +34,29 @@ export class NgCliWebpackConfig {
3234
buildOptions = this.addTargetDefaults(buildOptions);
3335
buildOptions = this.mergeConfigs(buildOptions, appConfig);
3436

35-
const wco: WebpackConfigOptions = { projectRoot, buildOptions, appConfig };
37+
this.wco = { projectRoot, buildOptions, appConfig };
38+
}
3639

40+
public buildConfig() {
3741
let webpackConfigs = [
38-
getCommonConfig(wco),
39-
getStylesConfig(wco),
40-
this.getTargetConfig(wco)
42+
getCommonConfig(this.wco),
43+
getBrowserConfig(this.wco),
44+
getStylesConfig(this.wco),
45+
this.getTargetConfig(this.wco)
4146
];
4247

43-
if (appConfig.main || appConfig.polyfills) {
44-
const typescriptConfigPartial = buildOptions.aot
45-
? getAotConfig(wco)
46-
: getNonAotConfig(wco);
48+
if (this.wco.appConfig.main || this.wco.appConfig.polyfills) {
49+
const typescriptConfigPartial = this.wco.buildOptions.aot
50+
? getAotConfig(this.wco)
51+
: getNonAotConfig(this.wco);
4752
webpackConfigs.push(typescriptConfigPartial);
4853
}
4954

50-
// add style config
5155
this.config = webpackMerge(webpackConfigs);
56+
return this.config;
5257
}
5358

54-
getTargetConfig(webpackConfigOptions: WebpackConfigOptions): any {
59+
public getTargetConfig(webpackConfigOptions: WebpackConfigOptions): any {
5560
switch (webpackConfigOptions.buildOptions.target) {
5661
case 'development':
5762
return getDevConfig(webpackConfigOptions);
@@ -61,14 +66,15 @@ export class NgCliWebpackConfig {
6166
}
6267

6368
// Validate build options
64-
private validateBuildOptions(buildOptions: BuildOptions) {
69+
public validateBuildOptions(buildOptions: BuildOptions) {
70+
buildOptions.target = buildOptions.target || 'development';
6571
if (buildOptions.target !== 'development' && buildOptions.target !== 'production') {
6672
throw new Error("Invalid build target. Only 'development' and 'production' are available.");
6773
}
6874
}
6975

7076
// Fill in defaults for build targets
71-
private addTargetDefaults(buildOptions: BuildOptions) {
77+
public addTargetDefaults(buildOptions: BuildOptions) {
7278
const targetDefaults: any = {
7379
development: {
7480
environment: 'dev',
@@ -89,7 +95,7 @@ export class NgCliWebpackConfig {
8995
}
9096

9197
// Fill in defaults from .angular-cli.json
92-
private mergeConfigs(buildOptions: BuildOptions, appConfig: any) {
98+
public mergeConfigs(buildOptions: BuildOptions, appConfig: any) {
9399
const mergeableOptions = {
94100
outputPath: appConfig.outDir,
95101
deployUrl: appConfig.deployUrl
@@ -98,7 +104,7 @@ export class NgCliWebpackConfig {
98104
return Object.assign({}, mergeableOptions, buildOptions);
99105
}
100106

101-
private addAppConfigDefaults(appConfig: any) {
107+
public addAppConfigDefaults(appConfig: any) {
102108
const appConfigDefaults: any = {
103109
scripts: [],
104110
styles: []
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import * as webpack from 'webpack';
2+
import * as path from 'path';
3+
const HtmlWebpackPlugin = require('html-webpack-plugin');
4+
5+
import { packageChunkSort } from '../../utilities/package-chunk-sort';
6+
import { BaseHrefWebpackPlugin } from '../../lib/base-href-webpack';
7+
import { extraEntryParser, lazyChunksFilter } from './utils';
8+
import { WebpackConfigOptions } from '../webpack-config';
9+
10+
11+
export function getBrowserConfig(wco: WebpackConfigOptions) {
12+
const { projectRoot, buildOptions, appConfig } = wco;
13+
14+
const appRoot = path.resolve(projectRoot, appConfig.root);
15+
const nodeModules = path.resolve(projectRoot, 'node_modules');
16+
17+
let extraPlugins: any[] = [];
18+
19+
// figure out which are the lazy loaded entry points
20+
const lazyChunks = lazyChunksFilter([
21+
...extraEntryParser(appConfig.scripts, appRoot, 'scripts'),
22+
...extraEntryParser(appConfig.styles, appRoot, 'styles')
23+
]);
24+
25+
if (buildOptions.vendorChunk) {
26+
extraPlugins.push(new webpack.optimize.CommonsChunkPlugin({
27+
name: 'vendor',
28+
chunks: ['main'],
29+
minChunks: (module: any) => module.resource && module.resource.startsWith(nodeModules)
30+
}));
31+
}
32+
33+
return {
34+
plugins: [
35+
new HtmlWebpackPlugin({
36+
template: path.resolve(appRoot, appConfig.index),
37+
filename: path.resolve(buildOptions.outputPath, appConfig.index),
38+
chunksSortMode: packageChunkSort(appConfig),
39+
excludeChunks: lazyChunks,
40+
xhtml: true
41+
}),
42+
new BaseHrefWebpackPlugin({
43+
baseHref: buildOptions.baseHref
44+
}),
45+
new webpack.optimize.CommonsChunkPlugin({
46+
minChunks: Infinity,
47+
name: 'inline'
48+
})
49+
].concat(extraPlugins)
50+
};
51+
}

packages/@angular/cli/models/webpack-configs/common.ts

+2-34
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import * as webpack from 'webpack';
22
import * as path from 'path';
33
import { GlobCopyWebpackPlugin } from '../../plugins/glob-copy-webpack-plugin';
4-
import { packageChunkSort } from '../../utilities/package-chunk-sort';
5-
import { BaseHrefWebpackPlugin } from '../../lib/base-href-webpack';
6-
import { extraEntryParser, lazyChunksFilter, getOutputHashFormat } from './utils';
4+
import { extraEntryParser, getOutputHashFormat } from './utils';
75
import { WebpackConfigOptions } from '../webpack-config';
86

97
const ProgressPlugin = require('webpack/lib/ProgressPlugin');
10-
const HtmlWebpackPlugin = require('html-webpack-plugin');
118

129

1310
/**
@@ -32,12 +29,6 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
3229
let extraRules: any[] = [];
3330
let entryPoints: { [key: string]: string[] } = {};
3431

35-
// figure out which are the lazy loaded entry points
36-
const lazyChunks = lazyChunksFilter([
37-
...extraEntryParser(appConfig.scripts, appRoot, 'scripts'),
38-
...extraEntryParser(appConfig.styles, appRoot, 'styles')
39-
]);
40-
4132
if (appConfig.main) {
4233
entryPoints['main'] = [path.resolve(appRoot, appConfig.main)];
4334
}
@@ -56,19 +47,10 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
5647
// add entry points and lazy chunks
5748
globalScripts.forEach(script => {
5849
let scriptPath = `script-loader!${script.path}`;
59-
if (script.lazy) { lazyChunks.push(script.entry); }
6050
entryPoints[script.entry] = (entryPoints[script.entry] || []).concat(scriptPath);
6151
});
6252
}
6353

64-
if (buildOptions.vendorChunk) {
65-
extraPlugins.push(new webpack.optimize.CommonsChunkPlugin({
66-
name: 'vendor',
67-
chunks: ['main'],
68-
minChunks: (module: any) => module.resource && module.resource.startsWith(nodeModules)
69-
}));
70-
}
71-
7254
// process asset entries
7355
if (appConfig.assets) {
7456
extraPlugins.push(new GlobCopyWebpackPlugin({
@@ -111,21 +93,7 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
11193
].concat(extraRules)
11294
},
11395
plugins: [
114-
new webpack.NoEmitOnErrorsPlugin(),
115-
new HtmlWebpackPlugin({
116-
template: path.resolve(appRoot, appConfig.index),
117-
filename: path.resolve(buildOptions.outputPath, appConfig.index),
118-
chunksSortMode: packageChunkSort(appConfig),
119-
excludeChunks: lazyChunks,
120-
xhtml: true
121-
}),
122-
new BaseHrefWebpackPlugin({
123-
baseHref: buildOptions.baseHref
124-
}),
125-
new webpack.optimize.CommonsChunkPlugin({
126-
minChunks: Infinity,
127-
name: 'inline'
128-
})
96+
new webpack.NoEmitOnErrorsPlugin()
12997
].concat(extraPlugins),
13098
node: {
13199
fs: 'empty',
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
export * from './browser';
12
export * from './common';
23
export * from './development';
34
export * from './production';
45
export * from './styles';
6+
export * from './test';
57
export * from './typescript';
68
export * from './utils';
79
export * from './xi18n';

packages/@angular/cli/models/webpack-configs/test.js

-129
This file was deleted.

0 commit comments

Comments
 (0)