As JeffryHouser stated above using the following method works but is deprecated and will be removed in future versions. I just attempted the following on 3.9.1 and it works fine.
Code:
// Variables
var gulp = require('gulp');
var less = require('gulp-less');
var watch = require('gulp-watch');
// Task to compile less -> css: Method 1- The bad way
gulp.task('default', function(){
gulp.src('src/*.less')
gulp.start('lessToCss');
});
gulp.task('lessToCss', function() {
gulp.src('src/*.less')
.pipe(less())
.pipe(gulp.dest('src/main.css'));
});
The second way mentioned in the comment by Novellizator is:
"Break tasks out into functions, then reuse them in other tasks if you need to" piece of advice from the aforementioned issue
I do this by use of lazypipe() https://www.npmjs.com/package/lazypipe
Code:
// Variables
var gulp = require('gulp');
var less = require('gulp-less');
var watch = require('gulp-watch');
var lazypipe = require('lazypipe');
var fixMyCSS = lazypipe()
.pipe(less); // Be CAREFUL here do not use less() it will give you an errror
// Task to compile less -> css: Method 2 - The nice way
gulp.task('default', function(){
//gulp.start('lessToCss');
gulp.src('src/*.less')
.pipe(fixMyCSS())
.pipe(gulp.dest('src/main.css'));
});
A brief comparison
Method 1 yielding the following:
- Starting 'default'...
- [22:59:12] Starting 'lessToCss'...
- [22:59:12] Finished 'lessToCss' after 13 ms
- [22:59:12] Finished 'default' after 48 ms
For a total of 61 ms to complete
Method 2?
- [23:03:24] Starting 'default'...
- [23:03:24] Finished 'default' after 38 ms
Practically half that at 38 ms
For a total difference of 23 ms
Now why this is the case? I honestly don't know enough about gulp to say, but let's say method 2 is clearly the more readable,maintainable and even the faster choice.
They are both easy to write so long as you understand not to call a stream directly within the lazypipe()
.