Skip to content

Commit ade17f0

Browse files
committed
fix has behavior when flag are not defined at buildtime
1 parent 54659ae commit ade17f0

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

has.js

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
define(["module"], function (module) {
22
var cache = (module.config && module.config()) || {};
3+
var tokensRE = /[\?:]|[^:\?]+/g;
34

45
function resolve(resource, has, isBuild) {
5-
var tokens = resource.match(/[\?:]|[^:\?]+/g);
6+
var tokens = resource.match(tokensRE);
67
var i = 0;
78
var get = function (skip) {
89
var term = tokens[i++];
@@ -24,13 +25,22 @@ define(["module"], function (module) {
2425
return get(skip);
2526
}
2627
}
27-
// a module
28-
return term;
28+
// A module or empty string.
29+
// This allows to tell apart "undefined flag at build time" and "no module required" cases.
30+
return term || "";
2931
}
3032
};
3133
return get();
3234
}
3335

36+
function forEachModule(tokens, callback) {
37+
for (var i = 0; i < tokens.length; i++) {
38+
if (tokens[i] !== ":" && tokens[i] !== "?" && tokens[i + 1] !== "?") {
39+
callback(tokens[i], i);
40+
}
41+
}
42+
}
43+
3444
var has = function (name) {
3545
var global = (function () {
3646
return this;
@@ -42,29 +52,26 @@ define(["module"], function (module) {
4252
has.cache = cache;
4353

4454
has.add = function (name, test, now, force) {
45-
(typeof cache[name] === "undefined" || force) && (cache[name] = test);
46-
return now && has(name);
55+
if (!has("builder")) {
56+
(typeof cache[name] === "undefined" || force) && (cache[name] = test);
57+
return now && has(name);
58+
}
4759
};
4860

4961
has.normalize = function (resource, normalize) {
50-
var tokens = resource.match(/[\?:]|[^:\?]+/g);
62+
var tokens = resource.match(tokensRE);
5163

52-
for (var i = 0; i < tokens.length; 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[0] = normalize(parts[0]);
57-
tokens[i] = parts.join("!");
58-
}
59-
}
64+
forEachModule(tokens, function (module, index) {
65+
tokens[index] = normalize(module);
66+
});
6067

6168
return tokens.join("");
6269
};
6370

6471
has.load = function (resource, req, onLoad, config) {
6572
config = config || {};
6673

67-
if (!resource || config.isBuild) {
74+
if (!resource) {
6875
onLoad();
6976
return;
7077
}
@@ -79,10 +86,20 @@ define(["module"], function (module) {
7986
};
8087

8188
has.addModules = function (pluginName, resource, addModules) {
89+
var modulesToInclude = [];
90+
8291
var mid = resolve(resource, has, true);
8392
if (mid) {
84-
addModules([mid]);
93+
modulesToInclude.push(mid);
94+
} else if (typeof mid === "undefined") {
95+
// has expression cannot be resolved at build time so include all the modules just in case.
96+
var tokens = resource.match(tokensRE);
97+
forEachModule(tokens, function (module) {
98+
modulesToInclude.push(module);
99+
});
85100
}
101+
102+
addModules(modulesToInclude);
86103
};
87104

88105
return has;

0 commit comments

Comments
 (0)