-
Notifications
You must be signed in to change notification settings - Fork 7
/
globals.js
74 lines (63 loc) · 1.84 KB
/
globals.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
"use strict";
/**
* Define a getter property on the given object that requires the given
* module. This enables delaying importing modules until the module is
* actually used.
*
* @param Object obj
* The object to define the property on.
* @param String property
* The property name.
* @param String|Function module
* The module path or a function that returns a module evaluated once.
* @param Boolean destructure
* Pass true if the property name is a member of the module's exports.
*/
function lazyRequire (obj, property, module, destructure) {
/*
Object.defineProperty(obj, property, {
get: () => {
// Redefine this accessor property as a data property.
// Delete it first, to rule out "too much recursion" in case obj is
// a proxy whose defineProperty handler might unwittingly trigger this
// getter again.
delete obj[property];
var value;
if (typeof module === "function") {
value = module();
}
else {
value = destructure
? require(module)[property]
: require(module || property);
}
Object.defineProperty(obj, property, {
value,
writable: true,
configurable: true,
enumerable: true
});
return value;
},
configurable: true,
enumerable: true
});
*/
}
// Shim out these lazy getters, as they can all be implemented
// with lazyRequire
function lazyGetter (obj, name, fn) {
return lazyRequire(obj, name, fn);
}
function lazyImporter (obj, name, path) {
return lazyRequire(obj, name, path, true);
}
function lazyServiceGetter (obj, name, fn) {
throw new Error("`lazyServiceGetter` cannot be implemented in content.");
}
var loader = {
lazyGetter: lazyGetter,
lazyImporter: lazyImporter,
lazyRequireGetter: lazyRequire,
lazyServiceGetter: lazyServiceGetter,
};