1
1
require ( './check-versions' ) ( )
2
2
3
3
const config = require ( '../config' )
4
+
4
5
if ( ! process . env . NODE_ENV ) {
5
6
process . env . NODE_ENV = JSON . parse ( config . dev . env . NODE_ENV )
6
7
}
7
8
8
- const opn = require ( 'opn ' )
9
+ const open = require ( 'open ' )
9
10
const path = require ( 'path' )
11
+ const chalk = require ( 'chalk' )
10
12
const express = require ( 'express' )
11
13
const webpack = require ( 'webpack' )
12
14
const proxyMiddleware = require ( 'http-proxy-middleware' )
15
+ const webpackDevMiddleware = require ( 'webpack-dev-middleware' ) ;
16
+ const webpackHotMiddleware = require ( 'webpack-hot-middleware' ) ;
13
17
const webpackConfig = ( process . env . NODE_ENV === 'testing' || process . env . NODE_ENV === 'production' )
14
18
? require ( './webpack.prod.conf' )
15
19
: require ( './webpack.dev.conf' )
@@ -23,17 +27,19 @@ const autoOpenBrowser = !!config.dev.autoOpenBrowser
23
27
const proxyTable = config . dev . proxyTable
24
28
25
29
const app = express ( )
30
+
26
31
const compiler = webpack ( webpackConfig )
27
32
28
- const devMiddleware = require ( 'webpack-dev-middleware' ) ( compiler , {
33
+ const wdmInstance = webpackDevMiddleware ( compiler , {
29
34
publicPath : webpackConfig . output . publicPath ,
30
35
quiet : true
31
36
} )
32
37
33
- const hotMiddleware = require ( 'webpack-hot-middleware' ) ( compiler , {
38
+ const hotMiddleware = webpackHotMiddleware ( compiler , {
34
39
log : false ,
35
40
heartbeat : 2000
36
41
} )
42
+
37
43
// force page reload when html-webpack-plugin template changes
38
44
compiler . hooks . compilation . tap ( 'html-webpack-plugin-after-emit' , ( ) => {
39
45
hotMiddleware . publish ( {
@@ -42,7 +48,7 @@ compiler.hooks.compilation.tap('html-webpack-plugin-after-emit', () => {
42
48
} ) ;
43
49
44
50
// proxy api requests
45
- Object . keys ( proxyTable ) . forEach ( function ( context ) {
51
+ Object . keys ( proxyTable ) . forEach ( ( context ) => {
46
52
const options = proxyTable [ context ]
47
53
if ( typeof options === 'string' ) {
48
54
options = { target : options }
@@ -54,7 +60,7 @@ Object.keys(proxyTable).forEach(function (context) {
54
60
app . use ( require ( 'connect-history-api-fallback' ) ( ) )
55
61
56
62
// serve webpack bundle output
57
- app . use ( devMiddleware )
63
+ app . use ( wdmInstance )
58
64
59
65
// enable hot-reload and state-preserving
60
66
// compilation error display
@@ -64,28 +70,26 @@ app.use(hotMiddleware)
64
70
const staticPath = path . posix . join ( config . dev . assetsPublicPath , config . dev . assetsSubDirectory )
65
71
app . use ( staticPath , express . static ( './static' ) )
66
72
67
- const uri = 'http://localhost:' + port
68
-
69
- let _resolve
70
- const readyPromise = new Promise ( resolve => {
71
- _resolve = resolve
72
- } )
73
-
74
- console . log ( '> Starting dev server...' )
75
- devMiddleware . waitUntilValid ( ( ) => {
76
- console . log ( '> Listening at ' + uri + '\n' )
77
- // when env is testing, don't need open it
78
- if ( autoOpenBrowser && process . env . NODE_ENV !== 'testing' ) {
79
- opn ( uri )
80
- }
81
- _resolve ( )
82
- } )
83
-
84
- const server = app . listen ( port )
73
+ module . exports = new Promise ( ( resolve ) => {
74
+ console . log ( '> Starting dev server...' )
75
+ const server = app . listen ( port , ( err ) => {
76
+ if ( err ) {
77
+ console . error ( err ) ;
78
+ }
79
+
80
+ wdmInstance . waitUntilValid ( ( ) => {
81
+ const uri = 'http://localhost:' + port
82
+ if ( autoOpenBrowser && process . env . NODE_ENV !== 'testing' ) {
83
+ open ( uri )
84
+ console . log ( '> Listening at ' + uri + '\n' )
85
+ }
86
+ } )
87
+ } ) ;
85
88
86
- module . exports = {
87
- ready : readyPromise ,
88
- close : ( ) => {
89
- server . close ( )
90
- }
91
- }
89
+ resolve ( {
90
+ port,
91
+ close : ( ) => {
92
+ server . close ( ) ;
93
+ } ,
94
+ } ) ;
95
+ } ) ;
0 commit comments