update requirejs

This commit is contained in:
Fabian Jakobs 2010-10-18 19:52:41 +02:00
commit 59584a0e11

View file

@ -13,7 +13,7 @@ setInterval: false, importScripts: false */
var require; var require;
(function () { (function () {
//Change this version number for each release. //Change this version number for each release.
var version = "0.14.1", var version = "0.14.2",
empty = {}, s, empty = {}, s,
i, defContextName = "_", contextLoads = [], i, defContextName = "_", contextLoads = [],
scripts, script, rePkg, src, m, dataMain, cfg = {}, setReadyState, scripts, script, rePkg, src, m, dataMain, cfg = {}, setReadyState,
@ -78,6 +78,33 @@ var require;
context.loaded[args[0]] = true; context.loaded[args[0]] = true;
} }
/**
* Used to set up package paths from a packagePaths or packages config object.
* @param {Object} packages the object to store the new package config
* @param {Array} currentPackages an array of packages to configure
* @param {String} [dir] a prefix dir to use.
*/
function configurePackageDir(packages, currentPackages, dir) {
var i, location, pkgObj;
for (i = 0; (pkgObj = currentPackages[i]); i++) {
pkgObj = typeof pkgObj === "string" ? { name: pkgObj } : pkgObj;
location = pkgObj.location;
//Add dir to the path, but avoid paths that start with a slash
//or have a colon (indicates a protocol)
if (dir && (!location || (location.indexOf("/") !== 0 && location.indexOf(":") === -1))) {
pkgObj.location = dir + "/" + (pkgObj.location || pkgObj.name);
}
//Normalize package paths.
pkgObj.location = pkgObj.location || pkgObj.name;
pkgObj.lib = pkgObj.lib || "lib";
pkgObj.main = pkgObj.main || "main";
packages[pkgObj.name] = pkgObj;
}
}
/** /**
* Resumes tracing of dependencies and then checks if everything is loaded. * Resumes tracing of dependencies and then checks if everything is loaded.
*/ */
@ -253,7 +280,7 @@ var require;
var context, newContext, loaded, pluginPrefix, var context, newContext, loaded, pluginPrefix,
canSetContext, prop, newLength, outDeps, mods, paths, index, i, canSetContext, prop, newLength, outDeps, mods, paths, index, i,
deferMods, deferModArgs, lastModArg, waitingName, packages, deferMods, deferModArgs, lastModArg, waitingName, packages,
packagePaths, pkgPath, pkgNames, pkgName, pkgObj; packagePaths;
contextName = contextName ? contextName : (config && config.context ? config.context : s.ctxName); contextName = contextName ? contextName : (config && config.context ? config.context : s.ctxName);
context = s.contexts[contextName]; context = s.contexts[contextName];
@ -365,43 +392,14 @@ var require;
if (packagePaths) { if (packagePaths) {
for (prop in packagePaths) { for (prop in packagePaths) {
if (!(prop in empty)) { if (!(prop in empty)) {
pkgPath = prop; configurePackageDir(packages, packagePaths[prop], prop);
pkgNames = packagePaths[pkgPath];
for (i = 0; (pkgName = pkgNames[i]); i++) {
if (typeof pkgName === "string") {
//Standard package mapping.
pkgObj = packages[pkgName] = {
name: pkgName,
location: pkgPath + "/" + pkgName
};
} else {
//A custom setup.
pkgObj = context.config.packages[pkgName.name] = pkgName;
pkgObj.location = pkgPath + "/" + (pkgObj.location || pkgObj.name);
}
}
} }
} }
} }
//Adjust packages if necessary. //Adjust packages if necessary.
if (config.packages) { if (config.packages) {
for (prop in config.packages) { configurePackageDir(packages, config.packages);
if (!(prop in empty)) {
pkgObj = packages[prop] = config.packages[prop];
pkgObj.name = pkgObj.name || prop;
}
}
}
//Normalize package paths.
for (prop in packages) {
if (!(prop in empty)) {
pkgObj = packages[prop];
pkgObj.location = pkgObj.location || pkgObj.name;
pkgObj.lib = pkgObj.lib || "lib";
pkgObj.main = pkgObj.main || "main";
}
} }
//Done with modifications, assing packages back to context config //Done with modifications, assing packages back to context config
@ -445,7 +443,7 @@ var require;
outDeps = deps; outDeps = deps;
deps = []; deps = [];
for (i = 0; i < outDeps.length; i++) { for (i = 0; i < outDeps.length; i++) {
deps[i] = req.splitPrefix(outDeps[i], name); deps[i] = req.splitPrefix(outDeps[i], name, context);
} }
} }
@ -817,10 +815,12 @@ var require;
} }
contextName = contextName || s.ctxName; contextName = contextName || s.ctxName;
//Normalize module name, if it contains . or .. var ret, context = s.contexts[contextName];
moduleName = req.normalizeName(moduleName, relModuleName);
var ret = s.contexts[contextName].defined[moduleName]; //Normalize module name, if it contains . or ..
moduleName = req.normalizeName(moduleName, relModuleName, context);
ret = context.defined[moduleName];
if (ret === undefined) { if (ret === undefined) {
req.onError(new Error("require: module name '" + req.onError(new Error("require: module name '" +
moduleName + moduleName +
@ -874,9 +874,10 @@ var require;
* @param {String} name the relative name * @param {String} name the relative name
* @param {String} baseName a real name that the name arg is relative * @param {String} baseName a real name that the name arg is relative
* to. * to.
* @param {Object} context
* @returns {String} normalized name * @returns {String} normalized name
*/ */
req.normalizeName = function (name, baseName) { req.normalizeName = function (name, baseName, context) {
//Adjust any relative paths. //Adjust any relative paths.
var part; var part;
if (name.charAt(0) === ".") { if (name.charAt(0) === ".") {
@ -885,6 +886,12 @@ var require;
name + name +
", no relative module name available.")); ", no relative module name available."));
} }
if (context.config.packages[baseName]) {
//If the baseName is a package name, then just treat it as one
//name to concat the name with.
baseName = [baseName];
} else {
//Convert baseName to array, and lop off the last part, //Convert baseName to array, and lop off the last part,
//so that . matches that "directory" and not name of the baseName's //so that . matches that "directory" and not name of the baseName's
//module. For instance, baseName of "one/two/three", maps to //module. For instance, baseName of "one/two/three", maps to
@ -892,6 +899,7 @@ var require;
//this normalization. //this normalization.
baseName = baseName.split("/"); baseName = baseName.split("/");
baseName = baseName.slice(0, baseName.length - 1); baseName = baseName.slice(0, baseName.length - 1);
}
name = baseName.concat(name.split("/")); name = baseName.concat(name.split("/"));
for (i = 0; (part = name[i]); i++) { for (i = 0; (part = name[i]); i++) {
@ -916,12 +924,13 @@ var require;
* @param {String} name the module name * @param {String} name the module name
* @param {String} [baseName] base name that name is * @param {String} [baseName] base name that name is
* relative to. * relative to.
* @param {Object} context
* *
* @returns {Object} with properties, 'prefix' (which * @returns {Object} with properties, 'prefix' (which
* may be null), 'name' and 'fullName', which is a combination * may be null), 'name' and 'fullName', which is a combination
* of the prefix (if it exists) and the name. * of the prefix (if it exists) and the name.
*/ */
req.splitPrefix = function (name, baseName) { req.splitPrefix = function (name, baseName, context) {
var index = name.indexOf("!"), prefix = null; var index = name.indexOf("!"), prefix = null;
if (index !== -1) { if (index !== -1) {
prefix = name.substring(0, index); prefix = name.substring(0, index);
@ -929,7 +938,7 @@ var require;
} }
//Account for relative paths if there is a base name. //Account for relative paths if there is a base name.
name = req.normalizeName(name, baseName); name = req.normalizeName(name, baseName, context);
return { return {
prefix: prefix, prefix: prefix,
@ -943,10 +952,11 @@ var require;
*/ */
req.nameToUrl = function (moduleName, ext, contextName, relModuleName) { req.nameToUrl = function (moduleName, ext, contextName, relModuleName) {
var paths, packages, pkg, pkgPath, syms, i, parentModule, url, var paths, packages, pkg, pkgPath, syms, i, parentModule, url,
config = s.contexts[contextName].config; context = s.contexts[contextName],
config = context.config;
//Normalize module name if have a base relative module name to work from. //Normalize module name if have a base relative module name to work from.
moduleName = req.normalizeName(moduleName, relModuleName); moduleName = req.normalizeName(moduleName, relModuleName, context);
//If a colon is in the URL, it indicates a protocol is used and it is just //If a colon is in the URL, it indicates a protocol is used and it is just
//an URL to a file, or if it starts with a slash or ends with .js, it is just a plain file. //an URL to a file, or if it starts with a slash or ends with .js, it is just a plain file.
@ -1281,7 +1291,7 @@ var require;
ret = defined[name]; ret = defined[name];
} else { } else {
if (cjsModule && "exports" in cjsModule) { if (cjsModule && "exports" in cjsModule) {
ret = defined[name] = depModule.exports; ret = defined[name] = cjsModule.exports;
} else { } else {
if (name in defined && !usingExports) { if (name in defined && !usingExports) {
req.onError(new Error(name + " has already been defined")); req.onError(new Error(name + " has already been defined"));