From 9677f4b8cc13948bd339e85465297ddcd4ea9251 Mon Sep 17 00:00:00 2001 From: Dario Gieselaar Date: Sat, 21 Nov 2015 14:04:43 +0100 Subject: [PATCH] Support for windows --- size_tree.ts | 5 +++-- tests/size_tree_test.ts | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/size_tree.ts b/size_tree.ts index eaeb62c9..30ee70c1 100644 --- a/size_tree.ts +++ b/size_tree.ts @@ -1,6 +1,7 @@ /// import filesize = require('filesize'); +import path = require('path'); import webpack_stats = require('./webpack_stats'); @@ -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 { diff --git a/tests/size_tree_test.ts b/tests/size_tree_test.ts index c13f13d3..4536eccb 100644 --- a/tests/size_tree_test.ts +++ b/tests/size_tree_test.ts @@ -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'); @@ -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 = 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); @@ -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);