react / wstein / node_modules / browserify / node_modules / insert-module-globals / node_modules / combine-source-map / node_modules / source-map / build / mini-require.js
80556 views/* -*- Mode: js; js-indent-level: 2; -*- */1/*2* Copyright 2011 Mozilla Foundation and contributors3* Licensed under the New BSD license. See LICENSE or:4* http://opensource.org/licenses/BSD-3-Clause5*/67/**8* Define a module along with a payload.9* @param {string} moduleName Name for the payload10* @param {ignored} deps Ignored. For compatibility with CommonJS AMD Spec11* @param {function} payload Function with (require, exports, module) params12*/13function define(moduleName, deps, payload) {14if (typeof moduleName != "string") {15throw new TypeError('Expected string, got: ' + moduleName);16}1718if (arguments.length == 2) {19payload = deps;20}2122if (moduleName in define.modules) {23throw new Error("Module already defined: " + moduleName);24}25define.modules[moduleName] = payload;26};2728/**29* The global store of un-instantiated modules30*/31define.modules = {};323334/**35* We invoke require() in the context of a Domain so we can have multiple36* sets of modules running separate from each other.37* This contrasts with JSMs which are singletons, Domains allows us to38* optionally load a CommonJS module twice with separate data each time.39* Perhaps you want 2 command lines with a different set of commands in each,40* for example.41*/42function Domain() {43this.modules = {};44this._currentModule = null;45}4647(function () {4849/**50* Lookup module names and resolve them by calling the definition function if51* needed.52* There are 2 ways to call this, either with an array of dependencies and a53* callback to call when the dependencies are found (which can happen54* asynchronously in an in-page context) or with a single string an no callback55* where the dependency is resolved synchronously and returned.56* The API is designed to be compatible with the CommonJS AMD spec and57* RequireJS.58* @param {string[]|string} deps A name, or names for the payload59* @param {function|undefined} callback Function to call when the dependencies60* are resolved61* @return {undefined|object} The module required or undefined for62* array/callback method63*/64Domain.prototype.require = function(deps, callback) {65if (Array.isArray(deps)) {66var params = deps.map(function(dep) {67return this.lookup(dep);68}, this);69if (callback) {70callback.apply(null, params);71}72return undefined;73}74else {75return this.lookup(deps);76}77};7879function normalize(path) {80var bits = path.split('/');81var i = 1;82while (i < bits.length) {83if (bits[i] === '..') {84bits.splice(i-1, 1);85} else if (bits[i] === '.') {86bits.splice(i, 1);87} else {88i++;89}90}91return bits.join('/');92}9394function join(a, b) {95a = a.trim();96b = b.trim();97if (/^\//.test(b)) {98return b;99} else {100return a.replace(/\/*$/, '/') + b;101}102}103104function dirname(path) {105var bits = path.split('/');106bits.pop();107return bits.join('/');108}109110/**111* Lookup module names and resolve them by calling the definition function if112* needed.113* @param {string} moduleName A name for the payload to lookup114* @return {object} The module specified by aModuleName or null if not found.115*/116Domain.prototype.lookup = function(moduleName) {117if (/^\./.test(moduleName)) {118moduleName = normalize(join(dirname(this._currentModule), moduleName));119}120121if (moduleName in this.modules) {122var module = this.modules[moduleName];123return module;124}125126if (!(moduleName in define.modules)) {127throw new Error("Module not defined: " + moduleName);128}129130var module = define.modules[moduleName];131132if (typeof module == "function") {133var exports = {};134var previousModule = this._currentModule;135this._currentModule = moduleName;136module(this.require.bind(this), exports, { id: moduleName, uri: "" });137this._currentModule = previousModule;138module = exports;139}140141// cache the resulting module object for next time142this.modules[moduleName] = module;143144return module;145};146147}());148149define.Domain = Domain;150define.globalDomain = new Domain();151var require = define.globalDomain.require.bind(define.globalDomain);152153154