1 /** vim: et:ts=4:sw=4:sts=4
2 * @license amdefine 0.1.0 Copyright (c) 2011, The Dojo Foundation All Rights Reserved.
3 * Available via the MIT or new BSD license.
4 * see: http://github.com/jrburke/amdefine for details
8 /*global module, process */
12 * Creates a define for node.
13 * @param {Object} module the "module" object that is defined by Node for the
15 * @param {Function} [requireFn]. Node's require function for the current module.
16 * It only needs to be passed in Node versions before 0.5, when module.require
18 * @returns {Function} a define function that is usable for the current node
21 function amdefine(module, requireFn) {
25 alreadyCalled = false,
26 path = require('path'),
27 makeRequire, stringRequire;
30 * Trims the . and .. from an array of path segments.
31 * It will keep a leading path segment if a .. will become
32 * the first path segment, to help with module name lookups,
33 * which act like paths, but can be remapped. But the end result,
34 * all paths that use this function should look normalized.
35 * NOTE: this method MODIFIES the input array.
36 * @param {Array} ary the array of path segments.
38 function trimDots(ary) {
40 for (i = 0; ary[i]; i+= 1) {
45 } else if (part === '..') {
46 if (i === 1 && (ary[2] === '..' || ary[0] === '..')) {
47 //End of the line. Keep at least one non-dot
48 //path segment at the front so it can be mapped
49 //correctly to disk. Otherwise, there is likely
50 //no path mapping for a path starting with '..'.
51 //This can still fail, but catches the most reasonable
62 function normalize(name, baseName) {
65 //Adjust any relative paths.
66 if (name && name.charAt(0) === '.') {
67 //If have a base name, try to normalize against it,
68 //otherwise, assume it is a top-level require that will
69 //be relative to baseUrl in the end.
71 baseParts = baseName.split('/');
72 baseParts = baseParts.slice(0, baseParts.length - 1);
73 baseParts = baseParts.concat(name.split('/'));
75 name = baseParts.join('/');
83 * Create the normalize() function passed to a loader plugin's
86 function makeNormalize(relName) {
87 return function (name) {
88 return normalize(name, relName);
92 function makeLoad(id) {
93 function load(value) {
94 loaderCache[id] = value;
97 load.fromText = function (id, text) {
98 //This one is difficult because the text can/probably uses
99 //define, and any relative paths and requires should be relative
100 //to that id was it would be found on disk. But this would require
101 //bootstrapping a module/require fairly deeply from node core.
102 //Not sure how best to go about that yet.
103 throw new Error('amdefine does not implement load.fromText');
109 makeRequire = function (systemRequire, exports, module, relId) {
110 function amdRequire(deps, callback) {
111 if (typeof deps === 'string') {
112 //Synchronous, single module require('')
113 return stringRequire(systemRequire, exports, module, deps, relId);
115 //Array of dependencies with a callback.
117 //Convert the dependencies to modules.
118 deps = deps.map(function (depName) {
119 return stringRequire(systemRequire, exports, module, depName, relId);
122 //Wait for next tick to call back the require call.
124 process.nextTick(function () {
125 callback.apply(null, deps);
131 amdRequire.toUrl = function (filePath) {
132 if (filePath.indexOf('.') === 0) {
133 return normalize(filePath, path.dirname(module.filename));
142 //Favor explicit value, passed in if the module wants to support Node 0.4.
143 requireFn = requireFn || function req() {
144 return module.require.apply(module, arguments);
147 function runFactory(id, deps, factory) {
151 e = loaderCache[id] = {};
157 r = makeRequire(requireFn, e, m, id);
159 //Only support one define call per file
161 throw new Error('amdefine with no module ID cannot be called more than once per file.');
163 alreadyCalled = true;
165 //Use the real variables from node
166 //Use module.exports for exports, since
167 //the exports in here is amdefine exports.
170 r = makeRequire(requireFn, e, m, module.id);
173 //If there are dependencies, they are strings, so need
174 //to convert them to dependency values.
176 deps = deps.map(function (depName) {
181 //Call the factory with the right dependencies.
182 if (typeof factory === 'function') {
183 result = factory.apply(m.exports, deps);
188 if (result !== undefined) {
191 loaderCache[id] = m.exports;
196 stringRequire = function (systemRequire, exports, module, id, relId) {
197 //Split the ID by a ! so that
198 var index = id.indexOf('!'),
203 id = normalize(id, relId);
205 //Straight module lookup. If it is one of the special dependencies,
206 //deal with it, otherwise, delegate to node.
207 if (id === 'require') {
208 return makeRequire(systemRequire, exports, module, relId);
209 } else if (id === 'exports') {
211 } else if (id === 'module') {
213 } else if (loaderCache.hasOwnProperty(id)) {
214 return loaderCache[id];
215 } else if (defineCache[id]) {
216 runFactory.apply(null, defineCache[id]);
217 return loaderCache[id];
220 return systemRequire(originalId);
222 throw new Error('No module with ID: ' + id);
226 //There is a plugin in play.
227 prefix = id.substring(0, index);
228 id = id.substring(index + 1, id.length);
230 plugin = stringRequire(systemRequire, exports, module, prefix, relId);
232 if (plugin.normalize) {
233 id = plugin.normalize(id, makeNormalize(relId));
235 //Normalize the ID normally.
236 id = normalize(id, relId);
239 if (loaderCache[id]) {
240 return loaderCache[id];
242 plugin.load(id, makeRequire(systemRequire, exports, module, relId), makeLoad(id), {});
244 return loaderCache[id];
249 //Create a define function specific to the module asking for amdefine.
250 function define(id, deps, factory) {
251 if (Array.isArray(id)) {
255 } else if (typeof id !== 'string') {
257 id = deps = undefined;
260 if (deps && !Array.isArray(deps)) {
266 deps = ['require', 'exports', 'module'];
269 //Set up properties for this module. If an ID, then use
270 //internal cache. If no ID, then use the external variables
271 //for this node module.
273 //Put the module in deep freeze until there is a
274 //require call for it.
275 defineCache[id] = [id, deps, factory];
277 runFactory(id, deps, factory);
281 //define.require, which has access to all the values in the
282 //cache. Useful for AMD modules that all have IDs in the file,
283 //but need to finally export a value to node based on one of those
285 define.require = function (id) {
286 if (loaderCache[id]) {
287 return loaderCache[id];
290 if (defineCache[id]) {
291 runFactory.apply(null, defineCache[id]);
292 return loaderCache[id];
301 module.exports = amdefine;