custom-require
Advanced tools
Comparing version 0.1.5 to 0.1.6
@@ -9,3 +9,6 @@ /// <reference types="node" /> | ||
__invalidateCache: () => void; | ||
__checkInvalid: () => boolean; | ||
__whoRequired: () => CustomNodeModule[]; | ||
__getChildModules: () => CustomNodeModule[]; | ||
__invalid: boolean; | ||
} | ||
@@ -12,0 +15,0 @@ export declare class CustomRequire { |
@@ -18,4 +18,8 @@ "use strict"; | ||
} | ||
var cachedModule = this.getCachedModule(id, callerModule); | ||
if (cachedModule && cachedModule.__checkInvalid()) { | ||
this.unrequire(cachedModule, undefined, true); | ||
} | ||
var res = callerModule.require(id); | ||
var cachedModule = this.getCachedModule(id, callerModule); | ||
cachedModule = this.getCachedModule(id, callerModule); | ||
cachedModule.__addCustomRequire(this); | ||
@@ -66,3 +70,12 @@ return res; | ||
exports.CustomRequire = CustomRequire; | ||
Module.__customCache = {}; | ||
Module.prototype.__require = Module.prototype.require; | ||
Module.prototype.__getChildModules = function () { | ||
var list = []; | ||
for (var _i = 0, _a = this.__childModules; _i < _a.length; _i++) { | ||
var childModule = _a[_i]; | ||
list = list.concat(childModule.__getChildModules()); | ||
} | ||
return list; | ||
}; | ||
Module.prototype.__cleanCalled = function (customRequire, mod) { | ||
@@ -100,2 +113,3 @@ var list = []; | ||
this.__customRequires.splice(this.__customRequires.indexOf(customRequire), 1); | ||
customRequire.attachedModules.splice(customRequire.attachedModules.indexOf(this), 1); | ||
list = this.__cleanCalled(customRequire, this); | ||
@@ -139,4 +153,16 @@ } | ||
}; | ||
Module.prototype.__checkInvalid = function () { | ||
if (this.__invalid) { | ||
return true; | ||
} | ||
for (var _i = 0, _a = this.__childModules; _i < _a.length; _i++) { | ||
var childModule = _a[_i]; | ||
if (childModule.__checkInvalid()) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
Module.prototype.__invalidateCache = function () { | ||
delete Module._cache[this.filename]; | ||
this.__invalid = true; | ||
for (var _i = 0, _a = this.__childModules; _i < _a.length; _i++) { | ||
@@ -173,6 +199,32 @@ var childModule = _a[_i]; | ||
Module.prototype.require = function (path) { | ||
var _this = this; | ||
this.__initialize(); | ||
var res = this.__require(path); | ||
var requiredFilename = Module._resolveFilename(path, this, false); | ||
var customCache = Module.__customCache[requiredFilename]; | ||
var cachedModule = Module._cache[requiredFilename]; | ||
if (cachedModule && cachedModule.__invalid) { | ||
delete Module._cache[requiredFilename]; | ||
} | ||
var res; | ||
(function () { | ||
try { | ||
res = _this.__require(path); | ||
} | ||
catch (e) { | ||
if (!customCache) { | ||
throw e; | ||
} | ||
res = customCache.exports; | ||
Module._cache[requiredFilename] = customCache; | ||
} | ||
})(); | ||
if (!cachedModule || cachedModule.__invalid) { | ||
cachedModule = Module._cache[requiredFilename]; | ||
} | ||
if (cachedModule && !cachedModule.__invalid) { | ||
Module.__customCache[requiredFilename] = cachedModule; | ||
} | ||
else { | ||
cachedModule = customCache; | ||
} | ||
if (cachedModule && this.__childModules.indexOf(cachedModule) < 0) { | ||
@@ -179,0 +231,0 @@ for (var i = 0; i < this.__childModules.length; i++) { |
@@ -34,3 +34,3 @@ { | ||
}, | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"dependencies": { | ||
@@ -37,0 +37,0 @@ "callsite": "^1.0.0" |
Sorry, the diff of this file is not supported yet
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
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
23919
265