-
Notifications
You must be signed in to change notification settings - Fork 21
/
webpack.dev.config.js
355 lines (322 loc) · 10.5 KB
/
webpack.dev.config.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
//font字体问题http://stackoverflow.com/questions/34133808/webpack-ots-parsing-error-loading-fonts
//http://xwartz.xyz/blog/2016/06/electron-with-hmr/ hot-update.json无法更新模块,其实就是路径问题
const fs = require("fs")
const os = require("os")
const path = require('path')
const open = require("child_process")
const webpack = require('webpack')
const glob = require('glob') //允许使用*等符号匹配对应规则的文件.
const HappyPack = require('happypack')
const happyThreadPool = HappyPack.ThreadPool({ size: os.cpus().length })
//https://github.com/nuysoft/Mock/wiki/Getting-Started
const Mock = require('mockjs')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const staticToBuild = require('./lib/staticToBuild')
const ImageminPlugin = require('imagemin-webpack-plugin').default
const UglifyJsParallelPlugin = require('webpack-uglify-parallel')
process.traceDeprecation = true
function getLocalIP() {
try{
var ifaces = os.networkInterfaces();
var reg = /^(10\.)|(172\.)|(192\.).+$/gi;
for (var dev in ifaces) {
var ip = ifaces[dev][1].address;
if(reg.test(ip)) {
return ip;
}
}
}catch(err){
return "localhost"
}
}
process.traceDeprecation = true
//配置启动项目
const APP_NAME = "doc"
//项目静态资源路径
const APP_PATH = path.join(__dirname, 'app', APP_NAME)
const APP_IMAGES = path.join(__dirname, 'app', APP_NAME, 'images')
const APP_FONTS = path.join(__dirname, 'app', APP_NAME, 'fonts')
const APP_SASS = path.join(__dirname, 'app', APP_NAME, 'sass')
const APP_COMPONENT = path.join(__dirname, 'app', APP_NAME, 'components')
const APP_MOCK = path.join(__dirname, 'app', APP_NAME, 'mock')
function getEntries(globPath) {
let files = [];
if (Object.prototype.toString.call(globPath) === '[object Array]') {
globPath.forEach( (o, i) => {
files = files.concat(glob.sync(o))
})
} else {
files = glob.sync(globPath);
}
let _entries = {},
entry, dirname, basename;
for (let i = 0; i < files.length; i++) {
entry = files[i];
dirname = path.dirname(entry)
basename = path.basename(entry, '.js');
// _entries[entry.replace(__dirname+'/', '').replace(/\.js/, '')] = entry
// 不推荐用路径做entry名字,会导致生成模版、图片、字体、样式路径不好确定
_entries[basename] = [entry, 'webpack-hot-middleware/client?path=/__webpack_hmr&timeout=2000&reload=true']
}
return _entries;
}
const entries = getEntries([APP_PATH+'/*.js'])
module.exports = {
entry: entries,
"cache": true,
"context": __dirname,
output: {
path: path.resolve(__dirname, 'dist/'),
publicPath: 'http://' + getLocalIP() + ':9999/',
// publicPath: 'http://localhost:9999/',
// publicPath: '/',
filename: '[name].js', //name,对应entry名称
chunkFilename: "[id].chunk.js"
},
module: {
rules: [
{
test: /\.js$/,
enforce: "pre", //module.preLoaders的替代方案
loader: "happypack/loader?id=eslint", //https://github.com/MoOx/eslint-loader
include: [
APP_PATH
]
},
{
"test": /\.vue$/, // ExtractTextPlugin https://github.com/vuejs/vue-loader/issues/622
"loader": "vue-loader",
"options": {
"loaders": {
"css": [
"vue-style-loader",
{
"loader": "css-loader",
"options": {
"minimize": false,
"sourceMap": false
}
}
],
"postcss": [
"vue-style-loader",
{
"loader": "css-loader",
"options": {
"minimize": false,
"sourceMap": false
}
}
],
"less": [
"vue-style-loader",
{
"loader": "css-loader",
"options": {
"minimize": false,
"sourceMap": false
}
},
{
"loader": "less-loader",
"options": {
"sourceMap": false
}
}
],
"sass": [
"vue-style-loader",
{
"loader": "css-loader",
"options": {
"minimize": false,
"sourceMap": false
}
},
{
"loader": "sass-loader",
"options": {
"indentedSyntax": true,
"sourceMap": false
}
}
],
"scss": [
"vue-style-loader",
{
"loader": "css-loader",
"options": {
"minimize": false,
"sourceMap": false
}
},
{
"loader": "sass-loader",
"options": {
"sourceMap": false
}
}
]
}
},
// include: [
// APP_COMPONENT
// ],
exclude: /node_modules/
},
{
test: /\.js$/,
exclude: /node_modules/, //排除node_modules文件夹
use: {
loader: 'happypack/loader?id=js'
// options: {
// presets: ['es2015','stage-2']
// }
},
include: [
APP_PATH
]
},
{
test: /\.json$/,
loader: 'json-loader'
},
{
test: /\.(css|scss)$/,
use: [
{
loader: 'style-loader',
options: {
name: './css/[name].[ext]?[hash]' //devServer预览都是相对dist输出目录
}
},
{
"loader": "css-loader",
"options": {
"minimize": false,
"sourceMap": false
}
},
"postcss-loader",
"sass-loader"
],
include: [
APP_SASS
]
},
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: 'file-loader',
options: {
limit: 10000,
name: './images/[name].[ext]?[hash]' //devServer预览都是相对dist输出目录
},
include: [
APP_IMAGES
]
},
{
test: /\.(woff2?|eot|ttf|otf|svg)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 8192,
name: './fonts/[name].[hash].[ext]'
// name: '/fonts/[name].[hash].[ext]'
// name: './fonts/[name].[ext]'
},
include: [
APP_FONTS
]
},
{
test: /\.html$/,
loader: 'html-loader',
options: {
name: '[name].[ext]?[hash]'
},
include: [
APP_PATH
]
}
]
},
//页面已经cdn加载,则不需要打包到chunk
externals: {
"av": "AV"
},
resolve: {
alias: {
'vue': 'vue/dist/vue.common.js',
'@font': "",
'@img': "../images"
},
modules: [path.resolve(__dirname, "node_modules")]
},
performance: {
"maxAssetSize": 250000,
"maxEntrypointSize": 250000,
hints: process.env.NODE_ENV === 'production' ? 'warning' : false
},
plugins: [
// 开启全局的模块热替换(HMR)
new webpack.HotModuleReplacementPlugin(),
// 当模块热替换(HMR)时在浏览器控制台输出对用户更友好的模块名字信息
new webpack.NamedModulesPlugin(),
new webpack.optimize.ModuleConcatenationPlugin(), //减少模块闭包函数数量从而加快JS的执行速度
new ImageminPlugin({
disable: process.env.NODE_ENV !== 'production', // Disable during development
pngquant: {
quality: '95-100'
}
}),
//https://github.com/webpack/webpack/tree/master/examples/multiple-commons-chunks#webpackconfigjs
new webpack.optimize.CommonsChunkPlugin({
name: 'vendors', // 将公共模块提取,生成名为`vendors`的chunk
chunks: Object.keys(entries),
// minChunks: Infinity
minChunks: Object.keys(entries).length // 提取所有entry共同依赖的模块
}),
new HappyPack({
id: 'js',
loaders: [ 'babel-loader?cacheDirectory=true&presets[]=es2015&presets[]=stage-2' ],
threadPool: happyThreadPool,
cache: true,
verbose: true
}),
new HappyPack({
id: 'eslint',
loaders: [ 'eslint-loader' ],
threadPool: happyThreadPool,
cache: true,
verbose: true
}),
// new webpack.optimize.CommonsChunkPlugin({
// name: 'manifest' //But since there are no more common modules between them we end up with just the runtime code included in the manifest file
// }),
// //静态资源分割存储
// new staticToBuild({
// dir: '/app/doc',
// regex: /\.(png|jpe?g|gif|svg|woff2?|eot|ttf|otf|css)(\?.*)?$/i
// })
]
};
Object.keys(entries).map( (name) => {
var plugin = new HtmlWebpackPlugin({
filename: name + '.html',
template: path.join(APP_PATH, name+'.html'),
inject: true,
chunks: ["vendors", name],
showErrors: true,
minify: {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true
// more options:
// https://github.com/kangax/html-minifier#options-quick-reference
}
})
module.exports.plugins.push(plugin);
} )
//静态资源是相对于output路径, js路径是相对入口路径