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

browserifyMiddleware.js « browserify « lib - github.com/twbs/rewire.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d62352ef9e3f9dc35c05fea46d3558dbe87973e8 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
var setterSrc = require("../__set__.js").toString(),
    getterSrc = require("../__get__.js").toString(),
    fs = require("fs"),
    path = require("path"),
    getImportGlobalsSrc = require("../getImportGlobalsSrc.js"),
    getRewireRequires = require("./getRewireRequires.js"),
    detectStrictMode = require("../detectStrictMode.js"),

    browserInit = fs.readFileSync(__dirname + "/browserInit.js", "utf8"),
    importGlobalsSrc = getImportGlobalsSrc(),
    injectionSrc = getInjectionSrc().replace(/\s+/g, " ");   // strip out unnecessary spaces to be unobtrusive in the debug view

/**
 * Returns a string that gets injected at the beginning of every module. Its purpose is to
 *
 * - register the setters and getters according to the module's filename
 * - override the internal require with a require proxy.
 *
 * @return {String}
 */
function getInjectionSrc() {
    // Registers the setters and getters of every module according to their filename. The setters and getters must be
    // injected as string here to gain access to the private scope of the module.
    return  'require("rewire").register(__filename, ' + setterSrc + ', ' + getterSrc + ');' +
    // Overrides the module internal require with a require proxy. This proxy is necessary to call rewire with the
    // module's filename at the first parameter to resolve the path. This way rewire() works exactly like require().
            'require = window.browserifyRequire.getProxy(require, __filename);';
}

function browserifyMiddleware(b) {
    function injectRewire(src, filename) {
        var rewireRequires,
            strictMode = "";

        // Search for all rewire() statements an return the required path.
        rewireRequires = getRewireRequires(src);

        // Add all modules that are loaded by rewire() manually to browserify because browserify's
        // require-sniffing doesn't work here.
        rewireRequires.forEach(function forEachRewireRequire(requirePath) {
            // Resolve absolute paths
            if (requirePath.charAt(0) === ".") {
                requirePath = path.resolve(path.dirname(filename), requirePath);
            }
            b.require(requirePath);
        });

        // If the module uses strict mode we must ensure that "use strict" stays at the beginning of the module.
        if (detectStrictMode(src) === true) {
            strictMode = ' "use strict"; ';
        }

        // Convert back slashes to normal slashes.
        filename = filename.replace(/\\/g, "/");

        // We don't want to inject this code at the beginning of a rewire/lib-module. Otherwise
        // it would cause a black hole that devours our universe.
        if (filename.indexOf("/rewire/lib") === -1) {
            src =
                strictMode +    // either '' or ' "use strict"; '
                "/* this line was injected by rewire() */" +
                "var global = window; " +   // window is our new global object
                importGlobalsSrc +
                injectionSrc + "\n" +
                src;
        }

        return src;
    }

    // Register file handler
    b.register(".js", injectRewire);
    // Append rewire initialization at the end of the bundle
    b.append(browserInit);

    return b;
}

module.exports = browserifyMiddleware;