Alex Alex - 7 months ago 67
Node.js Question

gulp-inject not working with gulp-watch

I am using gulp-inject to auto add SASS imports as they are newly created in my project. This works fine, the new SASS files are imported correctly, however when an already imported file is deleted whilst gulp-watch is running it crashes with the following error:

Error: _dev\style\style.scss
Error: File to import not found or unreadable: components/_test - Copy.scss
Parent style sheet: stdin
on line 2 of stdin
>> @import "components/_test - Copy.scss";

I have spent a good few hours trying to work out why it tries to compile an older version of the stylesheet with out of date imports. I set a delay on the SASS task and the imports are correct in the actual file by the time the gulp-sass runs. I have read that gulp-watch may be caching the stylehseet, but really not sure.

Below are the relevant bits of my Gulp file, at the bottom is a link to my full gulp file.

Here are my watch tasks:
(The components task triggers the SASS task)

// SASS['/**/*.scss',!'/style/components/**/*.scss'], function () {gulp.start(gulpsync.sync([
// COMPONENTS'/style/components/**/*.scss', function () {gulp.start(gulpsync.sync([

SASS task

gulp.task('build-sass', function() {
return gulp.src('/style/style.scss')
.pipe(plugins.sass({ noCache: true }))

Inject task

gulp.task('inject-deps', function() {
// Auto inject SASS
.pipe(plugins.inject(gulp.src('components/**/*.scss', {read: false,'/style/'}), {
relative: true,
starttag: '/* inject:imports */',
endtag: '/* endinject */',
transform: function (filepath) {
return '@import "' + filepath + '";';


As requested here is the SASS file with the imports, it works fine as long as the watch task isn't running, the imported files contain no CSS:


/* inject:imports */
@import "components/_test.scss";
@import "components/_test-copy.scss";
/* endinject */


/* inject:imports */
@import "components/_test.scss";
/* endinject */


Your build-sass task starts whenever you delete a file in style/components/. By that time the inject-deps task hasn't run yet, so the corresponding @import hasn't been deleted yet.

The reason this happens is because of this line:['/**/*.scss',!'/style/components/**/*.scss']

You're not actually creating a string with a ! at the beginning here. You're negating the string which evaluates to false (hurray JavaScript!). So your path ends up being 'false_dev/style/components/**/*.scss'

It should be this instead:['/**/*.scss','!' +'/style/components/**/*.scss']

(Don't worry about it. Took me longer to figure out than it should have, too...)