You should use Watchify with Browserify to monitor file changes with less performance overhead. When your application begins to expand, your code base will take a long time because Browserify rebuilds each file, even if only one file has been changed in the latest modification.
Viewing only restores what he needs. The initial build (when you run the gulp task) remains the same as before, but with each change you will see the difference.
In the JavaScript application 5578610 bytes, the original assembly takes 6.67s , and ~ 400 ms is required when rebuilding the file. Only with Browserify is there 6.67s with every change.
To get started, install the NPM packages:
npm install browserify watchify
Import Browserify and Watchify into your gulpfile.js :
var browserify = require('browserify'); var watchify = require('watchify');
Initialize the coherent (I use Lodash _ for the item). client.js is the entry point to the application:
var bundler = watchify(browserify(_.assign(watchify.args, { entries: ['./src/client.js'], debug: true }))); bundler.on('log', gutil.log);
Then create your bundle() function using Watchify:
function bundle() { return bundler.bundle() // Log errors if they happen. .on('error', gutil.log.bind(gutil, 'Browserify Error')) .pipe(source('client.js')) // Optional, remove if you don't need to buffer file contents. .pipe(buffer()) // Optional, remove if you dont want sourcemaps. // Loads map from Browserify file using gulp-sourcemaps. .pipe(sourcemaps.init({loadMaps: true})) // Add transformation tasks to the pipeline here. .pipe(sourcemaps.write('./')) // Writes .map file. .pipe(size(config.size)) // Checks output file size with gulp.size. .pipe(gulp.dest('./build')); }
Finally, the package starts when there is a dependency update:
gulp.task('scripts', bundle); gulp.task('watch', ['scripts'], function() { bundler.on('update', bundle);
Launch gulp watch and you are ready to go.
Note. You should only set entry points as plug-in entries. The browser and the dependency branch will take care of the rest, and you will not create the same file twice.