I have a basic application using LESS and grunt watch to compile changes on the fly.
Since a very long time I was complaining about the fact that when I change any LESS file, all get re-compiled. It was such a waste of my time not to be able to compile only what was needed. But I wanted it smart, so if there were any import it would also compile the impacted files, not just the one that gets modified.
Today I decided to fix this. It's not easy at all. Grunt watch don't take care of this, even using grunt-newer didn't work out, in both case it is required to write custom rules.
Finally, I didn't use https://github.com/tschaub/grunt-newer because it doesn't play well with grunt-sync but more importantly, because it doesn't fit my needs with LESS, since it can't deal with the LESS import stuff and so it will not compile files related but only the files that have been changed, it's not "smart".
(It is possible to customize it to take into account that kind of stuff, and people provide some scripts on gist, but there isn't any official repo so it's kinda hard to find what you need, but definitely worth a look.)
It's been a question asked multiple time with no real or easy answer to. See:
How do you watch multiple files, but only run task on changed file, in Grunt.js?
Grunt watch: compile only one file not all
Grunt watch less on changed file only
Grunt: Watch multiple files, Compile only Changed
My solution is a custom solution, specific to my app, but I hope it can be used for yours too! It's pretty simple.
Assuming you have a
stylesfolder containing several files and directories (less), then my solution was to define a white list of folders for which I would not compile all less files, but only those that have been changed.Grunt-less config: https://gist.github.com/Vadorequest/bd46bb4d6c326e837710
Grunt-watch config: https://gist.github.com/Vadorequest/b48bcfda2d0205ba3f95
Folders architecture:
The "easiest" way to deal with this so far is to override the
grunt.watchevent and so, to check whether or not the changed file may impact other files. There are several ways depending on your own architecture. Mine is simple enough because files that impact all others are either at the root of thestylesfolder or inside sub folders. So I "just" had to check if thefilePathof the file belongs to the white list or not. If so, then I update the grunt config on the fly, changing the propertysrcto match only the changed file name.Basically, if I change a file named
ayolan.lesswithin thethemesdirectory then here is what will be set in the memory when I'll update it. (See https://gist.github.com/Vadorequest/b48bcfda2d0205ba3f95#file-watch-js-L80-L81)This allows me now to use http://www.browsersync.io/ that will update the browser about 1 second after I make a simple change in a LESS file, that was about 5 seconds before, because it had to compile all LESS files and copy them. (I made other performance changes as well, but it definitely helped to reach that goal!)