diff options
author | Valerio Virgillito | 2012-02-02 00:11:51 -0800 |
---|---|---|
committer | Valerio Virgillito | 2012-02-02 15:33:42 -0800 |
commit | a3024011a91d3941f81481dd4d600e9684eb0fd4 (patch) | |
tree | 084b4856910f1db53973dd11617f7ffa03a6dd46 /node_modules/montage/require | |
parent | 97255032921178bdfbc25512ddf3e0867e353f7a (diff) | |
download | ninja-a3024011a91d3941f81481dd4d600e9684eb0fd4.tar.gz |
upgrading to Montage v0.6
Signed-off-by: Valerio Virgillito <valerio@motorola.com>
Diffstat (limited to 'node_modules/montage/require')
-rwxr-xr-x | node_modules/montage/require/browser.js | 183 | ||||
-rw-r--r-- | node_modules/montage/require/node.js | 101 | ||||
-rwxr-xr-x | node_modules/montage/require/require.js | 997 |
3 files changed, 541 insertions, 740 deletions
diff --git a/node_modules/montage/require/browser.js b/node_modules/montage/require/browser.js index 9094397b..e249d355 100755 --- a/node_modules/montage/require/browser.js +++ b/node_modules/montage/require/browser.js | |||
@@ -5,102 +5,29 @@ | |||
5 | </copyright> */ | 5 | </copyright> */ |
6 | bootstrap("require/browser", function (require) { | 6 | bootstrap("require/browser", function (require) { |
7 | 7 | ||
8 | var CJS = require("require/require"); | 8 | var Require = require("require/require"); |
9 | var Promise = require("core/promise").Promise; | 9 | var Promise = require("core/promise").Promise; |
10 | var URL = require("core/url"); | 10 | var URL = require("core/mini-url"); |
11 | 11 | ||
12 | var global = typeof global !== "undefined" ? global : window; | 12 | var global = typeof global !== "undefined" ? global : window; |
13 | 13 | ||
14 | CJS.pwd = function() { | 14 | Require.getLocation = function() { |
15 | return URL.resolve(window.location, "."); | 15 | return URL.resolve(window.location, "."); |
16 | }; | 16 | }; |
17 | 17 | ||
18 | function ScriptLoader(options) { | 18 | Require.overlays = ["browser", "montage"]; |
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 | 19 | ||
98 | // Due to crazy variabile availability of new and old XHR APIs across | 20 | // Due to crazy variabile availability of new and old XHR APIs across |
99 | // platforms, this implementation registers every known name for the event | 21 | // platforms, this implementation registers every known name for the event |
100 | // listeners. The promise library ascertains that the returned promise | 22 | // listeners. The promise library ascertains that the returned promise |
101 | // is resolved only by the first event. | 23 | // is resolved only by the first event. |
102 | // http://dl.dropbox.com/u/131998/yui/misc/get/browser-capabilities.html | 24 | // http://dl.dropbox.com/u/131998/yui/misc/get/browser-capabilities.html |
103 | CJS.read = function (url, options) { | 25 | Require.read = function (url) { |
26 | |||
27 | if (URL.resolve(window.location, url).indexOf("file:") === 0) { | ||
28 | throw new Error("XHR does not function for file: protocol"); | ||
29 | } | ||
30 | |||
104 | var request = new XMLHttpRequest(); | 31 | var request = new XMLHttpRequest(); |
105 | var response = Promise.defer(); | 32 | var response = Promise.defer(); |
106 | 33 | ||
@@ -108,7 +35,7 @@ CJS.read = function (url, options) { | |||
108 | if (xhrSuccess(request)) { | 35 | if (xhrSuccess(request)) { |
109 | response.resolve(request.responseText); | 36 | response.resolve(request.responseText); |
110 | } else { | 37 | } else { |
111 | response.reject("Can't XHR " + JSON.stringify(url)); | 38 | onerror(); |
112 | } | 39 | } |
113 | } | 40 | } |
114 | 41 | ||
@@ -118,8 +45,7 @@ CJS.read = function (url, options) { | |||
118 | 45 | ||
119 | try { | 46 | try { |
120 | request.open("GET", url, true); | 47 | request.open("GET", url, true); |
121 | options && options.overrideMimeType && request.overrideMimeType && | 48 | request.overrideMimeType("application/javascript"); |
122 | request.overrideMimeType(options.overrideMimeType); | ||
123 | request.onreadystatechange = function () { | 49 | request.onreadystatechange = function () { |
124 | if (request.readyState === 4) { | 50 | if (request.readyState === 4) { |
125 | onload(); | 51 | onload(); |
@@ -135,32 +61,6 @@ CJS.read = function (url, options) { | |||
135 | return response.promise; | 61 | return response.promise; |
136 | }; | 62 | }; |
137 | 63 | ||
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 | 64 | // Determine if an XMLHttpRequest was successful |
165 | // Some versions of WebKit return 0 for successful file:// URLs | 65 | // Some versions of WebKit return 0 for successful file:// URLs |
166 | function xhrSuccess(req) { | 66 | function xhrSuccess(req) { |
@@ -177,63 +77,36 @@ if (global.navigator && global.navigator.userAgent.indexOf("Firefox") >= 0) { | |||
177 | globalEval = new Function("evalString", "return eval(evalString)"); | 77 | globalEval = new Function("evalString", "return eval(evalString)"); |
178 | } | 78 | } |
179 | 79 | ||
180 | CJS.BrowserCompiler = function(config) { | 80 | Require.Compiler = function (config) { |
181 | return function(def) { | 81 | return function(module) { |
182 | if (def.factory) | 82 | if (module.factory || module.text === void 0) |
183 | return def; | 83 | return module; |
184 | 84 | ||
185 | // Here we use a couple tricks to make debugging better in various browsers: | 85 | // Here we use a couple tricks to make debugging better in various browsers: |
186 | // TODO: determine if these are all necessary / the best options | 86 | // 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) | 87 | // 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) | 88 | // 2. append the "//@ sourceURL=location" hack (Safari, Chrome, Firebug) |
189 | // * http://pmuellr.blogspot.com/2009/06/debugger-friendly.html | 89 | // * http://pmuellr.blogspot.com/2009/06/debugger-friendly.html |
190 | // * http://blog.getfirebug.com/2009/08/11/give-your-eval-a-name-with-sourceurl/ | 90 | // * http://blog.getfirebug.com/2009/08/11/give-your-eval-a-name-with-sourceurl/ |
191 | // TODO: investigate why this isn't working in Firebug. | 91 | // TODO: investigate why this isn't working in Firebug. |
192 | // 3. set displayName property on the factory function (Safari, Chrome) | 92 | // 3. set displayName property on the factory function (Safari, Chrome) |
193 | 93 | ||
194 | var displayName = "__FILE__"+def.path.replace(/\.\w+$|\W/g, "__"); | 94 | var displayName = "__FILE__"+module.location.replace(/\.\w+$|\W/g, "__"); |
195 | var sourceURLComment = "\n//@ sourceURL="+def.path; | ||
196 | 95 | ||
197 | def.factory = globalEval("(function "+displayName+"(require, exports, module) {"+def.text+"//*/\n})"+sourceURLComment); | 96 | try { |
97 | module.factory = globalEval("(function "+displayName+"(require, exports, module) {"+module.text+"//*/\n})"+"\n//@ sourceURL="+module.location); | ||
98 | } catch (exception) { | ||