Skip to content

Instantly share code, notes, and snippets.

@tvaliasek
Created January 15, 2019 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tvaliasek/c96a85a23588244b2aa04671ff25211d to your computer and use it in GitHub Desktop.
Save tvaliasek/c96a85a23588244b2aa04671ff25211d to your computer and use it in GitHub Desktop.
Gulp 4 - PostCSS wait for SASS compiler to write output files (File not found with singular glob)
/*
After migration to Gulp 4 and rewrite of gulpfile I have found out that gulp-sass plugin cannot
write compiled css files to disk before his task is resolved, even if the task is run in series()
call, causing unmatched glob pattern error. Usage of src(globs, { allowEmpty: true }) is not a
solution, because task must be run twice in order to output correct file.
..after few hours of despair and googling
*/
const { series, dest, src } = require('gulp')
const sass = require('gulp-sass')
sass.compiler = require('node-sass')
const postcss = require('gulp-postcss')
const rename = require('gulp-rename')
// file with options for plugins, globs etc.
const options = require('./build.options.js')
function sassTask(cb) {
src(['./some/file.scss'])
.pipe(
sass.sync(options.sass)
.on('error', sass.logError)
)
.pipe(postcss(options.postcss.plugins))
.pipe(dest('./www/css'))
// add callback for end event instead of calling cb() on last line of function
.on('end', () => {
cb()
})
}
function postCSSTask(cb) {
// now destination file is written, so glob is matched
src(['./www/css/some/file.css'])
.pipe(postcss(options.postcssMin.plugins))
.pipe(rename(options.rename))
.pipe(dest('./www/css'))
cb()
}
exports.default = series(sassTask, postCSSTask)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment