I try to DRY my gulpfile. There I have small duplication of code I not comfortable with. How can this be made better?
gulp.task('scripts', function() {
return gulp.src('src/scripts/**/*.coffee')
.pipe(coffeelint())
.pipe(coffeelint.reporter())
.pipe(coffee())
.pipe(gulp.dest('dist/scripts/'))
.pipe(gulp.src('src/index.html')) // this
.pipe(includeSource()) // needs
.pipe(gulp.dest('dist/')) // DRY
});
gulp.task('index', function() {
return gulp.src('src/index.html')
.pipe(includeSource())
.pipe(gulp.dest('dist/'))
});
I got index
as a separate task, since I need to watch src/index.html
to livereload. But I'm also watching my .coffee
sources and when they change, I need to update src/index.html
as well.
How can I pipe to index
in scripts
?
gulp
enables you to order a series of tasks based on arguments.
Example:
gulp.task('second', ['first'], function() {
// this occurs after 'first' finishes
});
Try the following code, you will be running the task 'index' to run both tasks:
gulp.task('scripts', function() {
return gulp.src('src/scripts/**/*.coffee')
.pipe(coffeelint())
.pipe(coffeelint.reporter())
.pipe(coffee())
.pipe(gulp.dest('dist/scripts/'));
});
gulp.task('index', ['scripts'], function() {
return gulp.src('src/index.html')
.pipe(includeSource())
.pipe(gulp.dest('dist/'))
});
The task index
will now require scripts
to be finished before it runs the code inside it's function.