Created by: henrytao-me
Let's say I have a less
task and I want to add sourcemaps
depending on environment
.
less
task with no environment support:
This is the original gulp.task('less', function() {
gulp.src('./dev/src/**/*.less')
.pipe(sourcemaps.init())
.pipe(less())
.pipe(sourcemaps.write())
.pipe(gulp.dest('./.tmp'));
});
This is the common solution:
gulp.task('less', function() {
var res = gulp.src('./dev/src/**/*.less');
if (env.DEBUG === 'yes') {
res = res.pipe(sourcemaps.init());
}
res = res.pipe(less());
if (env.DEBUG === 'yes') {
res = res.pipe(sourcemaps.write());
}
res = res.pipe(gulp.dest('./.tmp'));
});
This is my proposed solution, more elegant (I think):
gulp.task('less', function() {
gulp.src('./dev/src/**/*.less')
.pipe(env.DEBUG === 'yes' ? sourcemaps.init() : null)
.pipe(less())
.pipe(env.DEBUG === 'yes' ? sourcemaps.write() : null)
.pipe(gulp.dest('./.tmp'));
});
Overall idea:
If you add pipe
like this:
gulp.task('less', function() {
gulp.src('./dev/src/**/*.less')
.pipe(null)
.pipe(less())
.pipe(null)
.pipe(gulp.dest('./.tmp'));
});
Then it will execute like this:
gulp.task('less', function() {
gulp.src('./dev/src/**/*.less')
.pipe(less())
.pipe(gulp.dest('./.tmp'));
});