/
dev.js
46 lines (37 loc) · 1.41 KB
/
dev.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
// run express server with webpack middlewares and development config
process.env.NODE_ENV = 'development'
import chalk from 'chalk'
import webpack from 'webpack'
import webpackDevMiddleware from 'webpack-dev-middleware'
import webpackHotMiddleware from 'webpack-hot-middleware'
import history from 'connect-history-api-fallback'
import express from 'express'
import webpackConfigDev from '../webpack/config.dev.babel'
import {CURRENT_IP, WEBPACK_SERVER_PORT} from '../config'
const app = express()
const compiler = webpack(webpackConfigDev)
const webpackMiddleware = webpackDevMiddleware(compiler, {
publicPath: webpackConfigDev.output.publicPath,
// webpack build logs config
stats: {
colors: true,
chunks: false
}
})
const hotMiddleware = webpackHotMiddleware(compiler)
// force page reload when html-webpack-plugin template changes
compiler.plugin('compilation', function (compilation) {
compilation.plugin('html-webpack-plugin-after-emit', function (data, cb) {
hotMiddleware.publish({ action: 'reload' })
cb()
})
})
// handle fallback for HTML5 history API
app.use(history())
app.use(webpackMiddleware)
app.use(hotMiddleware)
app.listen(WEBPACK_SERVER_PORT, '0.0.0.0', function () {
console.log(chalk.yellow('Webpack dev-server listening at:'))
console.log(chalk.green(` http://localhost:${WEBPACK_SERVER_PORT}`))
console.log(chalk.green(` http://${CURRENT_IP}:${WEBPACK_SERVER_PORT} \n`))
})