1+ /*
2+ MIT License http://www.opensource.org/licenses/mit-license.php
3+ Author Tobias Koppers @sokra
4+ */
5+ var Watchpack = require ( "watchpack" ) ;
6+
7+ function NewWatchingPlugin ( ) {
8+ }
9+ module . exports = NewWatchingPlugin ;
10+
11+ NewWatchingPlugin . prototype . apply = function ( compiler ) {
12+ compiler . plugin ( "environment" , function ( ) {
13+ compiler . watchFileSystem = new NodeWatchFileSystem ( compiler . inputFileSystem ) ;
14+ } ) ;
15+ } ;
16+
17+ function NodeWatchFileSystem ( inputFileSystem ) {
18+ this . inputFileSystem = inputFileSystem ;
19+ this . watcherOptions = {
20+ aggregateTimeout : 0
21+ } ;
22+ this . watcher = new Watchpack ( this . watcherOptions ) ;
23+ }
24+
25+ NodeWatchFileSystem . prototype . watch = function watch ( files , dirs , startTime , delay , callback , callbackUndelayed ) {
26+ var oldWatcher = this . watcher ;
27+ this . watcher = new Watchpack ( {
28+ aggregateTimeout : delay
29+ } ) ;
30+
31+ var onChange = function ( ) {
32+ callbackUndelayed ( ) ;
33+ } . bind ( this ) ;
34+ this . watcher . once ( "change" , onChange ) ;
35+ this . watcher . once ( "aggregated" , function ( changes ) {
36+ if ( this . inputFileSystem && this . inputFileSystem . purge ) {
37+ this . inputFileSystem . purge ( changes ) ;
38+ }
39+ var times = this . watcher . getTimes ( ) ;
40+ callback ( null , changes . filter ( function ( file ) {
41+ return files . indexOf ( file ) >= 0 ;
42+ } ) . sort ( ) , changes . filter ( function ( file ) {
43+ return dirs . indexOf ( file ) >= 0 ;
44+ } ) . sort ( ) , times , times ) ;
45+ } . bind ( this ) ) ;
46+
47+ this . watcher . watch ( files , dirs , startTime ) ;
48+
49+ if ( oldWatcher ) {
50+ oldWatcher . close ( ) ;
51+ }
52+ return {
53+ close : function ( ) {
54+ this . watcher . pause ( ) ;
55+ } . bind ( this )
56+ }
57+ } ;
0 commit comments