Skip to content

Commit d96cea7

Browse files
committed
Merge remote-tracking branch 'FND/multiwatch'
Fixes postcss#33
2 parents dafdb9b + 9265924 commit d96cea7

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

Readme.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,21 @@ globally it will only look for plugins that are globally installed.
5454

5555
Observe file system changes and recompile as source files change.
5656

57-
When inlining CSS imports (e.g. with [postcss-import]), add an update handler
58-
to your JavaScript configuration file to ensure referenced modules are taken
59-
into account:
57+
When inlining CSS imports, add an update handler to your JavaScript
58+
configuration file to ensure referenced modules are taken into account:
6059

6160
```js
6261
{
6362
"postcss-import": {
6463
onImport: function(sources) {
65-
global.watchCSS(sources);
64+
global.watchCSS(sources, this.from);
6665
}
6766
}
6867
}
6968
```
7069

70+
For [postcss-import], this handler is added automatically.
71+
7172
#### `--config|-c`
7273

7374
JSON file with plugin configuration. Plugin names should be the keys.

index.js

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
7284
var inputFiles = globby.sync(argv._);
7385
if (!inputFiles.length) {
7486
if (argv.input) {
@@ -117,21 +129,39 @@ var processor = postcss(plugins);
117129
// hook for dynamically updating the list of watched files
118130
global.watchCSS = function() {};
119131
if (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

136166
function compile(input, fn) {
137167
var output = argv.output;

0 commit comments

Comments
 (0)