1
- var path = require ( 'path' )
2
- var express = require ( 'express' )
3
- var webpack = require ( 'webpack' )
4
- var config = require ( '../config' )
5
- var proxyMiddleware = require ( 'http-proxy-middleware' )
6
- var webpackConfig = process . env . NODE_ENV === 'testing'
1
+ 'use strict'
2
+ require ( './check-versions' ) ( )
3
+
4
+ const config = require ( '../config' )
5
+ if ( ! process . env . NODE_ENV ) {
6
+ process . env . NODE_ENV = JSON . parse ( config . dev . env . NODE_ENV )
7
+ }
8
+
9
+ const opn = require ( 'opn' )
10
+ const path = require ( 'path' )
11
+ const express = require ( 'express' )
12
+ const webpack = require ( 'webpack' )
13
+ const proxyMiddleware = require ( 'http-proxy-middleware' )
14
+ const webpackConfig = ( process . env . NODE_ENV === 'testing' || process . env . NODE_ENV === 'production' )
7
15
? require ( './webpack.prod.conf' )
8
16
: require ( './webpack.dev.conf' )
9
17
10
18
// default port where dev server listens for incoming traffic
11
- var port = process . env . PORT || config . dev . port
19
+ const port = process . env . PORT || config . dev . port
20
+ // automatically open browser, if not set will be false
21
+ const autoOpenBrowser = ! ! config . dev . autoOpenBrowser
12
22
// Define HTTP proxies to your custom API backend
13
23
// https://github.com/chimurai/http-proxy-middleware
14
- var proxyTable = config . dev . proxyTable
24
+ const proxyTable = config . dev . proxyTable
15
25
16
- var app = express ( )
17
- var compiler = webpack ( webpackConfig )
26
+ const app = express ( )
27
+ const compiler = webpack ( webpackConfig )
18
28
19
- var devMiddleware = require ( 'webpack-dev-middleware' ) ( compiler , {
29
+ const devMiddleware = require ( 'webpack-dev-middleware' ) ( compiler , {
20
30
publicPath : webpackConfig . output . publicPath ,
21
- stats : {
22
- colors : true ,
23
- chunks : false
24
- }
31
+ quiet : true
25
32
} )
26
33
27
- var hotMiddleware = require ( 'webpack-hot-middleware' ) ( compiler )
28
- // force page reload when html-webpack-plugin template changes
29
- compiler . plugin ( 'compilation' , function ( compilation ) {
30
- compilation . plugin ( 'html-webpack-plugin-after-emit' , function ( data , cb ) {
31
- hotMiddleware . publish ( { action : 'reload' } )
32
- cb ( )
33
- } )
34
+ const hotMiddleware = require ( 'webpack-hot-middleware' ) ( compiler , {
35
+ log : false ,
36
+ heartbeat : 2000
34
37
} )
38
+ // force page reload when html-webpack-plugin template changes
39
+ // currently disabled until this is resolved:
40
+ // https://github.com/jantimon/html-webpack-plugin/issues/680
41
+ // compiler.plugin('compilation', function (compilation) {
42
+ // compilation.plugin('html-webpack-plugin-after-emit', function (data, cb) {
43
+ // hotMiddleware.publish({ action: 'reload' })
44
+ // cb()
45
+ // })
46
+ // })
47
+
48
+ // enable hot-reload and state-preserving
49
+ // compilation error display
50
+ app . use ( hotMiddleware )
35
51
36
52
// proxy api requests
37
53
Object . keys ( proxyTable ) . forEach ( function ( context ) {
38
- var options = proxyTable [ context ]
54
+ const options = proxyTable [ context ]
39
55
if ( typeof options === 'string' ) {
40
56
options = { target : options }
41
57
}
42
- app . use ( proxyMiddleware ( context , options ) )
58
+ app . use ( proxyMiddleware ( options . filter || context , options ) )
43
59
} )
44
60
45
61
// handle fallback for HTML5 history API
@@ -48,18 +64,44 @@ app.use(require('connect-history-api-fallback')())
48
64
// serve webpack bundle output
49
65
app . use ( devMiddleware )
50
66
51
- // enable hot-reload and state-preserving
52
- // compilation error display
53
- app . use ( hotMiddleware )
54
-
55
67
// serve pure static assets
56
- var staticPath = path . posix . join ( config . build . assetsPublicPath , config . build . assetsSubDirectory )
68
+ const staticPath = path . posix . join ( config . dev . assetsPublicPath , config . dev . assetsSubDirectory )
57
69
app . use ( staticPath , express . static ( './static' ) )
58
70
59
- module . exports = app . listen ( port , function ( err ) {
60
- if ( err ) {
61
- console . log ( err )
62
- return
63
- }
64
- console . log ( 'Listening at http://localhost:' + port + '\n' )
71
+ const uri = 'http://localhost:' + port
72
+
73
+ var _resolve
74
+ var _reject
75
+ var readyPromise = new Promise ( ( resolve , reject ) => {
76
+ _resolve = resolve
77
+ _reject = reject
65
78
} )
79
+
80
+ var server
81
+ var portfinder = require ( 'portfinder' )
82
+ portfinder . basePort = port
83
+
84
+ console . log ( '> Starting dev server...' )
85
+ devMiddleware . waitUntilValid ( ( ) => {
86
+ portfinder . getPort ( ( err , port ) => {
87
+ if ( err ) {
88
+ _reject ( err )
89
+ }
90
+ process . env . PORT = port
91
+ var uri = 'http://localhost:' + port
92
+ console . log ( '> Listening at ' + uri + '\n' )
93
+ // when env is testing, don't need open it
94
+ if ( autoOpenBrowser && process . env . NODE_ENV !== 'testing' ) {
95
+ opn ( uri )
96
+ }
97
+ server = app . listen ( port )
98
+ _resolve ( )
99
+ } )
100
+ } )
101
+
102
+ module . exports = {
103
+ ready : readyPromise ,
104
+ close : ( ) => {
105
+ server . close ( )
106
+ }
107
+ }
0 commit comments