webpack-bundle-size-analyzer
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -7,4 +7,4 @@ /// <reference path="typings/typings.d.ts" /> | ||
var bundleStats = JSON.parse(json); | ||
var depTree = size_tree.dependencySizeTree(bundleStats); | ||
size_tree.printDependencySizeTree(depTree); | ||
var depTrees = size_tree.dependencySizeTree(bundleStats); | ||
depTrees.forEach(function (tree) { return size_tree.printDependencySizeTree(tree); }); | ||
} | ||
@@ -11,0 +11,0 @@ commander.version('1.1.0') |
/// <reference path="typings/typings.d.ts" /> | ||
var filesize = require('filesize'); | ||
var path = require('path'); | ||
var webpack_stats = require('./webpack_stats'); | ||
function modulePath(identifier) { | ||
@@ -16,2 +18,6 @@ // the format of module paths is | ||
if (outputFn === void 0) { outputFn = console.log; } | ||
if (node.hasOwnProperty('bundleName')) { | ||
var rootNode = node; | ||
outputFn("Bundle: " + rootNode.bundleName); | ||
} | ||
var childrenBySize = node.children.sort(function (a, b) { | ||
@@ -42,6 +48,3 @@ return b.size - a.size; | ||
exports.printDependencySizeTree = printDependencySizeTree; | ||
/** Takes the output of 'webpack --json', groups the require()'d modules | ||
* by their associated NPM package and outputs a tree of package dependencies. | ||
*/ | ||
function dependencySizeTree(stats) { | ||
function bundleSizeTree(stats) { | ||
var statsTree = { | ||
@@ -52,2 +55,5 @@ packageName: '<root>', | ||
}; | ||
if (stats.name) { | ||
statsTree.bundleName = stats.name; | ||
} | ||
// extract source path for each module | ||
@@ -74,7 +80,7 @@ var modules = stats.modules.map(function (mod) { | ||
// ['root', 'parent', 'child', 'file/path.js' | ||
var packages = mod.path.split(/\/node_modules\//); | ||
var packages = mod.path.split(new RegExp('\\' + path.sep + 'node_modules\\' + path.sep)); | ||
var filename = ''; | ||
if (packages.length > 1) { | ||
var lastSegment = packages.pop(); | ||
var lastPackageName = lastSegment.slice(0, lastSegment.search(/\/|$/)); | ||
var lastPackageName = lastSegment.slice(0, lastSegment.search(new RegExp('\\' + path.sep + '|$'))); | ||
packages.push(lastPackageName); | ||
@@ -108,2 +114,16 @@ filename = lastSegment.slice(lastPackageName.length + 1); | ||
} | ||
/** Takes the output of 'webpack --json', and returns | ||
* an array of trees of require()'d package names and sizes. | ||
* | ||
* There is one entry in the array for each bundle specified | ||
* in the Webpack compilation. | ||
*/ | ||
function dependencySizeTree(stats) { | ||
if (webpack_stats.isMultiCompilation(stats)) { | ||
return stats.children.map(bundleSizeTree); | ||
} | ||
else { | ||
return [bundleSizeTree(stats)]; | ||
} | ||
} | ||
exports.dependencySizeTree = dependencySizeTree; |
/// <reference path="../typings/typings.d.ts" /> | ||
var chai_1 = require('chai'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var size_tree = require('../size_tree'); | ||
@@ -8,8 +9,26 @@ describe('printDependencySizeTree()', function () { | ||
var output = ''; | ||
var statsJsonStr = fs.readFileSync('tests/stats.json').toString(); | ||
var statsJsonStr = fs.readFileSync(path.join('tests', 'stats.json')).toString(); | ||
var statsJson = JSON.parse(statsJsonStr); | ||
// convert paths in Json to WIN if necessary | ||
if (path.sep !== '/') { | ||
statsJson.modules.forEach(function (module) { | ||
module.identifier = module.identifier.replace(/\//g, path.sep); | ||
}); | ||
} | ||
var depsTree = size_tree.dependencySizeTree(statsJson); | ||
size_tree.printDependencySizeTree(depsTree, 0, function (line) { return output += '\n' + line; }); | ||
chai_1.expect(depsTree.length).to.equal(1); | ||
size_tree.printDependencySizeTree(depsTree[0], 0, function (line) { return output += '\n' + line; }); | ||
chai_1.expect(output).to.equal("\nmarked: 27.53 kB (14.9%)\n <self>: 27.53 kB (100%)\nlru-cache: 6.29 kB (3.40%)\n <self>: 6.29 kB (100%)\nstyle-loader: 717 B (0.379%)\n <self>: 717 B (100%)\n<self>: 150.33 kB (81.3%)"); | ||
}); | ||
it('should print the bundle name', function () { | ||
var output = ''; | ||
var namedBundle = { | ||
bundleName: 'a-bundle', | ||
packageName: '<self>', | ||
size: 123, | ||
children: [], | ||
}; | ||
size_tree.printDependencySizeTree(namedBundle, 0, function (line) { return output += '\n' + line; }); | ||
chai_1.expect(output).to.equal("\nBundle: a-bundle\n<self>: 123 B (100%)"); | ||
}); | ||
}); | ||
@@ -27,9 +46,12 @@ describe('dependencySizeTree()', function () { | ||
id: 0, | ||
identifier: '/path/to/loader.js!/path/to/project/node_modules/dep/foo.js', | ||
identifier: path.join('/', 'to', 'loader.js!', 'path', 'to', 'project', 'node_modules', 'dep', 'foo.js'), | ||
size: 1234, | ||
name: './foo.js' | ||
}] | ||
name: path.join('.', 'foo.js') | ||
}], | ||
errors: [], | ||
warnings: [], | ||
}; | ||
var depsTree = size_tree.dependencySizeTree(webpackOutput); | ||
chai_1.expect(depsTree).to.deep.equal({ | ||
chai_1.expect(depsTree.length).to.equal(1); | ||
chai_1.expect(depsTree[0]).to.deep.equal({ | ||
packageName: '<root>', | ||
@@ -44,2 +66,8 @@ size: 1234, | ||
}); | ||
it('should return a tree for each bundle in the config', function () { | ||
var statsJsonStr = fs.readFileSync(path.join('tests', 'multiple-bundle-stats.json')).toString(); | ||
var statsJson = JSON.parse(statsJsonStr); | ||
var depsTree = size_tree.dependencySizeTree(statsJson); | ||
chai_1.expect(depsTree.length).to.equal(2); | ||
}); | ||
}); |
{ | ||
"name": "webpack-bundle-size-analyzer", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "A utility to find how your dependencies are contributing to the size of your Webpack bundles", | ||
@@ -5,0 +5,0 @@ "main": "build/size_tree.js", |
12542
8
223