0

For a while now i have a very strange problem with Gulp and BrowserSync. Everytime i change the css (sometimes even the HTML) i realized that Gulp updates the browser even when i have not saved neither finished the changes resulting in console errors everytime i'm changing specially my CSS files but also the HTML, i have been working for a while now with this gulp.js configuration but it have slowly come to a point where is impossible to work fluently.

Also every now and then but frequently tho, im getting SASS importing errors when i'm importing everything correctly on my style.scss file.

Maybe is the way im using / calling BrowserSync related to the moment im calling the watching functions ? or is it just a bug related to the version of Gulp i'm using. Any help will be really appreciated.

Here's my current gulp.js file:

var banner = [
  '/*!\n' +
  ' * <%= package.name %>\n' +
  ' * <%= package.title %>\n' +
  ' * <%= package.url %>\n' +
  ' * @author <%= package.author %>\n' +
  ' * @version <%= package.version %>\n' +
  ' * Copyright ' + new Date().getFullYear() + '. <%= package.license %> licensed.\n' +
  ' */',
  '\n'
].join('');
var config = {
  bowerDir: './bower_components'
};
gulp.task('bower', function () {
  return gulp.src(mainBowerFiles('**/*.png', {includeDev:true}), { base: config.bowerDir } )
  .pipe(bowerNormalize({ bowerJson: './bower.json'  }))
  .pipe(gulp.dest('app/assets/img'));
});
gulp.task('icons', function() {
    return gulp.src(config.bowerDir+'/font-awesome/fonts/**.*')
    .pipe(gulp.dest('app/assets/fonts'));
});
gulp.task('add-components', function() {
  return gulp.src([
    config.bowerDir+'/modernizr/bin/modernizr.js',
    config.bowerDir+'/jquery/dist/jquery.min.js',
    config.bowerDir+'/bootstrap-sass/assets/javascripts/bootstrap/button.js',
    config.bowerDir+'/bootstrap-sass/assets/javascripts/bootstrap/transition.js',         
    config.bowerDir+'/bootstrap-sass/assets/javascripts/bootstrap/carousel.js',
    config.bowerDir+'/bootstrap-sass/assets/javascripts/bootstrap/dropdown.js',
  ])
  .pipe(concat('app.js'))    
  .pipe(gulp.dest('app/assets/js'));    
});
gulp.task('css', function () {
  var processors = [ autoprefixer, cssnext, precss, atImport, mqpacker, cssnano, fontmagician ];    
  return gulp.src('src/scss/style.scss')
  .pipe(sass({
    style: 'compressed',
    includePaths: [
      config.bowerDir + '/font-awesome/scss',
      config.bowerDir + '/bootstrap-sass/assets/stylesheets',
    ]  
  }))
  .pipe(postcss(processors))
  .pipe(gulp.dest('app/assets/css'))
  .pipe(cleanCSS())
  .pipe(rename({ suffix: '.min' }))
  .pipe(header(banner, { package : package }))
  .pipe(gulp.dest('app/assets/css'))
  .pipe(browserSync.reload({stream:true}));
});
gulp.task('js',function(){
  return gulp.src('src/js/scripts.js')
  .pipe(jshint('.jshintrc'))
  .pipe(jshint.reporter('default'))
  .pipe(header(banner, { package : package }))
  .pipe(gulp.dest('app/assets/js'))
  .pipe(uglify())
  .pipe(header(banner, { package : package }))
  .pipe(rename({ suffix: '.min' }))
  .pipe(gulp.dest('app/assets/js'))
  .pipe(browserSync.reload({stream:true, once: true}));
});
gulp.task('browser-sync', function() {
    browserSync.init(null, {
        server: {
            baseDir: "app"
        }
    });
});
gulp.task('bs-reload', function () {
    browserSync.reload();
});
gulp.task('default', ['bower','add-components','css','js', 'icons', 'browser-sync'], function () {
    gulp.watch("src/scss/*/*.scss", ['css']);
    gulp.watch("src/js/*.js", ['js']);
    gulp.watch("app/*.html", ['bs-reload']);
});
Lowtrux
  • 156
  • 2
  • 12
  • 41
  • Some of your issues, like gulp watch being triggered even you have not manually saved the page can be the result of having your coding environment set to autosave. – Mark Jun 16 '18 at 01:22
  • Sorry @Mark i dont think i get your point, when you say "autosave" it means i have a parameter or something that allows that on my code ? can you develope please, thanks ! – Lowtrux Jun 18 '18 at 07:03
  • 1
    Which code editor are you using? It may have an autosave feature turned on, so that as you type it saves the file automatically (or saves every minute or whatever is set). This autosave would then trigger your watch tasks. – Mark Jun 18 '18 at 10:24
  • I use Sublime Text and Visual Code, I didn't realize that I just had the problem on VC before your advice and when I checked the autosave options they were set to autosave after a short delay. After turning off the autosave everything is working fine. Thanks man ! – Lowtrux Jun 19 '18 at 10:13

0 Answers0