react / wstein / node_modules / browserify / node_modules / browser-pack / node_modules / combine-source-map / node_modules / source-map / node_modules / amdefine / amdefine.js
80559 views/** vim: et:ts=4:sw=4:sts=41* @license amdefine 0.1.0 Copyright (c) 2011, The Dojo Foundation All Rights Reserved.2* Available via the MIT or new BSD license.3* see: http://github.com/jrburke/amdefine for details4*/56/*jslint node: true */7/*global module, process */8'use strict';910/**11* Creates a define for node.12* @param {Object} module the "module" object that is defined by Node for the13* current module.14* @param {Function} [requireFn]. Node's require function for the current module.15* It only needs to be passed in Node versions before 0.5, when module.require16* did not exist.17* @returns {Function} a define function that is usable for the current node18* module.19*/20function amdefine(module, requireFn) {21'use strict';22var defineCache = {},23loaderCache = {},24alreadyCalled = false,25path = require('path'),26makeRequire, stringRequire;2728/**29* Trims the . and .. from an array of path segments.30* It will keep a leading path segment if a .. will become31* the first path segment, to help with module name lookups,32* which act like paths, but can be remapped. But the end result,33* all paths that use this function should look normalized.34* NOTE: this method MODIFIES the input array.35* @param {Array} ary the array of path segments.36*/37function trimDots(ary) {38var i, part;39for (i = 0; ary[i]; i+= 1) {40part = ary[i];41if (part === '.') {42ary.splice(i, 1);43i -= 1;44} else if (part === '..') {45if (i === 1 && (ary[2] === '..' || ary[0] === '..')) {46//End of the line. Keep at least one non-dot47//path segment at the front so it can be mapped48//correctly to disk. Otherwise, there is likely49//no path mapping for a path starting with '..'.50//This can still fail, but catches the most reasonable51//uses of ..52break;53} else if (i > 0) {54ary.splice(i - 1, 2);55i -= 2;56}57}58}59}6061function normalize(name, baseName) {62var baseParts;6364//Adjust any relative paths.65if (name && name.charAt(0) === '.') {66//If have a base name, try to normalize against it,67//otherwise, assume it is a top-level require that will68//be relative to baseUrl in the end.69if (baseName) {70baseParts = baseName.split('/');71baseParts = baseParts.slice(0, baseParts.length - 1);72baseParts = baseParts.concat(name.split('/'));73trimDots(baseParts);74name = baseParts.join('/');75}76}7778return name;79}8081/**82* Create the normalize() function passed to a loader plugin's83* normalize method.84*/85function makeNormalize(relName) {86return function (name) {87return normalize(name, relName);88};89}9091function makeLoad(id) {92function load(value) {93loaderCache[id] = value;94}9596load.fromText = function (id, text) {97//This one is difficult because the text can/probably uses98//define, and any relative paths and requires should be relative99//to that id was it would be found on disk. But this would require100//bootstrapping a module/require fairly deeply from node core.101//Not sure how best to go about that yet.102throw new Error('amdefine does not implement load.fromText');103};104105return load;106}107108makeRequire = function (systemRequire, exports, module, relId) {109function amdRequire(deps, callback) {110if (typeof deps === 'string') {111//Synchronous, single module require('')112return stringRequire(systemRequire, exports, module, deps, relId);113} else {114//Array of dependencies with a callback.115116//Convert the dependencies to modules.117deps = deps.map(function (depName) {118return stringRequire(systemRequire, exports, module, depName, relId);119});120121//Wait for next tick to call back the require call.122process.nextTick(function () {123callback.apply(null, deps);124});125}126}127128amdRequire.toUrl = function (filePath) {129if (filePath.indexOf('.') === 0) {130return normalize(filePath, path.dirname(module.filename));131} else {132return filePath;133}134};135136return amdRequire;137};138139//Favor explicit value, passed in if the module wants to support Node 0.4.140requireFn = requireFn || function req() {141return module.require.apply(module, arguments);142};143144function runFactory(id, deps, factory) {145var r, e, m, result;146147if (id) {148e = loaderCache[id] = {};149m = {150id: id,151uri: __filename,152exports: e153};154r = makeRequire(requireFn, e, m, id);155} else {156//Only support one define call per file157if (alreadyCalled) {158throw new Error('amdefine with no module ID cannot be called more than once per file.');159}160alreadyCalled = true;161162//Use the real variables from node163//Use module.exports for exports, since164//the exports in here is amdefine exports.165e = module.exports;166m = module;167r = makeRequire(requireFn, e, m, module.id);168}169170//If there are dependencies, they are strings, so need171//to convert them to dependency values.172if (deps) {173deps = deps.map(function (depName) {174return r(depName);175});176}177178//Call the factory with the right dependencies.179if (typeof factory === 'function') {180result = factory.apply(m.exports, deps);181} else {182result = factory;183}184185if (result !== undefined) {186m.exports = result;187if (id) {188loaderCache[id] = m.exports;189}190}191}192193stringRequire = function (systemRequire, exports, module, id, relId) {194//Split the ID by a ! so that195var index = id.indexOf('!'),196originalId = id,197prefix, plugin;198199if (index === -1) {200id = normalize(id, relId);201202//Straight module lookup. If it is one of the special dependencies,203//deal with it, otherwise, delegate to node.204if (id === 'require') {205return makeRequire(systemRequire, exports, module, relId);206} else if (id === 'exports') {207return exports;208} else if (id === 'module') {209return module;210} else if (loaderCache.hasOwnProperty(id)) {211return loaderCache[id];212} else if (defineCache[id]) {213runFactory.apply(null, defineCache[id]);214return loaderCache[id];215} else {216if(systemRequire) {217return systemRequire(originalId);218} else {219throw new Error('No module with ID: ' + id);220}221}222} else {223//There is a plugin in play.224prefix = id.substring(0, index);225id = id.substring(index + 1, id.length);226227plugin = stringRequire(systemRequire, exports, module, prefix, relId);228229if (plugin.normalize) {230id = plugin.normalize(id, makeNormalize(relId));231} else {232//Normalize the ID normally.233id = normalize(id, relId);234}235236if (loaderCache[id]) {237return loaderCache[id];238} else {239plugin.load(id, makeRequire(systemRequire, exports, module, relId), makeLoad(id), {});240241return loaderCache[id];242}243}244};245246//Create a define function specific to the module asking for amdefine.247function define(id, deps, factory) {248if (Array.isArray(id)) {249factory = deps;250deps = id;251id = undefined;252} else if (typeof id !== 'string') {253factory = id;254id = deps = undefined;255}256257if (deps && !Array.isArray(deps)) {258factory = deps;259deps = undefined;260}261262if (!deps) {263deps = ['require', 'exports', 'module'];264}265266//Set up properties for this module. If an ID, then use267//internal cache. If no ID, then use the external variables268//for this node module.269if (id) {270//Put the module in deep freeze until there is a271//require call for it.272defineCache[id] = [id, deps, factory];273} else {274runFactory(id, deps, factory);275}276}277278//define.require, which has access to all the values in the279//cache. Useful for AMD modules that all have IDs in the file,280//but need to finally export a value to node based on one of those281//IDs.282define.require = function (id) {283if (loaderCache[id]) {284return loaderCache[id];285}286287if (defineCache[id]) {288runFactory.apply(null, defineCache[id]);289return loaderCache[id];290}291};292293define.amd = {};294295return define;296}297298module.exports = amdefine;299300301