Skip to content

Commit

Permalink
substitue with a new json tree parse for webpack modules
Browse files Browse the repository at this point in the history
  • Loading branch information
ananavati committed Oct 10, 2016
1 parent e09674c commit e69f3f5
Show file tree
Hide file tree
Showing 6 changed files with 28,395 additions and 154 deletions.
171 changes: 22 additions & 149 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
var builtins = require('builtins')
var through = require('through')
var flatten = require('flatten')
var duplex = require('duplexer')
var pluck = require('plucker')
var uniq = require('uniq')

var commondir = require('commondir')
var fileTree = require('file-tree')
var path = require('path')
var fs = require('fs')
var bl = require('bl')

var versions = require('./lib/versions')
var jsonTree = require('./json-tree')

var sampleStats = require('./lib/sample-stats.json')
var sampleStats = require('./lib/stats/electrode-app-stats.json')

module.exports = createStream
createStream.json = json
createStream.bundle = bundle

function createStream(opts) {
Expand All @@ -39,125 +32,6 @@ function createStream(opts) {
return stream
}

function getBundleModules(bundles) {
bundles = Array.isArray(bundles)
? bundles
: bundles ? [bundles] : []

if (bundles.length > 0) {
bundles = JSON.parse(bundles.toString())
}

return bundles && bundles.modules
}

function json(bundles, callback) {
// bundles length is 1 because its an array of buffer
// example: [ <Buffer 7b 0a 20 20 ... > ]
// length 1 is in case of CLI
if (bundles.length === 1) {
bundles = getBundleModules(bundles)
} else {
bundles = sampleStats.modules
}

var modules = flatten(bundles).map(function(module) {
if (typeof module === 'undefined') return callback(new Error(
'Unable to compile one of the supplied bundles!'
))

if (typeof module.identifier !== 'number') return module

return callback(new Error(
'Please recompile this webpack bundle using the fields:null flag '
))
})

modules = modules.filter(function(module) {
return module && !isEmpty(module)
})

if (!modules.length) return

var reactModules = []

var otherModules = modules.filter(function(module) {
if (path.basename(module.identifier) === '_empty.js') return false
if (reactModules.indexOf(module) === -1) return true
})

otherModules = otherModules.filter(function(module) {
var ignoreFiles = /^ignored \//;

if (ignoreFiles.test(module.identifier)) {
return false
} else {
return true
}
})

var root = commondir(otherModules.map(pluck('identifier')))

reactModules.forEach(function(module) {
var regex = /^.+\/node_modules\/react\/(?:node_modules\/)(.+)$/g

module.identifier = module.identifier.replace(regex, function(_, subpath) {
return path.resolve(root, 'react/' + subpath)
})

return module
})

otherModules.forEach(function(module) {
var stylusRegex = /^.+\/node_modules\/stylus-relative-loader\/index(.+)/;
var babelRegex = /^.+\/node_modules\/babel-loader\/index(.+)/;
var cssModulesRegex = /^.+\/node_modules\/postcss-loader\/index(.+)/;
var intlMessageRegex = /^.+\/node_modules\/intl-messageformat\/lib\/main.js|/;

if (stylusRegex.test(module.identifier)
|| babelRegex.test(module.identifier)
|| cssModulesRegex.test(module.identifier)) {
var structure = module.identifier.split("!")
module.identifier = structure[structure.length - 1]
}

if (intlMessageRegex.test(module.identifier)) {
var intlStructure = module.identifier.split("|")
module.identifier = intlStructure[intlStructure.length - 1]
}

return module;
})

uniq(modules, function(a, b) {
return a.identifier === b.identifier ? 0 : 1
}, true)

var ids = modules.map(pluck('identifier'))
var main = path.basename(root)

var byid = modules.reduce(function(memo, mod) {
memo[mod.identifier] = mod
return memo
}, {})

fileTree(ids, function(id, next) {
var row = byid[id]
next(null, {
size: (row && row.source.length) || 0
, deps: 0
, path: id
})
}, function(err, tree) {
if (err) return callback(err)

tree = { name: main, children: tree }
dirsizes(tree)
versions(tree)
callback(null, tree)
})
}

function bundle(bundles, opts, callback) {

if (typeof opts === 'function') {
Expand All @@ -171,27 +45,26 @@ function bundle(bundles, opts, callback) {
var header = opts.header || opts.button || ''
var footer = opts.footer || ''

return json(bundles, function(err, data) {
if (err) return callback(err)

data.mode = opts.mode || 'size'
data = '<script type="text/javascript">'
+ ';window.disc = ('
+ JSON.stringify(data)
+ ');</script>'

var script = '<script type="text/javascript">'
+ bundled().replace(/\/script/gi, '\\/script')
+ '</script>'

callback(null, template()({
scripts: script
, styles: styles()
, markdown: footer
, header: header
, data: data
}))
})
var data = {};

data.mode = opts.mode || 'size'

data = '<script type="text/javascript">'
+ ';window.disc = ('
+ JSON.stringify(jsonTree(sampleStats))
+ ');</script>'

var script = '<script type="text/javascript">'
+ bundled().replace(/\/script/gi, '\\/script')
+ '</script>'

return callback(null, template()({
scripts: script
, styles: styles()
, markdown: footer
, header: header
, data: data
}))
}

function template() {
Expand Down
92 changes: 92 additions & 0 deletions json-tree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
module.exports = function jsonTree(json) {
var modules = json.modules;
var maxDepth = 1;
var rootSize = 0;

var root = {
children: [],
name: 'root'
};

modules.forEach(function addToTree(module) {
var size;

if (module.source) {
size = module.source.length;
} else {
size = module.size;
}

rootSize += size;

var mod = {
id: module.id,
fullName: module.name,
size: size,
reasons: module.reasons
};

var depth = mod.fullName.split('/').length - 1;
if (depth > maxDepth) {
maxDepth = depth;
}

var fileName = mod.fullName;

var beginning = mod.fullName.slice(0, 2);
if (beginning === './') {
fileName = fileName.slice(2);
}

rootSize += mod.size;

getFile(mod, fileName, root);
});

root.maxDepth = maxDepth;

return root;
}


function getFile(module, fileName, parentTree) {
var charIndex = fileName.indexOf('/');

if (charIndex !== -1) {
var folder = fileName.slice(0, charIndex);
if (folder === '~') {
folder = 'node_modules';
}

var childFolder = getChild(parentTree.children, folder);
if (!childFolder) {
childFolder = {
name: folder,
children: []
};

parentTree.children.push(childFolder);
}

getFile(module, fileName.slice(charIndex + 1), childFolder);
} else {
module.name = fileName;
parentTree.children.push(module);
}
}

function getChild(arr, name) {
for (var i = 0; i < arr.length; i++) {
if (arr[i].name === name) {
return arr[i];
}
}
}

function dirsizes(child) {
return child.size = "size" in child
? child.size
: child.children.reduce(function(size, child) {
return size + ("size" in child ? child.size : dirsizes(child))
}, 0);
}
File renamed without changes.
Loading

0 comments on commit e69f3f5

Please sign in to comment.