Search code examples
javascriptgulpbrowser-syncgulp-watch

Why is browser-sync with gulp not working


After following the browsersync docs this is what I came up with:

const gulp = require('gulp'),
   sass = require('gulp-sass'), 
   concat = require('gulp-concat'),
   browserSync = require('browser-sync').create();

gulp.task('watch', ['style', 'concatjs'], (cb) => {
   browserSync.init({
      server: {
         baseDir: "./src/"
      }
   });

   gulp.watch('./assets/scss/**/*.scss', ['style']);
   gulp.watch('./assets/js/**/*.js', ['concatjs']);
   gulp.watch('./**/*.html').on('change', browserSync.reload);
   gulp.watch('./**/*.php').on('change', browserSync.reload);
   cb();
});

// Process scss files to css and autoprefix
gulp.task('style', (cb) => {
   gulp.src('./src/assets/sass/**/*.scss')
      .pipe(sass())
      .pipe(cssprefix('last 2 versions'))
      .on('error', (err) => {
         console.log(`Error: ${err.message} on line: ${err.lineNumber} in: ${err.fileName}`)
      })
      .pipe(gulp.dest('./src/assets/css'))
      .pipe(browserSync.stream());
   cb();
});

// Concat `js` files to the `main.js` file
gulp.task('concatjs', (cb) => {
   gulp.src('./src/assets/js/*.js')
      .pipe(concat('main.js'))
      .on('error', (err) => {
         console.log(`Error: ${err.message} on line: ${err.lineNumber} in: ${err.fileName}`)
      })
      .pipe(gulp.dest('./src/assets/js'))
      .pipe(browserSync.stream());
   cb();
});

After thorough research I found similar questions here and here but none of them addressed my issue.

I was expecting the watch task would run the style task and concatjs task before loading the changes to the browser but instead am getting an error:

assert.js:339
    throw err;
    ^

AssertionError [ERR_ASSERTION]: Task function must be specified
    at Gulp.set [as _setTask] (C:\wamp64\www\Project\node_modules\undertaker\lib\set-task.js:10:3)
    at Gulp.task (C:\wamp64\www\Project\node_modules\undertaker\lib\task.js:13:8)
    at Object.<anonymous> (C:\wamp64\www\Project\gulpfile.js:28:6)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:690:17)
    at require (internal/modules/cjs/helpers.js:25:18)

Solution

  • Case solved. Follow gulp docs for more.

    const gulp = require('gulp'),
       sass = require('gulp-sass'),
       concat = require('gulp-concat'),
       cssprefix = require('gulp-autoprefixer'),
       browserSync = require('browser-sync').create();
    
    // Process `scss` files to `css` and autoprefix them
    function style() {
       return gulp.src(./src/assets/sass/**/*.scss)
          .pipe(sass()
             .on('error', (err) => {
                console.log(`Error: ${err.message} on line: ${err.lineNumber} in: ${err.fileName}`)
             }))
          .pipe(cssprefix('last 2 versions')
             .on('error', (err) => {
                console.log(`Error: ${err.message} on line: ${err.lineNumber} in: ${err.fileName}`)
             }))
          .pipe(gulp.dest(./src/assets/css))
          .pipe(browserSync.stream());
    }
    exports.style = style;
    
    // Concat `js` files to `main.js` file
    function concatjs() {
       return gulp.src(./src/assets/js/**/*.js)
          .pipe(concat('main.js'))
          .pipe(gulp.dest(['./src/assets/js/**/*.js', '!./src/assets/js/main.js'])) //Prevents infinite loop by excluding the processed `main.js` file
          .pipe(browserSync.stream());
    }
    exports.concatjs = concatjs;
    
    // Watch all the files for changes and stream/reload for changes
    function watch() {
       browserSync.init({
          server: {
             baseDir: './src/'
          }
       });
       gulp.watch('./src/assets/sass/**/*.scss', style);
       gulp.watch(['./src/assets/js/**/*.js','!./src/assets/js/main.js'], concatjs);
       gulp.watch('./src/**/*.html').on('change', browserSync.reload);
       gulp.watch('./src/**/*.php').on('change', browserSync.reload);
    }
    exports.watch = watch;
    

    Thank you and welcome :)