1
- define ( [ "module" ] , function ( module ) {
1
+ define ( [ "module" ] , function ( module ) {
2
2
var cache = ( module . config && module . config ( ) ) || { } ;
3
-
3
+
4
4
function resolve ( resource , has , isBuild ) {
5
5
var tokens = resource . match ( / [ \? : ] | [ ^ : \? ] + / g) ;
6
6
var i = 0 ;
7
- console . log ( resource )
8
- get = function ( skip ) {
7
+ var get = function ( skip ) {
9
8
var term = tokens [ i ++ ] ;
10
- if ( term === ":" ) {
9
+ if ( term === ":" ) {
11
10
// empty string module name; therefore, no dependency
12
11
return "" ;
13
- } else {
12
+ } else {
14
13
// postfixed with a ? means it is a feature to branch on, the term is the name of the feature
15
- if ( tokens [ i ++ ] === "?" ) {
14
+ if ( tokens [ i ++ ] === "?" ) {
16
15
var hasResult = has ( term ) ;
17
- if ( hasResult === undefined && isBuild ) {
16
+ if ( hasResult === undefined && isBuild ) {
18
17
return undefined ;
19
- } else if ( ! skip && hasResult ) {
18
+ } else if ( ! skip && hasResult ) {
20
19
// matched the feature, get the first value from the options
21
20
return get ( ) ;
22
- } else {
21
+ } else {
23
22
// did not match, get the second value, passing over the first
24
23
get ( true ) ;
25
24
return get ( skip ) ;
@@ -28,53 +27,63 @@ define(["module"], function(module){
28
27
// a module
29
28
return term ;
30
29
}
31
- }
30
+ } ;
32
31
return get ( ) ;
33
32
}
34
-
35
- var has = function ( name ) {
36
- var global = ( function ( ) {
37
- return this ;
33
+
34
+ var has = function ( name ) {
35
+ var global = ( function ( ) {
36
+ return this ;
38
37
} ) ( ) ;
39
-
38
+
40
39
return typeof cache [ name ] === "function" ? ( cache [ name ] = cache [ name ] ( global ) ) : cache [ name ] ; // Boolean
41
40
} ;
42
41
43
42
has . cache = cache ;
44
43
45
- has . add = function ( name , test , now , force ) {
44
+ has . add = function ( name , test , now , force ) {
46
45
( typeof cache [ name ] === "undefined" || force ) && ( cache [ name ] = test ) ;
47
46
return now && has ( name ) ;
48
47
} ;
49
-
50
- has . normalize = function ( resource , normalize ) {
48
+
49
+ has . normalize = function ( resource , normalize ) {
51
50
var tokens = resource . match ( / [ \? : ] | [ ^ : \? ] + / g) ;
52
-
51
+
53
52
for ( var i = 0 ; i < tokens . length ; i ++ ) {
54
- if ( tokens [ i ] !== ":" && tokens [ i ] !== "?" && tokens [ i + 1 ] !== "?" ) {
55
- tokens [ i ] = normalize ( tokens [ i ] ) ;
53
+ if ( tokens [ i ] !== ":" && tokens [ i ] !== "?" && tokens [ i + 1 ] !== "?" ) {
54
+ // The module could be another plugin
55
+ var parts = tokens [ i ] . split ( "!" ) ;
56
+ parts = parts . map ( normalize ) ;
57
+ tokens [ i ] = parts . join ( "!" ) ;
56
58
}
57
59
}
58
-
60
+
59
61
return tokens . join ( "" ) ;
60
62
} ;
61
63
62
- has . load = function ( resource , req , onLoad , config ) {
64
+ has . load = function ( resource , req , onLoad , config ) {
63
65
config = config || { } ;
64
-
66
+
65
67
if ( ! resource || config . isBuild ) {
66
68
onLoad ( ) ;
67
69
return ;
68
70
}
69
-
71
+
70
72
var mid = resolve ( resource , has , config . isBuild ) ;
71
-
72
- if ( mid ) {
73
+
74
+ if ( mid ) {
73
75
req ( [ mid ] , onLoad ) ;
74
- } else {
76
+ } else {
75
77
onLoad ( ) ;
76
78
}
77
79
} ;
78
80
81
+ has . addModules = function ( pluginName , resource , addModules ) {
82
+ var mid = resolve ( resource , has , true ) ;
83
+ if ( mid ) {
84
+ addModules ( [ mid ] ) ;
85
+ }
86
+ } ;
87
+
79
88
return has ;
80
89
} ) ;
0 commit comments