-
Notifications
You must be signed in to change notification settings - Fork 2
/
webpack.exserver.js
56 lines (48 loc) · 1.56 KB
/
webpack.exserver.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
var express = require('express')
var webpack = require('webpack')
var config = require('./webpack.dev.config.js')
// new webpack.optimize.OccurenceOrderPlugin(),
// new webpack.HotModuleReplacementPlugin(),
// new webpack.NoErrorsPlugin(),
var entry = config.entry;
for (var entryname in entry) {
entry[entryname].push('./npm-scripts/dev-client');
// entry[entryname].push('webpack-hot-middleware/client');
// entry[entryname].unshift('webpack/hot/dev-server');
// entry[entryname].unshift("webpack-dev-server/client?http://localhost:8080/");
}
// 创建一个express实例
var app = express()
// 调用webpack并把配置传递过去
var compiler = webpack(config)
// 使用 webpack-dev-middleware 中间件
var devMiddleware = require('webpack-dev-middleware')(compiler, {
publicPath: config.output.publicPath,
stats: {
colors: true,
chunks: false
}
})
// 使用 webpack-hot-middleware 中间件
var hotMiddleware = require('webpack-hot-middleware')(compiler)
// webpack插件,监听html文件改变事件
compiler.plugin('compilation', function (compilation) {
compilation.plugin('html-webpack-plugin-after-emit', function (data, cb) {
// 发布事件
hotMiddleware.publish({ action: 'reload' })
cb()
})
})
// 注册中间件
app.use(devMiddleware)
// 注册中间件
app.use(hotMiddleware)
app.use('/dll', express.static('./build/dll'));
// 监听 8888端口,开启服务器
module.exports = app.listen(8888, function (err) {
if (err) {
console.log(err)
return
}
console.log('Listening at http://localhost:8888')
})