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

index.js « lib - github.com/twbs/rewire.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 67633999c575dfae536f60a5c7f9413d8b91918c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
var rewireModule;

/**
 * This function is needed to determine the calling parent module.
 * Thus rewire acts exactly the same like require() in the test module.
 *
 * @param {!String} request Path to the module that shall be rewired. Use it exactly like require().
 * @param {Boolean} cache Indicates whether the rewired module should be cached by node so subsequent calls of require() will return the rewired module. Subsequent calls of rewire() will always overwrite the cache.
 * @return {*} the rewired module
 */
function rewire(request, cache) {
    delete require.cache[__filename];   // deleting self from module cache so the parent module is always up to date

    if (cache === undefined) {
        cache = true;
    }

    return rewireModule(module.parent, request, cache);
}

// Conditional require for different environments
if (process.title === "browser") {
    module.exports = require("./browserify/browserifyRewire.js");
} else {
    // Putting (require) within brackets is a hack to disable browserify's require sniffing
    // @see https://github.com/substack/node-browserify/issues/132#issuecomment-5281470
    rewireModule = (require)("./rewire.js");

    rewire.reset = rewireModule.reset;
    rewire.browserify = (require)("./browserify/browserifyMiddleware.js");

    module.exports = rewire;
}