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

appendix.js « browserify « lib - github.com/twbs/rewire.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 02fd9c114a10ceb1ba1fdeedcdbc40b766ee9c33 (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
34
35
36
37
38
39
40
41
42
43
44
/**
 * This code gets injected at the end of the browserify bundle via b.append().
 */

if (typeof window.browserifyRequire !== "undefined") {
    throw new Error("Naming collision detected: window.browserifyRequire seems to be occupied.");
}

// Saves the browserifyRequire under a new name. Necessary to call the original browserifyRequire within
// a module where the variable name "require" is overridden by the module's internal require.
window.browserifyRequire = require;

/**
 * Provides a special require-proxy. Every module calls window.browserifyRequire.getProxy(require, __filename) at the
 * beginning and overrides its own require with this proxy.
 *
 * This is necessary to call rewire() with the original __filename. Thus you can use rewire() like require().
 *
 * @param {!Function} internalRequire the module's own require
 * @param {String} filename the __filename of the module
 * @return {Function} requireProxy
 */
window.browserifyRequire.getProxy = function (internalRequire, filename) {
    var rewireModule = internalRequire("rewire"),
        key;

    function rewireProxy(path, cache) {
        return rewireModule(filename, path, cache);
    }

    for (key in rewireModule) {
        if (rewireModule.hasOwnProperty(key)) {
            rewireProxy[key] = rewireModule[key];
        }
    }

    return function requireProxy(path) {
        if (path === "rewire") {
            return rewireProxy;
        } else {
            return internalRequire(path);
        }
    };
};