pirxpilot / postcss-cli

Simple CLI for postcss
174 stars 17 forks source link

auto-recompiling (-w|--watch option) #19

Closed FND closed 9 years ago

FND commented 9 years ago

I'm not sure this is within postcss-cli's purview¹, but it would be very useful to have a --watch option to observe filesystem changes and automatically recompile sources when necessary

this would be also consistent with similar tools (e.g. Sass or Less, but also webpack, Babel etc.)

¹ it seems like most people currently use gulp for this, but I'd rather avoid that

pirxpilot commented 9 years ago

I am more in one tool for one thing camp. Instead of making each and every build related tool solving this particular problem in its own, usually less than perfect way, I'd rather use watch and make combination.

That said, I am probably in minority on that. So if this has a lot of fans and someone can implement it cleanly without adding too much cruft to the basic usage path I would probably merge it.

FND commented 9 years ago

I'd also very much prefer using a separate tool - however, I have yet to find a satisfactory solution (other than Grunt/gulp/...): Having watch periodically invoke make seems nice, but potentially troublesome¹ ². I've previously used inotify directly, but that's neither portable nor trivial to set up.

So I'll look into integrating chokidar (which seems to be the library of choice), hopefully that won't require any invasive adjustments.

Aside: Presumably incremental recompilation (as opposed to compiling all sources from scratch every time) is not worth the trouble with PostCSS? I don't currently have a good sample to test performance on large PostCSS code bases.

¹ assuming a 1s interval, you'll often end up with a race condition where you refresh the browser before things have been recompiled, resulting in subtle confusion and uncertainty - at least that was my experience with webpack's 300ms delay

² also, for whatever reason, there's a lot of resistance to using make (at least I've never had much success convincing anyone) sigh

FND commented 9 years ago

I made a start in FND/postcss-cli@9d891d3a7deafcf6ef3eeb18e791a4458a5ad608 (preceded by minor refactoring).

It should be possible to add support for imports by hooking into postcss-import's onimport (via the config).

Do you think this is worth pursuing?

pirxpilot commented 9 years ago

That looks better than I feared it would ;-)

FND commented 9 years ago

heh - I'll submit a pull request when it seems ready

VinSpee commented 9 years ago

looking forward to this. I'd love to have a watch tool that makes it easy to universally watch in npm scripts, but I also have not found a decent solution to this. How is everyone handling it now?

FND commented 9 years ago

@VinSpee If you wanna give it a try, I've left instructions in the pull request (#22) - would be great to get some feedback from another user's perspective.

As mentioned in the OP, I believe most people currently use gulp for this sort of thing.

pirxpilot commented 9 years ago

Spent some time on it preparing for the merge: everything looks good too me. I do have a question - which might just be a result of my ignorance - please bear with me. --watch|-w option only seems to work when postcss-import is used - otherwise postcss-cli compiles files once and exits. In other words:

postcss --watch -c test/config-watch.js  # this keeps recompiling sources
postcss --watch -u postcss-url -c test/config.json -o config.css test/in.css  # this compiles once and exits

Is that the intention? I am sort of OK with that - since without postcss-import plugin we cannot catch changes in imported files anyway. But I am kind of worried we'll get pelted with --watch does not work issues.

Would it be better if we did support --watch in all cases. Or maybe we should add a warning/error if someone is using --watch when we don't support it? Or maybe I am just missing something obvious.

Other things:

pirxpilot commented 9 years ago

Merged and released as 1.5.0 Thank you!