1

looking to watch all the less files in the root gulp project directory and then compile only the saved LESS file. I can't get past the first part.

When I run $ gulp watch in the project directory it hangs. Starts the task and never finishes.

var gulp = require('gulp');
var less = require('gulp-less');
var path = require('path');

gulp.task('compileCurrentTheme', function () {
 return gulp.src('./*.less')
 .pipe(less({
  compress: true
 }))
 .pipe(gulp.dest('./'));
});

// Gulp watch functions
gulp.task('watch', function(){
 gulp.watch('*.less', ['compileCurrentTheme']); 
}); 
Justin W Hall
  • 371
  • 1
  • 5
  • 21

1 Answers1

1

You made some mistake

gulp.task('compileCurrentTheme', function () {
  return gulp.src('./*.less') // ./ -> root directory
    .pipe(less())
    .pipe(gulp.dest('yourPath')); // you can also type ./ it will create .css files in your root directory
});

// Gulp watch functions
gulp.task('watch:less', function(){
  gulp.watch('./*.less', ['compileCurrentTheme']); 
});

Rename your watch task to watch:less and then run it from your console gulp watch:less

Please look at gulp-less and gulp especially gulp-watch just to make your understanding a bit clearly.

I hope it will help you

Thanks

The Reason
  • 7,705
  • 4
  • 24
  • 42
  • Updated my code to reflect your suggestion. Still hangs. [14:48:16] Using gulpfile ~/Sites/gdw/thedentalsitecontent/themes/gulpfile.js [14:48:16] Starting 'watch'... – Justin W Hall Mar 10 '16 at 21:51
  • @JustinWHall Does it re-run `compileCurrentTheme` task when you press save? *Starts the task and never finishes* - because gulp is waiting for your changes that's why it doesnt finished. This is normal behavior – The Reason Mar 10 '16 at 22:02
  • Actually, this works. It appears gulp is getting hung up on something else in that directory. – Justin W Hall Mar 10 '16 at 22:11
  • No. I can run the **compileCurrentTheme** task manually. The **watch** task hangs. – Justin W Hall Mar 10 '16 at 22:12
  • Actually, I was incorrect before. It works. The watch task just takes a really long time to run. There are a few hundred directories in the root of this project. Is there a way to exclude them from the watch so this executes faster? So just watch the root directory? – Justin W Hall Mar 10 '16 at 22:27
  • @JustinWHall yeap [here](http://stackoverflow.com/questions/23384239/excluding-files-directories-from-gulp-task) is the link – The Reason Mar 10 '16 at 22:31
  • This doesn't seem to have an effect on the watch task time. `gulp.watch(['*.less', '!./*' ], ['compileCurrentTheme']); ` – Justin W Hall Mar 10 '16 at 22:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/105951/discussion-between-the-and-justin-w-hall). – The Reason Mar 10 '16 at 22:38