diff options
author | Pierre Frisch | 2011-12-22 07:25:50 -0800 |
---|---|---|
committer | Valerio Virgillito | 2012-01-27 11:18:17 -0800 |
commit | b89a7ee8b956c96a1dcee995ea840feddc5d4b27 (patch) | |
tree | 0f3136ab0ecdbbbed6a83576581af0a53124d6f1 /node_modules/montage/require | |
parent | 2401f05d1f4b94d45e4568b81fc73e67b969d980 (diff) | |
download | ninja-b89a7ee8b956c96a1dcee995ea840feddc5d4b27.tar.gz |
First commit of Ninja to ninja-internal
Signed-off-by: Valerio Virgillito <rmwh84@motorola.com>
Diffstat (limited to 'node_modules/montage/require')
-rwxr-xr-x | node_modules/montage/require/browser.js | 239 | ||||
-rwxr-xr-x | node_modules/montage/require/require.js | 871 |
2 files changed, 1110 insertions, 0 deletions
diff --git a/node_modules/montage/require/browser.js b/node_modules/montage/require/browser.js new file mode 100755 index 00000000..9094397b --- /dev/null +++ b/node_modules/montage/require/browser.js | |||
@@ -0,0 +1,239 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | bootstrap("require/browser", function (require) { | ||
7 | |||
8 | var CJS = require("require/require"); | ||
9 | var Promise = require("core/promise").Promise; | ||
10 | var URL = require("core/url"); | ||
11 | |||
12 | var global = typeof global !== "undefined" ? global : window; | ||
13 | |||
14 | CJS.pwd = function() { | ||
15 | return URL.resolve(window.location, "."); | ||
16 | }; | ||
17 | |||
18 | function ScriptLoader(options) { | ||
19 | var pendingDefinitions = []; | ||
20 | var pendingScripts = {}; | ||
21 | |||
22 | window.define._subscribe(function(definition) { | ||
23 | if (definition.path && pendingScripts[definition.path]) { | ||
24 | pendingDefinitions.push(definition); | ||
25 | } else { | ||
26 | console.log("Ignoring "+definition.path + " (possibly concurrent )") | ||
27 | } | ||
28 | }); | ||
29 | |||
30 | return function(url, callback) { | ||
31 | if (!callback) { | ||
32 | CJS.console.warn("ScriptLoader does not support synchronous loading ("+url+")."); | ||
33 | return null; | ||
34 | } | ||
35 | |||
36 | // Firefox does not fire script tag events correct for scripts loaded from file:// | ||
37 | // This only runs if loaded from file:// | ||
38 | // TODO: make a configuration option to run only in debug mode? | ||
39 | if (HACK_checkFirefoxFileURL(url)) { | ||
40 | callback(null); | ||
41 | return; | ||
42 | } | ||
43 | |||
44 | var normalUrl = URL.resolve(url, ""); | ||
45 | pendingScripts[normalUrl] = true; | ||
46 | |||
47 | var script = document.createElement("script"); | ||
48 | script.onload = function() { | ||
49 | if (pendingDefinitions.length === 0) { | ||
50 | // Script tags seem to fire onload even for 404 status code in some browsers (Chrome, Safari). | ||
51 | // CJS.console.warn("No pending script definitions."); | ||
52 | } else if (pendingDefinitions.length > 1) { | ||
53 | CJS.console.warn("Support for multiple script definitions per file is not yet implemented."); | ||
54 | } | ||
55 | var definition = pendingDefinitions.pop(); | ||
56 | if (definition) { | ||
57 | finish(options.compiler(definition)) | ||
58 | } else { | ||
59 | finish(null); | ||
60 | } | ||
61 | } | ||
62 | script.onerror = function() { | ||
63 | if (pendingDefinitions.length !== 0) { | ||
64 | CJS.console.warn("Extra pending script definitions!"); | ||
65 | } | ||
66 | finish(null); | ||
67 | } | ||
68 | script.src = url; | ||
69 | document.getElementsByTagName("head")[0].appendChild(script); | ||
70 | |||
71 | function finish(result) { | ||
72 | pendingScripts[normalUrl] = false; | ||
73 | script.parentNode.removeChild(script); | ||
74 | callback(result); | ||
75 | } | ||
76 | } | ||
77 | } | ||
78 | |||
79 | function HACK_checkFirefoxFileURL(url) { | ||
80 | if (window.navigator.userAgent.indexOf("Firefox") >= 0) { | ||
81 | var protocol = url.match(/^([a-zA-Z]+:\/\/)?/)[1]; | ||
82 | if (protocol === "file://" || (!protocol && window.location.protocol === "file:")) { | ||
83 | try { | ||
84 | var req = new XMLHttpRequest(); | ||
85 | req.open("GET", url, false); | ||
86 | req.send(); | ||
87 | return !xhrSuccess(req); | ||
88 | } catch (e) { | ||
89 | return true; | ||
90 | } | ||
91 | } | ||
92 | } | ||
93 | return false; | ||
94 | } | ||
95 | |||
96 | CJS.overlays = ["browser"]; | ||
97 | |||
98 | // Due to crazy variabile availability of new and old XHR APIs across | ||
99 | // platforms, this implementation registers every known name for the event | ||
100 | // listeners. The promise library ascertains that the returned promise | ||
101 | // is resolved only by the first event. | ||
102 | // http://dl.dropbox.com/u/131998/yui/misc/get/browser-capabilities.html | ||
103 | CJS.read = function (url, options) { | ||
104 | var request = new XMLHttpRequest(); | ||
105 | var response = Promise.defer(); | ||
106 | |||
107 | function onload() { | ||
108 | if (xhrSuccess(request)) { | ||
109 | response.resolve(request.responseText); | ||
110 | } else { | ||
111 | response.reject("Can't XHR " + JSON.stringify(url)); | ||
112 | } | ||
113 | } | ||
114 | |||
115 | function onerror() { | ||
116 | response.reject("Can't XHR " + JSON.stringify(url)); | ||
117 | } | ||
118 | |||
119 | try { | ||
120 | request.open("GET", url, true); | ||
121 | options && options.overrideMimeType && request.overrideMimeType && | ||
122 | request.overrideMimeType(options.overrideMimeType); | ||
123 | request.onreadystatechange = function () { | ||
124 | if (request.readyState === 4) { | ||
125 | onload(); | ||
126 | } | ||
127 | }; | ||
128 | request.onload = request.load = onload; | ||
129 | request.onerror = request.error = onerror; | ||
130 | } catch (exception) { | ||
131 | response.reject(exception.message, exception); | ||
132 | } | ||
133 | |||
134 | request.send(); | ||
135 | return response.promise; | ||
136 | }; | ||
137 | |||
138 | function XHRLoader(options) { | ||
139 | return function(url, callback) { | ||
140 | CJS.read(url, { | ||
141 | overrideMimeType: "application/javascript" | ||
142 | }).then(function (content) { | ||
143 | if (/^\s*define\s*\(/.test(content)) { | ||
144 | CJS.console.log("Detected async module definition, load with script loader instead."); | ||
145 | callback(null); | ||
146 | } else { | ||
147 | callback(options.compiler({ text : content, path : url })); | ||
148 | } | ||
149 | }, function (error) { | ||
150 | console.warn(error); | ||
151 | callback(null); | ||
152 | }); | ||
153 | } | ||
154 | } | ||
155 | |||
156 | function CachingXHRLoader(options) { | ||
157 | return CJS.CachingLoader(options, XHRLoader(options)); | ||
158 | } | ||
159 | |||
160 | function CachingScriptLoader(options) { | ||
161 | return CJS.CachingLoader(options, ScriptLoader(options)); | ||
162 | } | ||
163 | |||
164 | // Determine if an XMLHttpRequest was successful | ||
165 | // Some versions of WebKit return 0 for successful file:// URLs | ||
166 | function xhrSuccess(req) { | ||
167 | return (req.status === 200 || (req.status === 0 && req.responseText)); | ||
168 | } | ||
169 | |||
170 | // By using a named "eval" most browsers will execute in the global scope. | ||
171 | // http://www.davidflanagan.com/2010/12/global-eval-in.html | ||
172 | // Unfortunately execScript doesn't always return the value of the evaluated expression (at least in Chrome) | ||
173 | var globalEval = /*this.execScript ||*/eval; | ||
174 | // For Firebug evaled code isn't debuggable otherwise | ||
175 | // http://code.google.com/p/fbug/issues/detail?id=2198 | ||
176 | if (global.navigator && global.navigator.userAgent.indexOf("Firefox") >= 0) { | ||
177 | globalEval = new Function("evalString", "return eval(evalString)"); | ||
178 | } | ||
179 | |||
180 | CJS.BrowserCompiler = function(config) { | ||
181 | return function(def) { | ||
182 | if (def.factory) | ||
183 | return def; | ||
184 | |||
185 | // Here we use a couple tricks to make debugging better in various browsers: | ||
186 | // TODO: determine if these are all necessary / the best options | ||
187 | // 1. name the function with something inteligible since some debuggers display the first part of each eval (Firebug) | ||
188 | // 2. append the "//@ sourceURL=path" hack (Safari, Chrome, Firebug) | ||
189 | // * http://pmuellr.blogspot.com/2009/06/debugger-friendly.html | ||
190 | // * http://blog.getfirebug.com/2009/08/11/give-your-eval-a-name-with-sourceurl/ | ||
191 | // TODO: investigate why this isn't working in Firebug. | ||
192 | // 3. set displayName property on the factory function (Safari, Chrome) | ||
193 | |||
194 | var displayName = "__FILE__"+def.path.replace(/\.\w+$|\W/g, "__"); | ||
195 | var sourceURLComment = "\n//@ sourceURL="+def.path; | ||
196 | |||
197 | def.factory = globalEval("(function "+displayName+"(require, exports, module) {"+def.text+"//*/\n})"+sourceURLComment); | ||
198 | |||
199 | // This should work and would be better, but Firebug does not show scripts executed via "new Function()" constructor. | ||
200 | // TODO: sniff browser? | ||
201 | // def.factory = new Function("require", "exports", "module", def.text + "\n//*/"+sourceURLComment); | ||
202 | |||
203 | delete def.text; | ||
204 | |||
205 | def.factory.displayName = displayName; | ||
206 | |||
207 | return def; | ||
208 | } | ||
209 | } | ||
210 | |||
211 | CJS.DefaultCompilerConstructor = function(config) { | ||
212 | return CJS.DefaultCompilerMiddleware(config, CJS.BrowserCompiler(config)); | ||
213 | } | ||
214 | |||
215 | // Try multiple paths | ||
216 | // Try XHRLoader then ScriptLoader | ||
217 | // ScriptLoader should probably always come after XHRLoader in case it's an unwrapped module | ||
218 | CJS.DefaultLoaderConstructor = function(options) { | ||
219 | var loaders = []; | ||
220 | if (options.xhr !== false) | ||
221 | loaders.push(CachingXHRLoader(options)); | ||
222 | if (options.script !== false) | ||
223 | loaders.push(CachingScriptLoader(options)); | ||
224 |