Skip to content

Commit

Permalink
✨ add findFunction method and constructor array
Browse files Browse the repository at this point in the history
  • Loading branch information
pixeldesu committed Feb 13, 2018
1 parent be068e1 commit d4cffb6
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ $ npm install moduleraid
Or if you directly want to use it in the browser

```html
<script src="https://unpkg.com/moduleraid@3.0.0/moduleraid.js"></script>
<script src="https://unpkg.com/moduleraid@3.1.0/moduleraid.js"></script>
<!-- minified -->
<script src="https://unpkg.com/moduleraid@3.0.0/moduleraid.min.js"></script>
<script src="https://unpkg.com/moduleraid@3.1.0/moduleraid.min.js"></script>
```

Alternatively, just copy the script from `moduleraid.js` and run it in a devtool console
Expand All @@ -42,11 +42,15 @@ Once `moduleRaid` is run or included on a page that includes a Webpack build (us
will return a object, containing:

* `modules`: An object containing all modules we could get from Webpack
* `constructors`: An array containing all module constructor functions
* `get(id)`: Get the module from the specified `id`
* `findModule(query)`: Return the module that has `query` as a key in its exports
* `findFunction(query)`: Return functions that include `query` (`query` can be either a string or a function)

If you run the code in devtools or load it as external script from unpkg/etc. the `moduleRaid` object can be found in `window.mR` by default.

**Note:** If moduleRaid had to get modules through iteration, `constructors` will be empty and so `findFunction` will not work.

## How it works

There already was a script basically doing the same as `moduleRaid` some months earlier, called [`webcrack`](https://gist.github.com/no-boot-device/cb63762000e606e50690911cac1bcead) (made by [no-boot-device](https://github.com/no-boot-device)), which was rendered obsolute due to structural changes in how you can access Webpack modules from the outside.
Expand Down
51 changes: 43 additions & 8 deletions moduleraid.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@

const moduleRaid = function () {
moduleRaid.mObj = {};
moduleRaid.cArr = [];

moduleRaid.args = [
[[0], [function(e, t, i) {
mCac = i.c;
Object.keys(mCac).forEach(function(mod) {
Object.keys(mCac).forEach (function(mod) {
moduleRaid.mObj[mod] = mCac[mod].exports;
})
moduleRaid.cArr = i.m;
}]],
[[], {'moduleraid': function(e, t, i) {
mCac = i.c;
Object.keys(mCac).forEach(function(mod) {
Object.keys(mCac).forEach (function(mod) {
moduleRaid.mObj[mod] = mCac[mod].exports;
})
moduleRaid.cArr = i.m;
}}, ['moduleraid']]
]

Expand All @@ -34,7 +37,7 @@ const moduleRaid = function () {
mIter = 0;

if (!webpackJsonp([],[],[mIter])) {
throw 'Unknown Webpack structure';
throw Error("Unknown Webpack structure");
}

while (!mEnd) {
Expand All @@ -57,10 +60,10 @@ const moduleRaid = function () {

findModule = function findModule (query) {
results = [];
modules = Object.keys(moduleRaid.mObj)
modules = Object.keys(moduleRaid.mObj);

modules.forEach(function(mKey) {
mod = moduleRaid.mObj[mKey]
mod = moduleRaid.mObj[mKey];

if (typeof mod !== 'undefined') {
if (typeof mod.default === "object") {
Expand All @@ -78,9 +81,41 @@ const moduleRaid = function () {
return results;
}

return {
modules: moduleRaid.mObj,
findModule: findModule,
findFunction = function(query) {
if (moduleRaid.cArr.length == 0) {
throw Error("No module constructors to search through!");
}

results = [];

if (typeof query === "string") {
moduleRaid.cArr.forEach(function (ctor, index) {
if (ctor.toString().includes(query)) {
results.push(moduleRaid.mObj[index]);
}
})
} else if (typeof query === "function") {
modules = Object.keys(moduleRaid.mObj);

modules.forEach(function(mKey, index) {
mod = moduleRaid.mObj[mKey];

if (query(mod)) {
results.push(moduleRaid.mObj[index]);
}
})
} else {
throw new TypeError('findFunction can only find via string and function, ' + (typeof query) + ' was passed');
}

return results;
}

return {
modules: moduleRaid.mObj,
constructors: moduleRaid.cArr,
findModule: findModule,
findFunction: findFunction,
get: get
}
}
Expand Down
2 changes: 1 addition & 1 deletion moduleraid.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "moduleraid",
"version": "3.0.0",
"version": "3.1.0",
"description": "Get modules from webpackJsonp",
"main": "moduleraid.js",
"scripts": {
Expand Down

0 comments on commit d4cffb6

Please sign in to comment.