-
Notifications
You must be signed in to change notification settings - Fork 2
/
define.js
805 lines (648 loc) · 23.2 KB
/
define.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
/*!
* Copyright 2002 - 2017 Webdetails, a Pentaho company. All rights reserved.
*
* This software was developed by Webdetails and is provided under the terms
* of the Mozilla Public License, Version 2.0, or any later version. You may not use
* this file except in compliance with the license. If you need a copy of the license,
* please go to http://mozilla.org/MPL/2.0/. The Initial Developer is Webdetails.
*
* Software distributed under the Mozilla Public License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
* the license for the specific language governing your rights and limitations.
*/
(function(global) {
var U; // undefined
var DEBUG = 0;
var O_hasOwn = Object.prototype.hasOwnProperty;
var O_getOwn = function(p, dv) { return O_hasOwn.call(this, p) ? this[p] : dv; };
var _reJsExt = /\.js$/i;
// "/", "//", "foo://", "bar:", "C:"
var _reAbsPath = /(^\/)|(^[a-z]+:)|(\.js$)/i;
var _rootModule;
var _defaultDeps = ['require', 'exports', 'module'];
var require;
// MAIN Methods
// dep (synchronous syntax)
// deps, fun
function require() {
return _rootModule.require.apply(_rootModule, arguments);
}
// id?, deps?, factory
function define() {
// factory
// deps, factory
// id, factory
// id, deps, factory
var args = arguments;
var L = args.length;
if(!L) throw new Error("Argument 'factory' is required.");
if(L > 3) L = 3; // Ignore args beyond 3.
var id, deps;
var factory = args[L-1]; // any value type will do, although functions are special.
if(L === 3) {
deps = args[1];
id = args[0];
} else if(L === 2) {
// L === 1
id = args[0];
if(id instanceof Array) {
deps = id;
id = U;
}
}
if(deps != null && !(deps instanceof Array))
throw new Error("Argument 'deps' should be an array.");
if(id != null && typeof id !== 'string')
throw new Error("Argument 'id' should be a string.");
_rootModule.dispatchDefine(id, deps, factory);
}
function reset() {
DEBUG && print("BEG RESET");
_rootModule = new Module();
// Read global configuration.
var config = global.__defineCfg__;
if(config) {
DEBUG && print("BEG Loading global configuration");
_rootModule.configure(config);
DEBUG && print("END Loading global configuration");
}
DEBUG && print("END RESET");
}
function configure(config) {
_rootModule.configure(config);
}
function toUrl(moduleId) {
if(_reAbsPath.test(moduleId))
return moduleId;
return _rootModule.get(moduleId).packageMainOrSelf().pathLoad(/* omitExtension: */true);
}
// Inform that this is an AMD define.
define.amd = {};
// Should be changed/mocked
// Can be configured through "loadSync" config option as well.
require.get = function(path) {
DEBUG && print("Should be loading '" + path + "'");
};
require.reset = reset;
require.config = configure;
require.toUrl = toUrl;
// Public module object
function AmdModule(module) {
this.id = module.id;
this.config = function() {
return module.config.config;
};
}
// ------------
function Module(name, parent) {
// NOTE: The following code strives to make every instance
// have the same number and type of properties, as well as their layout.
this.parent = parent || null;
this.children = [];
this._childrenByName = {};
this.config = {};
this._amdModule = null;
if(parent) {
if(!name) throw new Error("Non-root module must have a local name.");
this.name = name || '';
this.id = parent.id ? (parent.id + '/' + name) : name;
this.shared = parent.shared;
} else {
this.name = '';
this.id = '';
this.shared = {
root: this,
// All Modules by id.
all: {},
// Stack of modules that are being loaded.
// For `define(...)` to know to which module
// the dependencies and definition function belong to.
// TODO: once this is a synchronous implementation,
// is a stack really needed?
loadingModules: []
};
}
DEBUG && print("Creating module named '" + this.name + "' and with id '" + this.id + "'");
this.shared.all[this.id] = this;
this._path = null; // Cached path value.
this.isResolved = false; // Quick state check
this.isLoaded = false;
this.isLoading = false;
this.exports = U;
// -----------
if(parent) parent._addChild(this);
}
Module.prototype.dispose = function() {
var id = this.id;
if(id) {
this.id = null;
var parent = this.parent;
if(parent) {
this.parent = null;
var i = parent.children.indexOf(this);
if(i >= 0) parent.children.splice(i, 1);
delete parent._childrenByName[this.name];
}
delete this.shared.all[id];
}
};
Module.prototype.isRoot = function() { return !this.parent; };
Module.prototype.isTopLevel = function() { var p = this.parent; return !!p && !p.parent; };
Module.prototype._addChild = function(child) {
var name = child.name;
// assertion 1
if(DEBUG && O_hasOwn.call(this._childrenByName, name))
throw new Error("Already have a child module named '" + name + "'.");
this.children.push(child);
this._childrenByName[name] = child;
};
// SPECIAL modules' support.
Module.prototype.getAmdModule = function() {
return this._amdModule || (this._amdModule = new AmdModule(this));
};
Module.prototype.getExports = function() {
return this.exports !== U ? this.exports : (this.exports = {});
};
// The root module returns the global require function.
// Other modules return a contextual require.
// Only the global require function defines `config` and `reset`.
Module.prototype.getRequire = function() {
if(this.isRoot()) return require;
var ctxModule = this;
contextualRequire.toUrl = function(moduleId) {
if(_reAbsPath.test(moduleId))
return moduleId;
return ctxModule.get(moduleId).packageMainOrSelf().pathLoad(/* omitExtension: */true);
};
return contextualRequire;
function contextualRequire() {
return ctxModule.require.apply(ctxModule, arguments);
}
};
// -----------
Module.prototype.path = function() {
var path = this._path;
return path != null ? path : this._evalPath();
};
Module.prototype.basePath = function(_) {
if(arguments.length) {
this.shared.root.config.basePath = _ || '';
return this;
}
return this.shared.root.config.basePath;
};
Module.prototype.isPackage = function() {
return !!this.packageMain;
};
Module.prototype.packageMainOrSelf = function() {
return this.packageMain || this;
};
// The path for loading purposes (and not for parent child path concatenation, etc.)
Module.prototype.pathLoad = function(omitExtension) {
var path = this.path();
var basePath = this.basePath();
if(basePath && !_reAbsPath.test(path)) {
path = basePath + '/' + path;
}
return omitExtension ? path : (path.replace(_reJsExt, '') + '.js');
};
Module.prototype._evalPath = function() {
var path = this.config.path;
if(path == null) {
var parent = this.parent;
path = parent ? parent.path() : '';
// Fixating path to '', makes child paths become 'foo', 'bar', ...
// This has the added benefit of supporting the root node.
if(path) path += '/' + this.name;
else path = this.name;
}
return (this._path = path);
};
// To be called when this.config.path is changed.
Module.prototype._invalidatePath = function() {
this._path = null;
this.children.forEach(function(child) {
if(child.config.path == null) child._invalidatePath();
});
};
Module.prototype.child = function(name) {
return O_getOwn.call(this._childrenByName, name);
};
// -----
// Should only be called on a root module.
Module.prototype.configure = function(config) {
DEBUG && print("BEG configure");
if(DEBUG && !this.isRoot()) throw new Error("Invalid operation.");
var baseUrl = config.baseUrl;
if(baseUrl !== U) this.basePath(baseUrl);
if(typeof config.loadSync === 'function') require.get = config.loadSync;
// Packages - Array of PackageConfig
var packageList = config.packages;
if(packageList) {
packageList.forEach(function(pkg) {
if(!pkg) throw new Error("Invalid `packages` configuration.");
if(typeof pkg === 'string') pkg = {name: pkg};
var name = pkg.name;
if(!name) throw new Error("Package must have a name.");
this.get(name, true).configPackage(pkg);
}, this);
}
var amid;
// Shim - Map of "absolute module-id" to "shim-config".
var shims = config.shim;
for(amid in shims) {
var shim = shims[amid];
if(!shim) throw new Error("Invalid `shim` configuration.");
this.get(amid, true).configShim(shim);
}
// Paths - Map of "absolute module id prefixes" to "path".
var paths = config.paths;
for(amid in paths) {
var path = paths[amid];
// TODO: should allow an '' path?
if(!path) throw new Error("Invalid `paths` configuration.");
this.get(amid, true).configPath(path);
}
// Also need map
// Map - Map of "absolute module id prefixes" to a MapConfig.
var maps = config.map;
for(var contextAmid in maps) {
var map;
if(!contextAmid || !(map = maps[contextAmid]))
throw new Error("Invalid `map` configuration.");
var contextModule = contextAmid === '*'
? this //.shared.root
: this.get(contextAmid, true);
for(var fromAmid in map) {
var toAmid;
if(!fromAmid || !(toAmid = map[fromAmid]))
throw new Error("Invalid `map` configuration.");
// NOTE: fromAmid may be equal to toAmid (to undo an * config.)
contextModule
.configMap(this.get(fromAmid, true), this.get(toAmid, true));
}
}
// Configs - Map of "absolute module id" to configuration value.
var configs = config.config;
for(amid in configs) {
this.get(amid, true).configConfig(configs[amid]);
}
DEBUG && print("END configure");
};
Module.prototype.configPackage = function(pkg) {
var main = pkg.main;
if(main) { if(_reJsExt.test(main)) main = main.substr(0, main.length - 3); }
else { main = 'main'; }
this.packageMain = this.get('./' + this.name + '/' + main);
if(pkg.location) this.configPath(pkg.location);
};
Module.prototype.configShim = function(shim) {
if(shim instanceof Array) shim = {deps: shim};
var deps = shim.deps;
this.config.depIds = deps && deps.length ? deps : null;
this.config.shim = {
exportsVar: shim.exports,
init: shim.init
};
};
Module.prototype.configPath = function(path) {
this.config.path = path;
this._invalidatePath();
};
Module.prototype.configMap = function(fromModule, toModule) {
// this = contextModule
var maps = this.config.maps || (this.config.maps = []);
// Need more specific from-modules first.
// TODO: binary search...
maps.push({ctx: this, from: fromModule, to: toModule});
maps.sort(function(a, b) { return a.from.compare(b.from); });
};
Module.prototype.configConfig = function(config) {
var configOld = this.config.config;
if(config != null &&
configOld != null &&
typeof config === "object" &&
typeof configOld === "object") {
// Merge top-level
for(var p in config) {
configOld[p] = config[p];
}
return;
}
this.config.config = config;
};
// ----------
// Order by specificity (more specific first),
// and then by descending alphabetical order of ids (when unrelated).
Module.prototype.compare = function(b) {
var a = this;
if(a === b) return 0; // equal
return a.contains(b) === 0 ? +1 : // a is parent of b
b.contains(a) === 0 ? -1 : // a is child of b
// Unrelated.
// Don't want to return 0 on this case,
// to reserve it for the equal condition,
// so compare using descending alphabetical order of ids.
a.id < b.id ? +1 :
a.id > b.id ? -1 :
0; // should not happen...
};
Module.prototype.contains = function(other) {
if(this === other) return true;
if(DEBUG && this.shared !== other.shared)
throw new Error("Module is not from the same module hierarchy.");
if(this.isRoot()) return true;
do { if(this === other) return true; } while((other = other.parent));
return false;
};
Module.prototype.mapRuleLocal = function(fromModule) {
// this = contextModule;
if(fromModule !== this) { // TODO: Should this be ensured?
var maps = this.config.maps;
if(maps) {
// Relies on the "more-specific-from first" order of maps.
for(var i = 0, L = maps.length ; i < L ; i++) {
var map = maps[i];
if(map.from.contains(fromModule)) return map;
}
}
}
return null;
};
Module.prototype.mapRule = function(fromModule) {
// contextModule = this, this.parent, this.parent.parent, ...
return this.mapRuleLocal(fromModule) ||
(this.parent ? this.parent.mapRule(fromModule) : null);
};
// Excluding the name of self.
Module.prototype.namesTo = function(descendantOrSelf) {
var names = [];
while(descendantOrSelf && this !== descendantOrSelf) {
names.push(descendantOrSelf.name);
descendantOrSelf = descendantOrSelf.parent;
}
if(!descendantOrSelf) throw new Error("Not a descendant or self.");
names.reverse();
return names;
};
Module.prototype.get = function(mid, expectsAbsolute) {
DEBUG && print("get mid '" + mid + "' on '" + this.id + "'.");
// contextModule = this (for mapping)
// baseModule = this.parent (for starting relative mids)
// fromModule =~ mid
var isRelative = mid.charAt(0) === '.';
if(expectsAbsolute && isRelative) throw new Error("Absolute module id expected, but found '" + mid + "'.");
var module = !isRelative ? this.shared.root : (this.parent || this); // root can't go further up
var midTerms = mid.split('/');
while(midTerms.length) {
var term = midTerms.shift();
//DEBUG && print(" Checking term '" + term + "'");
// Two consecutive slashes? '//'
if(!term) throw new Error("Invalid module id '" + mid + "'. Contains empty term.");
if(term === '.') {
// Stay in same module.
} else if(term === '..') {
// One term up. Go to parent, if any, or it is an error.
// At most, can go to root.
module = module.parent;
if(!module)
throw new Error("Invalid module id '" + mid + "'. It is above root module.");
} else {
// Get existing child or create one.
var child = module.child(term);
var isNew = !child;
if(isNew || /*isLast*/!midTerms.length) {
// If it is a new module:
// Check if there is a module mapping rule first.
// Unfortunately, for that we need to create the "from" module...
// So we dispose it later if we jump.
// If so, don't create a new module,
// cause its not going to be used.
// There's no problem in this case,
// cause there's no danger of existing a more specific
// mapping rule "below", cause right now...
// there are no modules below,
// and so no configs as well.
if(isNew) {
DEBUG && print(" New module '" + term + "' on '" + module.id + "'.");
child = new Module(term, module);
}
var mapRule = this.mapRule(child);
if(mapRule) {
DEBUG && print(" Map module '" + mapRule.from.id + "' to '" + mapRule.to.id + "'.");
// Repeat "delta terms"
var deltaTerms = mapRule.from.namesTo(child);
if(deltaTerms.length)
midTerms = deltaTerms.concat(midTerms);
// Revert creation of new module...
if(isNew) child.dispose();
child = mapRule.to;
}
}
module = child;
}
}
// Cannot go up to/get the root module?
if(module.isRoot()) throw new Error("Invalid module id '" + mid + "'. It is the root module.");
DEBUG && print("get -> '" + module.id + "'");
return module;
};
Module.prototype._resolveDeps = function() {
// Root cannot have dependencies...
// Except for what mapping is concerned,
// dependencies are resolved relative to parent module.
if(this.parent) {
// Shim deps are set in config.depIds as well.
var depIds = this.config.depIds;
if(depIds) return this.requireMany(depIds, /*inDefine*/true);
}
};
Module.prototype.require = function() {
DEBUG && print("BEG require on '" + this.id + "'");
try {
var depId, depIds, callback, errback;
var i = -1;
var args = arguments;
var L = args.length;
while(++i < L) {
var a = args[i];
switch(typeof a) {
case 'function':
// Guard against errback.
if(!callback) callback = a;
break;
case 'string': depId = a; break;
case 'object': if(a instanceof Array) depIds = a; break;
}
}
if(callback) {
this.apply(depIds, callback);
} else {
// Synchronous only syntax
if(depId ) return this.requireOne(depId);
if(depIds) return this.requireMany(depIds);
}
} finally {
DEBUG && print("END require");
}
};
Module.prototype.requireMany = function(mids, inDefine) {
return mids.map(function(mid) {
return this.requireOne(mid, inDefine);
}, this);
};
Module.prototype.requireOne = function(mid, inDefine) {
// Special modules
if(mid === 'require') return this.getRequire();
if(mid === 'exports') return inDefine ? this.getExports() : U;
if(mid === 'module' ) return inDefine ? this.getAmdModule() : U;
// Load urls directly
if(_reAbsPath.test(mid)) {
return void this._loadPath(mid);
}
// Get or create a module instance
// that corresponds to the dependency module id.
// Mid can be relative.
var data;
var index = mid.indexOf('!');
var isPlugin = index >= 0;
if(isPlugin) {
data = mid.substr(index + 1)
mid = mid.substr(0, index);
}
var module = this.get(mid).packageMainOrSelf();
return isPlugin ? module.pluginLoad(data, /* fromModule: */this) : module.resolve();
};
Module.prototype.apply = function(mids, f, x) {
var args = mids && this.requireMany(mids);
if(f) return f.apply(x || global, args || []);
};
Module.prototype.loadingModule = function() {
var lms = this.shared.loadingModules;
return lms.length ? lms[lms.length - 1] : null;
};
// Should only be called on the root module.
// Called by the global `define(...)` to do the actual defining.
Module.prototype.dispatchDefine = function(id, deps, factory) {
// Hook up to loading module, if any.
DEBUG && print("BEG Defining mid " +
(id != null ? ("'" + id + "'") : "<anonymous>") +
" with deps [" + (deps || '') + "]");
var loadingModule;
if(!id) {
loadingModule = this.loadingModule();
// This was a requested module, or it is an error to have no name.
if(!loadingModule) throw new Error("There's no module currently being loaded.");
} else {
// Even if this was a request, the specified `id` is what counts.
loadingModule = this.get(id, true);
if(loadingModule.isLoaded) {
// Don't overwrite modules that were already loaded.
DEBUG && print("A module with id '" + loadingModule.id + "' was already loaded. Ignoring this one.");
return;
}
}
loadingModule.define(deps, factory);
DEBUG && print("END Defining");
};
Module.prototype.define = function(depIds, factory) {
DEBUG && print("BEG Define '" + this.id + "' with deps: [" + depIds + "]");
var isFactoryFun = typeof factory === 'function';
if(depIds) {
if(!depIds.length) depIds = null;
} else if(isFactoryFun && factory.length) {
depIds = _defaultDeps.slice(0, factory.length);
}
this.config.depIds = depIds;
// This is for unrequested defines.
this.isLoading = false;
this.isLoaded = true;
if(isFactoryFun) {
this.config.factory = factory;
} else {
this.exports = factory;
if(!depIds) this.isResolved = true;
}
DEBUG && print("END Define '" + this.id + "'");
};
Module.prototype.load = function() {
if(!this.isLoaded && !this.isLoading) {
this.isLoading = true;
var path = this.pathLoad();
DEBUG && print("BEG load '" + this.id + "' -> path '" + path + "'");
this.shared.loadingModules.push(this);
try {
this._loadPath(path);
} finally {
this.isLoading = false;
this.isLoaded = true;
this.shared.loadingModules.pop();
DEBUG && print("END load '" + this.id + "'");
}
}
};
Module.prototype._loadPath = function(path) {
require.get(path);
};
Module.prototype.resolve = function() {
if(!this.isResolved) {
// Prevent unbounded recursion when
// cyclic/mutual dependencies exist between modules.
// To load a module that has a cyclic dependency
// and grab a final reference to its exported value,
// the special `module` dependency must be asked for.
// `module.exports` is an object that (hopefully)
// when all/both modules are loaded, will contain all the
// properties of dependency module.
this.isResolved = true;
this._resolveCore();
}
return this.exports;
};
Module.prototype._resolveCore = function() {
var deps;
DEBUG && print("BEG resolve '" + this.id + "'");
var config = this.config;
var shim = config.shim;
if(shim) deps = this._resolveDeps();
this.load();
if(shim) {
if(shim.exportsVar) this.exports = global[shim.exportsVar];
if(shim.init) {
var exports = shim.init.apply(global, deps);
if(exports !== U) this.exports = exports;
}
config.shim = null;
} else {
deps = this._resolveDeps();
if(config.factory) {
var exports = config.factory.apply(global, deps || []);
if(exports !== U) this.exports = exports;
config.factory = null;
}
}
DEBUG && print("END resolve '" + this.id + "'");
};
Module.prototype.pluginLoad = function(id, fromModule) {
DEBUG && print("BEG pluginLoad '" + this.id + "' of '" + id + "'");
var exports;
var plugin = this.resolve();
if(plugin) {
var load = plugin.load;
if(typeof load === 'function') {
load.call(plugin, id, fromModule.getRequire(), function(value) {
exports = value;
}, {isBuild: false});
// Hey! This is a synchronous AMD loader :-?
// So the callback better have been called already...
}
}
DEBUG && print("END pluginLoad '" + this.id + "' of '" + id + "' (exports=" + exports + ")");
return exports;
};
// ----------------
reset();
// Export
global.require = require;
global.define = define;
}(this));