]>
Commit | Line | Data |
---|---|---|
399fa2ee SR |
1 | (function (global, factory) { |
2 | typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | |
3 | typeof define === 'function' && define.amd ? define(factory) : | |
4 | (global.BrowserESModuleLoader = factory()); | |
5 | }(this, (function () { 'use strict'; | |
6 | ||
d1316334 PO |
7 | /*\r |
8 | * Environment\r | |
9 | */\r | |
10 | var isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined';\r | |
11 | var isNode = typeof process !== 'undefined' && process.versions && process.versions.node;\r | |
12 | var isWindows = typeof process !== 'undefined' && typeof process.platform === 'string' && process.platform.match(/^win/);\r | |
13 | \r | |
14 | var envGlobal = typeof self !== 'undefined' ? self : global;\r | |
15 | /*\r | |
16 | * Simple Symbol() shim\r | |
17 | */\r | |
18 | var hasSymbol = typeof Symbol !== 'undefined';\r | |
19 | function createSymbol (name) {\r | |
20 | return hasSymbol ? Symbol() : '@@' + name;\r | |
21 | }\r | |
22 | \r | |
23 | var toStringTag = hasSymbol && Symbol.toStringTag;\r | |
24 | \r | |
25 | \r | |
26 | \r | |
27 | \r | |
28 | \r | |
29 | /*\r | |
30 | * Environment baseURI\r | |
31 | */\r | |
32 | var baseURI;\r | |
33 | \r | |
34 | // environent baseURI detection\r | |
35 | if (typeof document != 'undefined' && document.getElementsByTagName) {\r | |
36 | baseURI = document.baseURI;\r | |
37 | \r | |
38 | if (!baseURI) {\r | |
39 | var bases = document.getElementsByTagName('base');\r | |
40 | baseURI = bases[0] && bases[0].href || window.location.href;\r | |
41 | }\r | |
42 | }\r | |
43 | else if (typeof location != 'undefined') {\r | |
44 | baseURI = location.href;\r | |
45 | }\r | |
46 | \r | |
47 | // sanitize out the hash and querystring\r | |
48 | if (baseURI) {\r | |
49 | baseURI = baseURI.split('#')[0].split('?')[0];\r | |
50 | var slashIndex = baseURI.lastIndexOf('/');\r | |
51 | if (slashIndex !== -1)\r | |
52 | baseURI = baseURI.substr(0, slashIndex + 1);\r | |
53 | }\r | |
54 | else if (typeof process !== 'undefined' && process.cwd) {\r | |
55 | baseURI = 'file://' + (isWindows ? '/' : '') + process.cwd();\r | |
56 | if (isWindows)\r | |
57 | baseURI = baseURI.replace(/\\/g, '/');\r | |
58 | }\r | |
59 | else {\r | |
60 | throw new TypeError('No environment baseURI');\r | |
61 | }\r | |
62 | \r | |
63 | // ensure baseURI has trailing "/"\r | |
64 | if (baseURI[baseURI.length - 1] !== '/')\r | |
65 | baseURI += '/';\r | |
66 | \r | |
67 | /*\r | |
68 | * LoaderError with chaining for loader stacks\r | |
69 | */\r | |
70 | var errArgs = new Error(0, '_').fileName == '_';\r | |
71 | function LoaderError__Check_error_message_for_loader_stack (childErr, newMessage) {\r | |
72 | // Convert file:/// URLs to paths in Node\r | |
73 | if (!isBrowser)\r | |
74 | newMessage = newMessage.replace(isWindows ? /file:\/\/\//g : /file:\/\//g, '');\r | |
75 | \r | |
76 | var message = (childErr.message || childErr) + '\n ' + newMessage;\r | |
77 | \r | |
78 | var err;\r | |
79 | if (errArgs && childErr.fileName)\r | |
80 | err = new Error(message, childErr.fileName, childErr.lineNumber);\r | |
81 | else\r | |
82 | err = new Error(message);\r | |
83 | \r | |
84 | \r | |
85 | var stack = childErr.originalErr ? childErr.originalErr.stack : childErr.stack;\r | |
86 | \r | |
87 | if (isNode)\r | |
88 | // node doesn't show the message otherwise\r | |
89 | err.stack = message + '\n ' + stack;\r | |
90 | else\r | |
91 | err.stack = stack;\r | |
92 | \r | |
93 | err.originalErr = childErr.originalErr || childErr;\r | |
94 | \r | |
95 | return err;\r | |
399fa2ee SR |
96 | } |
97 | ||
d1316334 PO |
98 | var resolvedPromise$1 = Promise.resolve();\r |
99 | \r | |
100 | /*\r | |
101 | * Simple Array values shim\r | |
102 | */\r | |
103 | function arrayValues (arr) {\r | |
104 | if (arr.values)\r | |
105 | return arr.values();\r | |
106 | \r | |
107 | if (typeof Symbol === 'undefined' || !Symbol.iterator)\r | |
108 | throw new Error('Symbol.iterator not supported in this browser');\r | |
109 | \r | |
110 | var iterable = {};\r | |
111 | iterable[Symbol.iterator] = function () {\r | |
112 | var keys = Object.keys(arr);\r | |
113 | var keyIndex = 0;\r | |
114 | return {\r | |
115 | next: function () {\r | |
116 | if (keyIndex < keys.length)\r | |
117 | return {\r | |
118 | value: arr[keys[keyIndex++]],\r | |
119 | done: false\r | |
120 | };\r | |
121 | else\r | |
122 | return {\r | |
123 | value: undefined,\r | |
124 | done: true\r | |
125 | };\r | |
126 | }\r | |
127 | };\r | |
128 | };\r | |
129 | return iterable;\r | |
130 | }\r | |
131 | \r | |
132 | /*\r | |
133 | * 3. Reflect.Loader\r | |
134 | *\r | |
135 | * We skip the entire native internal pipeline, just providing the bare API\r | |
136 | */\r | |
137 | // 3.1.1\r | |
138 | function Loader () {\r | |
139 | this.registry = new Registry();\r | |
140 | }\r | |
141 | // 3.3.1\r | |
142 | Loader.prototype.constructor = Loader;\r | |
143 | \r | |
144 | function ensureInstantiated (module) {\r | |
145 | if (module === undefined)\r | |
146 | return;\r | |
147 | if (module instanceof ModuleNamespace === false && module[toStringTag] !== 'module')\r | |
148 | throw new TypeError('Module instantiation did not return a valid namespace object.');\r | |
149 | return module;\r | |
150 | }\r | |
151 | \r | |
152 | // 3.3.2\r | |
153 | Loader.prototype.import = function (key, parent) {\r | |
154 | if (typeof key !== 'string')\r | |
155 | throw new TypeError('Loader import method must be passed a module key string');\r | |
156 | // custom resolveInstantiate combined hook for better perf\r | |
157 | var loader = this;\r | |
158 | return resolvedPromise$1\r | |
159 | .then(function () {\r | |
160 | return loader[RESOLVE_INSTANTIATE](key, parent);\r | |
161 | })\r | |
162 | .then(ensureInstantiated)\r | |
163 | //.then(Module.evaluate)\r | |
164 | .catch(function (err) {\r | |
165 | throw LoaderError__Check_error_message_for_loader_stack(err, 'Loading ' + key + (parent ? ' from ' + parent : ''));\r | |
166 | });\r | |
167 | };\r | |
168 | // 3.3.3\r | |
169 | var RESOLVE = Loader.resolve = createSymbol('resolve');\r | |
170 | \r | |
171 | /*\r | |
172 | * Combined resolve / instantiate hook\r | |
173 | *\r | |
174 | * Not in current reduced spec, but necessary to separate RESOLVE from RESOLVE + INSTANTIATE as described\r | |
175 | * in the spec notes of this repo to ensure that loader.resolve doesn't instantiate when not wanted.\r | |
176 | *\r | |
177 | * We implement RESOLVE_INSTANTIATE as a single hook instead of a separate INSTANTIATE in order to avoid\r | |
178 | * the need for double registry lookups as a performance optimization.\r | |
179 | */\r | |
180 | var RESOLVE_INSTANTIATE = Loader.resolveInstantiate = createSymbol('resolveInstantiate');\r | |
181 | \r | |
182 | // default resolveInstantiate is just to call resolve and then get from the registry\r | |
183 | // this provides compatibility for the resolveInstantiate optimization\r | |
184 | Loader.prototype[RESOLVE_INSTANTIATE] = function (key, parent) {\r | |
185 | var loader = this;\r | |
186 | return loader.resolve(key, parent)\r | |
187 | .then(function (resolved) {\r | |
188 | return loader.registry.get(resolved);\r | |
189 | });\r | |
190 | };\r | |
191 | \r | |
192 | function ensureResolution (resolvedKey) {\r | |
193 | if (resolvedKey === undefined)\r | |
194 | throw new RangeError('No resolution found.');\r | |
195 | return resolvedKey;\r | |
196 | }\r | |
197 | \r | |
198 | Loader.prototype.resolve = function (key, parent) {\r | |
199 | var loader = this;\r | |
200 | return resolvedPromise$1\r | |
201 | .then(function() {\r | |
202 | return loader[RESOLVE](key, parent);\r | |
203 | })\r | |
204 | .then(ensureResolution)\r | |
205 | .catch(function (err) {\r | |
206 | throw LoaderError__Check_error_message_for_loader_stack(err, 'Resolving ' + key + (parent ? ' to ' + parent : ''));\r | |
207 | });\r | |
208 | };\r | |
209 | \r | |
210 | // 3.3.4 (import without evaluate)\r | |
211 | // this is not documented because the use of deferred evaluation as in Module.evaluate is not\r | |
212 | // documented, as it is not considered a stable feature to be encouraged\r | |
213 | // Loader.prototype.load may well be deprecated if this stays disabled\r | |
214 | /* Loader.prototype.load = function (key, parent) {\r | |
215 | return Promise.resolve(this[RESOLVE_INSTANTIATE](key, parent || this.key))\r | |
216 | .catch(function (err) {\r | |
217 | throw addToError(err, 'Loading ' + key + (parent ? ' from ' + parent : ''));\r | |
218 | });\r | |
219 | }; */\r | |
220 | \r | |
221 | /*\r | |
222 | * 4. Registry\r | |
223 | *\r | |
224 | * Instead of structuring through a Map, just use a dictionary object\r | |
225 | * We throw for construction attempts so this doesn't affect the public API\r | |
226 | *\r | |
227 | * Registry has been adjusted to use Namespace objects over ModuleStatus objects\r | |
228 | * as part of simplifying loader API implementation\r | |
229 | */\r | |
230 | var iteratorSupport = typeof Symbol !== 'undefined' && Symbol.iterator;\r | |
231 | var REGISTRY = createSymbol('registry');\r | |
232 | function Registry() {\r | |
233 | this[REGISTRY] = {};\r | |
234 | }\r | |
235 | // 4.4.1\r | |
236 | if (iteratorSupport) {\r | |
237 | // 4.4.2\r | |
238 | Registry.prototype[Symbol.iterator] = function () {\r | |
239 | return this.entries()[Symbol.iterator]();\r | |
240 | };\r | |
241 | \r | |
242 | // 4.4.3\r | |
243 | Registry.prototype.entries = function () {\r | |
244 | var registry = this[REGISTRY];\r | |
245 | return arrayValues(Object.keys(registry).map(function (key) {\r | |
246 | return [key, registry[key]];\r | |
247 | }));\r | |
248 | };\r | |
249 | }\r | |
250 | \r | |
251 | // 4.4.4\r | |
252 | Registry.prototype.keys = function () {\r | |
253 | return arrayValues(Object.keys(this[REGISTRY]));\r | |
254 | };\r | |
255 | // 4.4.5\r | |
256 | Registry.prototype.values = function () {\r | |
257 | var registry = this[REGISTRY];\r | |
258 | return arrayValues(Object.keys(registry).map(function (key) {\r | |
259 | return registry[key];\r | |
260 | }));\r | |
261 | };\r | |
262 | // 4.4.6\r | |
263 | Registry.prototype.get = function (key) {\r | |
264 | return this[REGISTRY][key];\r | |
265 | };\r | |
266 | // 4.4.7\r | |
267 | Registry.prototype.set = function (key, namespace) {\r | |
268 | if (!(namespace instanceof ModuleNamespace || namespace[toStringTag] === 'module'))\r | |
269 | throw new Error('Registry must be set with an instance of Module Namespace');\r | |
270 | this[REGISTRY][key] = namespace;\r | |
271 | return this;\r | |
272 | };\r | |
273 | // 4.4.8\r | |
274 | Registry.prototype.has = function (key) {\r | |
275 | return Object.hasOwnProperty.call(this[REGISTRY], key);\r | |
276 | };\r | |
277 | // 4.4.9\r | |
278 | Registry.prototype.delete = function (key) {\r | |
279 | if (Object.hasOwnProperty.call(this[REGISTRY], key)) {\r | |
280 | delete this[REGISTRY][key];\r | |
281 | return true;\r | |
282 | }\r | |
283 | return false;\r | |
284 | };\r | |
285 | \r | |
286 | /*\r | |
287 | * Simple ModuleNamespace Exotic object based on a baseObject\r | |
288 | * We export this for allowing a fast-path for module namespace creation over Module descriptors\r | |
289 | */\r | |
290 | // var EVALUATE = createSymbol('evaluate');\r | |
291 | var BASE_OBJECT = createSymbol('baseObject');\r | |
292 | \r | |
293 | // 8.3.1 Reflect.Module\r | |
294 | /*\r | |
295 | * Best-effort simplified non-spec implementation based on\r | |
296 | * a baseObject referenced via getters.\r | |
297 | *\r | |
298 | * Allows:\r | |
299 | *\r | |
300 | * loader.registry.set('x', new Module({ default: 'x' }));\r | |
301 | *\r | |
302 | * Optional evaluation function provides experimental Module.evaluate\r | |
303 | * support for non-executed modules in registry.\r | |
304 | */\r | |
305 | function ModuleNamespace (baseObject/*, evaluate*/) {\r | |
306 | Object.defineProperty(this, BASE_OBJECT, {\r | |
307 | value: baseObject\r | |
308 | });\r | |
309 | \r | |
310 | // evaluate defers namespace population\r | |
311 | /* if (evaluate) {\r | |
312 | Object.defineProperty(this, EVALUATE, {\r | |
313 | value: evaluate,\r | |
314 | configurable: true,\r | |
315 | writable: true\r | |
316 | });\r | |
317 | }\r | |
318 | else { */\r | |
319 | Object.keys(baseObject).forEach(extendNamespace, this);\r | |
320 | //}\r | |
321 | }\r | |
322 | // 8.4.2\r | |
323 | ModuleNamespace.prototype = Object.create(null);\r | |
324 | \r | |
325 | if (toStringTag)\r | |
326 | Object.defineProperty(ModuleNamespace.prototype, toStringTag, {\r | |
327 | value: 'Module'\r | |
328 | });\r | |
329 | \r | |
330 | function extendNamespace (key) {\r | |
331 | Object.defineProperty(this, key, {\r | |
332 | enumerable: true,\r | |
333 | get: function () {\r | |
334 | return this[BASE_OBJECT][key];\r | |
335 | }\r | |
336 | });\r | |
337 | }\r | |
338 | \r | |
339 | /* function doEvaluate (evaluate, context) {\r | |
340 | try {\r | |
341 | evaluate.call(context);\r | |
342 | }\r | |
343 | catch (e) {\r | |
344 | return e;\r | |
345 | }\r | |
346 | }\r | |
347 | \r | |
348 | // 8.4.1 Module.evaluate... not documented or used because this is potentially unstable\r | |
349 | Module.evaluate = function (ns) {\r | |
350 | var evaluate = ns[EVALUATE];\r | |
351 | if (evaluate) {\r | |
352 | ns[EVALUATE] = undefined;\r | |
353 | var err = doEvaluate(evaluate);\r | |
354 | if (err) {\r | |
355 | // cache the error\r | |
356 | ns[EVALUATE] = function () {\r | |
357 | throw err;\r | |
358 | };\r | |
359 | throw err;\r | |
360 | }\r | |
361 | Object.keys(ns[BASE_OBJECT]).forEach(extendNamespace, ns);\r | |
362 | }\r | |
363 | // make chainable\r | |
364 | return ns;\r | |
399fa2ee SR |
365 | }; */ |
366 | ||
d1316334 PO |
367 | /*\r |
368 | * Optimized URL normalization assuming a syntax-valid URL parent\r | |
369 | */\r | |
370 | function throwResolveError (relUrl, parentUrl) {\r | |
371 | throw new RangeError('Unable to resolve "' + relUrl + '" to ' + parentUrl);\r | |
372 | }\r | |
373 | var backslashRegEx = /\\/g;\r | |
374 | function resolveIfNotPlain (relUrl, parentUrl) {\r | |
375 | if (relUrl[0] === ' ' || relUrl[relUrl.length - 1] === ' ')\r | |
376 | relUrl = relUrl.trim();\r | |
377 | var parentProtocol = parentUrl && parentUrl.substr(0, parentUrl.indexOf(':') + 1);\r | |
378 | \r | |
379 | var firstChar = relUrl[0];\r | |
380 | var secondChar = relUrl[1];\r | |
381 | \r | |
382 | // protocol-relative\r | |
383 | if (firstChar === '/' && secondChar === '/') {\r | |
384 | if (!parentProtocol)\r | |
385 | throwResolveError(relUrl, parentUrl);\r | |
386 | if (relUrl.indexOf('\\') !== -1)\r | |
387 | relUrl = relUrl.replace(backslashRegEx, '/');\r | |
388 | return parentProtocol + relUrl;\r | |
389 | }\r | |
390 | // relative-url\r | |
391 | else if (firstChar === '.' && (secondChar === '/' || secondChar === '.' && (relUrl[2] === '/' || relUrl.length === 2 && (relUrl += '/')) ||\r | |
392 | relUrl.length === 1 && (relUrl += '/')) ||\r | |
393 | firstChar === '/') {\r | |
394 | if (relUrl.indexOf('\\') !== -1)\r | |
395 | relUrl = relUrl.replace(backslashRegEx, '/');\r | |
396 | var parentIsPlain = !parentProtocol || parentUrl[parentProtocol.length] !== '/';\r | |
397 | \r | |
398 | // read pathname from parent if a URL\r | |
399 | // pathname taken to be part after leading "/"\r | |
400 | var pathname;\r | |
401 | if (parentIsPlain) {\r | |
402 | // resolving to a plain parent -> skip standard URL prefix, and treat entire parent as pathname\r | |
403 | if (parentUrl === undefined)\r | |
404 | throwResolveError(relUrl, parentUrl);\r | |
405 | pathname = parentUrl;\r | |
406 | }\r | |
407 | else if (parentUrl[parentProtocol.length + 1] === '/') {\r | |
408 | // resolving to a :// so we need to read out the auth and host\r | |
409 | if (parentProtocol !== 'file:') {\r | |
410 | pathname = parentUrl.substr(parentProtocol.length + 2);\r | |
411 | pathname = pathname.substr(pathname.indexOf('/') + 1);\r | |
412 | }\r | |
413 | else {\r | |
414 | pathname = parentUrl.substr(8);\r | |
415 | }\r | |
416 | }\r | |
417 | else {\r | |
418 | // resolving to :/ so pathname is the /... part\r | |
419 | pathname = parentUrl.substr(parentProtocol.length + 1);\r | |
420 | }\r | |
421 | \r | |
422 | if (firstChar === '/') {\r | |
423 | if (parentIsPlain)\r | |
424 | throwResolveError(relUrl, parentUrl);\r | |
425 | else\r | |
426 | return parentUrl.substr(0, parentUrl.length - pathname.length - 1) + relUrl;\r | |
427 | }\r | |
428 | \r | |
429 | // join together and split for removal of .. and . segments\r | |
430 | // looping the string instead of anything fancy for perf reasons\r | |
431 | // '../../../../../z' resolved to 'x/y' is just 'z' regardless of parentIsPlain\r | |
432 | var segmented = pathname.substr(0, pathname.lastIndexOf('/') + 1) + relUrl;\r | |
433 | \r | |
434 | var output = [];\r | |
435 | var segmentIndex = -1;\r | |
436 | \r | |
437 | for (var i = 0; i < segmented.length; i++) {\r | |
438 | // busy reading a segment - only terminate on '/'\r | |
439 | if (segmentIndex !== -1) {\r | |
440 | if (segmented[i] === '/') {\r | |
441 | output.push(segmented.substring(segmentIndex, i + 1));\r | |
442 | segmentIndex = -1;\r | |
443 | }\r | |
444 | continue;\r | |
445 | }\r | |
446 | \r | |
447 | // new segment - check if it is relative\r | |
448 | if (segmented[i] === '.') {\r | |
449 | // ../ segment\r | |
450 | if (segmented[i + 1] === '.' && (segmented[i + 2] === '/' || i + 2 === segmented.length)) {\r | |
451 | output.pop();\r | |
452 | i += 2;\r | |
453 | }\r | |
454 | // ./ segment\r | |
455 | else if (segmented[i + 1] === '/' || i + 1 === segmented.length) {\r | |
456 | i += 1;\r | |
457 | }\r | |
458 | else {\r | |
459 | // the start of a new segment as below\r | |
460 | segmentIndex = i;\r | |
461 | continue;\r | |
462 | }\r | |
463 | \r | |
464 | // this is the plain URI backtracking error (../, package:x -> error)\r | |
465 | if (parentIsPlain && output.length === 0)\r | |
466 | throwResolveError(relUrl, parentUrl);\r | |
467 | \r | |
468 | continue;\r | |
469 | }\r | |
470 | \r | |
471 | // it is the start of a new segment\r | |
472 | segmentIndex = i;\r | |
473 | }\r | |
474 | // finish reading out the last segment\r | |
475 | if (segmentIndex !== -1)\r | |
476 | output.push(segmented.substr(segmentIndex));\r | |
477 | \r | |
478 | return parentUrl.substr(0, parentUrl.length - pathname.length) + output.join('');\r | |
479 | }\r | |
480 | \r | |
481 | // sanitizes and verifies (by returning undefined if not a valid URL-like form)\r | |
482 | // Windows filepath compatibility is an added convenience here\r | |
483 | var protocolIndex = relUrl.indexOf(':');\r | |
484 | if (protocolIndex !== -1) {\r | |
485 | if (isNode) {\r | |
486 | // C:\x becomes file:///c:/x (we don't support C|\x)\r | |
487 | if (relUrl[1] === ':' && relUrl[2] === '\\' && relUrl[0].match(/[a-z]/i))\r | |
488 | return 'file:///' + relUrl.replace(backslashRegEx, '/');\r | |
489 | }\r | |
490 | return relUrl;\r | |
491 | }\r | |
399fa2ee SR |
492 | } |
493 | ||
d1316334 PO |
494 | var resolvedPromise = Promise.resolve();\r |
495 | /*\r | |
496 | * Register Loader\r | |
497 | *\r | |
498 | * Builds directly on top of loader polyfill to provide:\r | |
499 | * - loader.register support\r | |
500 | * - hookable higher-level resolve\r | |
501 | * - instantiate hook returning a ModuleNamespace or undefined for es module loading\r | |
502 | * - loader error behaviour as in HTML and loader specs, caching load and eval errors separately\r | |
503 | * - build tracing support by providing a .trace=true and .loads object format\r | |
504 | */\r | |
505 | \r | |
506 | var REGISTER_INTERNAL = createSymbol('register-internal');\r | |
507 | \r | |
508 | function RegisterLoader$1 () {\r | |
509 | Loader.call(this);\r | |
510 | \r | |
511 | var registryDelete = this.registry.delete;\r | |
512 | this.registry.delete = function (key) {\r | |
513 | var deleted = registryDelete.call(this, key);\r | |
514 | \r | |
515 | // also delete from register registry if linked\r | |
516 | if (records.hasOwnProperty(key) && !records[key].linkRecord) {\r | |
517 | delete records[key];\r | |
518 | deleted = true;\r | |
519 | }\r | |
520 | \r | |
521 | return deleted;\r | |
522 | };\r | |
523 | \r | |
524 | var records = {};\r | |
525 | \r | |
526 | this[REGISTER_INTERNAL] = {\r | |
527 | // last anonymous System.register call\r | |
528 | lastRegister: undefined,\r | |
529 | // in-flight es module load records\r | |
530 | records: records\r | |
531 | };\r | |
532 | \r | |
533 | // tracing\r | |
534 | this.trace = false;\r | |
535 | }\r | |
536 | \r | |
537 | RegisterLoader$1.prototype = Object.create(Loader.prototype);\r | |
538 | RegisterLoader$1.prototype.constructor = RegisterLoader$1;\r | |
539 | \r | |
540 | var INSTANTIATE = RegisterLoader$1.instantiate = createSymbol('instantiate');\r | |
541 | \r | |
542 | // default normalize is the WhatWG style normalizer\r | |
543 | RegisterLoader$1.prototype[RegisterLoader$1.resolve = Loader.resolve] = function (key, parentKey) {\r | |
544 | return resolveIfNotPlain(key, parentKey || baseURI);\r | |
545 | };\r | |
546 | \r | |
547 | RegisterLoader$1.prototype[INSTANTIATE] = function (key, processAnonRegister) {};\r | |
548 | \r | |
549 | // once evaluated, the linkRecord is set to undefined leaving just the other load record properties\r | |
550 | // this allows tracking new binding listeners for es modules through importerSetters\r | |
551 | // for dynamic modules, the load record is removed entirely.\r | |
552 | function createLoadRecord (state, key, registration) {\r | |
553 | return state.records[key] = {\r | |
554 | key: key,\r | |
555 | \r | |
556 | // defined System.register cache\r | |
557 | registration: registration,\r | |
558 | \r | |
559 | // module namespace object\r | |
560 | module: undefined,\r | |
561 | \r | |
562 | // es-only\r | |
563 | // this sticks around so new module loads can listen to binding changes\r | |
564 | // for already-loaded modules by adding themselves to their importerSetters\r | |
565 | importerSetters: undefined,\r | |
566 | \r | |
567 | loadError: undefined,\r | |
568 | evalError: undefined,\r | |
569 | \r | |
570 | // in-flight linking record\r | |
571 | linkRecord: {\r | |
572 | // promise for instantiated\r | |
573 | instantiatePromise: undefined,\r | |
574 | dependencies: undefined,\r | |
575 | execute: undefined,\r | |
576 | executingRequire: false,\r | |
577 | \r | |
578 | // underlying module object bindings\r | |
579 | moduleObj: undefined,\r | |
580 | \r | |
581 | // es only, also indicates if es or not\r | |
582 | setters: undefined,\r | |
583 | \r | |
584 | // promise for instantiated dependencies (dependencyInstantiations populated)\r | |
585 | depsInstantiatePromise: undefined,\r | |
586 | // will be the array of dependency load record or a module namespace\r | |
587 | dependencyInstantiations: undefined,\r | |
588 | \r | |
589 | // top-level await!\r | |
590 | evaluatePromise: undefined,\r | |
591 | \r | |
592 | // NB optimization and way of ensuring module objects in setters\r | |
593 | // indicates setters which should run pre-execution of that dependency\r | |
594 | // setters is then just for completely executed module objects\r | |
595 | // alternatively we just pass the partially filled module objects as\r | |
596 | // arguments into the execute function\r | |
597 | // hoisted: undefined\r | |
598 | }\r | |
599 | };\r | |
600 | }\r | |
601 | \r | |
602 | RegisterLoader$1.prototype[Loader.resolveInstantiate] = function (key, parentKey) {\r | |
603 | var loader = this;\r | |
604 | var state = this[REGISTER_INTERNAL];\r | |
605 | var registry = this.registry[REGISTRY];\r | |
606 | \r | |
607 | return resolveInstantiate(loader, key, parentKey, registry, state)\r | |
608 | .then(function (instantiated) {\r | |
609 | if (instantiated instanceof ModuleNamespace || instantiated[toStringTag] === 'module')\r | |
610 | return instantiated;\r | |
611 | \r | |
612 | // resolveInstantiate always returns a load record with a link record and no module value\r | |
613 | var link = instantiated.linkRecord;\r | |
614 | \r | |
615 | // if already beaten to done, return\r | |
616 | if (!link) {\r | |
617 | if (instantiated.module)\r | |
618 | return instantiated.module;\r | |
619 | throw instantiated.evalError;\r | |
620 | }\r | |
621 | \r | |
622 | return deepInstantiateDeps(loader, instantiated, link, registry, state)\r | |
623 | .then(function () {\r | |
624 | return ensureEvaluate(loader, instantiated, link, registry, state);\r | |
625 | });\r | |
626 | });\r | |
627 | };\r | |
628 | \r | |
629 | function resolveInstantiate (loader, key, parentKey, registry, state) {\r | |
630 | // normalization shortpath for already-normalized key\r | |
631 | // could add a plain name filter, but doesn't yet seem necessary for perf\r | |
632 | var module = registry[key];\r | |
633 | if (module)\r | |
634 | return Promise.resolve(module);\r | |
635 | \r | |
636 | var load = state.records[key];\r | |
637 | \r | |
638 | // already linked but not in main registry is ignored\r | |
639 | if (load && !load.module) {\r | |
640 | if (load.loadError)\r | |
641 | return Promise.reject(load.loadError);\r | |
642 | return instantiate(loader, load, load.linkRecord, registry, state);\r | |
643 | }\r | |
644 | \r | |
645 | return loader.resolve(key, parentKey)\r | |
646 | .then(function (resolvedKey) {\r | |
647 | // main loader registry always takes preference\r | |
648 | module = registry[resolvedKey];\r | |
649 | if (module)\r | |
650 | return module;\r | |
651 | \r | |
652 | load = state.records[resolvedKey];\r | |
653 | \r | |
654 | // already has a module value but not already in the registry (load.module)\r | |
655 | // means it was removed by registry.delete, so we should\r | |
656 | // disgard the current load record creating a new one over it\r | |
657 | // but keep any existing registration\r | |
658 | if (!load || load.module)\r | |
659 | load = createLoadRecord(state, resolvedKey, load && load.registration);\r | |
660 | \r | |
661 | if (load.loadError)\r | |
662 | return Promise.reject(load.loadError);\r | |
663 | \r | |
664 | var link = load.linkRecord;\r | |
665 | if (!link)\r | |
666 | return load;\r | |
667 | \r | |
668 | return instantiate(loader, load, link, registry, state);\r | |
669 | });\r | |
670 | }\r | |
671 | \r | |
672 | function createProcessAnonRegister (loader, load, state) {\r | |
673 | return function () {\r | |
674 | var lastRegister = state.lastRegister;\r | |
675 | \r | |
676 | if (!lastRegister)\r | |
677 | return !!load.registration;\r | |
678 | \r | |
679 | state.lastRegister = undefined;\r | |
680 | load.registration = lastRegister;\r | |
681 | \r | |
682 | return true;\r | |
683 | };\r | |
684 | }\r | |
685 | \r | |
686 | function instantiate (loader, load, link, registry, state) {\r | |
687 | return link.instantiatePromise || (link.instantiatePromise =\r | |
688 | // if there is already an existing registration, skip running instantiate\r | |
689 | (load.registration ? resolvedPromise : resolvedPromise.then(function () {\r | |
690 | state.lastRegister = undefined;\r | |
691 | return loader[INSTANTIATE](load.key, loader[INSTANTIATE].length > 1 && createProcessAnonRegister(loader, load, state));\r | |
692 | }))\r | |
693 | .then(function (instantiation) {\r | |
694 | // direct module return from instantiate -> we're done\r | |
695 | if (instantiation !== undefined) {\r | |
696 | if (!(instantiation instanceof ModuleNamespace || instantiation[toStringTag] === 'module'))\r | |
697 | throw new TypeError('Instantiate did not return a valid Module object.');\r | |
698 | \r | |
699 | delete state.records[load.key];\r | |
700 | if (loader.trace)\r | |
701 | traceLoad(loader, load, link);\r | |
702 | return registry[load.key] = instantiation;\r | |
703 | }\r | |
704 | \r | |
705 | // run the cached loader.register declaration if there is one\r | |
706 | var registration = load.registration;\r | |
707 | // clear to allow new registrations for future loads (combined with registry delete)\r | |
708 | load.registration = undefined;\r | |
709 | if (!registration)\r | |
710 | throw new TypeError('Module instantiation did not call an anonymous or correctly named System.register.');\r | |
711 | \r | |
712 | link.dependencies = registration[0];\r | |
713 | \r | |
714 | load.importerSetters = [];\r | |
715 | \r | |
716 | link.moduleObj = {};\r | |
717 | \r | |
718 | // process System.registerDynamic declaration\r | |
719 | if (registration[2]) {\r | |
720 | link.moduleObj.default = link.moduleObj.__useDefault = {};\r | |
721 | link.executingRequire = registration[1];\r | |
722 | link.execute = registration[2];\r | |
723 | }\r | |
724 | \r | |
725 | // process System.register declaration\r | |
726 | else {\r | |
727 | registerDeclarative(loader, load, link, registration[1]);\r | |
728 | }\r | |
729 | \r | |
730 | return load;\r | |
731 | })\r | |
732 | .catch(function (err) {\r | |
733 | load.linkRecord = undefined;\r | |
734 | throw load.loadError = load.loadError || LoaderError__Check_error_message_for_loader_stack(err, 'Instantiating ' + load.key);\r | |
735 | }));\r | |
736 | }\r | |
737 | \r | |
738 | // like resolveInstantiate, but returning load records for linking\r | |
739 | function resolveInstantiateDep (loader, key, parentKey, registry, state, traceDepMap) {\r | |
740 | // normalization shortpaths for already-normalized key\r | |
741 | // DISABLED to prioritise consistent resolver calls\r | |
742 | // could add a plain name filter, but doesn't yet seem necessary for perf\r | |
743 | /* var load = state.records[key];\r | |
744 | var module = registry[key];\r | |
745 | \r | |
746 | if (module) {\r | |
747 | if (traceDepMap)\r | |
748 | traceDepMap[key] = key;\r | |
749 | \r | |
750 | // registry authority check in case module was deleted or replaced in main registry\r | |
751 | if (load && load.module && load.module === module)\r | |
752 | return load;\r | |
753 | else\r | |
754 | return module;\r | |
755 | }\r | |
756 | \r | |
757 | // already linked but not in main registry is ignored\r | |
758 | if (load && !load.module) {\r | |
759 | if (traceDepMap)\r | |
760 | traceDepMap[key] = key;\r | |
761 | return instantiate(loader, load, load.linkRecord, registry, state);\r | |
762 | } */\r | |
763 | return loader.resolve(key, parentKey)\r | |
764 | .then(function (resolvedKey) {\r | |
765 | if (traceDepMap)\r | |
766 | traceDepMap[key] = resolvedKey;\r | |
767 | \r | |
768 | // normalization shortpaths for already-normalized key\r | |
769 | var load = state.records[resolvedKey];\r | |
770 | var module = registry[resolvedKey];\r | |
771 | \r | |
772 | // main loader registry always takes preference\r | |
773 | if (module && (!load || load.module && module !== load.module))\r | |
774 | return module;\r | |
775 | \r | |
776 | if (load && load.loadError)\r | |
777 | throw load.loadError;\r | |
778 | \r | |
779 | // already has a module value but not already in the registry (load.module)\r | |
780 | // means it was removed by registry.delete, so we should\r | |
781 | // disgard the current load record creating a new one over it\r | |
782 | // but keep any existing registration\r | |
783 | if (!load || !module && load.module)\r | |
784 | load = createLoadRecord(state, resolvedKey, load && load.registration);\r | |
785 | \r | |
786 | var link = load.linkRecord;\r | |
787 | if (!link)\r | |
788 | return load;\r | |
789 | \r | |
790 | return instantiate(loader, load, link, registry, state);\r | |
791 | });\r | |
792 | }\r | |
793 | \r | |
794 | function traceLoad (loader, load, link) {\r | |
795 | loader.loads = loader.loads || {};\r | |
796 | loader.loads[load.key] = {\r | |
797 | key: load.key,\r | |
798 | deps: link.dependencies,\r | |
799 | dynamicDeps: [],\r | |
800 | depMap: link.depMap || {}\r | |
801 | };\r | |
802 | }\r | |
803 | \r | |
804 | /*\r | |
805 | * Convert a CJS module.exports into a valid object for new Module:\r | |
806 | *\r | |
807 | * new Module(getEsModule(module.exports))\r | |
808 | *\r | |
809 | * Sets the default value to the module, while also reading off named exports carefully.\r | |
810 | */\r | |
811 | function registerDeclarative (loader, load, link, declare) {\r | |
812 | var moduleObj = link.moduleObj;\r | |
813 | var importerSetters = load.importerSetters;\r | |
814 | \r | |
815 | var definedExports = false;\r | |
816 | \r | |
817 | // closure especially not based on link to allow link record disposal\r | |
818 | var declared = declare.call(envGlobal, function (name, value) {\r | |
819 | if (typeof name === 'object') {\r | |
820 | var changed = false;\r | |
821 | for (var p in name) {\r | |
822 | value = name[p];\r | |
823 | if (p !== '__useDefault' && (!(p in moduleObj) || moduleObj[p] !== value)) {\r | |
824 | changed = true;\r | |
825 | moduleObj[p] = value;\r | |
826 | }\r | |
827 | }\r | |
828 | if (changed === false)\r | |
829 | return value;\r | |
830 | }\r | |
831 | else {\r | |
832 | if ((definedExports || name in moduleObj) && moduleObj[name] === value)\r | |
833 | return value;\r | |
834 | moduleObj[name] = value;\r | |
835 | }\r | |
836 | \r | |
837 | for (var i = 0; i < importerSetters.length; i++)\r | |
838 | importerSetters[i](moduleObj);\r | |
839 | \r | |
840 | return value;\r | |
841 | }, new ContextualLoader(loader, load.key));\r | |
842 | \r | |
843 | link.setters = declared.setters || [];\r | |
844 | link.execute = declared.execute;\r | |
845 | if (declared.exports) {\r | |
846 | link.moduleObj = moduleObj = declared.exports;\r | |
847 | definedExports = true;\r | |
848 | }\r | |
849 | }\r | |
850 | \r | |
851 | function instantiateDeps (loader, load, link, registry, state) {\r | |
852 | if (link.depsInstantiatePromise)\r | |
853 | return link.depsInstantiatePromise;\r | |
854 | \r | |
855 | var depsInstantiatePromises = Array(link.dependencies.length);\r | |
856 | \r | |
857 | for (var i = 0; i < link.dependencies.length; i++)\r | |
858 | depsInstantiatePromises[i] = resolveInstantiateDep(loader, link.dependencies[i], load.key, registry, state, loader.trace && link.depMap || (link.depMap = {}));\r | |
859 | \r | |
860 | var depsInstantiatePromise = Promise.all(depsInstantiatePromises)\r | |
861 | .then(function (dependencyInstantiations) {\r | |
862 | link.dependencyInstantiations = dependencyInstantiations;\r | |
863 | \r | |
864 | // run setters to set up bindings to instantiated dependencies\r | |
865 | if (link.setters) {\r | |
866 | for (var i = 0; i < dependencyInstantiations.length; i++) {\r | |
867 | var setter = link.setters[i];\r | |
868 | if (setter) {\r | |
869 | var instantiation = dependencyInstantiations[i];\r | |
870 | \r | |
871 | if (instantiation instanceof ModuleNamespace || instantiation[toStringTag] === 'module') {\r | |
872 | setter(instantiation);\r | |
873 | }\r | |
874 | else {\r | |
875 | if (instantiation.loadError)\r | |
876 | throw instantiation.loadError;\r | |
877 | setter(instantiation.module || instantiation.linkRecord.moduleObj);\r | |
878 | // this applies to both es and dynamic registrations\r | |
879 | if (instantiation.importerSetters)\r | |
880 | instantiation.importerSetters.push(setter);\r | |
881 | }\r | |
882 | }\r | |
883 | }\r | |
884 | }\r | |
885 | \r | |
886 | return load;\r | |
887 | });\r | |
888 | \r | |
889 | if (loader.trace)\r | |
890 | depsInstantiatePromise = depsInstantiatePromise.then(function () {\r | |
891 | traceLoad(loader, load, link);\r | |
892 | return load;\r | |
893 | });\r | |
894 | \r | |
895 | depsInstantiatePromise = depsInstantiatePromise.catch(function (err) {\r | |
896 | // throw up the instantiateDeps stack\r | |
897 | link.depsInstantiatePromise = undefined;\r | |
898 | throw LoaderError__Check_error_message_for_loader_stack(err, 'Loading ' + load.key);\r | |
899 | });\r | |
900 | \r | |
901 | depsInstantiatePromise.catch(function () {});\r | |
902 | \r | |
903 | return link.depsInstantiatePromise = depsInstantiatePromise;\r | |
904 | }\r | |
905 | \r | |
906 | function deepInstantiateDeps (loader, load, link, registry, state) {\r | |
907 | var seen = [];\r | |
908 | function addDeps (load, link) {\r | |
909 | if (!link)\r | |
910 | return resolvedPromise;\r | |
911 | if (seen.indexOf(load) !== -1)\r | |
912 | return resolvedPromise;\r | |
913 | seen.push(load);\r | |
914 | \r | |
915 | return instantiateDeps(loader, load, link, registry, state)\r | |
916 | .then(function () {\r | |
917 | var depPromises;\r | |
918 | for (var i = 0; i < link.dependencies.length; i++) {\r | |
919 | var depLoad = link.dependencyInstantiations[i];\r | |
920 | if (!(depLoad instanceof ModuleNamespace || depLoad[toStringTag] === 'module')) {\r | |
921 | depPromises = depPromises || [];\r | |
922 | depPromises.push(addDeps(depLoad, depLoad.linkRecord));\r | |
923 | }\r | |
924 | }\r | |
925 | if (depPromises)\r | |
926 | return Promise.all(depPromises);\r | |
927 | });\r | |
928 | }\r | |
929 | \r | |
930 | return addDeps(load, link);\r | |
931 | }\r | |
932 | \r | |
933 | /*\r | |
934 | * System.register\r | |
935 | */\r | |
936 | RegisterLoader$1.prototype.register = function (key, deps, declare) {\r | |
937 | var state = this[REGISTER_INTERNAL];\r | |
938 | \r | |
939 | // anonymous modules get stored as lastAnon\r | |
940 | if (declare === undefined) {\r | |
941 | state.lastRegister = [key, deps, undefined];\r | |
942 | }\r | |
943 | \r | |
944 | // everything else registers into the register cache\r | |
945 | else {\r | |
946 | var load = state.records[key] || createLoadRecord(state, key, undefined);\r | |
947 | load.registration = [deps, declare, undefined];\r | |
948 | }\r | |
949 | };\r | |
950 | \r | |
951 | /*\r | |
952 | * System.registerDyanmic\r | |
953 | */\r | |
954 | RegisterLoader$1.prototype.registerDynamic = function (key, deps, executingRequire, execute) {\r | |
955 | var state = this[REGISTER_INTERNAL];\r | |
956 | \r | |
957 | // anonymous modules get stored as lastAnon\r | |
958 | if (typeof key !== 'string') {\r | |
959 | state.lastRegister = [key, deps, executingRequire];\r | |
960 | }\r | |
961 | \r | |
962 | // everything else registers into the register cache\r | |
963 | else {\r | |
964 | var load = state.records[key] || createLoadRecord(state, key, undefined);\r | |
965 | load.registration = [deps, executingRequire, execute];\r | |
966 | }\r | |
967 | };\r | |
968 | \r | |
969 | // ContextualLoader class\r | |
970 | // backwards-compatible with previous System.register context argument by exposing .id, .key\r | |
971 | function ContextualLoader (loader, key) {\r | |
972 | this.loader = loader;\r | |
973 | this.key = this.id = key;\r | |
974 | this.meta = {\r | |
975 | url: key\r | |
976 | // scriptElement: null\r | |
977 | };\r | |
978 | }\r | |
979 | /*ContextualLoader.prototype.constructor = function () {\r | |
980 | throw new TypeError('Cannot subclass the contextual loader only Reflect.Loader.');\r | |
981 | };*/\r | |
982 | ContextualLoader.prototype.import = function (key) {\r | |
983 | if (this.loader.trace)\r | |
984 | this.loader.loads[this.key].dynamicDeps.push(key);\r | |
985 | return this.loader.import(key, this.key);\r | |
986 | };\r | |
987 | /*ContextualLoader.prototype.resolve = function (key) {\r | |
988 | return this.loader.resolve(key, this.key);\r | |
989 | };*/\r | |
990 | \r | |
991 | function ensureEvaluate (loader, load, link, registry, state) {\r | |
992 | if (load.module)\r | |
993 | return load.module;\r | |
994 | if (load.evalError)\r | |
995 | throw load.evalError;\r | |
996 | if (link.evaluatePromise)\r | |
997 | return link.evaluatePromise;\r | |
998 | \r | |
999 | if (link.setters) {\r | |
1000 | var evaluatePromise = doEvaluateDeclarative(loader, load, link, registry, state, [load]);\r | |
1001 | if (evaluatePromise)\r | |
1002 | return evaluatePromise;\r | |
1003 | }\r | |
1004 | else {\r | |
1005 | doEvaluateDynamic(loader, load, link, registry, state, [load]);\r | |
1006 | }\r | |
1007 | return load.module;\r | |
1008 | }\r | |
1009 | \r | |
1010 | function makeDynamicRequire (loader, key, dependencies, dependencyInstantiations, registry, state, seen) {\r | |
1011 | // we can only require from already-known dependencies\r | |
1012 | return function (name) {\r | |
1013 | for (var i = 0; i < dependencies.length; i++) {\r | |
1014 | if (dependencies[i] === name) {\r | |
1015 | var depLoad = dependencyInstantiations[i];\r | |
1016 | var module;\r | |
1017 | \r | |
1018 | if (depLoad instanceof ModuleNamespace || depLoad[toStringTag] === 'module') {\r | |
1019 | module = depLoad;\r | |
1020 | }\r | |
1021 | else {\r | |
1022 | if (depLoad.evalError)\r | |
1023 | throw depLoad.evalError;\r | |
1024 | if (depLoad.module === undefined && seen.indexOf(depLoad) === -1 && !depLoad.linkRecord.evaluatePromise) {\r | |
1025 | if (depLoad.linkRecord.setters) {\r | |
1026 | doEvaluateDeclarative(loader, depLoad, depLoad.linkRecord, registry, state, [depLoad]);\r | |
1027 | }\r | |
1028 | else {\r | |
1029 | seen.push(depLoad);\r | |
1030 | doEvaluateDynamic(loader, depLoad, depLoad.linkRecord, registry, state, seen);\r | |
1031 | }\r | |
1032 | }\r | |
1033 | module = depLoad.module || depLoad.linkRecord.moduleObj;\r | |
1034 | }\r | |
1035 | \r | |
1036 | return '__useDefault' in module ? module.__useDefault : module;\r | |
1037 | }\r | |
1038 | }\r | |
1039 | throw new Error('Module ' + name + ' not declared as a System.registerDynamic dependency of ' + key);\r | |
1040 | };\r | |
1041 | }\r | |
1042 | \r | |
1043 | function evalError (load, err) {\r | |
1044 | load.linkRecord = undefined;\r | |
1045 | var evalError = LoaderError__Check_error_message_for_loader_stack(err, 'Evaluating ' + load.key);\r | |
1046 | if (load.evalError === undefined)\r | |
1047 | load.evalError = evalError;\r | |
1048 | throw evalError;\r | |
1049 | }\r | |
1050 | \r | |
1051 | // es modules evaluate dependencies first\r | |
1052 | // returns the error if any\r | |
1053 | function doEvaluateDeclarative (loader, load, link, registry, state, seen) {\r | |
1054 | var depLoad, depLink;\r | |
1055 | var depLoadPromises;\r | |
1056 | for (var i = 0; i < link.dependencies.length; i++) {\r | |
1057 | var depLoad = link.dependencyInstantiations[i];\r | |
1058 | if (depLoad instanceof ModuleNamespace || depLoad[toStringTag] === 'module')\r | |
1059 | continue;\r | |
1060 | \r | |
1061 | // custom Module returned from instantiate\r | |
1062 | depLink = depLoad.linkRecord;\r | |
1063 | if (depLink) {\r | |
1064 | if (depLoad.evalError) {\r | |
1065 | evalError(load, depLoad.evalError);\r | |
1066 | }\r | |
1067 | else if (depLink.setters) {\r | |
1068 | if (seen.indexOf(depLoad) === -1) {\r | |
1069 | seen.push(depLoad);\r | |
1070 | try {\r | |
1071 | var depLoadPromise = doEvaluateDeclarative(loader, depLoad, depLink, registry, state, seen);\r | |
1072 | }\r | |
1073 | catch (e) {\r | |
1074 | evalError(load, e);\r | |
1075 | }\r | |
1076 | if (depLoadPromise) {\r | |
1077 | depLoadPromises = depLoadPromises || [];\r | |
1078 | depLoadPromises.push(depLoadPromise.catch(function (err) {\r | |
1079 | evalError(load, err);\r | |
1080 | }));\r | |
1081 | }\r | |
1082 | }\r | |
1083 | }\r | |
1084 | else {\r | |
1085 | try {\r | |
1086 | doEvaluateDynamic(loader, depLoad, depLink, registry, state, [depLoad]);\r | |
1087 | }\r | |
1088 | catch (e) {\r | |
1089 | evalError(load, e);\r | |
1090 | }\r | |
1091 | }\r | |
1092 | }\r | |
1093 | }\r | |
1094 | \r | |
1095 | if (depLoadPromises)\r | |
1096 | return link.evaluatePromise = Promise.all(depLoadPromises)\r | |
1097 | .then(function () {\r | |
1098 | if (link.execute) {\r | |
1099 | // ES System.register execute\r | |
1100 | // "this" is null in ES\r | |
1101 | try {\r | |
1102 | var execPromise = link.execute.call(nullContext);\r | |
1103 | }\r | |
1104 | catch (e) {\r | |
1105 | evalError(load, e);\r | |
1106 | }\r | |
1107 | if (execPromise)\r | |
1108 | return execPromise.catch(function (e) {\r | |
1109 | evalError(load, e);\r | |
1110 | })\r | |
1111 | .then(function () {\r | |
1112 | load.linkRecord = undefined;\r | |
1113 | return registry[load.key] = load.module = new ModuleNamespace(link.moduleObj);\r | |
1114 | });\r | |
1115 | }\r | |
1116 | \r | |
1117 | // dispose link record\r | |
1118 | load.linkRecord = undefined;\r | |
1119 | registry[load.key] = load.module = new ModuleNamespace(link.moduleObj);\r | |
1120 | });\r | |
1121 | \r | |
1122 | if (link.execute) {\r | |
1123 | // ES System.register execute\r | |
1124 | // "this" is null in ES\r | |
1125 | try {\r | |
1126 | var execPromise = link.execute.call(nullContext);\r | |
1127 | }\r | |
1128 | catch (e) {\r | |
1129 | evalError(load, e);\r | |
1130 | }\r | |
1131 | if (execPromise)\r | |
1132 | return link.evaluatePromise = execPromise.catch(function (e) {\r | |
1133 | evalError(load, e);\r | |
1134 | })\r | |
1135 | .then(function () {\r | |
1136 | load.linkRecord = undefined;\r | |
1137 | return registry[load.key] = load.module = new ModuleNamespace(link.moduleObj);\r | |
1138 | });\r | |
1139 | }\r | |
1140 | \r | |
1141 | // dispose link record\r | |
1142 | load.linkRecord = undefined;\r | |
1143 | registry[load.key] = load.module = new ModuleNamespace(link.moduleObj);\r | |
1144 | }\r | |
1145 | \r | |
1146 | // non es modules explicitly call moduleEvaluate through require\r | |
1147 | function doEvaluateDynamic (loader, load, link, registry, state, seen) {\r | |
1148 | // System.registerDynamic execute\r | |
1149 | // "this" is "exports" in CJS\r | |
1150 | var module = { id: load.key };\r | |
1151 | var moduleObj = link.moduleObj;\r | |
1152 | Object.defineProperty(module, 'exports', {\r | |
1153 | configurable: true,\r | |
1154 | set: function (exports) {\r | |
1155 | moduleObj.default = moduleObj.__useDefault = exports;\r | |
1156 | },\r | |
1157 | get: function () {\r | |
1158 | return moduleObj.__useDefault;\r | |
1159 | }\r | |
1160 | });\r | |
1161 | \r | |
1162 | var require = makeDynamicRequire(loader, load.key, link.dependencies, link.dependencyInstantiations, registry, state, seen);\r | |
1163 | \r | |
1164 | // evaluate deps first\r | |
1165 | if (!link.executingRequire)\r | |
1166 | for (var i = 0; i < link.dependencies.length; i++)\r | |
1167 | require(link.dependencies[i]);\r | |
1168 | \r | |
1169 | try {\r | |
1170 | var output = link.execute.call(envGlobal, require, moduleObj.default, module);\r | |
1171 | if (output !== undefined)\r | |
1172 | module.exports = output;\r | |
1173 | }\r | |
1174 | catch (e) {\r | |
1175 | evalError(load, e);\r | |
1176 | }\r | |
1177 | \r | |
1178 | load.linkRecord = undefined;\r | |
1179 | \r | |
1180 | // pick up defineProperty calls to module.exports when we can\r | |
1181 | if (module.exports !== moduleObj.__useDefault)\r | |
1182 | moduleObj.default = moduleObj.__useDefault = module.exports;\r | |
1183 | \r | |
1184 | var moduleDefault = moduleObj.default;\r | |
1185 | \r | |
1186 | // __esModule flag extension support via lifting\r | |
1187 | if (moduleDefault && moduleDefault.__esModule) {\r | |
1188 | for (var p in moduleDefault) {\r | |
1189 | if (Object.hasOwnProperty.call(moduleDefault, p))\r | |
1190 | moduleObj[p] = moduleDefault[p];\r | |
1191 | }\r | |
1192 | }\r | |
1193 | \r | |
1194 | registry[load.key] = load.module = new ModuleNamespace(link.moduleObj);\r | |
1195 | \r | |
1196 | // run importer setters and clear them\r | |
1197 | // this allows dynamic modules to update themselves into es modules\r | |
1198 | // as soon as execution has completed\r | |
1199 | if (load.importerSetters)\r | |
1200 | for (var i = 0; i < load.importerSetters.length; i++)\r | |
1201 | load.importerSetters[i](load.module);\r | |
1202 | load.importerSetters = undefined;\r | |
1203 | }\r | |
1204 | \r | |
1205 | // the closest we can get to call(undefined)\r | |
1206 | var nullContext = Object.create(null);\r | |
1207 | if (Object.freeze)\r | |
399fa2ee SR |
1208 | Object.freeze(nullContext); |
1209 | ||
399fa2ee SR |
1210 | var loader; |
1211 | ||
1212 | // <script type="module"> support | |
1213 | var anonSources = {}; | |
1214 | if (typeof document != 'undefined' && document.getElementsByTagName) { | |
493ad1a2 | 1215 | var handleError = function(err) { |
36653517 PO |
1216 | // dispatch an error event so that we can display in errors in browsers |
1217 | // that don't yet support unhandledrejection | |
999b5da7 PO |
1218 | if (window.onunhandledrejection === undefined) { |
1219 | try { | |
1220 | var evt = new Event('error'); | |
1221 | } catch (_eventError) { | |
1222 | var evt = document.createEvent('Event'); | |
1223 | evt.initEvent('error', true, true); | |
1224 | } | |
1225 | evt.message = err.message; | |
5d00fd9b PO |
1226 | if (err.fileName) { |
1227 | evt.filename = err.fileName; | |
1228 | evt.lineno = err.lineNumber; | |
1229 | evt.colno = err.columnNumber; | |
1230 | } else if (err.sourceURL) { | |
1231 | evt.filename = err.sourceURL; | |
1232 | evt.lineno = err.line; | |
1233 | evt.colno = err.column; | |
1234 | } | |
999b5da7 PO |
1235 | evt.error = err; |
1236 | window.dispatchEvent(evt); | |
36653517 | 1237 | } |
36653517 PO |
1238 | |
1239 | // throw so it still shows up in the console | |
1240 | throw err; | |
493ad1a2 | 1241 | }; |
36653517 | 1242 | |
493ad1a2 | 1243 | var ready = function() { |
399fa2ee SR |
1244 | document.removeEventListener('DOMContentLoaded', ready, false ); |
1245 | ||
1246 | var anonCnt = 0; | |
1247 | ||
1248 | var scripts = document.getElementsByTagName('script'); | |
1249 | for (var i = 0; i < scripts.length; i++) { | |
1250 | var script = scripts[i]; | |
1251 | if (script.type == 'module' && !script.loaded) { | |
1252 | script.loaded = true; | |
1253 | if (script.src) { | |
36653517 | 1254 | loader.import(script.src).catch(handleError); |
399fa2ee SR |
1255 | } |
1256 | // anonymous modules supported via a custom naming scheme and registry | |
1257 | else { | |
50c31776 | 1258 | var uri = './<anon' + ++anonCnt + '>.js'; |
399fa2ee SR |
1259 | if (script.id !== ""){ |
1260 | uri = "./" + script.id; | |
1261 | } | |
1262 | ||
1263 | var anonName = resolveIfNotPlain(uri, baseURI); | |
1264 | anonSources[anonName] = script.innerHTML; | |
36653517 | 1265 | loader.import(anonName).catch(handleError); |
399fa2ee SR |
1266 | } |
1267 | } | |
1268 | } | |
493ad1a2 | 1269 | }; |
399fa2ee SR |
1270 | |
1271 | // simple DOM ready | |
1272 | if (document.readyState === 'complete') | |
1273 | setTimeout(ready); | |
1274 | else | |
1275 | document.addEventListener('DOMContentLoaded', ready, false); | |
1276 | } | |
1277 | ||
1278 | function BrowserESModuleLoader(baseKey) { | |
1279 | if (baseKey) | |
1280 | this.baseKey = resolveIfNotPlain(baseKey, baseURI) || resolveIfNotPlain('./' + baseKey, baseURI); | |
1281 | ||
1282 | RegisterLoader$1.call(this); | |
1283 | ||
1284 | var loader = this; | |
1285 | ||
1286 | // ensure System.register is available | |
1287 | envGlobal.System = envGlobal.System || {}; | |
1288 | if (typeof envGlobal.System.register == 'function') | |
1289 | var prevRegister = envGlobal.System.register; | |
1290 | envGlobal.System.register = function() { | |
1291 | loader.register.apply(loader, arguments); | |
1292 | if (prevRegister) | |
1293 | prevRegister.apply(this, arguments); | |
1294 | }; | |
1295 | } | |
1296 | BrowserESModuleLoader.prototype = Object.create(RegisterLoader$1.prototype); | |
1297 | ||
1298 | // normalize is never given a relative name like "./x", that part is already handled | |
1299 | BrowserESModuleLoader.prototype[RegisterLoader$1.resolve] = function(key, parent) { | |
1300 | var resolved = RegisterLoader$1.prototype[RegisterLoader$1.resolve].call(this, key, parent || this.baseKey) || key; | |
1301 | if (!resolved) | |
1302 | throw new RangeError('ES module loader does not resolve plain module names, resolving "' + key + '" to ' + parent); | |
1303 | ||
1304 | return resolved; | |
1305 | }; | |
1306 | ||
1307 | function xhrFetch(url, resolve, reject) { | |
1308 | var xhr = new XMLHttpRequest(); | |
493ad1a2 | 1309 | var load = function(source) { |
399fa2ee | 1310 | resolve(xhr.responseText); |
493ad1a2 PO |
1311 | }; |
1312 | var error = function() { | |
399fa2ee | 1313 | reject(new Error('XHR error' + (xhr.status ? ' (' + xhr.status + (xhr.statusText ? ' ' + xhr.statusText : '') + ')' : '') + ' loading ' + url)); |
493ad1a2 | 1314 | }; |
399fa2ee SR |
1315 | |
1316 | xhr.onreadystatechange = function () { | |
1317 | if (xhr.readyState === 4) { | |
1318 | // in Chrome on file:/// URLs, status is 0 | |
1319 | if (xhr.status == 0) { | |
1320 | if (xhr.responseText) { | |
1321 | load(); | |
1322 | } | |
1323 | else { | |
1324 | // when responseText is empty, wait for load or error event | |
1325 | // to inform if it is a 404 or empty file | |
1326 | xhr.addEventListener('error', error); | |
1327 | xhr.addEventListener('load', load); | |
1328 | } | |
1329 | } | |
1330 | else if (xhr.status === 200) { | |
1331 | load(); | |
1332 | } | |
1333 | else { | |
1334 | error(); | |
1335 | } | |
1336 | } | |
1337 | }; | |
1338 | xhr.open("GET", url, true); | |
1339 | xhr.send(null); | |
1340 | } | |
1341 | ||
1342 | var WorkerPool = function (script, size) { | |
edb78799 PO |
1343 | var current = document.currentScript; |
1344 | // IE doesn't support currentScript | |
1345 | if (!current) { | |
d1316334 | 1346 | // Find an entry with out basename |
edb78799 | 1347 | var scripts = document.getElementsByTagName('script'); |
d1316334 PO |
1348 | for (var i = 0; i < scripts.length; i++) { |
1349 | if (scripts[i].src.indexOf("browser-es-module-loader.js") !== -1) { | |
1350 | current = scripts[i]; | |
1351 | break; | |
1352 | } | |
1353 | } | |
1354 | if (!current) | |
1355 | throw Error("Could not find own <script> element"); | |
edb78799 PO |
1356 | } |
1357 | script = current.src.substr(0, current.src.lastIndexOf("/")) + "/" + script; | |
399fa2ee SR |
1358 | this._workers = new Array(size); |
1359 | this._ind = 0; | |
1360 | this._size = size; | |
1361 | this._jobs = 0; | |
1362 | this.onmessage = undefined; | |
1363 | this._stopTimeout = undefined; | |
858ea4a7 PO |
1364 | for (var i = 0; i < size; i++) { |
1365 | var wrkr = new Worker(script); | |
399fa2ee SR |
1366 | wrkr._count = 0; |
1367 | wrkr._ind = i; | |
1368 | wrkr.onmessage = this._onmessage.bind(this, wrkr); | |
36efb978 | 1369 | wrkr.onerror = this._onerror.bind(this); |
399fa2ee SR |
1370 | this._workers[i] = wrkr; |
1371 | } | |
1372 | ||
1373 | this._checkJobs(); | |
1374 | }; | |
1375 | WorkerPool.prototype = { | |
1376 | postMessage: function (msg) { | |
1377 | if (this._stopTimeout !== undefined) { | |
1378 | clearTimeout(this._stopTimeout); | |
1379 | this._stopTimeout = undefined; | |
1380 | } | |
858ea4a7 | 1381 | var wrkr = this._workers[this._ind % this._size]; |
399fa2ee SR |
1382 | wrkr._count++; |
1383 | this._jobs++; | |
1384 | wrkr.postMessage(msg); | |
1385 | this._ind++; | |
1386 | }, | |
1387 | ||
1388 | _onmessage: function (wrkr, evt) { | |
1389 | wrkr._count--; | |
1390 | this._jobs--; | |
1391 | this.onmessage(evt, wrkr); | |
1392 | this._checkJobs(); | |
1393 | }, | |
1394 | ||
36efb978 PO |
1395 | _onerror: function(err) { |
1396 | try { | |
1397 | var evt = new Event('error'); | |
1398 | } catch (_eventError) { | |
1399 | var evt = document.createEvent('Event'); | |
1400 | evt.initEvent('error', true, true); | |
1401 | } | |
1402 | evt.message = err.message; | |
1403 | evt.filename = err.filename; | |
1404 | evt.lineno = err.lineno; | |
1405 | evt.colno = err.colno; | |
1406 | evt.error = err.error; | |
1407 | window.dispatchEvent(evt); | |
1408 | }, | |
1409 | ||
399fa2ee SR |
1410 | _checkJobs: function () { |
1411 | if (this._jobs === 0 && this._stopTimeout === undefined) { | |
1412 | // wait for 2s of inactivity before stopping (that should be enough for local loading) | |
1413 | this._stopTimeout = setTimeout(this._stop.bind(this), 2000); | |
1414 | } | |
1415 | }, | |
1416 | ||
1417 | _stop: function () { | |
152c3995 | 1418 | this._workers.forEach(function(wrkr) { |
399fa2ee | 1419 | wrkr.terminate(); |
152c3995 | 1420 | }); |
399fa2ee SR |
1421 | } |
1422 | }; | |
1423 | ||
1424 | var promiseMap = new Map(); | |
aadcf47d | 1425 | var babelWorker = new WorkerPool('babel-worker.js', 3); |
399fa2ee SR |
1426 | babelWorker.onmessage = function (evt) { |
1427 | var promFuncs = promiseMap.get(evt.data.key); | |
1428 | promFuncs.resolve(evt.data); | |
1429 | promiseMap.delete(evt.data.key); | |
1430 | }; | |
1431 | ||
1432 | // instantiate just needs to run System.register | |
1433 | // so we fetch the source, convert into the Babel System module format, then evaluate it | |
1434 | BrowserESModuleLoader.prototype[RegisterLoader$1.instantiate] = function(key, processAnonRegister) { | |
1435 | var loader = this; | |
1436 | ||
1437 | // load as ES with Babel converting into System.register | |
1438 | return new Promise(function(resolve, reject) { | |
1439 | // anonymous module | |
1440 | if (anonSources[key]) { | |
1441 | resolve(anonSources[key]); | |
1442 | anonSources[key] = undefined; | |
1443 | } | |
1444 | // otherwise we fetch | |
1445 | else { | |
1446 | xhrFetch(key, resolve, reject); | |
1447 | } | |
1448 | }) | |
1449 | .then(function(source) { | |
1450 | // check our cache first | |
1433360a PO |
1451 | var cacheEntry = localStorage.getItem(key); |
1452 | if (cacheEntry) { | |
1453 | cacheEntry = JSON.parse(cacheEntry); | |
399fa2ee | 1454 | // TODO: store a hash instead |
1433360a PO |
1455 | if (cacheEntry.source === source) { |
1456 | return Promise.resolve({key: key, code: cacheEntry.code, source: cacheEntry.source}); | |
399fa2ee SR |
1457 | } |
1458 | } | |
1459 | return new Promise(function (resolve, reject) { | |
1460 | promiseMap.set(key, {resolve: resolve, reject: reject}); | |
1461 | babelWorker.postMessage({key: key, source: source}); | |
1462 | }); | |
1463 | }).then(function (data) { | |
1464 | // evaluate without require, exports and module variables | |
1465 | // we leave module in for now to allow module.require access | |
55d5353f | 1466 | try { |
39193b28 PO |
1467 | var cacheEntry = JSON.stringify({source: data.source, code: data.code}); |
1468 | localStorage.setItem(key, cacheEntry); | |
55d5353f PO |
1469 | } catch (e) { |
1470 | if (window.console) { | |
1471 | window.console.warn('Unable to cache transpiled version of ' + key + ': ' + e); | |
777df7c2 | 1472 | } |
d6c17390 | 1473 | } |
399fa2ee SR |
1474 | (0, eval)(data.code + '\n//# sourceURL=' + data.key + '!transpiled'); |
1475 | processAnonRegister(); | |
1476 | }); | |
1477 | }; | |
1478 | ||
1479 | // create a default loader instance in the browser | |
1480 | if (isBrowser) | |
1481 | loader = new BrowserESModuleLoader(); | |
1482 | ||
1483 | return BrowserESModuleLoader; | |
1484 | ||
1485 | }))); | |
f7c620d3 | 1486 | //# sourceMappingURL=browser-es-module-loader.js.map |