-
Notifications
You must be signed in to change notification settings - Fork 48
/
server.js
64 lines (49 loc) · 1.55 KB
/
server.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
/* eslint no-console: 0 */
import express from 'express'
import webpack from 'webpack'
import webpackDevMiddleware from 'webpack-dev-middleware'
import webpackHotMiddleware from 'webpack-hot-middleware'
import Dashboard from 'webpack-dashboard'
import DashboardPlugin from 'webpack-dashboard/plugin'
import { spawn } from 'child_process'
import config from './webpack.config.development'
const argv = require('minimist')(process.argv.slice(2))
const PORT = 3009
const app = express()
const compiler = webpack(config)
if (argv.dashboard) {
const dashboard = new Dashboard()
compiler.apply(new DashboardPlugin(dashboard.setData))
}
const webpackDevMiddlwareConfig = {
quiet: argv.dashboard,
publicPath: config.output.publicPath,
stats: {
colors: true
}
}
const wdm = webpackDevMiddleware(compiler, webpackDevMiddlwareConfig)
app.use(wdm)
const webpackHotMiddlewareConfig = argv.dashboard ? { log: () => {} } : {}
app.use(webpackHotMiddleware(compiler, webpackHotMiddlewareConfig))
const server = app.listen(PORT, 'localhost', (err) => {
if (err) {
return console.error(err)
}
if (argv['start-hot']) {
spawn('npm', ['run', 'start-hot'], { shell: true, env: process.env, stdio: 'inherit' })
.on('close', code => process.exit(code))
.on('error', spawnError => console.error(spawnError))
}
console.log(`Listening at http://localhost:${PORT}`)
})
server.on('error', (e) => {
console.error(e)
})
process.on('SIGTERM', () => {
console.log('Stopping dev server')
wdm.close()
server.close(() => {
process.exit(0)
})
})