I am coming from AMD and seem to already be doing something wrong.
I have made a setup like this:
client/js/index.js (entry point)
client/js/test.js
I want it to build to:
build/app.js
index.js
requires in test.js
like this:
var test = require('./test');
My gulp
watchify
task looks like this:
var gulp = require('gulp');
var gutil = require('gulp-util');
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var watchify = require('watchify');
// https://github.com/gulpjs/gulp/blob/master/docs/recipes/fast-browserify-builds-with-watchify.md
gulp.task('watch', function () {
var bundler = watchify(browserify('./client/js/index.js', watchify.args));
bundler.on('update', rebundle);
function rebundle () {
return bundler.bundle()
// Log errors if they happen.
.on('error', function(e) {
gutil.log('Browserify Error', e.message);
})
.pipe(source('app.js'))
.pipe(gulp.dest('./build'));
}
return rebundle();
});
The compiled code looks wrong though, for test.js
I see absolute local paths that surely are either broken or redundant for anyone consuming the code?
P.S. I am running the task with no args (just gulp watch
)
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({"./client/js/index.js":[function(require,module,exports){
var test = require('./test');
var ab = function(a, b2) {
return a + b2;
};
module.exports = ab;
},{"./test":"/Users/dtobias/Sites/browserify-test/client/js/test.js"}],"/Users/dtobias/Sites/browserify-test/client/js/test.js":[function(require,module,exports){
var helloworld = function () {
console.log('hello world');
};
module.exports = helloworld;
},{}]},{},["./client/js/index.js"]);
watchify is intended for watching changing of files and automatically updating them, it isn't intended for deployment, the paths you are seeing is a result of using watchify.args
on this line watchify(browserify('./client/js/index.js', watchify.args));
In the arguments passed to browserify it states fullPaths: true
, which is part of how watchify is able to speed up the build process on each change of a file. What I'd suggest doing is having the watchify task specifically for watching and browserify for the build process.
This can easily be accomplished by setting up some switch and setting it to true within the watch task (and thus modifying the code).
Something like this:
var gulp = require('gulp');
var gutil = require('gulp-util');
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var watchify = require('watchify');
// https://github.com/gulpjs/gulp/blob/master/docs/recipes/fast-browserify-builds-with-watchify.md
gulp.task('build', function(){
browserifyfun(false);
});
gulp.task('watch', function () {
browserifyfun(true);
});
function browserifyfun(watch){
var b;
if(watch){
b = watchify(browserify('./client/js/index.js', watchify.args));
b.on('update', rebundle(b));
}else{
b = browserify('./client/js/index.js');
}
function rebundle (bundler) {
return bundler.bundle()
// Log errors if they happen.
.on('error', function(e) {
gutil.log('Browserify Error', e.message);
})
.pipe(source('app.js'))
.pipe(gulp.dest('./build'));
}
return rebundle(b);
}