@@ -29,9 +29,10 @@ program
29
29
. option ( '--port [port]' , 'Server port' )
30
30
. option ( '--host [host]' , 'Server host' , 'localhost' )
31
31
. option ( '--prod, --production' , 'Build for production' )
32
+ . option ( '-w, --watch' , 'Run in watch mode' )
32
33
. option ( '-m, --mount' , 'Auto-create app instance from given component, true for `.vue` file' )
33
34
. option ( '-c, --config [file]' , 'Use custom config file' )
34
- . option ( '-w , --webpack [file]' , 'Use custom webpack config file' )
35
+ . option ( '--wp , --webpack [file]' , 'Use custom webpack config file' )
35
36
. option ( '--disable-config' , 'You do not want to use config file' )
36
37
. option ( '--disable-webpack-config' , 'You do not want to use webpack config file' )
37
38
. option ( '-o, --open' , 'Open browser' )
@@ -73,7 +74,8 @@ var options = merge({
73
74
mount : program . mount ,
74
75
proxy : program . proxy ,
75
76
production : program . production ,
76
- lib : program . lib
77
+ lib : program . lib ,
78
+ watch : program . watch
77
79
} )
78
80
79
81
function help ( ) {
@@ -254,12 +256,12 @@ if (production) {
254
256
new ExtractTextPlugin ( options . lib ? `${ replaceExtension ( options . entry , '.css' ) } ` : '[name].[contenthash:8].css' )
255
257
)
256
258
} else {
259
+ if ( ! options . watch ) {
260
+ webpackConfig . plugins . push ( new webpack . HotModuleReplacementPlugin ( ) )
261
+ webpackConfig . entry . client . unshift ( require . resolve ( 'webpack-hot-middleware/client' ) + '?reload=true' )
262
+ }
257
263
webpackConfig . devtool = 'eval-source-map'
258
- webpackConfig . entry . client . unshift (
259
- require . resolve ( 'webpack-hot-middleware/client' ) + '?reload=true'
260
- )
261
264
webpackConfig . plugins . push (
262
- new webpack . HotModuleReplacementPlugin ( ) ,
263
265
new FriendlyErrorsPlugin ( ) ,
264
266
new PostCompilePlugin ( ( ) => {
265
267
console . log ( `> Open http://${ options . host } :${ options . port } ` )
@@ -300,36 +302,15 @@ try {
300
302
301
303
checkEntryExists ( options . entry )
302
304
303
- if ( production ) {
305
+ if ( options . watch ) {
306
+ console . log ( '> Running in watch mode' )
307
+ rm ( path . join ( options . dist , '*' ) )
308
+ compiler . watch ( { } , ( err , stats ) => handleBuild ( err , stats , true ) )
309
+ } else if ( production ) {
304
310
console . log ( '> Creating an optimized production build:\n' )
305
311
// remove dist files but keep that folder in production mode
306
312
rm ( path . join ( options . dist , '*' ) )
307
- compiler . run ( ( err , stats ) => {
308
- if ( err ) {
309
- process . exitCode = 1
310
- return console . error ( err . stack )
311
- }
312
- // output warning
313
- if ( stats . hasWarnings ( ) && ! stats . hasErrors ( ) ) {
314
- console . error ( stats . toString ( 'errors-only' ) )
315
- }
316
- // output wanring and errors
317
- if ( stats . hasErrors ( ) ) {
318
- process . exitCode = 1
319
- return console . error ( stats . toString ( 'errors-only' ) )
320
- }
321
- console . log ( stats . toString ( {
322
- chunks : false ,
323
- children : false ,
324
- modules : false ,
325
- colors : true
326
- } ) )
327
- console . log ( `\n${ chalk . bgGreen . black ( ' SUCCESS ' ) } Compiled successfully.\n` )
328
- console . log ( `The ${ chalk . cyan ( options . dist ) } folder is ready to be deployed.` )
329
- console . log ( `You may also serve it locally with a static server:\n` )
330
- console . log ( ` ${ chalk . yellow ( 'npm' ) } i -g serve` )
331
- console . log ( ` ${ chalk . yellow ( 'serve' ) } ${ options . dist } \n` )
332
- } )
313
+ compiler . run ( handleBuild )
333
314
} else {
334
315
var server = createServer ( compiler , options )
335
316
@@ -369,3 +350,30 @@ function replaceExtension (entry, ext) {
369
350
function getLibraryName ( fileName ) {
370
351
return fileName . replace ( / [ - _ . ] ( [ \w ] ) / , ( _ , p1 ) => p1 . toUpperCase ( ) )
371
352
}
353
+
354
+ function handleBuild ( err , stats , watch ) {
355
+ if ( watch ) {
356
+ process . stdout . write ( '\x1Bc' )
357
+ }
358
+ if ( err ) {
359
+ process . exitCode = 1
360
+ return console . error ( err . stack )
361
+ }
362
+ if ( stats . hasErrors ( ) || stats . hasWarnings ( ) ) {
363
+ process . exitCode = 1
364
+ return console . error ( stats . toString ( 'errors-only' ) )
365
+ }
366
+ console . log ( stats . toString ( {
367
+ chunks : false ,
368
+ children : false ,
369
+ modules : false ,
370
+ colors : true
371
+ } ) )
372
+ console . log ( `\n${ chalk . bgGreen . black ( ' SUCCESS ' ) } Compiled successfully.\n` )
373
+ if ( ! watch ) {
374
+ console . log ( `The ${ chalk . cyan ( options . dist ) } folder is ready to be deployed.` )
375
+ console . log ( `You may also serve it locally with a static server:\n` )
376
+ console . log ( ` ${ chalk . yellow ( 'npm' ) } i -g serve` )
377
+ console . log ( ` ${ chalk . yellow ( 'serve' ) } ${ options . dist } \n` )
378
+ }
379
+ }
0 commit comments