Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

  • Loading branch information...
commit b23c6e2ef6f6908532bc52a94a501ecc6fdccd89 1 parent b61780f
@gotwarlost gotwarlost authored
Showing with 13 additions and 3 deletions.
  1. +13 −3 lib/hook.js
View
16 lib/hook.js
@@ -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,
Please sign in to comment.
Something went wrong with that request. Please try again.