From 22a66cb6e243a3f1c867b62e3942fd2e828019d9 Mon Sep 17 00:00:00 2001 From: Valerio Virgillito Date: Thu, 8 Mar 2012 13:56:09 -0800 Subject: integrating v0.7 montage into ninja Signed-off-by: Valerio Virgillito --- node_modules/montage/require/browser.js | 90 +++++++++++++- node_modules/montage/require/node.js | 49 +++++--- node_modules/montage/require/require.js | 205 ++++++++++++++------------------ 3 files changed, 206 insertions(+), 138 deletions(-) (limited to 'node_modules/montage/require') diff --git a/node_modules/montage/require/browser.js b/node_modules/montage/require/browser.js index 3fdd58d4..f4a11d97 100755 --- a/node_modules/montage/require/browser.js +++ b/node_modules/montage/require/browser.js @@ -47,7 +47,9 @@ Require.read = function (url) { try { request.open(GET, url, true); - request.overrideMimeType(APPLICATION_JAVASCRIPT_MIMETYPE); + if (request.overrideMimeType) { + request.overrideMimeType(APPLICATION_JAVASCRIPT_MIMETYPE); + } request.onreadystatechange = function () { if (request.readyState === 4) { onload(); @@ -76,7 +78,7 @@ var globalEval = /*this.execScript ||*/eval; // For Firebug evaled code isn't debuggable otherwise // http://code.google.com/p/fbug/issues/detail?id=2198 if (global.navigator && global.navigator.userAgent.indexOf("Firefox") >= 0) { - globalEval = new Function("evalString", "return eval(evalString)"); + globalEval = new Function("_", "return eval(_)"); } var __FILE__String = "__FILE__", @@ -89,6 +91,8 @@ Require.Compiler = function (config) { return function(module) { if (module.factory || module.text === void 0) return module; + if (config.define) + throw new Error("Can't use eval."); // Here we use a couple tricks to make debugging better in various browsers: // TODO: determine if these are all necessary / the best options @@ -117,4 +121,86 @@ Require.Compiler = function (config) { } }; +Require.XhrLoader = function (config) { + return function (url, module) { + return Require.read(url) + .then(function (text) { + module.type = "javascript"; + module.text = text; + module.location = url; + }); + }; +}; + +var definitions = {}; +var getDefinition = function (hash, id) { + definitions[hash] = definitions[hash] || {}; + definitions[hash][id] = definitions[hash][id] || Promise.defer(); + return definitions[hash][id]; +}; +define = function (hash, id, module) { + getDefinition(hash, id).resolve(module); +}; + +Require.ScriptLoader = function (config) { + var hash = config.packageDescription.hash; + return function (location, module) { + return Promise.call(function () { + + // short-cut by predefinition + if (definitions[hash] && definitions[hash][module.id]) { + return definitions[hash][module.id].promise; + } + + if (/\.js$/.test(location)) { + location = location.replace(/\.js/, ".load.js"); + } else { + location += ".load.js"; + } + + var script = document.createElement("script"); + script.onload = function() { + script.parentNode.removeChild(script); + }; + script.onerror = function (error) { + script.parentNode.removeChild(script); + }; + script.src = location; + script.defer = true; + document.getElementsByTagName("head")[0].appendChild(script); + + return getDefinition(hash, module.id).promise + }) + .then(function (definition) { + delete definitions[hash][module.id]; + for (var name in definition) { + module[name] = definition[name]; + } + module.location = location; + module.directory = URL.resolve(location, "."); + }); + }; +}; + +Require.makeLoader = function (config) { + if (config.define) { + Loader = Require.ScriptLoader; + } else { + Loader = Require.XhrLoader; + } + return Require.MappingsLoader( + config, + Require.ExtensionsLoader( + config, + Require.PathsLoader( + config, + Require.MemoizedLoader( + config, + Loader(config) + ) + ) + ) + ); +}; + }); diff --git a/node_modules/montage/require/node.js b/node_modules/montage/require/node.js index 9fcea3f5..3ba5a322 100644 --- a/node_modules/montage/require/node.js +++ b/node_modules/montage/require/node.js @@ -40,10 +40,13 @@ Require.Compiler = function (config) { var names = ["require", "exports", "module"]; var scopeNames = Object.keys(config.scope); names.push.apply(names, scopeNames); - return function(module) { - if (module.factory) + return function (module) { + if (module.factory) { return module; - if (!module.factory && module.text !== void 0) { + } else if ( + module.text !== void 0 && + module.type === "javascript" + ) { var factory = globalEval( "(function(" + names.join(",") + "){" + module.text + @@ -59,18 +62,41 @@ Require.Compiler = function (config) { // https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope //module.factory = new Function("require", "exports", "module", module.text + "\n//*/\n//@ sourceURL="+module.path); } - return module; }; }; -Require.DefaultLoaderConstructor = function(config) { +Require.Loader = function (config, load) { + return function (url, module) { + return Require.read(url) + .then(function (text) { + module.type = "javascript"; + module.text = text; + module.location = url; + }, function (reason, error, rejection) { + return load(url, module); + }); + }; +}; + +Require.NodeLoader = function (config) { + return function (url, module) { + var id = url.slice(config.location.length); + return { + type: "native", + exports: require(id), + location: url + } + }; +}; + +Require.makeLoader = function(config) { return Require.MappingsLoader( config, Require.ExtensionsLoader( config, Require.PathsLoader( config, - Require.CachingLoader( + Require.MemoizedLoader( config, Require.Loader( config, @@ -82,17 +108,6 @@ Require.DefaultLoaderConstructor = function(config) { ); }; -Require.NodeLoader = function (config) { - return function (url, module) { - var id = url.slice(config.location.length); - return { - type: "native", - exports: require(id), - location: url - } - }; -} - Require.main = function () { var require = Require.Sandbox(); require.async(process.argv[2]).end(); diff --git a/node_modules/montage/require/require.js b/node_modules/montage/require/require.js index 8273b660..aad6cef7 100755 --- a/node_modules/montage/require/require.js +++ b/node_modules/montage/require/require.js @@ -33,45 +33,46 @@ if (!this) throw new Error("Require does not work in strict mode."); - var global = this; var globalEval = eval; // reassigning causes eval to not use lexical scope. // Non-CommonJS speced extensions should be marked with an "// EXTENSION" comment. - // Sandbox is an instance of the loader system. Different sandboxes will have different instances of modules. - // Returns the root "require" function. If this root "require()" function is called the loader will be in synchronous mode. - // To get asynchronous loading you MUST call the root "require.async()". In async mode all subsequent calls to "require()" will - // be asynchronously loaded, and synchronously executed. - Require.Sandbox = function(config) { + Require.makeRequire = function (config) { + var require; + // Configuration defaults: config = config || {}; config.location = URL.resolve(config.location || Require.getLocation(), "."); config.lib = URL.resolve(config.location, config.lib || "."); config.paths = config.paths || [config.lib]; config.mappings = config.mappings || {}; // EXTENSION - config.exposedConfigs = config.exposedConfigs || Require.defaultExposedConfigs; - config.makeLoader = config.makeLoader || Require.DefaultLoaderConstructor; + config.exposedConfigs = config.exposedConfigs || Require.exposedConfigs; + config.makeLoader = config.makeLoader || Require.makeLoader; config.load = config.load || config.makeLoader(config); - config.makeCompiler = config.makeCompiler || Require.DefaultCompilerConstructor; + config.makeCompiler = config.makeCompiler || Require.makeCompiler; config.compile = config.compile || config.makeCompiler(config); - // Sandbox state: // Modules: { exports, id, location, directory, factory, dependencies, dependees, text, type } var modules = config.modules = config.modules || {}; - // Mapping from canonical IDs to the initial top ID used to load module - var locationsToIds = {}; + // produces an entry in the module state table, which gets built + // up through loading and execution, ultimately serving as the + // ``module`` free variable inside the corresponding module. function getModule(id) { if (!has(modules, id)) { modules[id] = { id: id, - display: config.location + "#" + id // EXTENSION + display: config.location + "#" + id, // EXTENSION + require: require, }; } return modules[id]; } - config.module = getModule; + // for preloading modules by their id and exports, useful to + // prevent wasteful multiple instantiation if a module was loaded + // in the bootstrapping process and can be trivially injected into + // the system. function inject(id, exports) { var module = getModule(id) module.exports = exports; @@ -83,59 +84,55 @@ var load = memoize(function (topId, viaId) { var module = getModule(topId); return Promise.call(function () { - // already loaded, already instantiated, or redirection + // if not already loaded, already instantiated, or + // configured as a redirection to another module if ( - module.factory !== void 0 || - module.exports !== void 0 || - module.redirect !== void 0 + module.factory === void 0 && + module.exports === void 0 && + module.redirect === void 0 ) { - return module; - // load - } else { + // load and + // trace progress Require.progress.requiredModules.push(module.display); return Promise.call(config.load, null, topId, module) .then(function () { Require.progress.loadedModules.push(module.display); - return module; }); } }) - .then(function (module) { - // analyze dependencies + .then(function () { + // compile and analyze dependencies config.compile(module); var dependencies = module.dependencies = module.dependencies || []; if (module.redirect !== void 0) { dependencies.push(module.redirect); } - return module; }); }); // Load a module definition, and the definitions of its transitive // dependencies - function deepLoad(id, viaId, loading) { + function deepLoad(topId, viaId, loading) { + var module = getModule(topId); // this is a memo of modules already being loaded so we don’t // data-lock on a cycle of dependencies. loading = loading || {}; // has this all happened before? will it happen again? - if (has(loading, id)) + if (has(loading, topId)) return; // break the cycle of violence. - loading[id] = true; // this has happened before - return load(id, viaId) - .then(function (module) { + loading[topId] = true; // this has happened before + return load(topId, viaId) + .then(function () { // load the transitive dependencies using the magic of // recursion. return Promise.all(module.dependencies.map(function (depId) { - depId = resolve(depId, id) + depId = resolve(depId, topId) // create dependees set, purely for debug purposes var module = getModule(depId); var dependees = module.dependees = module.dependees || {}; - dependees[id] = true; - return deepLoad(depId, id, loading); + dependees[topId] = true; + return deepLoad(depId, topId, loading); })) - .then(function () { - return module; - }) }) } @@ -169,7 +166,7 @@ // Execute the factory function: var returnValue = module.factory.call( // in the context of the module: - global, // this + void 0, // this (defaults to global) makeRequire(topId), // require module.exports, // exports module // module @@ -177,7 +174,10 @@ // Modules should never have a return value. if (returnValue !== void 0) { - console.warn('require: module "'+topId+'" returned a value.'); + console.warn( + 'require: module ' + JSON.stringify(topId) + + ' returned a value.' + ); } // Update the list of modules that are ready to use @@ -262,7 +262,8 @@ return require; } - return makeRequire(""); + require = makeRequire(""); + return require; }; Require.progress = { @@ -271,7 +272,7 @@ initializedModules: [] }; - Require.PackageSandbox = function (location, config) { + Require.loadPackage = function (location, config) { location = URL.resolve(location, "."); config = config || {}; var loadingPackages = config.loadingPackages = config.loadingPackages || {}; @@ -304,7 +305,7 @@ packageDescription, config ); - var pkg = Require.Sandbox(subconfig); + var pkg = Require.makeRequire(subconfig); loadedPackages[location] = pkg; return pkg; }); @@ -312,15 +313,15 @@ return loadingPackages[location]; }; - var _require = config.loadPackage(location); - _require.location = location; - _require.async = function (id, callback) { - return _require.then(function (require) { + var pkg = config.loadPackage(location); + pkg.location = location; + pkg.async = function (id, callback) { + return pkg.then(function (require) { return require.async(id, callback); }); }; - return _require; + return pkg; }; function Dependency(dependency) { @@ -340,6 +341,7 @@ config.name = description.name; config.location = location || Require.getLocation(); config.packageDescription = description; + config.define = description.define; // explicitly mask definitions and modules, which must // not apply to child packages var modules = config.modules = config.modules || {}; @@ -418,7 +420,7 @@ function has(object, property) { return Object.prototype.hasOwnProperty.call(object, property); - }; + } // Resolves CommonJS module IDs (not paths) Require.resolve = resolve; @@ -443,27 +445,6 @@ } } return target.join("/"); - }; - - // ES5 shim: - - // ES5 15.4.3.2 - if (!Array.isArray) { - Array.isArray = function(obj) { - return Object.prototype.toString.call(obj) == "[object Array]"; - }; - } - // ES5 15.2.3.14 - if (!Object.keys) { - Object.keys = function(object) { - var keys = []; - for (var name in object) { - if (Object.prototype.hasOwnProperty.call(object, name)) { - keys.push(name); - } - } - return keys; - }; } Require.base = function (location) { @@ -475,7 +456,7 @@ // Tests whether the location or URL is a absolute. Require.isAbsolute = function(location) { - return /^\w+:/.test(location); + return /^[\w\-]+:/.test(location); }; // Extracts dependencies by parsing code and looking for "require" (currently using a simple regexp) @@ -528,49 +509,47 @@ throw error; } }; - } + }; - Require.defaultExposedConfigs = [ + Require.exposedConfigs = [ "paths", "mappings", "location", "packageDescription", "packages", - "modules", - "module" + "modules" ]; - Require.DefaultCompilerConstructor = function(config) { - return Require.ShebangCompiler( - config, - Require.DependenciesCompiler( - config, - Require.LintCompiler( - config, - Require.Compiler(config) - ) - ) - ); - }; - - // Built-in loader "middleware": - - Require.DefaultLoaderConstructor = function(config) { - return Require.MappingsLoader( + Require.makeCompiler = function(config) { + return Require.JsonCompiler( config, - Require.ExtensionsLoader( + Require.ShebangCompiler( config, - Require.PathsLoader( + Require.DependenciesCompiler( config, - Require.MemoizedLoader( + Require.LintCompiler( config, - Require.Loader(config) + Require.Compiler(config) ) ) ) ); }; + Require.JsonCompiler = function (config, compile) { + return function (module) { + var json = module.id.match(/\.json$/); + if (json) { + module.exports = JSON.parse(module.text); + return module; + } else { + return compile(module); + } + }; + }; + + // Built-in loader "middleware": + // Using mappings hash to load modules that match a mapping. Require.MappingsLoader = function(config, load) { config.mappings = config.mappings || {}; @@ -616,8 +595,12 @@ var loadWithExtension = extensions.reduceRight(function (next, extension) { return function (id, module) { return load(id + "." + extension, module) - .fail(function (error) { - return next(id, module); + .fail(function (reason, error, rejection) { + if (/^Can't find /.test(reason)) { + return next(id, module); + } else { + return rejection; + } }); }; }, function (id, module) { @@ -635,7 +618,7 @@ return loadWithExtension(id, module); } } - } + }; // Attempts to load using multiple base paths (or one absolute path) with a single loader. Require.PathsLoader = function(config, load) { @@ -643,8 +626,12 @@ return function (id, module) { var newId = URL.resolve(path, id); return load(newId, module) - .fail(function () { - return next(id, module); + .fail(function (reason, error, rejection) { + if (/^Can't find /.test(reason)) { + return next(id, module); + } else { + return rejection; + } }); }; }, function (id, module) { @@ -665,26 +652,6 @@ return memoize(load, cache); }; - Require.Loader = function (config, load) { - return function (url, module) { - return Require.read(url) - .then(function (text) { - module.type = "javascript"; - module.text = text; - module.location = url; - }, function (reason, error, rejection) { - // This is a hook that allows a Loader to be chained to a - // fallback, such as the NodeLoader, if a local module can’t be - // found. - if (load) { - return load(url, module); - } else { - return rejection; - } - }); - }; - }; - var memoize = function (callback, cache) { cache = cache || {}; return function (key, arg) { -- cgit v1.2.3