1
1
const {
2
2
info,
3
- error,
4
3
hasYarn,
5
4
openBrowser
6
5
} = require ( '@vue/cli-shared-utils' )
@@ -23,7 +22,7 @@ module.exports = (api, options) => {
23
22
'--port' : `specify port (default: ${ defaults . port } )` ,
24
23
'--https' : `use https (default: ${ defaults . https } )`
25
24
}
26
- } , args => {
25
+ } , async function serve ( args ) {
27
26
info ( 'Starting development server...' )
28
27
29
28
api . setMode ( args . mode || defaults . mode )
@@ -40,45 +39,109 @@ module.exports = (api, options) => {
40
39
const prepareProxy = require ( '../util/prepareProxy' )
41
40
const launchEditorMiddleware = require ( 'launch-editor-middleware' )
42
41
42
+ // load user devServer options
43
43
const projectDevServerOptions = options . devServer || { }
44
+
45
+ // resolve webpack config
46
+ const webpackConfig = api . resolveWebpackConfig ( )
47
+
48
+ // inject dev & hot-reload middleware entries
49
+ if ( ! isProduction ) {
50
+ const devClients = [
51
+ // dev server client
52
+ require . resolve ( `webpack-dev-server/client` ) ,
53
+ // hmr client
54
+ require . resolve ( projectDevServerOptions . hotOnly
55
+ ? 'webpack/hot/only-dev-server'
56
+ : 'webpack/hot/dev-server' )
57
+ // TODO custom overlay client
58
+ // `@vue/cli-overlay/dist/client`
59
+ ]
60
+ if ( process . env . APPVEYOR ) {
61
+ devClients . push ( `webpack/hot/poll?500` )
62
+ }
63
+ // inject dev/hot client
64
+ addDevClientToEntry ( webpackConfig , devClients )
65
+ }
66
+
67
+ // create compiler
68
+ const compiler = webpack ( webpackConfig )
69
+
70
+ if ( ! process . env . VUE_CLI_TEST ) {
71
+ compiler . apply ( new webpack . ProgressPlugin ( ) )
72
+ }
73
+
74
+ // resolve server options
44
75
const useHttps = args . https || projectDevServerOptions . https || defaults . https
45
76
const host = args . host || process . env . HOST || projectDevServerOptions . host || defaults . host
46
77
portfinder . basePort = args . port || process . env . PORT || projectDevServerOptions . port || defaults . port
47
-
48
- const portPromise = portfinder . getPortPromise ( )
49
- return portPromise . then ( port => new Promise ( ( resolve , reject ) => {
50
- const webpackConfig = api . resolveWebpackConfig ( )
51
-
52
- const urls = prepareURLs (
53
- useHttps ? 'https' : 'http' ,
54
- host ,
55
- port
56
- )
57
-
58
- if ( ! isProduction ) {
59
- const devClients = [
60
- // dev server client
61
- require . resolve ( `webpack-dev-server/client` ) ,
62
- // hmr client
63
- require . resolve ( projectDevServerOptions . hotOnly
64
- ? 'webpack/hot/only-dev-server'
65
- : 'webpack/hot/dev-server' )
66
- // TODO custom overlay client
67
- // `@vue/cli-overlay/dist/client`
68
- ]
69
- if ( process . env . APPVEYOR ) {
70
- devClients . push ( `webpack/hot/poll?500` )
71
- }
72
- // inject dev/hot client
73
- addDevClientToEntry ( webpackConfig , devClients )
78
+ const port = await portfinder . getPortPromise ( )
79
+
80
+ const urls = prepareURLs (
81
+ useHttps ? 'https' : 'http' ,
82
+ host ,
83
+ port
84
+ )
85
+
86
+ const proxySettings = prepareProxy (
87
+ projectDevServerOptions . proxy ,
88
+ api . resolve ( 'public' )
89
+ )
90
+
91
+ // create server
92
+ const server = new WebpackDevServer ( compiler , Object . assign ( {
93
+ clientLogLevel : 'none' ,
94
+ historyApiFallback : {
95
+ disableDotRule : true
96
+ } ,
97
+ contentBase : api . resolve ( 'public' ) ,
98
+ watchContentBase : ! isProduction ,
99
+ hot : ! isProduction ,
100
+ quiet : true ,
101
+ compress : isProduction ,
102
+ publicPath : '/' ,
103
+ overlay : isProduction // TODO disable this
104
+ ? false
105
+ : { warnings : false , errors : true }
106
+ } , projectDevServerOptions , {
107
+ https : useHttps ,
108
+ proxy : proxySettings ,
109
+ before ( app ) {
110
+ // launch editor support.
111
+ // this works with vue-devtools & @vue/cli-overlay
112
+ app . use ( '/__open-in-editor' , launchEditorMiddleware ( ( ) => console . log (
113
+ `To specify an editor, sepcify the EDITOR env variable or ` +
114
+ `add "editor" field to your Vue project config.\n`
115
+ ) ) )
116
+ // allow other plugins to register middlewares, e.g. PWA
117
+ api . service . devServerConfigFns . forEach ( fn => fn ( app ) )
118
+ // apply in project middlewares
119
+ projectDevServerOptions . before && projectDevServerOptions . before ( app )
74
120
}
121
+ } ) )
75
122
76
- const compiler = webpack ( webpackConfig )
123
+ ; [ 'SIGINT' , 'SIGTERM' ] . forEach ( signal => {
124
+ process . on ( signal , ( ) => {
125
+ server . close ( ( ) => {
126
+ process . exit ( 0 )
127
+ } )
128
+ } )
129
+ } )
77
130
78
- if ( ! process . env . VUE_CLI_TEST ) {
79
- compiler . apply ( new webpack . ProgressPlugin ( ) )
80
- }
131
+ // on appveyor, killing the process with SIGTERM causes execa to
132
+ // throw error
133
+ if ( process . env . VUE_CLI_TEST ) {
134
+ process . stdin . on ( 'data' , data => {
135
+ if ( data . toString ( ) === 'close' ) {
136
+ console . log ( 'got close signal!' )
137
+ server . close ( ( ) => {
138
+ process . exit ( 0 )
139
+ } )
140
+ }
141
+ } )
142
+ }
81
143
144
+ return new Promise ( ( resolve , reject ) => {
82
145
// log instructions & open browser on first compilation complete
83
146
let isFirstCompile = true
84
147
compiler . plugin ( 'done' , stats => {
@@ -123,69 +186,12 @@ module.exports = (api, options) => {
123
186
}
124
187
} )
125
188
126
- const proxySettings = prepareProxy (
127
- projectDevServerOptions . proxy ,
128
- api . resolve ( 'public' )
129
- )
130
-
131
- const server = new WebpackDevServer ( compiler , Object . assign ( {
132
- clientLogLevel : 'none' ,
133
- historyApiFallback : {
134
- disableDotRule : true
135
- } ,
136
- contentBase : api . resolve ( 'public' ) ,
137
- watchContentBase : ! isProduction ,
138
- hot : ! isProduction ,
139
- quiet : true ,
140
- compress : isProduction ,
141
- publicPath : '/' ,
142
- overlay : isProduction // TODO disable this
143
- ? false
144
- : { warnings : false , errors : true }
145
- } , projectDevServerOptions , {
146
- https : useHttps ,
147
- proxy : proxySettings ,
148
- before ( app ) {
149
- // launch editor support.
150
- // this works with vue-devtools & @vue/cli-overlay
151
- app . use ( '/__open-in-editor' , launchEditorMiddleware ( ( ) => console . log (
152
- `To specify an editor, sepcify the EDITOR env variable or ` +
153
- `add "editor" field to your Vue project config.\n`
154
- ) ) )
155
- // allow other plugins to register middlewares, e.g. PWA
156
- api . service . devServerConfigFns . forEach ( fn => fn ( app ) )
157
- // apply in project middlewares
158
- projectDevServerOptions . before && projectDevServerOptions . before ( app )
159
- }
160
- } ) )
161
-
162
- ; [ 'SIGINT' , 'SIGTERM' ] . forEach ( signal => {
163
- process . on ( signal , ( ) => {
164
- server . close ( ( ) => {
165
- process . exit ( 0 )
166
- } )
167
- } )
168
- } )
169
-
170
- // on appveyor, killing the process with SIGTERM causes execa to
171
- // throw error
172
- if ( process . env . VUE_CLI_TEST ) {
173
- process . stdin . on ( 'data' , data => {
174
- if ( data . toString ( ) === 'close' ) {
175
- console . log ( 'got close signal!' )
176
- server . close ( ( ) => {
177
- process . exit ( 0 )
178
- } )
179
- }
180
- } )
181
- }
182
-
183
189
server . listen ( port , host , err => {
184
190
if ( err ) {
185
- return error ( err )
191
+ reject ( err )
186
192
}
187
193
} )
188
- } ) )
194
+ } )
189
195
} )
190
196
}
191
197
0 commit comments