I have a gulp css task that picks up a CSS file and runs several postCSS processors on it, then writes the file to a destination directory.
I have an html task that uses gulp-smoosher to pull the CSS file into the HTML file, replacing the link
tag to the CSS file.
When I run the tasks separately from the command line, everything works as expected. However, when I use gulp.watch
to automatically run the tasks when the CSS file changes, the changes aren't reflected in the final HTML file.
Is the html task somehow picking up the CSS file before the css task writes the changes? Is there a way to make sure the css task has finished before running the html task?
Update: I've done some reading and I realize Gulp runs both my css and html tasks at the same time. That explains why the CSS file isn't written yet when I start the html task. I've seen some solutions, but they either don't work or I don't understand how to use them. Here's my attempt at using the run-sequence plugin:
gulp.task('csshtml', function() {
runSequence('css', 'html');
});
gulp.task('watch', function() {
gulp.watch(paths.css, ['csshtml']);
});
... but the results were the same. I'm sure I'm doing something wrong.
gulpfile.js:
var gulp = require('gulp');
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer');
var cssnext = require('cssnext');
var precss = require('precss');
var nesting = require('postcss-nesting');
var cssnano = require('cssnano');
var htmlmin = require('gulp-htmlmin');
var smoosher = require('gulp-smoosher');
var paths = {
css: 'src/*.css',
html: 'src/*.html'
};
gulp.task('css', function() {
var processors = [
nesting,
autoprefixer,
cssnext,
precss,
cssnano
];
return gulp.src(paths.css)
.pipe(postcss(processors))
.pipe(gulp.dest('css'));
});
gulp.task('html', function() {
return gulp.src('src/*.html')
.pipe(smoosher({ base: '.' }))
.pipe(htmlmin({
collapseWhitespace: true,
conservativeCollapse: true,
removeComments: true,
collapseInlineTagWhitespace: true,
collapseBooleanAttributes: true,
removeAttributeQuotes: true,
removeRedundantAttributes: true,
removeEmptyAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
removeOptionalTags: true,
minifyCSS: true
}))
.pipe(gulp.dest('.'))
});
gulp.task('watch', function(){
gulp.watch(paths.css, ['css', 'html']);
gulp.watch(paths.html, ['html']);
});