Skip to content

Commit

Permalink
Merge pull request webpack-contrib#5 from plestik/master
Browse files Browse the repository at this point in the history
Support for windows
  • Loading branch information
robertknight committed Nov 22, 2015
2 parents 35fc8f0 + 9677f4b commit 585eff4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
5 changes: 3 additions & 2 deletions size_tree.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// <reference path="typings/typings.d.ts" />

import filesize = require('filesize');
import path = require('path');

import webpack_stats = require('./webpack_stats');

Expand Down Expand Up @@ -92,11 +93,11 @@ export function dependencySizeTree(stats: webpack_stats.WebpackJsonOutput) {
// root/node_modules/parent/node_modules/child/file/path.js =>
// ['root', 'parent', 'child', 'file/path.js'

let packages = mod.path.split(/\/node_modules\//);
let packages = mod.path.split(new RegExp('\\' + path.sep + 'node_modules\\' + path.sep));
let filename = '';
if (packages.length > 1) {
let lastSegment = packages.pop();
let lastPackageName = lastSegment.slice(0, lastSegment.search(/\/|$/));
let lastPackageName = lastSegment.slice(0, lastSegment.search(new RegExp('\\' + path.sep + '|$')));
packages.push(lastPackageName);
filename = lastSegment.slice(lastPackageName.length + 1);
} else {
Expand Down
15 changes: 12 additions & 3 deletions tests/size_tree_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import {expect} from 'chai';
import fs = require('fs');
import path = require('path');

import size_tree = require('../size_tree');
import webpack_stats = require('../webpack_stats');
Expand All @@ -10,8 +11,16 @@ describe('printDependencySizeTree()', () => {
it('should print the size tree', () => {
let output = '';

const statsJsonStr = fs.readFileSync('tests/stats.json').toString();
const statsJsonStr = fs.readFileSync(path.join('tests', 'stats.json')).toString();
const statsJson = <webpack_stats.WebpackJsonOutput>JSON.parse(statsJsonStr);

// convert paths in Json to WIN if necessary
if(path.sep !== '/') {
statsJson.modules.forEach(module => {
module.identifier = module.identifier.replace(/\//g, path.sep);
});
}

const depsTree = size_tree.dependencySizeTree(statsJson);
size_tree.printDependencySizeTree(depsTree, 0, line => output += '\n' + line);

Expand Down Expand Up @@ -39,9 +48,9 @@ describe('dependencySizeTree()', () => {
chunks: [],
modules: [{
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')
}]
};
const depsTree = size_tree.dependencySizeTree(webpackOutput);
Expand Down

0 comments on commit 585eff4

Please sign in to comment.