2

So, I've come across a problem where running my gulp watch task breaks my build, however; this only occurs when I edit the .scss file and gulp detects the change.

For example, the styles have been applied correctly to a specific document on my website. I start watching my assets by invoking gulp. I run gulp to invoke the default task (which builds everything correctly, concatenates my assets, etc.)

This doesn't break my production file. It isn't until I edit the file, (and gulp detects that and runs the necessary tasks again) this occurs.

This is my gulpfile

//Node dependencies 
var gulp = require('gulp'),
    minifyCSS = require('gulp-clean-css'),
    minifyJS = require('gulp-uglify'),
    rename = require('gulp-rename'),
    sass = require('gulp-sass'),
    pump = require('pump'),
    sync = require('run-sequence');

//Sass transpilation task
gulp.task('sass', function(cb) {
  pump([
    gulp.src('app/scss/*.scss'),
    sass(),
    gulp.dest('app/css')
  ], cb);
});

//Uglify CSS task
gulp.task('ugCSS', function(cb) {
  pump([
    gulp.src('app/css/*.css'),
    minifyCSS(),
    rename({
      suffix: ".min"
    }),
    gulp.dest('dist/css')
  ], cb);
});

//Uglify JS task
gulp.task('ugJS', function(cb) {
  pump([
    gulp.src('app/js/*.js'),
    minifyJS(),
    rename({
      suffix: ".min"
    }),
    gulp.dest('dist/js')
  ], cb);
});

//Watch task
gulp.task('watch', function() {
  return gulp.watch('app/scss/*.scss', ['default']);
});

//Default task to make sure everythings runs syncronously
gulp.task('default', function(callback) {
  sync('sass', 'ugCSS', 'ugJS', 'watch', callback);
});

If what I've said hasn't made sense, I'll systematically list how I invoke this problem.

  1. gulp: this runs the default task (this doesnt break styling)
  2. edit .scss file in ide: I want to make a change to my styling, so I edit the file, the gulp watch task detects this and returns the 'default' task again, starting the cycle all over again.
  3. open browser and navigate to site to see applied styling: this is where it breaks, all of my styling has been removed, the 'gulp.dest' file for my css (that I've uglified and passed into the distribution folder) is blank.
  4. gulp: cancelling previous gulp process and running it again fixes the problem. Navigating back to css file in /dist - all the styles are there.

Long story short, my styles are ONLY removed when I 'actively' edit the .scss file and the gulp watch task invokes the necessary tasks to build the file into the production folder, so the relevant documents can see those changes.

Nhan
  • 3,595
  • 6
  • 30
  • 38
  • instead of the 'default' task in watch do a test and run only 'sass' job to see if correctly compiles the styles – emvidi Nov 29 '16 at 09:24
  • Tried that, same thing. If I run the sass task on its own as in gulp sass - that works fine. If I run the watch task and the watch task detects a change and then runs the sass task, that's where it breaks. – Bravo Whiskey Nov 29 '16 at 09:32
  • I am facing a similar situation and I think the problem lies in the tasks not being done in sequential order. Pretty sure 'ugCSS' runs before 'sass' has done its job. – emvidi Nov 29 '16 at 09:59
  • Well, that's why I included the 'runSequence' module, so I could specify which order the tasks would be executed. Watching gulp output the tasks that have 'started and finished' - it appears to run in correct order. – Bravo Whiskey Nov 29 '16 at 10:02
  • I recommend you don't call default with the watch as the default starts the watch and when you make a change you're repeatedly calling it. Instead just list what tasks you want, ie. ['sass', 'ugcss', ect..] . Just a quick note to make writing in gulp less prone to errors. In addition I suggest you use gulp-plumber in all your gulp tasks so your streams do not break on an error. Quick note: you don't need to include function(cb) and return cb in every task, you just need that with use of run-sequence, other tasks can just be function(). Best – CriCri Nov 29 '16 at 17:28

1 Answers1

1

Being myself a newbie with gulp and having a similar issue which I solved by looking at: How to run Gulp tasks sequentially one after the other, here is something you could try:

//Sass transpilation task
gulp.task('sass', function() {
  return gulp.src('app/scss/*.scss') // return solved my problem
             .pipe(sass()/*.on('error', sass.logError)*/) // optional on error
             .pipe(dest('app/css'));
});

//Uglify CSS task
gulp.task('ugCSS', function() {
  return gulp.src('app/css/*.css')
             .pipe(minifyCSS())
             .pipe(rename({suffix: ".min"})
             .pipe(dest('dist/css'));
});

//Uglify JS task
gulp.task('ugJS', function() {
  return gulp.src('app/js/*.js')
             .pipe(minifyJS())
             .pipe(rename({suffix: ".min"})
             .pipe(dest('dist/js'));
});

//Watch task
gulp.task('watch', function() {
  return gulp.watch('app/scss/*.scss', ['default']);
});

//Default task to make sure everythings runs syncronously
gulp.task('default', function() {
  sync('sass', 'ugCSS', 'ugJS');
});
Community
  • 1
  • 1
emvidi
  • 1,210
  • 2
  • 14
  • 18
  • This doesn't fix my problem. I have tried to return chained methods too, but that doesn't work. Also, I have removed synchronous execution as being a problem by using gulps deps argument they provide on the task method. https://github.com/gulpjs/gulp/blob/master/docs/API.md#deps Thanks for the suggestions though! – Bravo Whiskey Nov 29 '16 at 10:42
  • Sorry if it does not work, just posted what worked for me – emvidi Nov 29 '16 at 10:46