aboutsummaryrefslogtreecommitdiff
path: root/node_modules/montage/require/browser.js
blob: 9094397bbd35a1e5793bfd8b108ca7e354cb6f13 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* <copyright>
 This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
 No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
 (c) Copyright 2011 Motorola Mobility, Inc.  All Rights Reserved.
 </copyright> */
bootstrap("require/browser", function (require) {

var CJS = require("require/require");
var Promise = require("core/promise").Promise;
var URL = require("core/url");

var global = typeof global !== "undefined" ? global : window;

CJS.pwd = function() {
    return URL.resolve(window.location, ".");
};

function ScriptLoader(options) {
    var pendingDefinitions = [];
    var pendingScripts = {};

    window.define._subscribe(function(definition) {
        if (definition.path && pendingScripts[definition.path]) {
            pendingDefinitions.push(definition);
        } else {
            console.log("Ignoring "+definition.path + " (possibly concurrent )")
        }
    });

    return function(url, callback) {
        if (!callback) {
            CJS.console.warn("ScriptLoader does not support synchronous loading ("+url+").");
            return null;
        }

        // Firefox does not fire script tag events correct for scripts loaded from file://
        // This only runs if loaded from file://
        // TODO: make a configuration option to run only in debug mode?
        if (HACK_checkFirefoxFileURL(url)) {
            callback(null);
            return;
        }

        var normalUrl = URL.resolve(url, "");
        pendingScripts[normalUrl] = true;

        var script = document.createElement("script");
        script.onload = function() {
            if (pendingDefinitions.length === 0) {
                // Script tags seem to fire onload even for 404 status code in some browsers (Chrome, Safari).
                // CJS.console.warn("No pending script definitions.");
            } else if (pendingDefinitions.length > 1) {
                CJS.console.warn("Support for multiple script definitions per file is not yet implemented.");
            }
            var definition = pendingDefinitions.pop();
            if (definition) {
                finish(options.compiler(definition))
            } else {
                finish(null);
            }
        }
        script.onerror = function() {
            if (pendingDefinitions.length !== 0) {
                CJS.console.warn("Extra pending script definitions!");
            }
            finish(null);
        }
        script.src = url;
        document.getElementsByTagName("head")[0].appendChild(script);

        function finish(result) {
            pendingScripts[normalUrl] = false;
            script.parentNode.removeChild(script);
            callback(result);
        }
    }
}

function HACK_checkFirefoxFileURL(url) {
    if (window.navigator.userAgent.indexOf("Firefox") >= 0) {
        var protocol = url.match(/^([a-zA-Z]+:\/\/)?/)[1];
        if (protocol === "file://" || (!protocol && window.location.protocol === "file:")) {
            try {
                var req = new XMLHttpRequest();
                req.open("GET", url, false);
                req.send();
                return !xhrSuccess(req);
            } catch (e) {
                return true;
            }
        }
    }
    return false;
}

CJS.overlays = ["browser"];

// Due to crazy variabile availability of new and old XHR APIs across
// platforms, this implementation registers every known name for the event
// listeners.  The promise library ascertains that the returned promise
// is resolved only by the first event.
// http://dl.dropbox.com/u/131998/yui/misc/get/browser-capabilities.html
CJS.read = function (url, options) {
    var request = new XMLHttpRequest();
    var response = Promise.defer();

    function onload() {
        if (xhrSuccess(request)) {
            response.resolve(request.responseText);
        } else {
            response.reject("Can't XHR " + JSON.stringify(url));
        }
    }

    function onerror() {
        response.reject("Can't XHR " + JSON.stringify(url));
    }

    try {
        request.open("GET", url, true);
        options && options.overrideMimeType && request.overrideMimeType &&
            request.overrideMimeType(options.overrideMimeType);
        request.onreadystatechange = function () {
            if (request.readyState === 4) {
                onload();
            }
        };
        request.onload = request.load = onload;
        request.onerror = request.error = onerror;
    } catch (exception) {
        response.reject(exception.message, exception);
    }

    request.send();
    return response.promise;
};

function XHRLoader(options) {
    return function(url, callback) {
        CJS.read(url, {
            overrideMimeType: "application/javascript"
        }).then(function (content) {
            if (/^\s*define\s*\(/.test(content)) {
                CJS.console.log("Detected async module definition, load with script loader instead.");
                callback(null);
            } else {
                callback(options.compiler({ text : content, path : url }));
            }
        }, function (error) {
            console.warn(error);
            callback(null);
        });
    }
}

function CachingXHRLoader(options) {
    return CJS.CachingLoader(options, XHRLoader(options));
}

function CachingScriptLoader(options) {
    return CJS.CachingLoader(options, ScriptLoader(options));
}

// Determine if an XMLHttpRequest was successful
// Some versions of WebKit return 0 for successful file:// URLs
function xhrSuccess(req) {
    return (req.status === 200 || (req.status === 0 && req.responseText));
}

// By using a named "eval" most browsers will execute in the global scope.
// http://www.davidflanagan.com/2010/12/global-eval-in.html
// Unfortunately execScript doesn't always return the value of the evaluated expression (at least in Chrome)
var globalEval = /*this.execScript ||*/eval;
// For Firebug evaled code isn't debuggable otherwise
// http://code.google.com/p/fbug/issues/detail?id=2198
if (global.navigator && global.navigator.userAgent.indexOf("Firefox") >= 0) {
    globalEval = new Function("evalString", "return eval(evalString)");
}

CJS.BrowserCompiler = function(config) {
    return function(def) {
        if (def.factory)
            return def;

        // Here we use a couple tricks to make debugging better in various browsers:
        // TODO: determine if these are all necessary / the best options
        // 1. name the function with something inteligible since some debuggers display the first part of each eval (Firebug)
        // 2. append the "//@ sourceURL=path" hack (Safari, Chrome, Firebug)
        //  * http://pmuellr.blogspot.com/2009/06/debugger-friendly.html
        //  * http://blog.getfirebug.com/2009/08/11/give-your-eval-a-name-with-sourceurl/
        //      TODO: investigate why this isn't working in Firebug.
        // 3. set displayName property on the factory function (Safari, Chrome)

        var displayName = "__FILE__"+def.path.replace(/\.\w+$|\W/g, "__");
        var sourceURLComment = "\n//@ sourceURL="+def.path;

        def.factory = globalEval("(function "+displayName+"(require, exports, module) {"+def.text+"//*/\n})"+sourceURLComment);

        // This should work and would be better, but Firebug does not show scripts executed via "new Function()" constructor.
        // TODO: sniff browser?
        // def.factory = new Function("require", "exports", "module", def.text + "\n//*/"+sourceURLComment);

        delete def.text;

        def.factory.displayName = displayName;

        return def;
    }
}

CJS.DefaultCompilerConstructor = function(config) {
    return CJS.DefaultCompilerMiddleware(config, CJS.BrowserCompiler(config));
}

// Try multiple paths
// Try XHRLoader then ScriptLoader
// ScriptLoader should probably always come after XHRLoader in case it's an unwrapped module
CJS.DefaultLoaderConstructor = function(options) {
    var loaders = [];
    if (options.xhr !== false)
        loaders.push(CachingXHRLoader(options));
    if (options.script !== false)
        loaders.push(CachingScriptLoader(options));
    return CJS.Mappings(
        options,
        CJS.Extensions(
            options,
            CJS.Paths(
                options,
                CJS.Multi(
                    options,
                    loaders
                )
            )
        )
    );
}

});