Skip to content

Instantly share code, notes, and snippets.

@shuhei
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shuhei/74cf6547714216c443ee to your computer and use it in GitHub Desktop.
Save shuhei/74cf6547714216c443ee to your computer and use it in GitHub Desktop.
possible bug of watchify
npm install
npm start

and save a.js.

Files are bundled twice.

This is because a.js is watched twice as a relative path and an absoulete path. The former comes from 'file' event of browserify that comes from module-deps' 'file' event. The latter comes from 'dep' event of browserify. If we use absolute paths for browserify, this problem doesn't happen. But why does watchify check 'file' and 'dep'?

Also, it's dangerous to watch a relative path because it doesn't consider options.baseDir.

require('b');
module.exports = 'b';
var util = require('util');
var gulp = require('gulp');
var browserify = require('browserify');
var watchify = require('watchify');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
gulp.task('js', function() {
var options = util._extend({ debug: true }, watchify.args);
var bundler = watchify(browserify('./a.js', options));
bundler
.on('update', function() {
console.log('update');
bundle(bundler).on('end', function() {
console.log('done');
});
})
.on('log', function(message) {
console.log(message);
});
bundle(bundler);
function bundle(bundler) {
return bundler
.bundle()
.pipe(source('out.js'))
.pipe(buffer())
.pipe(gulp.dest('./'));
}
});
{
"name": "watchify-bug",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "gulp js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"browserify": "^6.0.0",
"gulp": "^3.8.10",
"vinyl-buffer": "^1.0.0",
"vinyl-source-stream": "^1.0.0",
"watchify": "^2.1.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment