Skip to content

Commit 0d24717

Browse files
committed
Unloadable modules
1 parent 8aa4dc7 commit 0d24717

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

README.md

+15-2
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,11 @@ To install:
123123
npm install --save browserify-tree
124124
```
125125

126-
Example:
126+
Would probably use this in conjunction with the `browser-unpack` package.
127+
128+
## getUnusedModules
129+
130+
Get a list of bundle module IDs for bundle modules that are not in use on the bundle entry module's dependency graph:
127131

128132
```javascript
129133
const browserifyTree = require('browserify-tree');
@@ -132,7 +136,16 @@ const unusedModules = browserifyTree.getUnusedModules('./target/classes/org/jenk
132136
// Do something with unusedModules
133137
```
134138

135-
Would probably use this in conjunction with the `browser-unpack` package.
139+
## getUnloadableModules
140+
141+
Get a list of bundle module IDs for bundle modules that are not loadable for some reason e.g. they "require" unresolvable modules:
142+
143+
```javascript
144+
const browserifyTree = require('browserify-tree');
145+
const unloadableModules = browserifyTree.getUnloadableModules('./target/classes/org/jenkins/ui/jsmodules/blueocean-usain/jenkins-js-extension.js'); // or pass the already unpackaged bundle object
146+
147+
// Do something with unloadableModules e.g. stub them out of the bundle with an exception.
148+
```
136149

137150
[Browserify]: http://browserify.org/
138151
[disc]: https://github.com/hughsk/disc/

index.js

+31-12
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ exports.drawTree = function(bundlePath, userConfig) {
88
const twoWayPackEntryList = unpackBundle(bundlePath);
99

1010
options = Object.assign({
11-
depth: 2
1211
}, userConfig);
1312

1413
console.log(`\nThe bundle entry module is:\n\t${twoWayPackEntryList.entryModule.id}`);
@@ -27,7 +26,7 @@ exports.drawTree = function(bundlePath, userConfig) {
2726
var hasUnused = (tree.getTreeNodes().length < twoWayPackEntryList.bundlePackEntries.length);
2827
if (hasUnused) {
2928
if (options.unusedt) {
30-
twoWayPackEntryList.listUnusedPacksInDepTree(tree.getTreeNodes());
29+
twoWayPackEntryList.listUnusedPacksInDepTree();
3130
console.log('------------------------------------------------');
3231
}
3332
if (options.unuseda) {
@@ -37,13 +36,13 @@ exports.drawTree = function(bundlePath, userConfig) {
3736
}
3837
};
3938

39+
/**
40+
* Get a list of bundle module IDs for bundle modules that are not
41+
* in use on the bundle entry module's dependency graph.
42+
* @param bundle The bundle file path, or the unpacked bundle.
43+
* @returns {Array}
44+
*/
4045
exports.getUnusedModules = function(bundle) {
41-
// Yeah not the nicest thing having
42-
// this as a global. Might fix it later ;)
43-
options = {
44-
depth: 2
45-
};
46-
4746
const twoWayPackEntryList = unpackBundle(bundle);
4847
const unusedPackEntries = twoWayPackEntryList.findUnusedPacksEntries();
4948
const unusedModuleIds = [];
@@ -55,10 +54,30 @@ exports.getUnusedModules = function(bundle) {
5554
return unusedModuleIds;
5655
};
5756

57+
/**
58+
* Get a list of bundle module IDs for bundle modules that are not
59+
* loadable for some reason e.g. they "require" unresolvable modules.
60+
* @param bundle The bundle file path, or the unpacked bundle.
61+
* @returns {Array}
62+
*/
63+
exports.getUnloadableModules = function(bundle) {
64+
const twoWayPackEntryList = unpackBundle(bundle);
65+
const unloadableModuleIds = [];
66+
67+
twoWayPackEntryList.tree.getTreeNodes().forEach((treeNode) => {
68+
// Add other reasons here as we evolve...
69+
if (treeNode.hasUnresolvableDeps) {
70+
unloadableModuleIds.push(treeNode.moduleId);
71+
}
72+
});
73+
74+
return unloadableModuleIds;
75+
};
76+
5877
function unpackBundle(bundle) {
5978
if (typeof bundle !== 'string') {
6079
// Assume it is already unpacked...
61-
return bundle;
80+
return new TwoWayPackEntryList(bundle);
6281
}
6382

6483
if (!fs.existsSync(bundle)) {
@@ -195,6 +214,8 @@ class TreeNode {
195214
const depModuleNode = new TreeNode(depModule, {parentNode: self});
196215
this.dependencies.push(depModuleNode);
197216
depModuleNode.resolveDeps();
217+
} else {
218+
this.hasUnresolvableDeps = true;
198219
}
199220
}
200221
}
@@ -319,8 +340,6 @@ class TwoWayPackEntryList {
319340
if (depModule2Way) {
320341
twoWayPackEntry.addDependency(depModule2Way.packEntry.id);
321342
depModule2Way.addDependant(twoWayPackEntry.packEntry.id);
322-
} else {
323-
console.warn(`*** No module having Id '${depModuleId}' found in bundle.`);
324343
}
325344
}
326345
}
@@ -348,7 +367,7 @@ class TwoWayPackEntryList {
348367

349368
listUnusedPacksInDepTree() {
350369
console.log('\nThe following modules do not appear to be in use via the bundle entry module:\n');
351-
const packEntries = this.findUnusedPacksEntries(this.tree);
370+
const packEntries = this.findUnusedPacksEntries();
352371
packEntries.forEach((packEntry) => {
353372
this.printPackDetails(packEntry);
354373
});

0 commit comments

Comments
 (0)