custom-require
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -0,17 +1,19 @@ | ||
"use strict"; | ||
var Module = require("module"); | ||
function CustomRequire(callback) { | ||
this.callback = callback; | ||
this.called = []; | ||
} | ||
CustomRequire.prototype.require = function(path) { | ||
var requiredFilename = Module._resolveFilename(path, this, false); | ||
var res = require(path); | ||
var cachedModule = Module._cache[requiredFilename]; | ||
cachedModule.__addCustomRequire(this); | ||
return res; | ||
} | ||
var CustomRequire = (function () { | ||
function CustomRequire(callback) { | ||
this.called = []; | ||
this.callback = callback; | ||
} | ||
CustomRequire.prototype.require = function (id) { | ||
var requiredFilename = Module._resolveFilename(id, module, false); | ||
var res = require(id); | ||
var cachedModule = Module._cache[requiredFilename]; | ||
cachedModule.__addCustomRequire(this); | ||
return res; | ||
}; | ||
return CustomRequire; | ||
}()); | ||
Module.prototype.__require = Module.prototype.require; | ||
Module.prototype.__addCustomRequire = function(customRequire) { | ||
Module.prototype.__addCustomRequire = function (customRequire) { | ||
if (this.__customRequires.indexOf(customRequire) < 0) { | ||
@@ -23,4 +25,4 @@ this.__customRequires.push(customRequire); | ||
} | ||
} | ||
Module.prototype.__callChildRequires = function(customRequire) { | ||
}; | ||
Module.prototype.__callChildRequires = function (customRequire) { | ||
if (customRequire.called.indexOf(this) < 0) { | ||
@@ -34,4 +36,4 @@ customRequire.called.push(this); | ||
} | ||
} | ||
Module.prototype.__callRequires = function(mod) { | ||
}; | ||
Module.prototype.__callRequires = function (mod) { | ||
for (var i = 0; i < this.__customRequires.length; i++) { | ||
@@ -43,4 +45,4 @@ var customRequire = this.__customRequires[i]; | ||
} | ||
} | ||
Module.prototype.__initialize = function() { | ||
}; | ||
Module.prototype.__initialize = function () { | ||
if (!this.__childModules) { | ||
@@ -51,4 +53,4 @@ this.__childModules = []; | ||
} | ||
} | ||
Module.prototype.__callParentRequires = function(mod) { | ||
}; | ||
Module.prototype.__callParentRequires = function (mod) { | ||
this.__callRequires(mod); | ||
@@ -59,4 +61,4 @@ for (var i = 0; i < this.__parentModules.length; i++) { | ||
} | ||
} | ||
Module.prototype.require = function(path) { | ||
}; | ||
Module.prototype.require = function (path) { | ||
this.__initialize(); | ||
@@ -73,4 +75,4 @@ var requiredFilename = Module._resolveFilename(path, this, false); | ||
return res; | ||
} | ||
module.exports = CustomRequire; | ||
}; | ||
module.exports = CustomRequire; | ||
//# sourceMappingURL=CustomRequire.js.map |
@@ -7,3 +7,13 @@ { | ||
}, | ||
"dependencies": {}, | ||
"files": [ | ||
"dist/", | ||
"LICENSE" | ||
], | ||
"scripts": { | ||
"build": "npm run build-ts", | ||
"build-ts": "tsc" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^6.0.46" | ||
}, | ||
"description": "Detect what dependencies a module has", | ||
@@ -18,3 +28,3 @@ "keywords": [ | ||
"license": "MIT", | ||
"main": "./dist/CustomRequire.js", | ||
"main": "./dist/CustomRequire", | ||
"name": "custom-require", | ||
@@ -25,3 +35,3 @@ "repository": { | ||
}, | ||
"version": "0.0.3" | ||
"version": "0.0.4" | ||
} |
@@ -15,2 +15,4 @@ # Custom Require | ||
require("react"); | ||
module.exports = "yay"; | ||
``` | ||
@@ -48,3 +50,7 @@ | ||
// Second walker will receive all the dependencies too | ||
secondWalker.require("./test"); | ||
// The require method will work as the default require one, returning the exports contents | ||
var yay = secondWalker.require("./test"); | ||
// Yay it | ||
console.log(yay); | ||
``` | ||
@@ -62,2 +68,10 @@ | ||
## Typescript | ||
Because of Typescript limitations when exporting (See https://github.com/Microsoft/TypeScript/issues/2719), you need to use this syntax to import the library: | ||
```ts | ||
/* FILE: main.ts */ | ||
import CustomRequire = require("custom-require"); | ||
``` | ||
## Limitations | ||
@@ -83,2 +97,2 @@ | ||
NOTE: Custom Require will start tracking right after requiring it. Is not necessary to immediatly create an instance. | ||
NOTE: Custom Require will start tracking right after requiring it. Is not necessary to immediatly create an instance. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
10412
79
95
1