Created
October 15, 2015 03:38
-
-
Save enykeev/49801fa350b7bf3fc349 to your computer and use it in GitHub Desktop.
Browserify analyser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Outputs a JSON serialized array of all the dependencies and their approximate length in utf8 characters