Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i change my gulp file so that it runs build task whenever there's a change in the codebase?

Tags:

gulp

Currently I've this gulp, everytime I make a change in the application to view the changes I've to run gulp build, how can i improve this gulp file by adding a watch task so that it automatically detects that a change has been introduces somewhere in the js folder / in html files and it would automatically run the build task for me.

var gulp = require('gulp'),
    csso = require('gulp-csso'),
    uglify = require('gulp-uglify'),
    concat = require('gulp-concat'),
    ngAnnotate = require('gulp-ng-annotate'),
    minifyHtml = require("gulp-minify-html"),
    ngHtml2Js = require('gulp-ng-html2js');

gulp.task('build', function () {
    gulp.src(['../css/bootstrap_min.css', '../css/inputs.css', '../css/main.css',
        '../css/recording.css','../css/images.css','../css/responsive.css','../css/calendar.css'])
        .pipe(concat('index.min.css'))
        .pipe(csso())
        .pipe(gulp.dest('../css/'));

    gulp.src(['../lib/date.js', '../lib/angular/angular-1.2.17.js','../lib/angular/angular-resource.js',
        '../lib/ui-bootstrap-tpls-0.11.0.js','../lib/angular-ui-router.js',
        '../lib/underscore.min.js','../lib/sortable.js','../lib/localize.js','../lib/bindonce.min.js',
        '../lib/screenfull.js', '../lib/zeroclipboard.min.js'])
        .pipe(concat('libs.min.js'))
        .pipe(ngAnnotate())
        .pipe(uglify())
        .pipe(gulp.dest('../lib'));

    gulp.src(['../js/app.js','../js/services.js','../js/controllers.js','../js/filters.js','../js/directives.js'])
        .pipe(concat('index.min.js'))
        .pipe(ngAnnotate())
        .pipe(uglify())
        .pipe(gulp.dest('../js'));

    gulp.src("../partials/*.html")
        .pipe(minifyHtml({
            empty: true,
            spare: true,
            quotes: true
        }))
        .pipe(ngHtml2Js({
            moduleName: "Partials",
            prefix: "partials/"
        }))
        .pipe(concat("partials.min.js"))
        .pipe(uglify())
        .pipe(gulp.dest("../js"));

});

gulp.task('partials', function () {
    gulp.src("../partials/*.html")
        .pipe(minifyHtml({
            empty: true,
            spare: true,
            quotes: true
        }))
        .pipe(ngHtml2Js({
            moduleName: "Partials",
            prefix: "partials/"
        }))
        .pipe(concat("partials.min.js"))
        .pipe(uglify())
        .pipe(gulp.dest("../js"));
});
like image 829
JP. Avatar asked Nov 22 '25 00:11

JP.


2 Answers

You can just add watch task like this

gulp.task('watch', function() {
    gulp.watch('../js/**/*.js', ['build']);
    gulp.watch('../css/**/*.css', ['build']);
    gulp.watch('../partials/**/*.html', ['build']);
})

Hope this help!

like image 59
Zan RAKOTO Avatar answered Nov 23 '25 19:11

Zan RAKOTO


The other answer works for older versions of gulp. The syntax changed in version 4.0.

gulp.task('watch', function() {
    gulp.watch('../js/**/*.js', gulp.series('build'));
    gulp.watch('../css/**/*.css', gulp.series('build'));
    gulp.watch('../partials/**/*.html', gulp.series('build'));
})
like image 38
Belle Avatar answered Nov 23 '25 18:11

Belle