diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e29e9b..7cd037f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.0.3 +- turn static helper `extractModuleData` into instance helper +- fix an error for multi entry points + ## 0.0.2 - Add `getModuleNames()` diff --git a/index.js b/index.js index 056aa8a..bb05ae9 100644 --- a/index.js +++ b/index.js @@ -7,25 +7,27 @@ function WebpackDependencyStats (webpackStats, options) { options = _.extend({ onlyLocal: true }, options); - this.modules = WebpackDependencyStats.extractModuleData(webpackStats, options); - this.contextHelpers = WebpackDependencyStats.getContextHelperNames(this.modules); this.cache = { dependencies: {}, dependents: {} }; + this.moduleNames = {}; + this.allModules = {}; + this.filteredModules = this.extractModuleData(webpackStats, options); + this.contextHelpers = WebpackDependencyStats.getContextHelperNames(this.filteredModules); } WebpackDependencyStats.prototype.getAllDependencies = function () { if (Object.keys(this.cache.dependencies).length) { return this.cache.dependencies; } - var moduleIds = Object.keys(this.modules.byId).map((id) => this.modules.byId[id].id); + var moduleIds = Object.keys(this.filteredModules.byId).map((id) => this.filteredModules.byId[id].id); moduleIds.forEach((id) => { this.cache.dependencies[id] = []; }); moduleIds .map((id) => ({ id: id, depenedents: this.getDependentIdsById(id) })) .forEach((dependentsMap) => dependentsMap.depenedents - .forEach((id) => this.cache.dependencies[id].push(dependentsMap.id))); + .forEach((id) => this.cache.dependencies[id] && this.cache.dependencies[id].push(dependentsMap.id))); moduleIds.forEach((id) => { this.cache.dependencies[id].sort(); @@ -36,21 +38,21 @@ WebpackDependencyStats.prototype.getAllDependencies = function () { }; WebpackDependencyStats.prototype.getModuleNames = function () { - return Object.keys(this.modules.byName); + return Object.keys(this.filteredModules.byId).map((id) => this.getModuleNameById(id)); }; WebpackDependencyStats.prototype.getDependencies = function (moduleName) { return this.getDependencyIds(moduleName) - .map((id) => WebpackDependencyStats.stripLoaders(this.modules.byId[id].name)); + .map((id) => this.getModuleNameById(id)); }; WebpackDependencyStats.prototype.getDependents = function (moduleName) { return this.getDependentIds(moduleName) - .map((id) => WebpackDependencyStats.stripLoaders(this.modules.byId[id].name)); + .map((id) => this.getModuleNameById(id)); }; WebpackDependencyStats.prototype.getDependencyIds = function (moduleName) { - var module = this.modules.byName[moduleName]; + var module = this.filteredModules.byName[moduleName]; if (!module) { throw new Error('Module "' + moduleName + '" was not found in webpack stats'); } @@ -58,7 +60,7 @@ WebpackDependencyStats.prototype.getDependencyIds = function (moduleName) { }; WebpackDependencyStats.prototype.getDependentIds = function (moduleName) { - var module = this.modules.byName[moduleName]; + var module = this.filteredModules.byName[moduleName]; if (!module) { throw new Error('Module "' + moduleName + '" was not found in webpack stats'); } @@ -70,7 +72,7 @@ WebpackDependencyStats.prototype.getDependencyIdsById = function (id) { }; WebpackDependencyStats.prototype.getDependentIdsById = function (id) { - var module = this.modules.byId[id]; + var module = this.filteredModules.byId[id]; if (!module) { throw new Error('Module "' + id + '" was not found in webpack stats'); } @@ -89,8 +91,8 @@ WebpackDependencyStats.prototype.getDependentIdsById = function (id) { } module.reasons.forEach((reason) => { - addDependent(reason.moduleId); - if (this.modules.byId[reason.moduleId]) { + if (this.filteredModules.byId[reason.moduleId]) { + addDependent(reason.moduleId); this.getDependentIdsById(reason.moduleId).forEach(addDependent); } }); @@ -98,10 +100,20 @@ WebpackDependencyStats.prototype.getDependentIdsById = function (id) { return _.differenceWith(_.sortedUniq(dependents), this.contextHelpers.ids); }; +WebpackDependencyStats.prototype.getModuleNameById = function (id) { + if (!this.moduleNames[id]) { + var name = this.allModules[id].name; + this.moduleNames[id] = name + .replace(/^.+[!]/, '') + .replace(/^.+[~]/, '~'); + } + return this.moduleNames[id]; +}; + /** * Extracts the real modules from webpackStats */ -WebpackDependencyStats.extractModuleData = function (webpackStats, options) { +WebpackDependencyStats.prototype.extractModuleData = function (webpackStats, options) { options = _.extend({ onlyLocal: true }, options); @@ -118,13 +130,20 @@ WebpackDependencyStats.extractModuleData = function (webpackStats, options) { var modules = { byId: {}, byName: {} }; + stats.modules + .forEach((module) => { + this.allModules[module.id] = module; + }); + stats.modules // Remove internal modules .filter((module) => module.name.indexOf('(webpack)') === -1) // Remove files outside the src folder - .filter((module) => !options.onlyLocal || WebpackDependencyStats.stripLoaders(module.name).indexOf('./') === 0) + .filter((module) => options.onlyLocal + ? this.getModuleNameById(module.id).substr(0, 2) === './' + : ['~', '.'].indexOf(this.getModuleNameById(module.id).substr(0, 1)) !== -1) .forEach((module) => { - modules.byName[WebpackDependencyStats.stripLoaders(module.name)] = module; + modules.byName[this.getModuleNameById(module.id)] = module; modules.byId[module.id] = module; }); @@ -138,10 +157,4 @@ WebpackDependencyStats.getContextHelperNames = function (modules) { return { names: names, ids: ids }; }; -WebpackDependencyStats.stripLoaders = function (moduleName) { - return moduleName - .replace(/^.+[!]/, '') - .replace(/^.+[~]/, '~'); -}; - module.exports = WebpackDependencyStats; diff --git a/package.json b/package.json index 1d67911..77c3a32 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "webpack-dependency-stats", - "version": "0.0.2", + "version": "0.0.3", "description": "Extracts a flat list of all dependencies and dependents of a given webpack module", "main": "index.js", "files": [ @@ -11,7 +11,7 @@ "posttest": "npm-run-all posttest:*", "posttest:lint": "semistandard index.js", "posttest:coverage-report": "nyc report --reporter=html", - "posttest:coverage": "nyc check-coverage --lines 95 --functions 95 --branches 90", + "posttest:coverage": "nyc check-coverage --lines 100 --functions 100 --branches 100", "prepublish": "npm test" }, "author": "Jan Nicklas ", diff --git a/test/fixtures/demo/another-entry.js b/test/fixtures/demo/another-entry.js new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/demo/dist/bundle.js b/test/fixtures/demo/dist/bundle.js index 8dfa479..b2bf8e9 100644 --- a/test/fixtures/demo/dist/bundle.js +++ b/test/fixtures/demo/dist/bundle.js @@ -44,17 +44,25 @@ /* 0 */ /***/ function(module, exports, __webpack_require__) { - console.log(__webpack_require__(1)); - __webpack_require__(2); + __webpack_require__(1); + module.exports = __webpack_require__(11); + /***/ }, /* 1 */ +/***/ function(module, exports, __webpack_require__) { + + console.log(__webpack_require__(2)); + __webpack_require__(3); + +/***/ }, +/* 2 */ /***/ function(module, exports) { module.exports = "Hello"; /***/ }, -/* 2 */ +/* 3 */ /***/ function(module, exports, __webpack_require__) { 'use strict'; @@ -63,15 +71,15 @@ return requireContext.keys().map(requireContext); } - requireAll(__webpack_require__(3)); + requireAll(__webpack_require__(4)); /***/ }, -/* 3 */ +/* 4 */ /***/ function(module, exports, __webpack_require__) { var map = { - "./index.js": 4 + "./index.js": 5 }; function webpackContext(req) { return __webpack_require__(webpackContextResolve(req)); @@ -84,38 +92,38 @@ }; webpackContext.resolve = webpackContextResolve; module.exports = webpackContext; - webpackContext.id = 3; + webpackContext.id = 4; /***/ }, -/* 4 */ +/* 5 */ /***/ function(module, exports, __webpack_require__) { - __webpack_require__(5); - __webpack_require__(7); + __webpack_require__(6); + __webpack_require__(8); /***/ }, -/* 5 */ +/* 6 */ /***/ function(module, exports, __webpack_require__) { - console.log(__webpack_require__(6)); + console.log(__webpack_require__(7)); /***/ }, -/* 6 */ +/* 7 */ /***/ function(module, exports) { module.exports = "Some text"; /***/ }, -/* 7 */ +/* 8 */ /***/ function(module, exports, __webpack_require__) { - __webpack_require__(5); - __webpack_require__(8); + __webpack_require__(6); + __webpack_require__(9); /***/ }, -/* 8 */ +/* 9 */ /***/ function(module, exports, __webpack_require__) { var __WEBPACK_AMD_DEFINE_RESULT__;/* WEBPACK VAR INJECTION */(function(module, global) {/** @@ -16523,10 +16531,10 @@ } }.call(this)); - /* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(9)(module), (function() { return this; }()))) + /* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(10)(module), (function() { return this; }()))) /***/ }, -/* 9 */ +/* 10 */ /***/ function(module, exports) { module.exports = function(module) { @@ -16541,5 +16549,11 @@ } +/***/ }, +/* 11 */ +/***/ function(module, exports) { + + + /***/ } /******/ ]); \ No newline at end of file diff --git a/test/fixtures/demo/webpack.config.js b/test/fixtures/demo/webpack.config.js index 95a0454..20ae7e7 100644 --- a/test/fixtures/demo/webpack.config.js +++ b/test/fixtures/demo/webpack.config.js @@ -1,6 +1,6 @@ module.exports = { context: __dirname, - entry: './entry.js', + entry: ['./entry.js', './another-entry.js'], output: { path: __dirname + '/dist', filename: 'bundle.js' diff --git a/test/test.js b/test/test.js index 3c094fe..30b9362 100644 --- a/test/test.js +++ b/test/test.js @@ -20,7 +20,10 @@ test.before(async t => { }); test('extracts modules from stats', t => { - var {byId, byName} = WebpackDependencyStats.extractModuleData(stats, { + var webpackDependencyStats = new WebpackDependencyStats(stats, { + srcFolder: path.resolve(__dirname, 'fixtures/demo/') + }) + var {byId, byName} = webpackDependencyStats.extractModuleData(stats, { srcFolder: path.resolve(__dirname, 'fixtures/demo/') }); var moduleNames = Object.keys(byName); @@ -32,7 +35,8 @@ test('extracts modules from stats', t => { './space/earth/index.js', './space/earth/ocean/island.js', './space/earth/ocean/text.html', - './space/earth/europe/index.js' + './space/earth/europe/index.js', + './another-entry.js' ]); var expectedIds = _.values(byName).map((module) => `${module.id}`); var moduleIds = Object.keys(byId); @@ -40,7 +44,10 @@ test('extracts modules from stats', t => { }); test('extracts modules names from stats', t => { - var {byId, byName} = WebpackDependencyStats.extractModuleData(stats, { + var webpackDependencyStats = new WebpackDependencyStats(stats, { + srcFolder: path.resolve(__dirname, 'fixtures/demo/') + }) + var {byId, byName} = webpackDependencyStats.extractModuleData(stats, { srcFolder: path.resolve(__dirname, 'fixtures/demo/') }); var webpackDependencyStats = new WebpackDependencyStats(stats, {