diff options
Diffstat (limited to 'node_modules/montage/require/require.js')
-rwxr-xr-x | node_modules/montage/require/require.js | 871 |
1 files changed, 871 insertions, 0 deletions
diff --git a/node_modules/montage/require/require.js b/node_modules/montage/require/require.js new file mode 100755 index 00000000..988dd811 --- /dev/null +++ b/node_modules/montage/require/require.js | |||
@@ -0,0 +1,871 @@ | |||
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/require", function (require, CJS) { | ||
7 | |||
8 | var Promise = require("core/promise").Promise; | ||
9 | var URL = require("core/url"); | ||
10 | |||
11 | var global = (function () {return this})(); | ||
12 | var globalEval = eval; // reassigning causes eval to not use lexical scope. | ||
13 | |||
14 | // Non-CommonJS speced extensions should be marked with an "// EXTENSION" comment. | ||
15 | |||
16 | // Sandbox is an instance of the loader system. Different sandboxes will have different instances of modules. | ||
17 | // Returns the root "require" function. If this root "require()" function is called the loader will be in synchronous mode. | ||
18 | // To get asynchronous loading you MUST call the root "require.async()". In async mode all subsequent calls to "require()" will | ||
19 | // be asynchronously loaded, and synchronously executed. | ||
20 | CJS.Sandbox = function(config) { | ||
21 | // Configuration defaults: | ||
22 | config = config || {}; | ||
23 | config.location = URL.resolve(config.location || CJS.pwd(), "."); | ||
24 | config.lib = URL.resolve(config.location, config.lib || "."); | ||
25 | config.paths = config.paths || [config.lib]; | ||
26 | config.mappings = config.mappings || {}; // EXTENSION | ||
27 | config.definitions = config.definitions || {}; | ||
28 | config.modules = config.modules || {}; | ||
29 | config.exposedConfigs = config.exposedConfigs || [ | ||
30 | "paths", | ||
31 | "mappings", | ||
32 | "loader", | ||
33 | "definitions", | ||
34 | "base", | ||
35 | "location", | ||
36 | "packageDescription", | ||
37 | "loadPackage" | ||
38 | ]; | ||
39 | config.makeLoader = config.makeLoader || CJS.DefaultLoaderConstructor; | ||
40 | config.loader = config.loader || config.makeLoader(config); | ||
41 | config.makeCompiler = config.makeCompiler || CJS.DefaultCompilerConstructor; | ||
42 | config.compiler = config.compiler || config.makeCompiler(config); | ||
43 | |||
44 | // Sandbox state: | ||
45 | // Module instances: { exports, id, path, uri } | ||
46 | var modules = config.modules; | ||
47 | // Module definition objects: { factory, dependencies, path } | ||
48 | var definitions = config.definitions; | ||
49 | // Arrays of callbacks to be executed once a module definition has been loaded | ||
50 | var definitionListeners = {}; | ||
51 | // Mapping from canonical IDs to the initial top ID used to load module | ||
52 | var urisToIds = {}; | ||
53 | |||
54 | // Ensures a module definition is loaded before returning or executing the callback. | ||
55 | // Supports multiple calls for the same topId by registering callback as a listener if it has already been initiated. | ||
56 | function loadDefinition(topId, callback) { | ||
57 | if (callback) { | ||
58 | // already loaded | ||
59 | if (has(definitions, topId) && definitions[topId].factory) { | ||
60 | callback(topId); | ||
61 | } | ||
62 | // in progress | ||
63 | else if (has(definitionListeners, topId)) { | ||
64 | definitionListeners[topId].push(callback); | ||
65 | } | ||
66 | // pre-arranged | ||
67 | else if (has(definitions, topId) && definitions[topId].path !== undefined) { | ||
68 | var def = definitions[topId]; | ||
69 | CJS.read(def.path).then(function (text) { | ||
70 | def.text = text; | ||
71 | config.compiler(def); | ||
72 | callback(topId); | ||
73 | }, function (reason) { | ||
74 | console.warn("Can't read " + JSON.stringify(def.path) + ": " + reason); | ||
75 | callback(null); | ||
76 | }); | ||
77 | } | ||
78 | // hasn't started | ||
79 | else { | ||
80 | definitionListeners[topId] = [callback]; | ||
81 | |||
82 | config.loader(topId, function(definition) { | ||
83 | if (!definition) { | ||
84 | CJS.warn("Can't find module " + JSON.stringify(topId)); | ||
85 | } | ||
86 | definitions[topId] = definition || null; | ||
87 | |||
88 | CJS.progress.loadedModules.push([ | ||
89 | config.location, | ||
90 | topId | ||
91 | ].join("#")); | ||
92 | |||
93 | definitionListeners[topId].forEach(function(fn) { | ||
94 | fn(topId); | ||
95 | }); | ||
96 | |||
97 | }); | ||
98 | } | ||
99 | } else { | ||
100 | // already loaded | ||
101 | if (has(definitions, topId)) { | ||
102 | return; | ||
103 | } | ||
104 | // hasn't started | ||
105 | else { | ||
106 | var definition = config.loader(topId); | ||
107 | if (!definition) { | ||
108 | CJS.warn("Can't find module " + JSON.stringify(topId)); | ||
109 | } | ||
110 | definitions[topId] = definition || null; | ||
111 | } | ||
112 | } | ||
113 | } | ||
114 | |||
115 | function loadDeepDefinitions(topId, callback) { | ||
116 | if (has(modules, topId)) { | ||
117 | CJS.warn("module already init (1): " + topId); | ||
118 | return callback && callback(); | ||
119 | } | ||
120 | if (callback) { | ||
121 | // in async mode we need to load the transitive dependencies first | ||
122 | var transitiveDependencies = {}; // undefined = not yet seen; false = not yet loaded; true = already loaded; | ||
123 | var loaded = false; | ||
124 | function loadDependencies(id) { | ||
125 | transitiveDependencies[id] = true; | ||
126 | if (definitions[id]) { | ||
127 | (definitions[id].dependencies || []).map(function(dependency) { | ||
128 | var depId = resolve(dependency, id); | ||
129 | if (!has(transitiveDependencies, depId)) { | ||
130 | transitiveDependencies[depId] = false; | ||
131 | return depId; | ||
132 | } | ||
133 | }).forEach(function(depId) { | ||
134 | depId && loadDefinition(depId, loadDependencies); | ||
135 | }); | ||
136 | } | ||
137 | // if any dependency is still unloaded, bail early | ||
138 | // TODO: could eliminate this loop by counting | ||
139 | for (var dependency in transitiveDependencies) { | ||
140 | if (transitiveDependencies[dependency] === false) { | ||
141 | return; | ||
142 | } | ||
143 | } | ||
144 | // otherwise we're done loading transitive dependencies | ||
145 | if (!loaded) { | ||
146 | loaded = true; | ||
147 | callback(); | ||
148 | } | ||
149 | } | ||
150 | // kick it off with the root module: | ||
151 | loadDefinition(topId, loadDependencies); | ||
152 | } else { | ||
153 | loadDefinition(topId); | ||
154 | } | ||
155 | } | ||
156 | |||
157 | // Loads module definition (and it's transitive dependencies if in async loading mode) then initializes the module. | ||
158 | function loadModule(topId) { | ||
159 | var result = Promise.defer(); | ||
160 | |||
161 | // Update the list of modules that need to load | ||
162 | CJS.progress.requiredModules.push( | ||
163 | [config.location, topId].join("#") | ||
164 | ); | ||
165 | |||
166 | loadDeepDefinitions(topId, function () { | ||
167 | try { | ||
168 | initModule(topId); | ||
169 | result.resolve(); | ||
170 | } catch (exception) { | ||
171 | result.reject(exception.message, exception); | ||
172 | } | ||
173 | }); | ||
174 | |||
175 | return result.promise; | ||
176 | } | ||
177 | |||
178 | // Initializes a module by executing the factory function with a new module "exports" object. | ||
179 | function initModule(topId) { | ||
180 | if (has(definitions, topId)) { | ||
181 | if (definitions[topId] && typeof definitions[topId].factory === "function") { | ||
182 | // HACK: look up canonical URI in previously initialized modules (different topId, same URI) | ||
183 | // TODO: Handle this at higher level? | ||
184 | var uri = URL.resolve(definitions[topId].path, ""); | ||
185 | if (has(urisToIds, uri)) { | ||
186 | var canonicalId = urisToIds[uri]; | ||
187 | modules[topId] = modules[canonicalId]; | ||
188 | } else { | ||
189 | urisToIds[uri] = topId; | ||
190 | |||
191 | var module = modules[topId] = { | ||
192 | exports: {}, | ||
193 | id: topId, | ||
194 | path: definitions[topId].path, | ||
195 | directory: URL.resolve(definitions[topId].path, "."), | ||
196 | uri: uri // EXTENSION | ||
197 | }; | ||
198 | |||
199 | var requireArg = makeRequire(topId); | ||
200 | var exportsArg = module.exports; | ||
201 | var moduleArg = module; | ||
202 | |||
203 | // Execute the factory function: | ||
204 | var returnValue = definitions[topId].factory.call(global, requireArg, exportsArg, moduleArg); | ||
205 | |||
206 | // Modules should never have a return value. | ||
207 | if (returnValue !== undefined) { | ||
208 | CJS.warn('require: module "'+topId+'" returned a value.'); | ||
209 | } | ||
210 | |||
211 | // Update the list of modules that are ready to use | ||
212 | CJS.progress.initializedModules.push([ | ||
213 | config.location, | ||
214 | topId | ||
215 | ].join("#")); | ||
216 | |||
217 | } | ||
218 | } else { | ||
219 | CJS.warn("Can't require module "+JSON.stringify(topId)); | ||
220 | throw new Error("Can't require module "+JSON.stringify(topId)); | ||