1
1
var fs = require ( "fs" ) ;
2
+ var chokidar = require ( "chokidar" ) ;
2
3
var path = require ( "path" ) ;
3
4
var webpackDevMiddleware = require ( "webpack-dev-middleware" ) ;
4
5
var express = require ( "express" ) ;
@@ -22,6 +23,7 @@ function Server(compiler, options) {
22
23
this . headers = options . headers ;
23
24
this . clientLogLevel = options . clientLogLevel ;
24
25
this . sockets = [ ] ;
26
+ this . contentBaseWatchers = [ ] ;
25
27
26
28
// Listening for events
27
29
var invalidPlugin = function ( ) {
@@ -269,6 +271,18 @@ function Server(compiler, options) {
269
271
}
270
272
} ,
271
273
274
+ watchContentBase : function ( ) {
275
+ if ( / ^ ( h t t p s ? : ) ? \/ \/ / . test ( contentBase ) || typeof contentBase === "number" ) {
276
+ throw new Error ( "Watching remote files is not supported." ) ;
277
+ } else if ( Array . isArray ( contentBase ) ) {
278
+ contentBase . forEach ( function ( item ) {
279
+ this . _watch ( item ) ;
280
+ } . bind ( this ) ) ;
281
+ } else {
282
+ this . _watch ( contentBase ) ;
283
+ }
284
+ } . bind ( this ) ,
285
+
272
286
middleware : function ( ) {
273
287
// include our middleware to ensure it is able to handle '/index.html' request after redirect
274
288
app . use ( this . middleware ) ;
@@ -293,6 +307,8 @@ function Server(compiler, options) {
293
307
defaultFeatures . push ( "proxy" , "middleware" ) ;
294
308
if ( contentBase !== false )
295
309
defaultFeatures . push ( "contentBaseFiles" ) ;
310
+ if ( options . watchContentBase )
311
+ defaultFeatures . push ( "watchContentBase" ) ;
296
312
if ( options . historyApiFallback )
297
313
defaultFeatures . push ( "historyApiFallback" , "middleware" ) ;
298
314
defaultFeatures . push ( "magicHtml" ) ;
@@ -396,6 +412,11 @@ Server.prototype.close = function(callback) {
396
412
this . listeningApp . close ( function ( ) {
397
413
this . middleware . close ( callback ) ;
398
414
} . bind ( this ) ) ;
415
+
416
+ this . contentBaseWatchers . forEach ( function ( watcher ) {
417
+ watcher . close ( ) ;
418
+ } ) ;
419
+ this . contentBaseWatchers = [ ] ;
399
420
}
400
421
401
422
Server . prototype . sockWrite = function ( sockets , type , data ) {
@@ -445,6 +466,14 @@ Server.prototype._sendStats = function(sockets, stats, force) {
445
466
this . sockWrite ( sockets , "ok" ) ;
446
467
}
447
468
469
+ Server . prototype . _watch = function ( path ) {
470
+ var watcher = chokidar . watch ( path ) . on ( "change" , function ( ) {
471
+ this . sockWrite ( this . sockets , "content-changed" ) ;
472
+ } . bind ( this ) )
473
+
474
+ this . contentBaseWatchers . push ( watcher ) ;
475
+ }
476
+
448
477
Server . prototype . invalidate = function ( ) {
449
478
if ( this . middleware ) this . middleware . invalidate ( ) ;
450
479
}
0 commit comments