Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get dependency tree in browserify?

Tags:

browserify

Is there any way to retrieve dependency tree which browserify uses to build bundle?

Browserify takes a bunch of scripts and makes nice bundle of them, resolving all require dependencies. But I want to see structure of that dependencies.

var scripts = [ 'a.js', 'b.js' ];//a & b require a lot of other scripts
var b = browserify({
        entries:scripts
    });
b.bundle().pipe(fs.createWriteStream('bundle.js'));
//looking on b in debugger I can't find anything like dependency tree
like image 891
setec Avatar asked Mar 31 '15 12:03

setec


1 Answers

This code cribbed from the --list handler in the Browserify bin/cmd.js script will get you a flat list of files:

// Your setup:
var scripts = [ 'a.js', 'b.js' ]; //a & b require a lot of other scripts
var b = browserify({
  entries: scripts
});

// Logging out each of the filenames:
b.pipeline.get('deps').push(require('through2').obj(
  function (row, enc, next) {
    console.log(row.file || row.id);
    next();
  }
));

// Bundle as normal:
b.bundle().pipe(fs.createWriteStream('bundle.js'));

(Note: You'll need the through2 package installed for the above to work out of the box.)

A tree can be constructed with the code from the --deps handler right next to it, but all that code does is spit out a list of JSON blobs, each blob containing a list of other files it depends on; you'll need to build the tree yourself.

like image 132
Rob Howard Avatar answered Oct 01 '22 06:10

Rob Howard