-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
52 lines (41 loc) · 1.11 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
'use strict'
const fs = require('fs')
const path = require('path')
const chalk = require('chalk')
const express = require('express')
const webpack = require('webpack')
const webpackConfig = require('./webpack.dev')
const config = require('./config')
const LogPlugin = require('./log-plugin')
const app = express()
const port = config.port
webpackConfig.entry.client = [
`webpack-hot-middleware/client?reload=true`,
webpackConfig.entry.client
]
webpackConfig.plugins.push(new LogPlugin(port))
let compiler
try {
compiler = webpack(webpackConfig)
} catch (err) {
console.log(err.message)
process.exit(1)
}
const devMiddleWare = require('webpack-dev-middleware')(compiler, {
publicPath: webpackConfig.output.publicPath,
quiet: true
})
app.use(devMiddleWare)
app.use(require('webpack-hot-middleware')(compiler, {
log: () => {}
}))
const mfs = devMiddleWare.fileSystem
const file = path.join(webpackConfig.output.path, 'index.html')
devMiddleWare.waitUntilValid()
app.get('*', (req, res) => {
devMiddleWare.waitUntilValid(() => {
const html = mfs.readFileSync(file)
res.end(html)
})
})
app.listen(port)