-
Notifications
You must be signed in to change notification settings - Fork 9
/
has.js
106 lines (89 loc) · 2.58 KB
/
has.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
define(["module"], function (module) {
var cache = (module.config && module.config()) || {};
var tokensRE = /[\?:]|[^:\?]+/g;
function resolve(resource, has, isBuild) {
var tokens = resource.match(tokensRE);
var i = 0;
var get = function (skip) {
var term = tokens[i++];
if (term === ":") {
// empty string module name; therefore, no dependency
return "";
} else {
// postfixed with a ? means it is a feature to branch on, the term is the name of the feature
if (tokens[i++] === "?") {
var hasResult = has(term);
if (hasResult === undefined && isBuild) {
return undefined;
} else if (!skip && hasResult) {
// matched the feature, get the first value from the options
return get();
} else {
// did not match, get the second value, passing over the first
get(true);
return get(skip);
}
}
// A module or empty string.
// This allows to tell apart "undefined flag at build time" and "no module required" cases.
return term || "";
}
};
return get();
}
function forEachModule(tokens, callback) {
for (var i = 0; i < tokens.length; i++) {
if (tokens[i] !== ":" && tokens[i] !== "?" && tokens[i + 1] !== "?") {
callback(tokens[i], i);
}
}
}
var has = function (name) {
var global = (function () {
return this;
})();
return typeof cache[name] === "function" ? (cache[name] = cache[name](global)) : cache[name]; // Boolean
};
has.cache = cache;
has.add = function (name, test, now, force) {
if (!has("builder")) {
(typeof cache[name] === "undefined" || force) && (cache[name] = test);
return now && has(name);
}
};
has.normalize = function (resource, normalize) {
var tokens = resource.match(tokensRE);
forEachModule(tokens, function (module, index) {
tokens[index] = normalize(module);
});
return tokens.join("");
};
has.load = function (resource, req, onLoad, config) {
config = config || {};
if (!resource) {
onLoad();
return;
}
var mid = resolve(resource, has, config.isBuild);
if (mid) {
req([mid], onLoad);
} else {
onLoad();
}
};
has.addModules = function (pluginName, resource, addModules) {
var modulesToInclude = [];
var mid = resolve(resource, has, true);
if (mid) {
modulesToInclude.push(mid);
} else if (typeof mid === "undefined") {
// has expression cannot be resolved at build time so include all the modules just in case.
var tokens = resource.match(tokensRE);
forEachModule(tokens, function (module) {
modulesToInclude.push(module);
});
}
addModules(modulesToInclude);
};
return has;
});