I have just started using Browserify
with gulp
and I came across examples using watchify
.
What I don't understand is that why not use gulp.watch
instead?
What is the difference between watchify
and gulp.watch
?
Asked
Active
Viewed 2,976 times
13

Sven Schoenung
- 30,224
- 8
- 65
- 70

Flake
- 1,386
- 17
- 31
-
I'm not sure on the details, but I'm trying to avoid them by actually using two "root" javascript files; one bundle for the application code and the other for all libraries, since they are very less likely to change on a debug cycle. I took the approach from C++ projects. – Gerardo Lima Oct 07 '16 at 13:44
1 Answers
12
watchify
understands commonjs modules (require(./foo.js)
stuff) and will watch for changes for all dependencies. It can then recompile the bundle with the changes needed and only reload the changed files from disk. If you use gulp.watch
and manually call browserify, it has to build up the dependency tree every time a change happens. This means a lot more disk i/o and hence it will be much slower.

Prinzhorn
- 22,120
- 7
- 61
- 65
-
1
-
"to just update the changed files", how should that look like? Invoking browserify always starts with the entry file (e.g. `index.js`) and has to load all dependencies. It then writes the bundle and the process exits. Another invocation will have to do everything all over again. Gulp doesn't know about the files browserify needs, except for the entry file. – Prinzhorn Feb 21 '16 at 18:17
-
Your OS likely has a cache in front of the disk already, but browserify still has to create the dependency graph with all those files, even if they're cached. Unless you use watchify, in which case the graph can be held in memory. – Prinzhorn Feb 21 '16 at 18:18
-
you mean that if the file contains `require('x')` and `require('y')` and if contents of `x` change, then only the `require('x')` part of the file is recompiled, instead of `require('x') + require('y')` – Flake Feb 21 '16 at 18:22
-
That's what I'm saying. While gulp doesn't even know `x` and `y` are needed for your bundle. – Prinzhorn Feb 21 '16 at 18:24
-
so it doesn't write the whole destination file but only a part of it...right?? – Flake Feb 21 '16 at 18:26
-
1Everything we talked about has nothing to do with writing, only reading. Both watchify and browserify will always write a full new bundle. It's impossible to do otherwise. – Prinzhorn Feb 21 '16 at 18:35