How do I watch multiple files with gulp-browserify but process only one?

柔情痞子 提交于 2019-11-30 05:05:55

Just call a normal task on file change, like this:

gulp.task("build-js", function() {
    return gulp.src('src/js/app.js')
        .pipe(browserify())
        .pipe(gulp.dest('dist'))
});

gulp.task("watch", function() {
    // calls "build-js" whenever anything changes
    gulp.watch("src/**/*.js", ["build-js"]);
});

If you want to use gulp-watch (because it can look for new files), then you need to do something like this:

gulp.task("watch", function() {
    watch({glob: "src/**/*.js"}, function() {
        gulp.start("build-js");
    });
});

Using gulp-watch also has the benefit of batching operations, so if you modify several files at once, you won't get a bunch of builds in a row.

gulp-browserify has been black-listed on the npm-repository

The preferred method is to use browserify directly in combination with vinyl-source-stream.

This means declaring browserify and vinyl-source-stream in your build script:

var browserify = require('browserify'),
    source = require('vinyl-source-stream');

And then utilizing them in your functions to build your combined JS file.

function buildVendorJs()
{ 
    return browserify('./js/vendor.js')
        .bundle()
        .pipe(source('./js/vendor.js'))
        .pipe(debug({verbose: true}))
        .pipe(gulp.dest(outputDir));
}

With that done, browserify will create a dependency tree using the requires('...') calls in vendor.js and build a new vendor.js where all of the dependencies are modularized and pulled into a single vendor.js file.

Adapting @OverZealous answer to a total gulp newb, here's the gulpfile.js code, with inline explanations. (This file would be placed at the project root and run from that location, and that is all you'd need other than the npm installs detailed at the bottom).

var gulp = require('gulp');
var watch = require('gulp-watch');
var browserify = require('gulp-browserify');

//
// task for building - invoked simply via 'gulp'
// 
gulp.task('default', function() {
  return gulp.src('public-script-source/main.js') /* source to build */
        .pipe(browserify())
        .pipe(gulp.dest('public/script'))         /* output directory */
});

//
// task for continuously building upon javascript change - 
// invoked via 'gulp watch'
// 
gulp.task("watch", function() {
    watch({glob: "public-script-source/*.js"}, function() {
        gulp.start("default");
    });
});

Don't forget npm installing the three requires, if not already installed:

npm install --save-dev gulp gulp-watch gulp-browserify

Please don't accept this answer as it was adapted from @OverZealous. As an alternative to all the above, you may try https://github.com/substack/watchify (didn't try it myself), but a task manager approach, like above, can also scale for you when you later need additional things running for your build beyond just browserify.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!