Dileet Dileet - 4 days ago 6
Node.js Question

How to run gulp tasks on execute

I currently only have gulp working, but need to make a change to a file before any update is done. Is there a way to run the build tasks then watch for changes?

This is my gulp file:

var gulp = require('gulp');
var sass = require('gulp-sass');
var browserify = require('browserify');
var babelify = require('babelify');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');


gulp.task('styles', function() {
gulp.src('sass/**/*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('css/'));
});

gulp.task('scripts', function() {
browserify('js/main.js')
.transform(babelify,{presets: ["es2015", "react"]})
.bundle()
.pipe(source('bundle.js'))
.pipe(gulp.dest('.'))
.pipe(buffer())
});

gulp.task('default', function() {
gulp.watch('sass/**/*.scss',['styles']);
gulp.watch('js/*.js',['scripts']);
});

Answer

Add them as dependent tasks that are run prior to your task.

gulp.task('default', ['styles', 'scripts'], function() {
    gulp.watch('sass/**/*.scss',['styles']);
    gulp.watch('js/*.js',['scripts']);
});

The tasks 'styles' and then 'scripts' will be run, then the callback function for 'default' will be fired calling the two gulp.watch() statements.

Comments