diff options
author | Jose Antonio Marquez | 2012-04-20 10:44:02 -0700 |
---|---|---|
committer | Jose Antonio Marquez | 2012-04-20 10:44:02 -0700 |
commit | 0b7848540a7ca84b7c67821fedc275cf243eff2f (patch) | |
tree | a26b2986d412a02eb93fc7fa1b0f495cab40e757 /node_modules/montage/require | |
parent | 154d1a8fdbbe050dbfc97cd13baa8981d8fc1e47 (diff) | |
parent | 8a546291a9d0de3bda1a7765ae9008f2ad052bba (diff) | |
download | ninja-0b7848540a7ca84b7c67821fedc275cf243eff2f.tar.gz |
Merge branch 'refs/heads/Ninja-Internal' into FileIO
Diffstat (limited to 'node_modules/montage/require')
-rwxr-xr-x | node_modules/montage/require/require.js | 213 |
1 files changed, 168 insertions, 45 deletions
diff --git a/node_modules/montage/require/require.js b/node_modules/montage/require/require.js index aad6cef7..4ea8a46d 100755 --- a/node_modules/montage/require/require.js +++ b/node_modules/montage/require/require.js | |||
@@ -35,7 +35,8 @@ | |||
35 | 35 | ||
36 | var globalEval = eval; // reassigning causes eval to not use lexical scope. | 36 | var globalEval = eval; // reassigning causes eval to not use lexical scope. |
37 | 37 | ||
38 | // Non-CommonJS speced extensions should be marked with an "// EXTENSION" comment. | 38 | // Non-CommonJS speced extensions should be marked with an "// EXTENSION" |
39 | // comment. | ||
39 | 40 | ||
40 | Require.makeRequire = function (config) { | 41 | Require.makeRequire = function (config) { |
41 | var require; | 42 | var require; |
@@ -52,7 +53,8 @@ | |||
52 | config.makeCompiler = config.makeCompiler || Require.makeCompiler; | 53 | config.makeCompiler = config.makeCompiler || Require.makeCompiler; |
53 | config.compile = config.compile || config.makeCompiler(config); | 54 | config.compile = config.compile || config.makeCompiler(config); |
54 | 55 | ||
55 | // Modules: { exports, id, location, directory, factory, dependencies, dependees, text, type } | 56 | // Modules: { exports, id, location, directory, factory, dependencies, |
57 | // dependees, text, type } | ||
56 | var modules = config.modules = config.modules || {}; | 58 | var modules = config.modules = config.modules || {}; |
57 | 59 | ||
58 | // produces an entry in the module state table, which gets built | 60 | // produces an entry in the module state table, which gets built |
@@ -103,7 +105,9 @@ | |||
103 | .then(function () { | 105 | .then(function () { |
104 | // compile and analyze dependencies | 106 | // compile and analyze dependencies |
105 | config.compile(module); | 107 | config.compile(module); |
106 | var dependencies = module.dependencies = module.dependencies || []; | 108 | var dependencies = |
109 | module.dependencies = | ||
110 | module.dependencies || []; | ||
107 | if (module.redirect !== void 0) { | 111 | if (module.redirect !== void 0) { |
108 | dependencies.push(module.redirect); | 112 | dependencies.push(module.redirect); |
109 | } | 113 | } |
@@ -136,7 +140,8 @@ | |||
136 | }) | 140 | }) |
137 | } | 141 | } |
138 | 142 | ||
139 | // Initializes a module by executing the factory function with a new module "exports" object. | 143 | // Initializes a module by executing the factory function with a new |
144 | // module "exports" object. | ||
140 | function getExports(topId, viaId) { | 145 | function getExports(topId, viaId) { |
141 | var module = getModule(topId); | 146 | var module = getModule(topId); |
142 | 147 | ||
@@ -157,7 +162,10 @@ | |||
157 | 162 | ||
158 | // do not initialize modules that do not define a factory function | 163 | // do not initialize modules that do not define a factory function |
159 | if (module.factory === void 0) { | 164 | if (module.factory === void 0) { |
160 | throw new Error("Can't require module " + JSON.stringify(topId) + " via " + JSON.stringify(viaId)); | 165 | throw new Error( |
166 | "Can't require module " + JSON.stringify(topId) + | ||
167 | " via " + JSON.stringify(viaId) | ||
168 | ); | ||
161 | } | 169 | } |
162 | 170 | ||
163 | module.directory = URL.resolve(module.location, "."); // EXTENSION | 171 | module.directory = URL.resolve(module.location, "."); // EXTENSION |
@@ -211,11 +219,14 @@ | |||
211 | if (internal) { | 219 | if (internal) { |
212 | return null; | 220 | return null; |
213 | } else { | 221 | } else { |
214 | throw new Error("Can't identify " + id2 + " from " + require2.location); | 222 | throw new Error( |
223 | "Can't identify " + id2 + " from " + require2.location | ||
224 | ); | ||
215 | } | 225 | } |
216 | } | 226 | } |
217 | 227 | ||
218 | // Creates a unique require function for each module that encapsulates that module's id for resolving relative module IDs against. | 228 | // Creates a unique require function for each module that encapsulates |
229 | // that module's id for resolving relative module IDs against. | ||
219 | function makeRequire(viaId) { | 230 | function makeRequire(viaId) { |
220 | 231 | ||
221 | // Main synchronously executing "require()" function | 232 | // Main synchronously executing "require()" function |
@@ -224,7 +235,8 @@ | |||
224 | return getExports(topId, viaId); | 235 | return getExports(topId, viaId); |
225 | }; | 236 | }; |
226 | 237 | ||
227 | // Asynchronous "require.async()" which ensures async executation (even with synchronous loaders) | 238 | // Asynchronous "require.async()" which ensures async executation |
239 | // (even with synchronous loaders) | ||
228 | require.async = function(id, callback) { | 240 | require.async = function(id, callback) { |
229 | var topId = resolve(id, viaId); | 241 | var topId = resolve(id, viaId); |
230 | return deepLoad(topId, viaId) | 242 | return deepLoad(topId, viaId) |
@@ -248,7 +260,19 @@ | |||
248 | 260 | ||
249 | require.load = load; | 261 | require.load = load; |
250 | require.deepLoad = deepLoad; | 262 | require.deepLoad = deepLoad; |
251 | require.loadPackage = config.loadPackage; | 263 | |
264 | require.loadPackage = function (dependency, givenConfig) { | ||
265 | if (givenConfig) { // explicit configuration, fresh environment | ||
266 | return Require.loadPackage(dependency, givenConfig); | ||
267 | } else { // inherited environment | ||
268 | return config.loadPackage(dependency, config); | ||
269 | } | ||
270 | }; | ||
271 | |||
272 | require.injectPackageDescription = function (location, description) { | ||
273 | Require.injectPackageDescription(location, description, config); | ||
274 | }; | ||
275 | |||
252 | require.identify = identify; | 276 | require.identify = identify; |
253 | require.inject = inject; | 277 | require.inject = inject; |
254 | require.progress = Require.progress; | 278 | require.progress = Require.progress; |
@@ -272,34 +296,55 @@ | |||
272 | initializedModules: [] | 296 | initializedModules: [] |
273 | }; | 297 | }; |
274 | 298 | ||
299 | Require.injectPackageDescription = function (location, description, config) { | ||
300 | var descriptions = config.descriptions = config.descriptions || {}; | ||
301 | descriptions[location] = Promise.call(function () { | ||
302 | return description; | ||
303 | }); | ||
304 | }; | ||
305 | |||
306 | Require.loadPackageDescription = function (location, config) { | ||
307 | var descriptions = config.descriptions = config.descriptions || {}; | ||
308 | if (descriptions[location] === void 0) { | ||
309 | var jsonPath = URL.resolve(location, 'package.json'); | ||
310 | descriptions[location] = Require.read(jsonPath) | ||
311 | .then(function (json) { | ||
312 | try { | ||
313 | return JSON.parse(json); | ||
314 | } catch (exception) { | ||
315 | throw new SyntaxError( | ||
316 | "in " + JSON.stringify(jsonPath) + ": " + | ||
317 | exception.message | ||
318 | ); | ||
319 | } | ||
320 | }); | ||
321 | } | ||
322 | return descriptions[location]; | ||
323 | }; | ||
324 | |||
275 | Require.loadPackage = function (location, config) { | 325 | Require.loadPackage = function (location, config) { |
276 | location = URL.resolve(location, "."); | 326 | location = URL.resolve(location, "."); |
277 | config = config || {}; | 327 | config = config || {}; |
278 | var loadingPackages = config.loadingPackages = config.loadingPackages || {}; | 328 | var loadingPackages = config.loadingPackages = config.loadingPackages || {}; |
279 | var loadedPackages = config.packages = {}; | 329 | var loadedPackages = config.packages = {}; |
330 | var registry = config.registry = config.registry || Object.create(null); | ||
280 | 331 | ||
281 | config.getPackage = function (dependency) { | 332 | config.getPackage = function (dependency) { |
282 | dependency = Dependency(dependency); | 333 | dependency = normalizeDependency(dependency, config); |
283 | // TODO handle other kinds of dependency | ||
284 | var location = dependency.location; | 334 | var location = dependency.location; |
285 | if (!loadedPackages[location]) | 335 | if (!loadedPackages[location]) |
286 | throw new Error("Dependency is not loaded: " + JSON.stringify(location)); | 336 | throw new Error( |
337 | "Dependency is not loaded: " + JSON.stringify(location) | ||
338 | ); | ||
287 | return loadedPackages[location]; | 339 | return loadedPackages[location]; |
288 | }; | 340 | }; |
289 | 341 | ||
290 | config.loadPackage = function (dependency) { | 342 | config.loadPackage = function (dependency, viaConfig) { |
291 | dependency = Dependency(dependency); | 343 | dependency = normalizeDependency(dependency, viaConfig); |
292 | // TODO handle other kinds of dependency | 344 | var location = dependency.location; |
293 | var location = URL.resolve(dependency.location, "."); | ||
294 | if (!loadingPackages[location]) { | 345 | if (!loadingPackages[location]) { |
295 | var jsonPath = URL.resolve(location, 'package.json'); | 346 | loadingPackages[location] = Require.loadPackageDescription(location, config) |
296 | loadingPackages[location] = Require.read(jsonPath) | 347 | .then(function (packageDescription) { |
297 | .then(function (json) { | ||
298 | try { | ||
299 | var packageDescription = JSON.parse(json); | ||
300 | } catch (exception) { | ||
301 | throw new SyntaxError("in " + JSON.stringify(jsonPath) + ": " + exception.message); | ||
302 | } | ||
303 | var subconfig = configurePackage( | 348 | var subconfig = configurePackage( |
304 | location, | 349 | location, |
305 | packageDescription, | 350 | packageDescription, |
@@ -324,9 +369,67 @@ | |||
324 | return pkg; | 369 | return pkg; |
325 | }; | 370 | }; |
326 | 371 | ||
327 | function Dependency(dependency) { | 372 | function normalizeDependency(dependency, config, name) { |
373 | config = config || {}; | ||
328 | if (typeof dependency === "string") { | 374 | if (typeof dependency === "string") { |
329 | dependency = {"location": dependency}; | 375 | if (dependency.indexOf("@") >= 0) { |
376 | var parts = dependency.split("@"); | ||
377 | dependency = { | ||
378 | name: parts[0] || name, | ||
379 | version: parts[1] | ||
380 | }; | ||
381 | } else { | ||
382 | dependency = { | ||
383 | location: dependency | ||
384 | }; | ||