This repository was archived by the owner on Jul 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy pathloader-polyfill.js
272 lines (249 loc) · 7.3 KB
/
loader-polyfill.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
import { addToError, createSymbol, toStringTag } from './common.js';
export { Loader, ModuleNamespace, REGISTRY }
var resolvedPromise = Promise.resolve();
/*
* Simple Array values shim
*/
function arrayValues (arr) {
if (arr.values)
return arr.values();
if (typeof Symbol === 'undefined' || !Symbol.iterator)
throw new Error('Symbol.iterator not supported in this browser');
var iterable = {};
iterable[Symbol.iterator] = function () {
var keys = Object.keys(arr);
var keyIndex = 0;
return {
next: function () {
if (keyIndex < keys.length)
return {
value: arr[keys[keyIndex++]],
done: false
};
else
return {
value: undefined,
done: true
};
}
};
};
return iterable;
}
/*
* 3. Reflect.Loader
*
* We skip the entire native internal pipeline, just providing the bare API
*/
// 3.1.1
function Loader () {
this.registry = new Registry();
}
// 3.3.1
Loader.prototype.constructor = Loader;
function ensureInstantiated (module) {
if (module === undefined)
return;
if (module instanceof ModuleNamespace === false && module[toStringTag] !== 'Module')
throw new TypeError('Module instantiation did not return a valid namespace object.');
return module;
}
// 3.3.2
Loader.prototype.import = function (key, parent) {
if (typeof key !== 'string')
throw new TypeError('Loader import method must be passed a module key string');
// custom resolveInstantiate combined hook for better perf
var loader = this;
return resolvedPromise
.then(function () {
return loader[RESOLVE_INSTANTIATE](key, parent);
})
.then(ensureInstantiated)
//.then(Module.evaluate)
.catch(function (err) {
throw addToError(err, 'Loading ' + key + (parent ? ' from ' + parent : ''));
});
};
// 3.3.3
var RESOLVE = Loader.resolve = createSymbol('resolve');
/*
* Combined resolve / instantiate hook
*
* Not in current reduced spec, but necessary to separate RESOLVE from RESOLVE + INSTANTIATE as described
* in the spec notes of this repo to ensure that loader.resolve doesn't instantiate when not wanted.
*
* We implement RESOLVE_INSTANTIATE as a single hook instead of a separate INSTANTIATE in order to avoid
* the need for double registry lookups as a performance optimization.
*/
var RESOLVE_INSTANTIATE = Loader.resolveInstantiate = createSymbol('resolveInstantiate');
// default resolveInstantiate is just to call resolve and then get from the registry
// this provides compatibility for the resolveInstantiate optimization
Loader.prototype[RESOLVE_INSTANTIATE] = function (key, parent) {
var loader = this;
return loader.resolve(key, parent)
.then(function (resolved) {
return loader.registry.get(resolved);
});
};
function ensureResolution (resolvedKey) {
if (resolvedKey === undefined)
throw new RangeError('No resolution found.');
return resolvedKey;
}
Loader.prototype.resolve = function (key, parent) {
var loader = this;
return resolvedPromise
.then(function() {
return loader[RESOLVE](key, parent);
})
.then(ensureResolution)
.catch(function (err) {
throw addToError(err, 'Resolving ' + key + (parent ? ' to ' + parent : ''));
});
};
// 3.3.4 (import without evaluate)
// this is not documented because the use of deferred evaluation as in Module.evaluate is not
// documented, as it is not considered a stable feature to be encouraged
// Loader.prototype.load may well be deprecated if this stays disabled
/* Loader.prototype.load = function (key, parent) {
return Promise.resolve(this[RESOLVE_INSTANTIATE](key, parent || this.key))
.catch(function (err) {
throw addToError(err, 'Loading ' + key + (parent ? ' from ' + parent : ''));
});
}; */
/*
* 4. Registry
*
* Instead of structuring through a Map, just use a dictionary object
* We throw for construction attempts so this doesn't affect the public API
*
* Registry has been adjusted to use Namespace objects over ModuleStatus objects
* as part of simplifying loader API implementation
*/
var iteratorSupport = typeof Symbol !== 'undefined' && Symbol.iterator;
var REGISTRY = createSymbol('registry');
function Registry() {
this[REGISTRY] = {};
}
// 4.4.1
if (iteratorSupport) {
// 4.4.2
Registry.prototype[Symbol.iterator] = function () {
return this.entries()[Symbol.iterator]();
};
// 4.4.3
Registry.prototype.entries = function () {
var registry = this[REGISTRY];
return arrayValues(Object.keys(registry).map(function (key) {
return [key, registry[key]];
}));
};
}
// 4.4.4
Registry.prototype.keys = function () {
return arrayValues(Object.keys(this[REGISTRY]));
};
// 4.4.5
Registry.prototype.values = function () {
var registry = this[REGISTRY];
return arrayValues(Object.keys(registry).map(function (key) {
return registry[key];
}));
};
// 4.4.6
Registry.prototype.get = function (key) {
return this[REGISTRY][key];
};
// 4.4.7
Registry.prototype.set = function (key, namespace) {
if (!(namespace instanceof ModuleNamespace || namespace[toStringTag] === 'Module'))
throw new Error('Registry must be set with an instance of Module Namespace');
this[REGISTRY][key] = namespace;
return this;
};
// 4.4.8
Registry.prototype.has = function (key) {
return Object.hasOwnProperty.call(this[REGISTRY], key);
};
// 4.4.9
Registry.prototype.delete = function (key) {
if (Object.hasOwnProperty.call(this[REGISTRY], key)) {
delete this[REGISTRY][key];
return true;
}
return false;
};
/*
* Simple ModuleNamespace Exotic object based on a baseObject
* We export this for allowing a fast-path for module namespace creation over Module descriptors
*/
// var EVALUATE = createSymbol('evaluate');
var BASE_OBJECT = createSymbol('baseObject');
// 8.3.1 Reflect.Module
/*
* Best-effort simplified non-spec implementation based on
* a baseObject referenced via getters.
*
* Allows:
*
* loader.registry.set('x', new Module({ default: 'x' }));
*
* Optional evaluation function provides experimental Module.evaluate
* support for non-executed modules in registry.
*/
function ModuleNamespace (baseObject/*, evaluate*/) {
Object.defineProperty(this, BASE_OBJECT, {
value: baseObject
});
// evaluate defers namespace population
/* if (evaluate) {
Object.defineProperty(this, EVALUATE, {
value: evaluate,
configurable: true,
writable: true
});
}
else { */
Object.keys(baseObject).forEach(extendNamespace, this);
//}
};
// 8.4.2
ModuleNamespace.prototype = Object.create(null);
if (toStringTag)
Object.defineProperty(ModuleNamespace.prototype, toStringTag, {
value: 'Module'
});
function extendNamespace (key) {
Object.defineProperty(this, key, {
enumerable: true,
get: function () {
return this[BASE_OBJECT][key];
}
});
}
/* function doEvaluate (evaluate, context) {
try {
evaluate.call(context);
}
catch (e) {
return e;
}
}
// 8.4.1 Module.evaluate... not documented or used because this is potentially unstable
Module.evaluate = function (ns) {
var evaluate = ns[EVALUATE];
if (evaluate) {
ns[EVALUATE] = undefined;
var err = doEvaluate(evaluate);
if (err) {
// cache the error
ns[EVALUATE] = function () {
throw err;
};
throw err;
}
Object.keys(ns[BASE_OBJECT]).forEach(extendNamespace, ns);
}
// make chainable
return ns;
}; */