@@ -14,7 +14,7 @@ function watchify (b, opts) {
14
14
var pkgcache = b . _options . packageCache ;
15
15
var changingDeps = { } ;
16
16
var pending = false ;
17
-
17
+
18
18
b . on ( 'dep' , function ( dep ) {
19
19
if ( typeof dep . id === 'string' ) {
20
20
cache [ dep . id ] = dep ;
@@ -23,25 +23,25 @@ function watchify (b, opts) {
23
23
watchFile ( dep . file ) ;
24
24
}
25
25
} ) ;
26
-
26
+
27
27
b . on ( 'file' , function ( file ) {
28
28
watchFile ( file ) ;
29
29
} ) ;
30
-
30
+
31
31
b . on ( 'package' , function ( pkg ) {
32
32
watchFile ( path . join ( pkg . __dirname , 'package.json' ) ) ;
33
33
} ) ;
34
-
34
+
35
35
b . on ( 'reset' , reset ) ;
36
36
reset ( ) ;
37
-
37
+
38
38
function reset ( ) {
39
39
var time = null ;
40
40
var bytes = 0 ;
41
41
b . pipeline . get ( 'record' ) . on ( 'end' , function ( ) {
42
42
time = Date . now ( ) ;
43
43
} ) ;
44
-
44
+
45
45
b . pipeline . get ( 'wrap' ) . push ( through ( write , end ) ) ;
46
46
function write ( buf , enc , next ) {
47
47
bytes += buf . length ;
@@ -58,28 +58,50 @@ function watchify (b, opts) {
58
58
this . push ( null ) ;
59
59
}
60
60
}
61
-
61
+
62
62
var fwatchers = { } ;
63
63
var fwatcherFiles = { } ;
64
-
64
+
65
+ if ( opts . glob ) {
66
+ var w = chokidar . watch ( opts . glob , { ignoreInitial : true , persistent : true } ) ;
67
+ w . setMaxListeners ( 0 ) ;
68
+ w . on ( 'error' , b . emit . bind ( b , 'error' ) ) ;
69
+ w . on ( 'add' , function ( file ) {
70
+ b . _options . entries . push ( file ) ;
71
+ b . constructor . call ( b , b . _options ) ;
72
+ invalidate ( file ) ;
73
+ } ) ;
74
+ w . on ( 'unlink' , function ( file ) {
75
+ var i = b . _options . entries . map ( function ( str ) {
76
+ return path . resolve ( str ) ;
77
+ } ) . indexOf ( path . resolve ( file ) ) ;
78
+ if ( i === - 1 )
79
+ return ;
80
+ b . _options . entries . splice ( i , 1 ) ;
81
+ b . constructor . call ( b , b . _options ) ;
82
+ invalidate ( file ) ;
83
+ } ) ;
84
+ fwatchers . glob = w ;
85
+ }
86
+
65
87
b . on ( 'transform' , function ( tr , mfile ) {
66
88
tr . on ( 'file' , function ( file ) {
67
89
watchDepFile ( mfile , file ) ;
68
90
} ) ;
69
91
} ) ;
70
-
92
+
71
93
function watchFile ( file ) {
72
94
fs . lstat ( file , function ( err , stat ) {
73
95
if ( err || stat . isDirectory ( ) ) return ;
74
96
watchFile_ ( file ) ;
75
97
} ) ;
76
98
}
77
-
99
+
78
100
function watchFile_ ( file ) {
79
101
if ( ! fwatchers [ file ] ) fwatchers [ file ] = [ ] ;
80
102
if ( ! fwatcherFiles [ file ] ) fwatcherFiles [ file ] = [ ] ;
81
103
if ( fwatcherFiles [ file ] . indexOf ( file ) >= 0 ) return ;
82
-
104
+
83
105
var w = chokidar . watch ( file , { persistent : true } ) ;
84
106
w . setMaxListeners ( 0 ) ;
85
107
w . on ( 'error' , b . emit . bind ( b , 'error' ) ) ;
@@ -89,7 +111,7 @@ function watchify (b, opts) {
89
111
fwatchers [ file ] . push ( w ) ;
90
112
fwatcherFiles [ file ] . push ( file ) ;
91
113
}
92
-
114
+
93
115
function watchDepFile ( mfile , file ) {
94
116
if ( ! fwatchers [ mfile ] ) fwatchers [ mfile ] = [ ] ;
95
117
if ( ! fwatcherFiles [ mfile ] ) fwatcherFiles [ mfile ] = [ ] ;
@@ -104,7 +126,7 @@ function watchify (b, opts) {
104
126
fwatchers [ mfile ] . push ( w ) ;
105
127
fwatcherFiles [ mfile ] . push ( file ) ;
106
128
}
107
-
129
+
108
130
function invalidate ( id ) {
109
131
if ( cache ) delete cache [ id ] ;
110
132
if ( fwatchers [ id ] ) {
@@ -115,22 +137,22 @@ function watchify (b, opts) {
115
137
delete fwatcherFiles [ id ] ;
116
138
}
117
139
changingDeps [ id ] = true
118
-
140
+
119
141
// wait for the disk/editor to quiet down first:
120
142
if ( ! pending ) setTimeout ( function ( ) {
121
143
pending = false ;
122
144
b . emit ( 'update' , Object . keys ( changingDeps ) ) ;
123
145
changingDeps = { } ;
124
-
146
+
125
147
} , opts . delay || 600 ) ;
126
148
pending = true ;
127
149
}
128
-
150
+
129
151
b . close = function ( ) {
130
152
Object . keys ( fwatchers ) . forEach ( function ( id ) {
131
153
fwatchers [ id ] . forEach ( function ( w ) { w . close ( ) } ) ;
132
154
} ) ;
133
155
} ;
134
-
156
+
135
157
return b ;
136
158
}
0 commit comments