Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
136
rated 0 times [  137] [ 1]  / answers: 1 / hits: 17202  / 10 Years ago, fri, may 23, 2014, 12:00:00

I have browserify bundling up files and it's working great. But what if I need to generate multiple bundles?



I would like to end up with dist/appBundle.js and dist/publicBundle.js



gulp.task(js, function(){

return browserify([
./js/app.js,
./js/public.js
])
.bundle()
.pipe(source(bundle.js))
.pipe(gulp.dest(./dist));

});


Obviously this isn't going to work since I am only specifying one output (bundle.js). I can accomplish this by repeating the above statement like so (but it doesn't feel right, because of the repetition):



gulp.task(js, function(){

browserify([
./js/app.js
])
.bundle()
.pipe(source(appBundle.js))
.pipe(gulp.dest(./dist));


browserify([
./js/public.js
])
.bundle()
.pipe(source(publicBundle.js))
.pipe(gulp.dest(./dist));

});


Is there a better way to tackle this? Thanks!


More From » node.js

 Answers
15

I don't have a good environment to test this in right now, but my guess is that it would look something like:



gulp.task(js, function(){
var destDir = ./dist;

return browserify([
./js/app.js,
./js/public.js
])
.bundle()
.pipe(source(appBundle.js))
.pipe(gulp.dest(destDir))
.pipe(rename(publicBundle.js))
.pipe(gulp.dest(destDir));

});


EDIT: I just realized I mis-read the question, there should be two separate bundles coming from two separate .js files. In light of that, the best alternative I can think of looks like:



gulp.task(js, function(){
var destDir = ./dist;

var bundleThis = function(srcArray) {
_.each(srcArray, function(source) {
var bundle = browserify([./js/ + source + .js]).bundle();
bundle.pipe(source(source + Bundle.js))
.pipe(gulp.dest(destDir));
});
};

bundleThis([app, public]);
});

[#70873] Thursday, May 22, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jadyngraysons

Total Points: 455
Total Questions: 109
Total Answers: 98

Location: Trinidad and Tobago
Member since Fri, May 8, 2020
4 Years ago
jadyngraysons questions
Thu, Apr 23, 20, 00:00, 4 Years ago
Sat, Jan 18, 20, 00:00, 4 Years ago
Tue, Dec 31, 19, 00:00, 5 Years ago
;