Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/twbs/rewire.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohannes <johannes.ewald@roomieplanet.de>2012-08-20 04:36:35 +0400
committerJohannes <johannes.ewald@roomieplanet.de>2012-08-20 04:36:35 +0400
commit3cc25aec3698c7becd463bd75524a3b2a319ecab (patch)
treecff8c03016f05a7646ed864ca7238e5f9f09b7ce /lib/bundlers/browserify/browserifyRewire.js
parentc5d8fab07f0edc568e45e0747f863afd5876abc2 (diff)
- All tests running
- Added changelog
Diffstat (limited to 'lib/bundlers/browserify/browserifyRewire.js')
-rw-r--r--lib/bundlers/browserify/browserifyRewire.js98
1 files changed, 50 insertions, 48 deletions
diff --git a/lib/bundlers/browserify/browserifyRewire.js b/lib/bundlers/browserify/browserifyRewire.js
index 2798b69..b09b51a 100644
--- a/lib/bundlers/browserify/browserifyRewire.js
+++ b/lib/bundlers/browserify/browserifyRewire.js
@@ -1,5 +1,5 @@
var pathUtil = require("path"),
- getImportGlobalsSrc = require("./getImportGlobalsSrc.js"); /* must be relative to lib/index.js */
+ getImportGlobalsSrc = require("./getImportGlobalsSrc.js"); // must be relative to lib/index.js because of forwardBrowserifyRewire()
/**
* Clones an object deeply. Used to clone the module-object that is
@@ -33,9 +33,7 @@ function clone(obj) {
}
// Saves all setters and getters for every module according to its filename
-var registry = {},
-// Cache for all rewired modules so it can be reset anytime
- rewiredModules = [];
+var registry = {};
/**
* Executes the given module and adds a special setter and getter that allow you to set and get private variables.
@@ -43,30 +41,31 @@ var registry = {},
*
* @param {!String} parentModulePath __filename of the module, that wants to rewire() another module.
* @param {!String} path path to the module that shall be rewired
- * @param {Boolean=true} cache indicates whether the rewired module should be cached or not
- * @return {Object}
+ * @return {Object} the rewired module
*/
-function browserifyRewire(parentModulePath, path, cache) {
- var originalModule,
+function browserifyRewire(parentModulePath, path) {
+ var cached,
+ originalModule,
+ originalExports,
absPath,
- rewiredExports,
- rewiredModule,
+ rewiredExports = {},
registryEntry,
- _require = require; // hide it from browserify
-
- // Default cache to true
- if (cache === undefined) {
- cache = true;
- }
+ _require = require; // hide it from browserify to avoid annoying console warnings
// Normalize path with file extensions
absPath = pathUtil.resolve(parentModulePath, path);
// Retrieve original module from cache
- originalModule = require.cache[absPath];
+ cached = originalModule = require.cache[absPath];
- if (originalModule && cache) {
- // Delete the original module from the cache so the next call to browserifyRequre()
+ if (cached) {
+ // If there is already a module instance in the cache we have to store the original exports-object
+ // manually so it won't be overwritten by the next execution. This is all necessary due to browserify's
+ // odd way of module creation.
+ originalExports = originalModule.exports;
+ originalModule.exports = rewiredExports;
+
+ // Delete the original module from the cache so the next call to _require()
// executes the module
delete require.cache[absPath];
}
@@ -74,26 +73,24 @@ function browserifyRewire(parentModulePath, path, cache) {
// Require module to trigger rewire.register().
_require(absPath);
+ originalModule = require.cache[absPath];
+
+ // Now we're cloning the exports-obj so later modifications of the rewired module won't influence the
+ // cached, original version of this module.
+ rewiredExports = clone(originalModule.exports);
+
+ if (cached) {
+ // Restore original exports
+ originalModule.exports = originalExports;
+ }
+
// Get registry entry of the target module
registryEntry = registry[absPath];
- originalModule = registryEntry.module;
-
- // Make an independent copy of the original module so we can modify the copy
- // without influencing the original module.
- rewiredModule = clone(originalModule);
- rewiredExports = rewiredModule.exports;
// Apply setter and getters
rewiredExports.__set__ = registryEntry.setter;
rewiredExports.__get__ = registryEntry.getter;
- if (cache) {
- require.cache[absPath] = rewiredModule;
- }
-
- // Store rewired modules for rewire.reset()
- rewiredModules.push(absPath);
-
return rewiredExports;
}
@@ -113,22 +110,6 @@ browserifyRewire.register = function (filename, module, setter, getter) {
};
/**
- * Deletes all rewired modules from the cache
- */
-browserifyRewire.reset = function () {
- var cache = require.cache,
- i,
- absPath;
-
- for (i = 0; i < rewiredModules.length; i++) {
- absPath = rewiredModules[i];
- delete cache[absPath];
- }
-
- rewiredModules = [];
-};
-
-/**
* Provides a special require-proxy. Every module calls require("rewire").getProxy(require, __filename) at the
* beginning and overrides its own require with this proxy.
*
@@ -184,4 +165,25 @@ browserifyRewire.getImportGlobalsSrc = function () {
return getImportGlobalsSrc(['require','module','exports','__dirname','__filename','process']);
};
+/**
+ * Returns a new object that inherits from the original module via prototype inheritance.
+ *
+ * Any changes to the module, e.g. assigning another exports-object will now modify the object
+ * instead of original module.
+ *
+ * @param {Object} originalModule
+ * @return {Object} the independent module
+ */
+browserifyRewire.getIndependentModule = function (originalModule) {
+ var independentModule;
+
+ function IndependentModule() {}
+ IndependentModule.prototype = originalModule;
+
+ independentModule = new IndependentModule();
+ independentModule.exports = originalModule.exports;
+
+ return independentModule;
+};
+
module.exports = browserifyRewire; \ No newline at end of file