Abdennour TOUMI
Abdennour TOUMI

Reputation: 93213

Specify custom output file path for browserify

Using CLI , set value to -o to specify the output filepath( bundle)

node node_modules/browserify/bin/cmd src/index -o lib/bundle.js

In the above example , the output file path is ./lib/bundle.js.

However, I don't want to use CLI , i want to use JS SDK :

    const browserify = require('browserify');

    const b = browserify();
    b.add('./src/index.js');
    b.bundle(/* Where to specify the output filepath, is it here */)
     .pipe(/* or here*/)

My head will break because of this library. Frankly, webpack documentation is better.

Any help is appreciative

Upvotes: 0

Views: 1154

Answers (2)

Ubunfu
Ubunfu

Reputation: 1123

If you happen to be using the Gulp build system, you can also do it like this.

(Much thanks to Dan Tello for his article that helped me get this going in my own environment!).

This approach utilizes the help of another Node module called vinyl-source-stream. By using this helper module, you're not dependent upon the deprecated gulp-browserify package - you can use the latest vanilla browserify package as-is.

var gulp = require('gulp');

// add in browserify module to bundle the JS
// We can use this directly instead of 'gulp-browserify' with help 
// from 'vinyl-source-stream'
var browserify = require('browserify');

// Add in vinyl-source-stream to help link browserify and gulp streams
var source = require('vinyl-source-stream');

gulp.task('browserify', () => {

    return browserify('./js/main.js') // source to compile
        .bundle()   // compile it...
        .pipe(source('popup.js'))  // pipe to output file
        .pipe(gulp.dest('./js/'));  // put output back into ./js/
});

Upvotes: 0

kemotoe
kemotoe

Reputation: 1777

Just pipe to a standard file stream

const browserify = require('browserify');
const fs = require('fs');

browserify()
    .add('./src/index.js')
    .bundle()
    .pipe(fs.createWriteStream('./lib/bundle.js'));

Upvotes: 1

Related Questions