forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebpack-config.ts
131 lines (111 loc) · 3.4 KB
/
webpack-config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const webpackMerge = require('webpack-merge');
import { CliConfig } from './config';
import {
getCommonConfig,
getDevConfig,
getProdConfig,
getStylesConfig,
getNonAotConfig,
getAotConfig
} from './webpack-configs';
const path = require('path');
export interface BuildOptions {
target?: string;
environment?: string;
outputPath?: string;
aot?: boolean;
sourcemap?: boolean;
vendorChunk?: boolean;
baseHref?: string;
deployUrl?: string;
verbose?: boolean;
progress?: boolean;
i18nFile?: string;
i18nFormat?: string;
locale?: string;
extractCss?: boolean;
outputHashing?: string;
}
export interface WebpackConfigOptions {
projectRoot: string;
buildOptions: BuildOptions;
appConfig: any;
}
export class NgCliWebpackConfig {
public config: any;
constructor(buildOptions: BuildOptions) {
this.validateBuildOptions(buildOptions);
const configPath = CliConfig.configFilePath();
const projectRoot = path.dirname(configPath);
let appConfig = CliConfig.fromProject().config.apps[0];
appConfig = this.addAppConfigDefaults(appConfig);
buildOptions = this.addTargetDefaults(buildOptions);
buildOptions = this.mergeConfigs(buildOptions, appConfig);
const wco: WebpackConfigOptions = { projectRoot, buildOptions, appConfig };
let webpackConfigs = [
getCommonConfig(wco),
getStylesConfig(wco),
this.getTargetConfig(wco)
];
if (appConfig.main || appConfig.polyfills) {
const typescriptConfigPartial = buildOptions.aot
? getAotConfig(wco)
: getNonAotConfig(wco);
webpackConfigs.push(typescriptConfigPartial);
}
// add style config
this.config = webpackMerge(webpackConfigs);
}
getTargetConfig(webpackConfigOptions: WebpackConfigOptions): any {
switch (webpackConfigOptions.buildOptions.target) {
case 'development':
return getDevConfig(webpackConfigOptions);
case 'production':
return getProdConfig(webpackConfigOptions);
}
}
// Validate build options
private validateBuildOptions(buildOptions: BuildOptions) {
if (buildOptions.target !== 'development' && buildOptions.target !== 'production') {
throw new Error("Invalid build target. Only 'development' and 'production' are available.");
}
}
// Fill in defaults for build targets
private addTargetDefaults(buildOptions: BuildOptions) {
const targetDefaults: any = {
development: {
environment: 'dev',
outputHashing: 'none',
sourcemap: true,
extractCss: false
},
production: {
environment: 'prod',
outputHashing: 'all',
sourcemap: false,
extractCss: true,
aot: true
}
};
return Object.assign({}, targetDefaults[buildOptions.target], buildOptions);
}
// Fill in defaults from angular-cli.json
private mergeConfigs(buildOptions: BuildOptions, appConfig: any) {
const mergeableOptions = {
outputPath: appConfig.outDir,
deployUrl: appConfig.deployUrl
};
return Object.assign({}, mergeableOptions, buildOptions);
}
private addAppConfigDefaults(appConfig: any) {
const appConfigDefaults: any = {
scripts: [],
styles: []
};
// can't use Object.assign here because appConfig has a lot of getters/setters
for (let key of Object.keys(appConfigDefaults)) {
appConfig[key] = appConfig[key] || appConfigDefaults[key];
}
return appConfig;
}
}