@@ -69,6 +69,18 @@ if (!Array.isArray(argv.use)) {
6969 argv . use = [ argv . use ] ;
7070}
7171
72+ // support for postcss-import
73+ if ( argv . use . indexOf ( "postcss-import" ) !== - 1 ) {
74+ var importConfig = argv [ "postcss-import" ] || { } ;
75+ argv [ "postcss-import" ] = importConfig ;
76+ // auto-configure watch update hook
77+ if ( ! importConfig . onImport ) {
78+ importConfig . onImport = function ( sources ) {
79+ global . watchCSS ( sources , this . from ) ;
80+ } ;
81+ }
82+ }
83+
7284var inputFiles = globby . sync ( argv . _ ) ;
7385if ( ! inputFiles . length ) {
7486 if ( argv . input ) {
@@ -117,21 +129,39 @@ var processor = postcss(plugins);
117129// hook for dynamically updating the list of watched files
118130global . watchCSS = function ( ) { } ;
119131if ( argv . watch ) {
120- var watchedFiles = inputFiles ;
132+ global . watchCSS = fsWatcher ( inputFiles ) ;
133+ }
134+
135+ async . forEach ( inputFiles , compile , onError ) ;
136+
137+ function fsWatcher ( entryPoints ) {
138+ var watchedFiles = entryPoints ;
139+ var index = { } ; // source files by entry point
140+
121141 var watcher = require ( 'chokidar' ) . watch ( watchedFiles ) ;
122- watcher . on ( 'change' , function ( ) { // TODO: support for "add", "unlink" etc.?
123- async . forEach ( inputFiles , compile , function ( err ) {
142+ // recompile if any watched file is modified
143+ // TODO: only recompile relevant entry point
144+ watcher . on ( 'change' , function ( ) {
145+ async . forEach ( entryPoints , compile , function ( err ) {
124146 return onError . call ( this , err , true ) ;
125147 } ) ;
126148 } ) ;
127149
128- global . watchCSS = function ( files ) {
150+ return function updateWatchedFiles ( files , entryPoint ) {
151+ // update source files for current entry point
152+ entryPoint = entryPoint || null ;
153+ index [ entryPoint ] = files ;
154+ // aggregate source files across entry points
155+ var entryPoints = Object . keys ( index ) ;
156+ var sources = entryPoints . reduce ( function ( files , entryPoint ) {
157+ return files . concat ( index [ entryPoint ] ) ;
158+ } , [ ] ) ;
159+ // update watch list
129160 watcher . unwatch ( watchedFiles ) ;
130- watcher . add ( files ) ;
131- watchedFiles = files ;
161+ watcher . add ( sources ) ;
162+ watchedFiles = sources ;
132163 } ;
133164}
134- async . forEach ( inputFiles , compile , onError ) ;
135165
136166function compile ( input , fn ) {
137167 var output = argv . output ;
0 commit comments