@@ -15,15 +15,14 @@ module.exports = function serveCommand(api, opts) {
1515
1616 api . modifyCreateDevServer ( ( ) => {
1717
18- const { _, tryRequire, chalk, openBrowser, Env } = require ( '@micro-app/shared-utils' ) ;
19- const path = require ( 'path' ) ;
18+ const { _, tryRequire, chalk, openBrowser, Env, semver } = require ( '@micro-app/shared-utils' ) ;
2019 const url = require ( 'url' ) ;
21- const launchEditorMiddleware = require ( 'launch-editor-middleware ' ) ;
20+ const launchEditorMiddleware = require ( 'launch-editor-middleware' ) ;
2221 const portfinder = require ( 'portfinder' ) ;
23- const prepareURLs = require ( '../../../ utils/prepareURLs' ) ;
24- const prepareProxy = require ( '../../../ utils/prepareProxy' ) ;
25- const validateWebpackConfig = require ( '../../../ utils/validateWebpackConfig' ) ;
26- const isAbsoluteUrl = require ( '../../../ utils/isAbsoluteUrl' ) ;
22+ const prepareURLs = require ( '../../utils/prepareURLs' ) ;
23+ const prepareProxy = require ( '../../utils/prepareProxy' ) ;
24+ const validateWebpackConfig = require ( '../../utils/validateWebpackConfig' ) ;
25+ const isAbsoluteUrl = require ( '../../utils/isAbsoluteUrl' ) ;
2726
2827 function addConfig ( id , fn ) {
2928 if ( tryRequire . resolve ( id ) ) {
@@ -44,13 +43,22 @@ module.exports = function serveCommand(api, opts) {
4443 logger . throw ( '[serve]' , 'Not Found "webpack-dev-server"!' ) ;
4544 }
4645
46+ const webpackVersion = require ( 'webpack/package.json' ) . version ;
47+ const webpackDevServerVersion = require ( 'webpack-dev-server/package.json' ) . version ;
48+
4749 const isInContainer = checkInContainer ( ) ;
4850 const isProduction = mode === 'production' ;
4951
5052 const options = api . config || { } ;
5153
54+ const spinner = logger . spinner ( `Starting for ${ mode } ...` ) ;
55+
5256 // configs that only matters for dev server
5357 api . modifyChainWebpcakConfig ( webpackChain => {
58+
59+ // webpack 4
60+ const isWebpack4 = semver . satisfies ( webpackVersion , '>=4' ) ;
61+
5462 if ( mode !== 'production' && mode !== 'test' ) {
5563 webpackChain
5664 . devtool ( 'cheap-module-eval-source-map' ) ;
@@ -61,20 +69,40 @@ module.exports = function serveCommand(api, opts) {
6169 . use ( require ( id ) ) ;
6270 } ) ;
6371
64- // https://github.com/webpack/webpack/issues/6642
65- // https://github.com/vuejs/vue-cli/issues/3539
66- webpackChain
67- . output
68- . globalObject ( '(typeof self !== \'undefined\' ? self : this)' ) ;
72+ if ( isWebpack4 ) {
73+ // https://github.com/webpack/webpack/issues/6642
74+ // https://github.com/vuejs/vue-cli/issues/3539
75+ webpackChain
76+ . output
77+ . globalObject ( '(typeof self !== \'undefined\' ? self : this)' ) ;
78+ }
6979
7080 if ( options . devServer && options . devServer . progress !== false ) {
7181 addConfig ( 'webpack/lib/ProgressPlugin' , id => {
7282 webpackChain
7383 . plugin ( 'progress' )
74- . use ( require ( id ) ) ;
84+ . use ( require ( id ) , [
85+ {
86+ modules : false ,
87+ profile : false ,
88+ handler : ( percentage , message /* , ...args */ ) => {
89+ if ( spinner && percentage <= 0 ) {
90+ spinner . start ( ) ;
91+ }
92+ if ( spinner ) {
93+ spinner . text = Number ( percentage * 100 ) . toFixed ( 2 ) + '% ' + chalk . gray ( `( ${ message } )` ) ;
94+ }
95+ // api.logger.logo(percentage, message, ...args);
96+ if ( spinner && percentage >= 1 ) {
97+ spinner . succeed ( 'Compiled Done!' ) ;
98+ }
99+ } ,
100+ } ,
101+ ] ) ;
75102 } ) ;
76103 }
77104 }
105+ return webpackChain ;
78106 } ) ;
79107
80108 const webpackConfig = api . resolveWebpackConfig ( ) ;
@@ -88,7 +116,7 @@ module.exports = function serveCommand(api, opts) {
88116 // in webpack config
89117 const projectDevServerOptions = Object . assign (
90118 webpackConfig . devServer || { } ,
91- options . devServer
119+ options . devServer || { }
92120 ) ;
93121
94122 // entry arg
@@ -125,7 +153,7 @@ module.exports = function serveCommand(api, opts) {
125153
126154 const proxySettings = prepareProxy (
127155 projectDevServerOptions . proxy ,
128- api . resolve ( 'public' )
156+ options . staticPaths || [ ]
129157 ) ;
130158
131159 // inject dev & hot-reload middleware entries
@@ -164,20 +192,24 @@ module.exports = function serveCommand(api, opts) {
164192 // create compiler
165193 const compiler = webpack ( webpackConfig ) ;
166194
195+ const isWebpackDevServer3 = semver . satisfies ( webpackDevServerVersion , '>=3' ) ;
196+
167197 // create server
168- const server = new WebpackDevServer ( compiler , Object . assign ( {
198+ const server = new WebpackDevServer ( compiler , Object . assign ( isWebpackDevServer3 ? {
169199 logLevel : 'silent' ,
200+ } : { } , {
170201 clientLogLevel : 'silent' ,
171202 historyApiFallback : {
172203 disableDotRule : true ,
173204 rewrites : genHistoryApiFallbackRewrites ( options . publicPath , options . pages ) ,
174205 } ,
175- contentBase : api . resolve ( 'public' ) ,
206+ contentBase : options . staticPaths || [ ] ,
176207 watchContentBase : ! isProduction ,
177208 hot : ! isProduction ,
178209 compress : isProduction ,
179210 publicPath : options . publicPath ,
180- overlay : isProduction // TODO disable this
211+ // TODO disable this
212+ overlay : isProduction
181213 ? false
182214 : { warnings : false , errors : true } ,
183215 } , projectDevServerOptions , {
@@ -210,21 +242,23 @@ module.exports = function serveCommand(api, opts) {
210242 } ) ) ;
211243
212244 [ 'SIGINT' , 'SIGTERM' ] . forEach ( signal => {
213- process . on ( signal , ( ) => {
245+ process . once ( signal , ( ) => {
214246 server . close ( ( ) => {
215247 process . exit ( 0 ) ;
216248 } ) ;
217249 } ) ;
218250 } ) ;
219251
220- const spinner = logger . spinner ( `Starting for ${ mode } ...` ) ;
221-
222252 return new Promise ( ( resolve , reject ) => {
223253 spinner . start ( ) ;
224254
225255 // log instructions & open browser on first compilation complete
226256 let isFirstCompile = true ;
227257 const done = stats => {
258+ if ( isFirstCompile ) {
259+ spinner . stop ( ) ;
260+ }
261+
228262 if ( stats . hasErrors ( ) ) {
229263 return ;
230264 }
0 commit comments