Permalink
Browse files

Add option to not unload the require cache for matches on [un]hooking

  • Loading branch information...
1 parent b61780f commit b23c6e2ef6f6908532bc52a94a501ecc6fdccd89 @gotwarlost gotwarlost committed Nov 2, 2012
Showing with 13 additions and 3 deletions.
  1. +13 −3 lib/hook.js
View
@@ -35,7 +35,12 @@ var fs = require('fs'),
vm = require('vm'),
originalLoader = Module._extensions['.js'],
originalCreateScript = vm.createScript,
- lastMatcher;
+ lastMatcher,
+ destructive = true;
+
+function setDestructive(flag) {
+ destructive = !!flag;
+}
function transformFn(matcher, transformer, verbose) {
@@ -102,7 +107,9 @@ function hookRequire(matcher, transformer, options) {
postLoadHook(filename);
}
};
- unloadRequireCache(matcher);
+ if (destructive) {
+ unloadRequireCache(matcher);
+ }
}
/**
* unhook `require` to restore it to its original state. Also unloads the modules in
@@ -113,7 +120,9 @@ function hookRequire(matcher, transformer, options) {
*/
function unhookRequire() {
Module._extensions['.js'] = originalLoader;
- unloadRequireCache(lastMatcher);
+ if (destructive) {
+ unloadRequireCache(lastMatcher);
+ }
}
/**
* hooks `vm.createScript` to return transformed code out of which a `Script` object will be created.
@@ -146,6 +155,7 @@ function unhookCreateScript() {
}
module.exports = {
+ setDestructive: setDestructive,
hookRequire: hookRequire,
unhookRequire: unhookRequire,
hookCreateScript: hookCreateScript,

0 comments on commit b23c6e2

Please sign in to comment.