Skip to content

Instantly share code, notes, and snippets.

@enykeev
Created October 15, 2015 03:38
Show Gist options
  • Save enykeev/49801fa350b7bf3fc349 to your computer and use it in GitHub Desktop.
Save enykeev/49801fa350b7bf3fc349 to your computer and use it in GitHub Desktop.
Browserify analyser
'use strict';
var fs = require('fs');
var vm = require('vm');
var filename = process.argv[process.argv.length - 1];
var fingerprint = '(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})';
var module_names = {
1: filename
};
var sandbox = {
exports: {},
module: {
exports: {}
},
_interceptor: function (modules) {
return function () {
var result = Object.keys(modules).map(function (id) {
var module = modules[id];
var fn = module[0];
var deps = module[1];
Object.keys(deps).forEach(function (name) {
module_names[deps[name]] = name;
});
return {
id: id,
length: fn.toString().length
};
}).map(function (module) {
module.name = module_names[module.id];
return module;
}).sort(function (a, b) {
return a.length - b.length;
});
process.stdout.write(JSON.stringify(result, null, 2) + '\n');
};
}
};
vm.createContext(sandbox);
var file = fs.readFileSync(filename).toString('utf8');
var pos = file.indexOf(fingerprint);
if (~pos) {
file = file.replace(fingerprint, '_interceptor');
vm.runInContext(file, sandbox);
} else {
throw new Error('Could not find a fingerprint');
}
@enykeev
Copy link
Author

enykeev commented Oct 15, 2015

Outputs a JSON serialized array of all the dependencies and their approximate length in utf8 characters

[
  {
    "id": "19",
    "length": 77,
    "name": "axios"
  },
  {
    "id": "18",
    "length": 88,
    "name": "https"
  },
  {
    "id": "17",
    "length": 106,
    "name": "http"
  },
  {
    "id": "42",
    "length": 164,
    "name": "querystring"
  },
  ...
  {
    "id": "45",
    "length": 15784,
    "name": "util"
  },
  {
    "id": "43",
    "length": 22294,
    "name": "url"
  },
  {
    "id": "32",
    "length": 28558,
    "name": "buffer"
  },
  {
    "id": "52",
    "length": 230082,
    "name": "lodash"
  }
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment