Skip to content

Commit

Permalink
turn static helper extractModuleData into instance helper
Browse files Browse the repository at this point in the history
fix an error for multi entry points
  • Loading branch information
jantimon committed Jun 25, 2016
1 parent 4c9ebf6 commit cdf0df0
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 46 deletions.
4 changes: 4 additions & 0 deletions 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()`
Expand Down
55 changes: 34 additions & 21 deletions index.js
Expand Up @@ -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();
Expand All @@ -36,29 +38,29 @@ 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');
}
return this.getDependencyIdsById(module.id);
};

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');
}
Expand All @@ -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');
}
Expand All @@ -89,19 +91,29 @@ 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);
}
});

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);
Expand All @@ -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;
});

Expand All @@ -138,10 +157,4 @@ WebpackDependencyStats.getContextHelperNames = function (modules) {
return { names: names, ids: ids };
};

WebpackDependencyStats.stripLoaders = function (moduleName) {
return moduleName
.replace(/^.+[!]/, '')
.replace(/^.+[~]/, '~');
};

module.exports = WebpackDependencyStats;
4 changes: 2 additions & 2 deletions 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": [
Expand All @@ -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 <j.nicklas@me.com>",
Expand Down
Empty file.
52 changes: 33 additions & 19 deletions test/fixtures/demo/dist/bundle.js
Expand Up @@ -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';
Expand All @@ -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));
Expand All @@ -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) {/**
Expand Down Expand Up @@ -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) {
Expand All @@ -16541,5 +16549,11 @@
}


/***/ },
/* 11 */
/***/ function(module, exports) {



/***/ }
/******/ ]);
2 changes: 1 addition & 1 deletion 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'
Expand Down
13 changes: 10 additions & 3 deletions test/test.js
Expand Up @@ -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);
Expand All @@ -32,15 +35,19 @@ 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);
t.deepEqual(expectedIds, moduleIds);
});

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, {
Expand Down

0 comments on commit cdf0df0

Please sign in to comment.