/
conditional.js
409 lines (356 loc) · 12.3 KB
/
conditional.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
define(["module", "exports", "@loader"], function(module, exports, System) {
exports.extensionBuilder = "steal-conditional/slim";
function addConditionals(loader) {
var conditionalRegEx = /#\{[^\}]+\}|#\?.+$/;
var isNode = typeof process === "object" &&
{}.toString.call(process) === "[object process]";
if (loader._extensions) {
loader._extensions.push(addConditionals);
}
loader.set("@@conditional-helpers", loader.newModule({
isConditionalModuleName: function(moduleName){
return conditionalRegEx.test(moduleName);
}
}));
var normalize = loader.normalize;
function readMemberExpression(p, value) {
var pParts = p.split(".");
while (pParts.length) {
value = value[pParts.shift()];
}
return value;
}
function includeInBuild(loader, name) {
var load = loader.getModuleLoad(name);
load.metadata.includeInBuild = true;
}
// get some node modules through @node-require which is a noop in the browser
function getGlob() {
if (isNode) {
return loader.import("@node-require", { name: module.id })
.then(function(nodeRequire) {
return nodeRequire("glob");
});
}
return Promise.resolve();
}
function pushIfUnique(array, item) {
return array.indexOf(item) === -1 ? array.push(item) : array.length;
}
/**
* Returns the bundle module name for a string substitution
* @param {string} nameWithConditional The module identifier including the condition
* @param {string} variation A match of the glob pattern
* @return {string} The bundle module name
*/
function getModuleName(nameWithConditional, variation) {
var modName;
var conditionIndex = nameWithConditional.search(conditionalRegEx);
// look for any "/" after the condition
var lastSlashIndex = nameWithConditional.indexOf("/",
nameWithConditional.indexOf("}"));
// substitution of a folder name
if (lastSlashIndex !== -1) {
modName = nameWithConditional.substr(0, conditionIndex) + variation;
}
else {
modName = nameWithConditional.replace(conditionalRegEx, variation);
}
return modName;
}
loader.normalize = function(name, parentName, parentAddress, pluginNormalize) {
var loader = this;
var conditionalMatch = name.match(conditionalRegEx);
if (conditionalMatch) {
var substitution = conditionalMatch[0][1] !== "?";
var conditionModule = substitution ?
conditionalMatch[0].substr(2, conditionalMatch[0].length - 3) :
conditionalMatch[0].substr(2);
var conditionExport = "default";
var conditionExportParts = conditionModule
.match(/^(?:\.\/|\.\.\/)+/); // split './' or '../' in relative names
var conditionExportIndex = conditionModule.indexOf(".",
conditionExportParts && conditionExportParts[0].length); // only look for export
if (conditionExportIndex !== -1) {
conditionExport = conditionModule.substr(conditionExportIndex + 1);
conditionModule = conditionModule.substr(0, conditionExportIndex);
}
var booleanNegation = !substitution && conditionModule[0] === "~";
if (booleanNegation) {
conditionModule = conditionModule.substr(1);
}
var handleConditionalBuild = function() {};
/**
* Adds bundles needed to load conditional modules in production
*
* Conditional modules are loaded dynamically, these modules are
* not part of the dependency graph and steal-tools has to create
* individual bundles for each of them.
*
* ** The boolean syntax **
*
* After removing the condition from the module identifier, the
* result is the identifier of the module that could be loaded
* during runtime, e.g:
*
* With an indentifier like: `es5-shim#?conditions.needs-es5shim`,
* removing everything after the condition gives us back `es5-shim`
* which is then configured to be made its own bundle by steal-tools.
*
* ** The substitution syntax **
*
* This one is more involved, since there is no way statically to
* figure out all of the possible string substitution value. The
* following algorithm is used to detect them:
*
* 1) The condition is replaced by a placeholder in the module
* identifier; e.g: `jquery/#{browser.grade}` is turned into
* `jquery/__PLACEHOLDER__`.
*
* 2) The result of step 1 is normalised
*
* 3) The placeholder is replaced by `*` which results in a module
* name that looks like: `app@0.0.1#jquery/*`
*
* 4) The module name from step 3 is run through `locate` which
* returns the address where the variations are located.
*
* 5) The address is then turned into a glob pattern and used to
* match the files that could be loaded
*
* 6) The process is then reverted; the modules addresses are turned
* into module names which will be set to `loader.bundle` so
* bundles are created for each of them.
*/
//!steal-remove-start
handleConditionalBuild = function() {
var nameWithConditional = name;
var PLACEHOLDER = "__PLACEHOLDER__";
// remove the conditional and the trailing slash
var nameSansConditional = nameWithConditional
.replace(conditionalRegEx, PLACEHOLDER)
.replace(/\/+$/, "");
var normalizeConditionModule = loader.normalize(
conditionModule,
parentName,
parentAddress,
pluginNormalize
);
var normalizeNameSansConditional = loader.normalize(
nameSansConditional,
parentName,
parentAddress,
pluginNormalize
);
var setLoaderConfig = Promise
.all([
normalizeNameSansConditional,
normalizeConditionModule
])
// normalize the identifiers that make the condition
// on their own then re-build the condition
.then(function(names) {
var res;
var id = names[0];
var cond = names[1];
if (conditionExportIndex !== -1) {
cond = cond + "." + conditionExport;
}
if (substitution) {
res = id.replace(PLACEHOLDER, "#{" + cond + "}");
} else {
var prefix = booleanNegation ? "#?~" : "#?";
res = id.replace(PLACEHOLDER, prefix + cond);
}
return [res, names[1]];
})
.then(function(names) {
if (loader.normalizeMap) {
loader.normalizeMap[nameWithConditional] = names[0];
}
if (loader.slimConfig) {
pushIfUnique(loader.slimConfig.extensions, module.id);
pushIfUnique(loader.slimConfig.identifiersToResolve, names[0]);
pushIfUnique(loader.slimConfig.toMap, names[1]);
}
});
// make sure loader.bundle is an array
loader.bundle =
typeof loader.bundle === "undefined" ? [] : loader.bundle;
if (substitution) {
var glob = null;
setLoaderConfig = setLoaderConfig
.then(function() {
return getGlob();
})
.then(function(nodeGlob) {
// in the browser we don't load the node modules
if (!nodeGlob) {
throw new Error("glob module not loaded");
}
// make glob available down the pipeline
glob = nodeGlob;
// call the full normalize in case the condition
// module is using the tilde lookup scheme or the
// package name
return normalizeNameSansConditional;
})
.then(function(normalized) {
return loader.locate({
metadata: {},
name: normalized.replace(PLACEHOLDER, "*")
});
})
.then(function(address) {
var path = address.replace("file:", "");
var cwd = path.substr(0, path.indexOf("*"));
var pattern = path.substring(path.indexOf("*"));
return new Promise(function(resolve, reject) {
var options = {
cwd: cwd,
dot: true,
nobrace: true,
noglobstar: true,
noext: true,
nodir: true
};
glob(pattern, options, function(err, files) {
if (err) { reject(err); }
resolve(files);
});
});
})
.then(function(variations) {
var promises = [];
/*
* With an conditional import like this:
*
* import 'locate/#{lang}';
*
* and an app tree that looks like the one below:
*
* locale
* ├── ar.js
* ├── en.js
* ├── es.js
* ├── hi.js
* └── zh.js
*
* `variations` will be an array of the files
* relative to the `locale` folder, e.g:
*
* `['ar.js', 'en.js', 'es.js', 'hi.js', 'zh.js']`
*
* we iterate over the array, remove the extension,
* then take the original module name with the
* substitution syntax and replace it with the
* variation name, after this we get the following
* modules names:
*
* `['locale/ar', 'local/en', ..., 'locale/zh']`
*
* We run each of those module names through the
* `normalize` hook to handle any relative paths,
* and finally we add them to `loader.bundle` (if
* not added already) to make sure we get bundles
* for each variation when the app is built.
*/
for (var i = 0; i < variations.length; i += 1) {
var variation = variations[i];
// remove the extension
variation = variation.substr(0, variation.lastIndexOf("."));
var modName = getModuleName(nameWithConditional, variation);
var promise = loader.normalize.call(loader, modName,
parentName, parentAddress, pluginNormalize);
promises.push(promise.then(function(normalized) {
var isBundle = loader.bundle.indexOf(normalized) !== -1;
if (!isBundle) {
loader.bundle.push(normalized);
}
}));
}
return Promise.all(promises);
});
}
// boolean conditional syntax
else {
var bundleName = name.replace(conditionalRegEx, "");
var isBundle = loader.bundle.indexOf(bundleName) !== -1;
if(!isBundle) {
loader.bundle.push(bundleName);
}
}
name = "@empty";
return setLoaderConfig.then(function() {
return normalize.call(
loader,
name,
parentName,
parentAddress,
pluginNormalize
);
});
};
//!steal-remove-end
var handleConditionalEval = function(m) {
var conditionValue = (typeof m === "object") ?
readMemberExpression(conditionExport, m) : m;
if (substitution) {
if (typeof conditionValue !== "string") {
throw new TypeError(
"The condition value for " +
conditionalMatch[0] +
" doesn't resolve to a string."
);
}
name = name.replace(conditionalRegEx, conditionValue);
}
else {
if (typeof conditionValue !== "boolean") {
throw new TypeError(
"The condition value for " +
conditionalMatch[0] +
" isn't resolving to a boolean."
);
}
if (booleanNegation) {
conditionValue = !conditionValue;
}
if (!conditionValue) {
name = "@empty";
} else {
name = name.replace(conditionalRegEx, "");
}
}
if (name === "@empty") {
return normalize.call(loader, name, parentName, parentAddress, pluginNormalize);
} else {
// call the full normalize in case the module name
// is an npm package (that needs to be normalized)
return loader.normalize.call(loader, name, parentName, parentAddress, pluginNormalize);
}
};
var isBuild = (loader.env || "").indexOf("build") === 0;
var pluginLoader = isBuild ? loader : (loader.pluginLoader || loader);
return pluginLoader["import"](conditionModule, { name: parentName, address: parentAddress })
.then(function(m) {
return pluginLoader
.normalize(conditionModule, parentName, parentAddress, pluginNormalize)
.then(function(fullName) {
includeInBuild(pluginLoader, fullName);
return m;
});
})
.then(function(m) {
return isBuild ?
handleConditionalBuild() :
handleConditionalEval(m);
});
}
return Promise.resolve(normalize.call(loader, name, parentName, parentAddress, pluginNormalize));
};
}
if (typeof System !== "undefined") {
addConditionals(System);
}
});