-
Notifications
You must be signed in to change notification settings - Fork 8
/
build.js
225 lines (224 loc) · 7.41 KB
/
build.js
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import webpackDefaultConfig from "./getWebpackDefaultConfig";
import mergeCustomConfig from "./mergeWebpackConfig";
import path from "path";
import webpack from "webpack";
import StatsPlugin from "stats-webpack-plugin";
import DllPluginDync from "dllplugindync";
import HtmlWebpackPlugin from "html-webpack-plugin";
import bundleWDevServer from "./devServer";
import webpackWatch from "./webpackWatch";
import { existsSync } from "fs";
import util from "util";
import updateRules from "./updateRules";
import merge from "webpack-merge";
import uniqueRule from "./updateRules/dedupeRule";
import uniquePlugin from "./updateRules/dedupePlugin";
import uniqueItem from "./updateRules/dedupeItem";
//Unique plugin and rule and item etc
const exist = require("exist.js");
const mangleWebpackConfig = require("./livehook");
const BabiliPlugin = require("babili-webpack-plugin");
export default function build(program, callback) {
const commonName = program.hash ? "common-[chunkhash].js" : "common.js";
const defaultHtml = "../test/index.html";
let useDefinedHtml = "";
//With no html template configured, we use our own
if (program.htmlTemplate) {
useDefinedHtml = existsSync(
path.resolve(process.cwd(), program.htmlTemplate)
)
? path.resolve(process.cwd(), program.htmlTemplate)
: defaultHtml;
}
let defaultWebpackConfig = webpackDefaultConfig(program);
//get default webpack configuration
if (program.outputPath) {
defaultWebpackConfig.output.path = program.outputPath;
}
//update output path
if (program.publichPath) {
defaultWebpackConfig.output.publicPath = program.publicPath;
}
console.log('program.disableCSSModules==='+program.disableCSSModules);
//update public path
if (program.stj) {
defaultWebpackConfig.plugins.push(
new StatsPlugin(program.stj, {
//options passed to stats.json
})
);
}
//inject HotModuleReplacementPlugin
if (program.dev) {
defaultWebpackConfig.plugins.push(new webpack.HotModuleReplacementPlugin());
}
//we inject html by HtmlWebpackPlugin
if (!program.dev) {
defaultWebpackConfig.plugins.push(
new HtmlWebpackPlugin({
title: "HtmlPlugin",
// filename :"index.html",
template: useDefinedHtml || path.join(__dirname, "../test/index.html"),
// template:(useDefinedHtml ? useDefinedHtml : defaultHtml),
//we must use html-loader here instead of file-loader
inject: "body",
cache: false,
xhtml: false
}),
new BabiliPlugin(
{},
{
test: /\.(js|jsx)$/
}
)
);
} else {
defaultWebpackConfig.plugins.push(
new HtmlWebpackPlugin({
title: "HtmlPlugin",
// filename :"index.html",
template:
useDefinedHtml || path.join(__dirname, "../test/warning.html"),
// template:(useDefinedHtml ? useDefinedHtml : defaultHtml),
//we must use html-loader here instead of file-loader
inject: "body",
cache: false,
xhtml: false
})
);
}
//we inject DllReferencePlugin
if (program.manifest) {
defaultWebpackConfig.plugins.push(
new DllPluginDync({
manifest: program.manifest,
context: program.cwd
})
);
}
if (!program.dev) {
//https://github.com/mishoo/UglifyJS2
defaultWebpackConfig.plugins = [
...defaultWebpackConfig.plugins,
// new webpack.optimize.UglifyJsPlugin({
// beautify: false,
// sourceMap: true,
// // use SourceMaps to map error message locations to modules.
// //This slows down the compilation. (default: true)
// comments: false,
// //Defaults to preserving comments containing /*!, /**!, @preserve or @license.
// output: {
// ascii_only: true
// },
// compress: {
// warnings: false,
// //no warnings when remove unused code,
// drop_console: true,
// //drop console
// collapse_vars: true,
// //Collapse single-use var and const definitions when possible.
// reduce_vars: true
// // Improve optimization on variables assigned with and used as constant values.
// }
// }),
new webpack.DefinePlugin({
"process.env.NODE_ENV": JSON.stringify(
process.env.NODE_ENV || "production"
)
})
];
} else {
if (process.env.NODE_ENV) {
defaultWebpackConfig.plugins = [
...defaultWebpackConfig.plugins,
new webpack.DefinePlugin({
"process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV)
})
];
}
}
//User defined webpack.config.js to update our common webpack config
if (program.config) {
const customWebpackConfigPath = path.resolve(
program.cwd,
typeof program.config == "object"
? ""
: program.config || "webpack.config.js"
);
if (
typeof program.config !== "object" &&
existsSync(customWebpackConfigPath)
) {
const customConfig = require(customWebpackConfigPath);
defaultWebpackConfig = uniqueItem.dedupeItem(
defaultWebpackConfig,
customConfig
);
//unique webpack loaders
if (exist.get(customConfig, "module.rules")) {
uniqueRule.dedupeRule(defaultWebpackConfig, customConfig);
}
//unique our plugins
if (exist.get(customConfig, "plugins")) {
uniquePlugin.dedupePlugin(defaultWebpackConfig, customConfig);
}
if (program.karma) uniquePlugin.optimizeKarmaPlugin(defaultWebpackConfig);
} else {
// 可以是一个对象,比如程序使用,此时直接合并即可
defaultWebpackConfig = uniqueItem.dedupeItem(
defaultWebpackConfig,
program.config
);
if (exist.get(program.config, "module.rules")) {
uniqueRule.dedupeRule(defaultWebpackConfig, program.config);
}
//unique our plugins
if (exist.get(program.config, "plugins")) {
uniquePlugin.dedupePlugin(defaultWebpackConfig, program.config);
}
// 通过程序设置entry
if (exist.get(program.config, "entry")) {
defaultWebpackConfig.entry = program.config.entry;
}
}
}
defaultWebpackConfig = updateRules(
defaultWebpackConfig,
program.dev,
(program.config && program.config.disableCSSModules) ||
!!program.disableCSSModules
);
//You can manipulate config last chance
//complicate see https://github.com/webpack/tapable
if (typeof program.hook == "function") {
mangleWebpackConfig(defaultWebpackConfig, program.hook);
}
// console.log('-------------',util.inspect(defaultWebpackConfig,{showHidden:true,depth:4}));
//in production mode
//Whether we should start DevServer which serve file from memory instead of fileSystem
if (program.karma) {
//We are now in `test` mode , we just only get webpack configuration with commonchunkplugin removed!
return defaultWebpackConfig;
}
//Just get config without bundling
if (program.onlyCf) {
return defaultWebpackConfig;
}
//Extract to vendor.js
if (program.vendor) {
defaultWebpackConfig.plugins.push(
new webpack.optimize.CommonsChunkPlugin({
name: "common",
minChunks: 2,
filename: commonName
})
);
}
if (program.devServer) {
bundleWDevServer(defaultWebpackConfig, program);
} else {
//we use watch method of webpack
webpackWatch(defaultWebpackConfig, program, callback);
}
return defaultWebpackConfig;
}