Skip to content

Commit 82a28a0

Browse files
committed
add support for watching multiple entry points
note that this requires passing `this.from` to `watchCSS` - though that's optional and thus backwards-compatible, i.e. existing users with only a single entry point don't need to update their configuration
1 parent dafdb9b commit 82a28a0

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ into account:
6262
{
6363
"postcss-import": {
6464
onImport: function(sources) {
65-
global.watchCSS(sources);
65+
global.watchCSS(sources, this.from);
6666
}
6767
}
6868
}

index.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,39 @@ var processor = postcss(plugins);
117117
// hook for dynamically updating the list of watched files
118118
global.watchCSS = function() {};
119119
if (argv.watch) {
120-
var watchedFiles = inputFiles;
120+
global.watchCSS = fsWatcher(inputFiles);
121+
}
122+
123+
async.forEach(inputFiles, compile, onError);
124+
125+
function fsWatcher(entryPoints) {
126+
var watchedFiles = entryPoints;
127+
var index = {}; // source files by entry point
128+
121129
var watcher = require('chokidar').watch(watchedFiles);
122-
watcher.on('change', function() { // TODO: support for "add", "unlink" etc.?
123-
async.forEach(inputFiles, compile, function(err) {
130+
// recompile if any watched file is modified
131+
// TODO: only recompile relevant entry point
132+
watcher.on('change', function() {
133+
async.forEach(entryPoints, compile, function(err) {
124134
return onError.call(this, err, true);
125135
});
126136
});
127137

128-
global.watchCSS = function(files) {
138+
return function updateWatchedFiles(files, entryPoint) {
139+
// update source files for current entry point
140+
entryPoint = entryPoint || null;
141+
index[entryPoint] = files;
142+
// aggregate source files across entry points
143+
var entryPoints = Object.keys(index);
144+
var sources = entryPoints.reduce(function(files, entryPoint) {
145+
return files.concat(index[entryPoint]);
146+
}, []);
147+
// update watch list
129148
watcher.unwatch(watchedFiles);
130-
watcher.add(files);
131-
watchedFiles = files;
149+
watcher.add(sources);
150+
watchedFiles = sources;
132151
};
133152
}
134-
async.forEach(inputFiles, compile, onError);
135153

136154
function compile(input, fn) {
137155
var output = argv.output;

0 commit comments

Comments
 (0)