-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathdev-server.js
80 lines (64 loc) · 2.19 KB
/
dev-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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const config = require('../config')
const opn = require('opn')
const path = require('path')
const express = require('express')
const webpack = require('webpack')
const webpackConfig = require('./webpack.dev.conf')
// default port where dev server listens for incoming traffic
const port = process.env.PORT || config.dev.port
// Define HTTP proxies to your custom API backend
// https://github.com/chimurai/http-proxy-middleware
// var proxyMiddleware = require('http-proxy-middleware')
// var proxyTable = config.dev.proxyTable
//进度
const readline = require('readline');
webpackConfig.plugins.push(new webpack.ProgressPlugin((percentage, msg) => {
//移动光标
readline.clearLine(process.stdout);
console.log(' ' + (percentage * 100).toFixed(2) + '%', msg);
readline.moveCursor(process.stdout, 0, -1);
}));
const app = express()
const compiler = webpack(webpackConfig, _ => {
console.log(_)
});
const devMiddleware = require('webpack-dev-middleware')(compiler, {
publicPath: webpackConfig.output.publicPath,
serverSideRender: false,
writeToDisk: false,
//logLevel: 'warn',
//logTime: true,
stats: 'minimal'
})
const hotMiddleware = require('webpack-hot-middleware')(compiler)
// handle fallback for HTML5 history API
//app.use(require('connect-history-api-fallback')())
app.use(devMiddleware)
app.use(hotMiddleware)
// serve pure static assets
const staticPath = path.posix.join(config.dev.assetsPublicPath, config.dev.assetsSubDirectory)
app.use(staticPath || '/', express.static('./static'))
app.use('/h5/_dev', express.static('./src'))
const uri = 'http://localhost:' + require('../mock/mock-config').port
let _resolve
const readyPromise = new Promise(resolve => {
_resolve = resolve
})
console.log('> Starting dev server...')
devMiddleware.waitUntilValid(() => {
console.log('> Listening at ' + uri + '\n')
// when env is testing, don't need open it
if (config.dev.autoOpenBrowser && process.env.NODE_ENV !== 'testing') {
opn(uri)
}
_resolve()
})
const server = app.listen(port)
module.exports = {
ready: readyPromise,
close: () => {
server.close()
}
}
// 启用mock
require('dynamic-mocker').start('./mock/mock-config.js')