1
1
/**
2
- * almond 0.0.3 Copyright (c) 2011, The Dojo Foundation All Rights Reserved.
2
+ * almond 0.1.1 Copyright (c) 2011, The Dojo Foundation All Rights Reserved.
3
3
* Available via the MIT or new BSD license.
4
4
* see: http://github.com/jrburke/almond for details
5
5
*/
6
- /*jslint strict: false, plusplus: false */
6
+ //Going sloppy to avoid 'use strict' string cost, but strict practices should
7
+ //be followed.
8
+ /*jslint sloppy: true */
7
9
/*global setTimeout: false */
8
10
9
11
var requirejs , require , define ;
10
12
( function ( undef ) {
11
-
12
13
var defined = { } ,
13
14
waiting = { } ,
15
+ config = { } ,
16
+ defining = { } ,
14
17
aps = [ ] . slice ,
15
18
main , req ;
16
19
17
- if ( typeof define === "function" ) {
18
- //If a define is already in play via another AMD loader,
19
- //do not overwrite.
20
- return ;
21
- }
22
-
23
20
/**
24
21
* Given a relative module name, like ./something, normalize it to
25
22
* a real name that can be mapped to a path.
@@ -29,6 +26,11 @@ var requirejs, require, define;
29
26
* @returns {String } normalized name
30
27
*/
31
28
function normalize ( name , baseName ) {
29
+ var baseParts = baseName && baseName . split ( "/" ) ,
30
+ map = config . map ,
31
+ starMap = ( map && map [ '*' ] ) || { } ,
32
+ nameParts , nameSegment , mapValue , foundMap , i , j , part ;
33
+
32
34
//Adjust any relative paths.
33
35
if ( name && name . charAt ( 0 ) === "." ) {
34
36
//If have a base name, try to normalize against it,
@@ -40,13 +42,11 @@ var requirejs, require, define;
40
42
//module. For instance, baseName of "one/two/three", maps to
41
43
//"one/two/three.js", but we want the directory, "one/two" for
42
44
//this normalization.
43
- baseName = baseName . split ( "/" ) ;
44
- baseName = baseName . slice ( 0 , baseName . length - 1 ) ;
45
+ baseParts = baseParts . slice ( 0 , baseParts . length - 1 ) ;
45
46
46
- name = baseName . concat ( name . split ( "/" ) ) ;
47
+ name = baseParts . concat ( name . split ( "/" ) ) ;
47
48
48
49
//start trimDots
49
- var i , part ;
50
50
for ( i = 0 ; ( part = name [ i ] ) ; i ++ ) {
51
51
if ( part === "." ) {
52
52
name . splice ( i , 1 ) ;
@@ -59,7 +59,7 @@ var requirejs, require, define;
59
59
//no path mapping for a path starting with '..'.
60
60
//This can still fail, but catches the most reasonable
61
61
//uses of ..
62
- break ;
62
+ return true ;
63
63
} else if ( i > 0 ) {
64
64
name . splice ( i - 1 , 2 ) ;
65
65
i -= 2 ;
@@ -71,6 +71,43 @@ var requirejs, require, define;
71
71
name = name . join ( "/" ) ;
72
72
}
73
73
}
74
+
75
+ //Apply map config if available.
76
+ if ( ( baseParts || starMap ) && map ) {
77
+ nameParts = name . split ( '/' ) ;
78
+
79
+ for ( i = nameParts . length ; i > 0 ; i -= 1 ) {
80
+ nameSegment = nameParts . slice ( 0 , i ) . join ( "/" ) ;
81
+
82
+ if ( baseParts ) {
83
+ //Find the longest baseName segment match in the config.
84
+ //So, do joins on the biggest to smallest lengths of baseParts.
85
+ for ( j = baseParts . length ; j > 0 ; j -= 1 ) {
86
+ mapValue = map [ baseParts . slice ( 0 , j ) . join ( '/' ) ] ;
87
+
88
+ //baseName segment has config, find if it has one for
89
+ //this name.
90
+ if ( mapValue ) {
91
+ mapValue = mapValue [ nameSegment ] ;
92
+ if ( mapValue ) {
93
+ //Match, update name to the new value.
94
+ foundMap = mapValue ;
95
+ break ;
96
+ }
97
+ }
98
+ }
99
+ }
100
+
101
+ foundMap = foundMap || starMap [ nameSegment ] ;
102
+
103
+ if ( foundMap ) {
104
+ nameParts . splice ( 0 , i , foundMap ) ;
105
+ name = nameParts . join ( '/' ) ;
106
+ break ;
107
+ }
108
+ }
109
+ }
110
+
74
111
return name ;
75
112
}
76
113
@@ -99,8 +136,13 @@ var requirejs, require, define;
99
136
if ( waiting . hasOwnProperty ( name ) ) {
100
137
var args = waiting [ name ] ;
101
138
delete waiting [ name ] ;
139
+ defining [ name ] = true ;
102
140
main . apply ( undef , args ) ;
103
141
}
142
+
143
+ if ( ! defined . hasOwnProperty ( name ) ) {
144
+ throw new Error ( 'No ' + name ) ;
145
+ }
104
146
return defined [ name ] ;
105
147
}
106
148
@@ -136,27 +178,27 @@ var requirejs, require, define;
136
178
} ;
137
179
}
138
180
181
+ function makeConfig ( name ) {
182
+ return function ( ) {
183
+ return ( config && config . config && config . config [ name ] ) || { } ;
184
+ } ;
185
+ }
186
+
139
187
main = function ( name , deps , callback , relName ) {
140
188
var args = [ ] ,
141
189
usingExports ,
142
- cjsModule , depName , i , ret , map ;
190
+ cjsModule , depName , ret , map , i ;
143
191
144
192
//Use name if no relName
145
- if ( ! relName ) {
146
- relName = name ;
147
- }
193
+ relName = relName || name ;
148
194
149
195
//Call the callback to define the module, if necessary.
150
196
if ( typeof callback === 'function' ) {
151
197
152
- //Default to require, exports, module if no deps if
153
- //the factory arg has any arguments specified.
154
- if ( ! deps . length && callback . length ) {
155
- deps = [ 'require' , 'exports' , 'module' ] ;
156
- }
157
-
158
198
//Pull out the defined dependencies and pass the ordered
159
199
//values to the callback.
200
+ //Default to [require, exports, module] if no deps
201
+ deps = ! deps . length && callback . length ? [ 'require' , 'exports' , 'module' ] : deps ;
160
202
for ( i = 0 ; i < deps . length ; i ++ ) {
161
203
map = makeMap ( deps [ i ] , relName ) ;
162
204
depName = map . f ;
@@ -173,15 +215,16 @@ var requirejs, require, define;
173
215
cjsModule = args [ i ] = {
174
216
id : name ,
175
217
uri : '' ,
176
- exports : defined [ name ]
218
+ exports : defined [ name ] ,
219
+ config : makeConfig ( name )
177
220
} ;
178
221
} else if ( defined . hasOwnProperty ( depName ) || waiting . hasOwnProperty ( depName ) ) {
179
222
args [ i ] = callDep ( depName ) ;
180
223
} else if ( map . p ) {
181
224
map . p . load ( map . n , makeRequire ( relName , true ) , makeLoad ( depName ) , { } ) ;
182
225
args [ i ] = defined [ depName ] ;
183
- } else {
184
- throw name + ' missing ' + depName ;
226
+ } else if ( ! defining [ depName ] ) {
227
+ throw new Error ( name + ' missing ' + depName ) ;
185
228
}
186
229
}
187
230
@@ -191,9 +234,10 @@ var requirejs, require, define;
191
234
//If setting exports via "module" is in play,
192
235
//favor that over return value and exports. After that,
193
236
//favor a non-undefined return value over exports use.
194
- if ( cjsModule && cjsModule . exports !== undef ) {
237
+ if ( cjsModule && cjsModule . exports !== undef &&
238
+ cjsModule . exports !== defined [ name ] ) {
195
239
defined [ name ] = cjsModule . exports ;
196
- } else if ( ! usingExports ) {
240
+ } else if ( ret !== undef || ! usingExports ) {
197
241
//Use the return value from the function.
198
242
defined [ name ] = ret ;
199
243
}
@@ -205,27 +249,30 @@ var requirejs, require, define;
205
249
}
206
250
} ;
207
251
208
- requirejs = req = function ( deps , callback , relName , forceSync ) {
252
+ requirejs = require = req = function ( deps , callback , relName , forceSync ) {
209
253
if ( typeof deps === "string" ) {
210
-
211
254
//Just return the module wanted. In this scenario, the
212
255
//deps arg is the module name, and second arg (if passed)
213
256
//is just the relName.
214
257
//Normalize module name, if it contains . or ..
215
258
return callDep ( makeMap ( deps , callback ) . f ) ;
216
259
} else if ( ! deps . splice ) {
217
260
//deps is a config object, not an array.
218
- //Drop the config stuff on the ground.
261
+ config = deps ;
219
262
if ( callback . splice ) {
220
263
//callback is an array, which means it is a dependency list.
221
264
//Adjust args if there are dependencies
222
265
deps = callback ;
223
- callback = arguments [ 2 ] ;
266
+ callback = relName ;
267
+ relName = null ;
224
268
} else {
225
- deps = [ ] ;
269
+ deps = undef ;
226
270
}
227
271
}
228
272
273
+ //Support require(['a'])
274
+ callback = callback || function ( ) { } ;
275
+
229
276
//Simulate async callback;
230
277
if ( forceSync ) {
231
278
main ( undef , deps , callback , relName ) ;
@@ -242,17 +289,11 @@ var requirejs, require, define;
242
289
* Just drops the config on the floor, but returns req in case
243
290
* the config return value is used.
244
291
*/
245
- req . config = function ( ) {
292
+ req . config = function ( cfg ) {
293
+ config = cfg ;
246
294
return req ;
247
295
} ;
248
296
249
- /**
250
- * Export require as a global, but only if it does not already exist.
251
- */
252
- if ( ! require ) {
253
- require = req ;
254
- }
255
-
256
297
define = function ( name , deps , callback ) {
257
298
258
299
//This module may not have dependencies
@@ -264,11 +305,7 @@ var requirejs, require, define;
264
305
deps = [ ] ;
265
306
}
266
307
267
- if ( define . unordered ) {
268
- waiting [ name ] = [ name , deps , callback ] ;
269
- } else {
270
- main ( name , deps , callback ) ;
271
- }
308
+ waiting [ name ] = [ name , deps , callback ] ;
272
309
} ;
273
310
274
311
define . amd = {
0 commit comments