es-module-shims 2.4.0 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,473 +1,478 @@
1
- /** ES Module Shims @version 2.4.0 */
1
+ /** ES Module Shims @version 2.5.0 */
2
2
  (function (exports) {
3
3
 
4
- let invalidate;
5
- const hotReload$1 = url => invalidate(new URL(url, baseUrl).href);
6
- const initHotReload = () => {
7
- let _importHook = importHook,
8
- _resolveHook = resolveHook,
9
- _metaHook = metaHook;
4
+ let invalidate;
5
+ const hotReload$1 = url => invalidate(new URL(url, baseUrl).href);
6
+ const initHotReload = () => {
7
+ let _importHook = importHook,
8
+ _resolveHook = resolveHook,
9
+ _metaHook = metaHook;
10
+
11
+ let defaultResolve;
12
+ let hotResolveHook = (id, parent, _defaultResolve) => {
13
+ if (!defaultResolve) defaultResolve = _defaultResolve;
14
+ const originalParent = stripVersion(parent);
15
+ const url = stripVersion(defaultResolve(id, originalParent));
16
+ const hotState = getHotState(url);
17
+ const parents = hotState.p;
18
+ if (!parents.includes(originalParent)) parents.push(originalParent);
19
+ return toVersioned(url, hotState);
20
+ };
21
+ const hotImportHook = (url, _, __, source, sourceType) => {
22
+ const hotState = getHotState(url);
23
+ hotState.e = typeof source === 'string' ? source : true;
24
+ hotState.t = sourceType;
25
+ };
26
+ const hotMetaHook = (metaObj, url) => {
27
+ metaObj.hot = new Hot(url);
28
+ };
29
+
30
+ const Hot = class Hot {
31
+ constructor(url) {
32
+ this.data = getHotState((this.url = stripVersion(url))).d;
33
+ }
34
+ accept(deps, cb) {
35
+ if (typeof deps === 'function') {
36
+ cb = deps;
37
+ deps = null;
38
+ }
39
+ const hotState = getHotState(this.url);
40
+ if (!hotState.A) return;
41
+ (hotState.a = hotState.a || []).push([
42
+ typeof deps === 'string' ? defaultResolve(deps, this.url)
43
+ : deps ? deps.map(d => defaultResolve(d, this.url))
44
+ : null,
45
+ cb
46
+ ]);
47
+ }
48
+ dispose(cb) {
49
+ getHotState(this.url).u = cb;
50
+ }
51
+ invalidate() {
52
+ const hotState = getHotState(this.url);
53
+ hotState.a = null;
54
+ hotState.A = true;
55
+ const seen = [this.url];
56
+ for (const p of hotState.p) invalidate(p, this.url, seen);
57
+ }
58
+ };
59
+
60
+ const versionedRegEx = /\?v=\d+$/;
61
+ const stripVersion = url => {
62
+ const versionMatch = url.match(versionedRegEx);
63
+ return versionMatch ? url.slice(0, -versionMatch[0].length) : url;
64
+ };
65
+
66
+ const toVersioned = (url, hotState) => {
67
+ const { v } = hotState;
68
+ return url + (v ? '?v=' + v : '');
69
+ };
70
+
71
+ let hotRegistry = {},
72
+ curInvalidationRoots = new Set(),
73
+ curInvalidationInterval;
74
+ const getHotState = url =>
75
+ hotRegistry[url] ||
76
+ (hotRegistry[url] = {
77
+ // version
78
+ v: 0,
79
+ // accept list ([deps, cb] pairs)
80
+ a: null,
81
+ // accepting acceptors
82
+ A: true,
83
+ // unload callback
84
+ u: null,
85
+ // entry point or inline script source
86
+ e: false,
87
+ // hot data
88
+ d: {},
89
+ // parents
90
+ p: [],
91
+ // source type
92
+ t: undefined
93
+ });
94
+
95
+ invalidate = (url, fromUrl, seen = []) => {
96
+ if (!seen.includes(url)) {
97
+ seen.push(url);
98
+ const hotState = hotRegistry[url];
99
+ if (hotState) {
100
+ hotState.A = false;
101
+ if (
102
+ fromUrl &&
103
+ hotState.a &&
104
+ hotState.a.some(([d]) => d && (typeof d === 'string' ? d === fromUrl : d.includes(fromUrl)))
105
+ ) {
106
+ curInvalidationRoots.add(fromUrl);
107
+ } else {
108
+ if (hotState.e || hotState.a) curInvalidationRoots.add(url);
109
+ hotState.v++;
110
+ if (!hotState.a) for (const parent of hotState.p) invalidate(parent, url, seen);
111
+ }
112
+ }
113
+ if (!curInvalidationInterval) curInvalidationInterval = setTimeout(handleInvalidations, hotReloadInterval);
114
+ }
115
+ };
116
+
117
+ const handleInvalidations = () => {
118
+ curInvalidationInterval = null;
119
+ const earlyRoots = new Set();
120
+ for (const root of curInvalidationRoots) {
121
+ const hotState = hotRegistry[root];
122
+ topLevelLoad(
123
+ toVersioned(root, hotState),
124
+ baseUrl,
125
+ defaultFetchOpts,
126
+ typeof hotState.e === 'string' ? hotState.e : undefined,
127
+ false,
128
+ undefined,
129
+ hotState.t
130
+ ).then(m => {
131
+ if (hotState.a) {
132
+ hotState.a.every(([d, c]) => d === null && !earlyRoots.has(c) && c(m));
133
+ // unload should be the latest unload handler from the just loaded module
134
+ if (hotState.u) {
135
+ hotState.u(hotState.d);
136
+ hotState.u = null;
137
+ }
138
+ }
139
+ for (const parent of hotState.p) {
140
+ const hotState = hotRegistry[parent];
141
+ if (hotState && hotState.a)
142
+ hotState.a.every(async ([d, c]) => {
143
+ return (
144
+ d &&
145
+ !earlyRoots.has(c) &&
146
+ (typeof d === 'string' ?
147
+ d === root && c(m)
148
+ : c(await Promise.all(d.map(d => (earlyRoots.push(c), importShim(toVersioned(d, getHotState(d))))))))
149
+ );
150
+ });
151
+ }
152
+ }, throwError);
153
+ }
154
+ curInvalidationRoots = new Set();
155
+ };
156
+
157
+ return [
158
+ _importHook ? chain(_importHook, hotImportHook) : hotImportHook,
159
+ _resolveHook ?
160
+ (id, parent, defaultResolve) =>
161
+ hotResolveHook(id, parent, (id, parent) => _resolveHook(id, parent, defaultResolve))
162
+ : hotResolveHook,
163
+ _metaHook ? chain(_metaHook, hotMetaHook) : hotMetaHook
164
+ ];
165
+ };
10
166
 
11
- let defaultResolve;
12
- let hotResolveHook = (id, parent, _defaultResolve) => {
13
- if (!defaultResolve) defaultResolve = _defaultResolve;
14
- const originalParent = stripVersion(parent);
15
- const url = stripVersion(defaultResolve(id, originalParent));
16
- const parents = getHotState(url).p;
17
- if (!parents.includes(originalParent)) parents.push(originalParent);
18
- return toVersioned(url);
19
- };
20
- const hotImportHook = (url, _, __, source, sourceType) => {
21
- const hotState = getHotState(url);
22
- hotState.e = typeof source === 'string' ? source : true;
23
- hotState.t = sourceType;
24
- };
25
- const hotMetaHook = (metaObj, url) => {
26
- metaObj.hot = new Hot(url);
167
+ const hasDocument = typeof document !== 'undefined';
168
+
169
+ const noop = () => {};
170
+
171
+ const chain = (a, b) =>
172
+ function () {
173
+ a.apply(this, arguments);
174
+ b.apply(this, arguments);
27
175
  };
28
176
 
29
- const Hot = class Hot {
30
- constructor(url) {
31
- this.data = getHotState((this.url = stripVersion(url))).d;
177
+ const dynamicImport = (u, errUrl) => import(u);
178
+
179
+ const defineValue = (obj, prop, value) =>
180
+ Object.defineProperty(obj, prop, { writable: false, configurable: false, value });
181
+
182
+ const optionsScript = hasDocument ? document.querySelector('script[type=esms-options]') : undefined;
183
+
184
+ const esmsInitOptions = optionsScript ? JSON.parse(optionsScript.innerHTML) : {};
185
+ Object.assign(esmsInitOptions, self.esmsInitOptions || {});
186
+
187
+ const version = "2.5.0";
188
+
189
+ const r$1 = esmsInitOptions.version;
190
+ if (self.importShim || (r$1 && r$1 !== version)) {
191
+ return;
192
+ }
193
+
194
+ // shim mode is determined on initialization, no late shim mode
195
+ const shimMode =
196
+ esmsInitOptions.shimMode ||
197
+ (hasDocument &&
198
+ document.querySelectorAll('script[type=module-shim],script[type=importmap-shim],link[rel=modulepreload-shim]')
199
+ .length > 0);
200
+
201
+ let importHook,
202
+ resolveHook,
203
+ fetchHook = fetch,
204
+ metaHook,
205
+ tsTransform =
206
+ esmsInitOptions.tsTransform ||
207
+ (hasDocument && document.currentScript && document.currentScript.src.replace(/(\.\w+)?\.js$/, '-typescript.js')) ||
208
+ './es-module-shims-typescript.js';
209
+
210
+ const defaultFetchOpts = { credentials: 'same-origin' };
211
+
212
+ const {
213
+ revokeBlobURLs,
214
+ noLoadEventRetriggers,
215
+ enforceIntegrity,
216
+ hotReload,
217
+ hotReloadInterval = 100,
218
+ nativePassthrough = !hotReload
219
+ } = esmsInitOptions;
220
+
221
+ const globalHook = name => (typeof name === 'string' ? self[name] : name);
222
+
223
+ if (esmsInitOptions.onimport) importHook = globalHook(esmsInitOptions.onimport);
224
+ if (esmsInitOptions.resolve) resolveHook = globalHook(esmsInitOptions.resolve);
225
+ if (esmsInitOptions.fetch) fetchHook = globalHook(esmsInitOptions.fetch);
226
+ if (esmsInitOptions.meta) metaHook = globalHook(esmsInitOptions.meta);
227
+
228
+ if (hotReload) [importHook, resolveHook, metaHook] = initHotReload();
229
+
230
+ const mapOverrides = esmsInitOptions.mapOverrides;
231
+
232
+ let nonce = esmsInitOptions.nonce;
233
+ if (!nonce && hasDocument) {
234
+ const nonceElement = document.querySelector('script[nonce]');
235
+ if (nonceElement) nonce = nonceElement.nonce || nonceElement.getAttribute('nonce');
236
+ }
237
+
238
+ const onerror = globalHook(esmsInitOptions.onerror || console.error.bind(console));
239
+
240
+ const enable = Array.isArray(esmsInitOptions.polyfillEnable) ? esmsInitOptions.polyfillEnable : [];
241
+ const enableAll = esmsInitOptions.polyfillEnable === 'all' || enable.includes('all');
242
+ const wasmInstancePhaseEnabled =
243
+ enable.includes('wasm-modules') || enable.includes('wasm-module-instances') || enableAll;
244
+ const wasmSourcePhaseEnabled =
245
+ enable.includes('wasm-modules') || enable.includes('wasm-module-sources') || enableAll;
246
+ const deferPhaseEnabled = enable.includes('import-defer') || enableAll;
247
+
248
+ const onpolyfill =
249
+ esmsInitOptions.onpolyfill ?
250
+ globalHook(esmsInitOptions.onpolyfill)
251
+ : () => {
252
+ console.log(`%c^^ Module error above is polyfilled and can be ignored ^^`, 'font-weight:900;color:#391');
253
+ };
254
+
255
+ const baseUrl =
256
+ hasDocument ?
257
+ document.baseURI
258
+ : `${location.protocol}//${location.host}${
259
+ location.pathname.includes('/') ?
260
+ location.pathname.slice(0, location.pathname.lastIndexOf('/') + 1)
261
+ : location.pathname
262
+ }`;
263
+
264
+ const createBlob = (source, type = 'text/javascript') => URL.createObjectURL(new Blob([source], { type }));
265
+ let { skip } = esmsInitOptions;
266
+ if (Array.isArray(skip)) {
267
+ const l = skip.map(s => new URL(s, baseUrl).href);
268
+ skip = s => l.some(i => (i[i.length - 1] === '/' && s.startsWith(i)) || s === i);
269
+ } else if (typeof skip === 'string') {
270
+ const r = new RegExp(skip);
271
+ skip = s => r.test(s);
272
+ } else if (skip instanceof RegExp) {
273
+ skip = s => skip.test(s);
274
+ }
275
+
276
+ const dispatchError = error => self.dispatchEvent(Object.assign(new Event('error'), { error }));
277
+
278
+ const throwError = err => {
279
+ (self.reportError || dispatchError)(err), void onerror(err);
280
+ };
281
+
282
+ const fromParent = parent => (parent ? ` imported from ${parent}` : '');
283
+
284
+ const backslashRegEx = /\\/g;
285
+
286
+ const asURL = url => {
287
+ try {
288
+ if (url.indexOf(':') !== -1) return new URL(url).href;
289
+ } catch (_) {}
290
+ };
291
+
292
+ const resolveUrl = (relUrl, parentUrl) =>
293
+ resolveIfNotPlainOrUrl(relUrl, parentUrl) || asURL(relUrl) || resolveIfNotPlainOrUrl('./' + relUrl, parentUrl);
294
+
295
+ const resolveIfNotPlainOrUrl = (relUrl, parentUrl) => {
296
+ const hIdx = parentUrl.indexOf('#'),
297
+ qIdx = parentUrl.indexOf('?');
298
+ if (hIdx + qIdx > -2)
299
+ parentUrl = parentUrl.slice(
300
+ 0,
301
+ hIdx === -1 ? qIdx
302
+ : qIdx === -1 || qIdx > hIdx ? hIdx
303
+ : qIdx
304
+ );
305
+ if (relUrl.indexOf('\\') !== -1) relUrl = relUrl.replace(backslashRegEx, '/');
306
+ // protocol-relative
307
+ if (relUrl[0] === '/' && relUrl[1] === '/') {
308
+ return parentUrl.slice(0, parentUrl.indexOf(':') + 1) + relUrl;
309
+ }
310
+ // relative-url
311
+ else if (
312
+ (relUrl[0] === '.' &&
313
+ (relUrl[1] === '/' ||
314
+ (relUrl[1] === '.' && (relUrl[2] === '/' || (relUrl.length === 2 && (relUrl += '/')))) ||
315
+ (relUrl.length === 1 && (relUrl += '/')))) ||
316
+ relUrl[0] === '/'
317
+ ) {
318
+ const parentProtocol = parentUrl.slice(0, parentUrl.indexOf(':') + 1);
319
+ if (parentProtocol === 'blob:') {
320
+ throw new TypeError(
321
+ `Failed to resolve module specifier "${relUrl}". Invalid relative url or base scheme isn't hierarchical.`
322
+ );
32
323
  }
33
- accept(deps, cb) {
34
- if (typeof deps === 'function') {
35
- cb = deps;
36
- deps = null;
324
+ // Disabled, but these cases will give inconsistent results for deep backtracking
325
+ //if (parentUrl[parentProtocol.length] !== '/')
326
+ // throw new Error('Cannot resolve');
327
+ // read pathname from parent URL
328
+ // pathname taken to be part after leading "/"
329
+ let pathname;
330
+ if (parentUrl[parentProtocol.length + 1] === '/') {
331
+ // resolving to a :// so we need to read out the auth and host
332
+ if (parentProtocol !== 'file:') {
333
+ pathname = parentUrl.slice(parentProtocol.length + 2);
334
+ pathname = pathname.slice(pathname.indexOf('/') + 1);
335
+ } else {
336
+ pathname = parentUrl.slice(8);
37
337
  }
38
- const hotState = getHotState(this.url);
39
- if (!hotState.A) return;
40
- (hotState.a = hotState.a || []).push([
41
- typeof deps === 'string' ? defaultResolve(deps, this.url)
42
- : deps ? deps.map(d => defaultResolve(d, this.url))
43
- : null,
44
- cb
45
- ]);
46
- }
47
- dispose(cb) {
48
- getHotState(this.url).u = cb;
49
- }
50
- invalidate() {
51
- const hotState = getHotState(this.url);
52
- hotState.a = null;
53
- hotState.A = true;
54
- const seen = [this.url];
55
- for (const p of hotState.p) invalidate(p, this.url, seen);
338
+ } else {
339
+ // resolving to :/ so pathname is the /... part
340
+ pathname = parentUrl.slice(parentProtocol.length + (parentUrl[parentProtocol.length] === '/'));
56
341
  }
57
- };
58
342
 
59
- const versionedRegEx = /\?v=\d+$/;
60
- const stripVersion = url => {
61
- const versionMatch = url.match(versionedRegEx);
62
- return versionMatch ? url.slice(0, -versionMatch[0].length) : url;
63
- };
343
+ if (relUrl[0] === '/') return parentUrl.slice(0, parentUrl.length - pathname.length - 1) + relUrl;
64
344
 
65
- const toVersioned = url => {
66
- const v = getHotState(url).v;
67
- return url + (v ? '?v=' + v : '');
68
- };
345
+ // join together and split for removal of .. and . segments
346
+ // looping the string instead of anything fancy for perf reasons
347
+ // '../../../../../z' resolved to 'x/y' is just 'z'
348
+ const segmented = pathname.slice(0, pathname.lastIndexOf('/') + 1) + relUrl;
69
349
 
70
- let hotRegistry = {},
71
- curInvalidationRoots = new Set(),
72
- curInvalidationInterval;
73
- const getHotState = url =>
74
- hotRegistry[url] ||
75
- (hotRegistry[url] = {
76
- // version
77
- v: 0,
78
- // accept list ([deps, cb] pairs)
79
- a: null,
80
- // accepting acceptors
81
- A: true,
82
- // unload callback
83
- u: null,
84
- // entry point or inline script source
85
- e: false,
86
- // hot data
87
- d: {},
88
- // parents
89
- p: [],
90
- // source type
91
- t: undefined
92
- });
93
-
94
- invalidate = (url, fromUrl, seen = []) => {
95
- if (!seen.includes(url)) {
96
- seen.push(url);
97
- const hotState = hotRegistry[url];
98
- if (hotState) {
99
- hotState.A = false;
100
- if (
101
- hotState.a &&
102
- hotState.a.some(([d]) => d && (typeof d === 'string' ? d === fromUrl : d.includes(fromUrl)))
103
- ) {
104
- curInvalidationRoots.add(fromUrl);
105
- } else {
106
- if (hotState.e || hotState.a) curInvalidationRoots.add(url);
107
- hotState.v++;
108
- if (!hotState.a) for (const parent of hotState.p) invalidate(parent, url, seen);
350
+ const output = [];
351
+ let segmentIndex = -1;
352
+ for (let i = 0; i < segmented.length; i++) {
353
+ // busy reading a segment - only terminate on '/'
354
+ if (segmentIndex !== -1) {
355
+ if (segmented[i] === '/') {
356
+ output.push(segmented.slice(segmentIndex, i + 1));
357
+ segmentIndex = -1;
109
358
  }
359
+ continue;
110
360
  }
111
- }
112
- if (!curInvalidationInterval)
113
- curInvalidationInterval = setTimeout(() => {
114
- curInvalidationInterval = null;
115
- const earlyRoots = new Set();
116
- for (const root of curInvalidationRoots) {
117
- const hotState = hotRegistry[root];
118
- topLevelLoad(
119
- toVersioned(root),
120
- baseUrl,
121
- defaultFetchOpts,
122
- typeof hotState.e === 'string' ? hotState.e : undefined,
123
- false,
124
- undefined,
125
- hotState.t
126
- ).then(m => {
127
- if (hotState.a) {
128
- hotState.a.every(([d, c]) => d === null && !earlyRoots.has(c) && c(m));
129
- // unload should be the latest unload handler from the just loaded module
130
- if (hotState.u) {
131
- hotState.u(hotState.d);
132
- hotState.u = null;
133
- }
134
- }
135
- for (const parent of hotState.p) {
136
- const hotState = hotRegistry[parent];
137
- if (hotState && hotState.a)
138
- hotState.a.every(async ([d, c]) => {
139
- return (
140
- d &&
141
- !earlyRoots.has(c) &&
142
- (typeof d === 'string' ?
143
- d === root && c(m)
144
- : c(await Promise.all(d.map(d => (earlyRoots.push(c), importShim(toVersioned(d)))))))
145
- );
146
- });
147
- }
148
- }, throwError);
361
+ // new segment - check if it is relative
362
+ else if (segmented[i] === '.') {
363
+ // ../ segment
364
+ if (segmented[i + 1] === '.' && (segmented[i + 2] === '/' || i + 2 === segmented.length)) {
365
+ output.pop();
366
+ i += 2;
367
+ continue;
149
368
  }
150
- curInvalidationRoots = new Set();
151
- }, hotReloadInterval);
369
+ // ./ segment
370
+ else if (segmented[i + 1] === '/' || i + 1 === segmented.length) {
371
+ i += 1;
372
+ continue;
373
+ }
374
+ }
375
+ // it is the start of a new segment
376
+ while (segmented[i] === '/') i++;
377
+ segmentIndex = i;
378
+ }
379
+ // finish reading out the last segment
380
+ if (segmentIndex !== -1) output.push(segmented.slice(segmentIndex));
381
+ return parentUrl.slice(0, parentUrl.length - pathname.length) + output.join('');
382
+ }
383
+ };
384
+
385
+ const resolveAndComposeImportMap = (json, baseUrl, parentMap) => {
386
+ const outMap = {
387
+ imports: { ...parentMap.imports },
388
+ scopes: { ...parentMap.scopes },
389
+ integrity: { ...parentMap.integrity }
152
390
  };
153
391
 
154
- return [
155
- _importHook ? chain(_importHook, hotImportHook) : hotImportHook,
156
- _resolveHook ?
157
- (id, parent, defaultResolve) =>
158
- hotResolveHook(id, parent, (id, parent) => _resolveHook(id, parent, defaultResolve))
159
- : hotResolveHook,
160
- _metaHook ? chain(_metaHook, hotMetaHook) : hotMetaHook
161
- ];
392
+ if (json.imports) resolveAndComposePackages(json.imports, outMap.imports, baseUrl, parentMap);
393
+
394
+ if (json.scopes)
395
+ for (let s in json.scopes) {
396
+ const resolvedScope = resolveUrl(s, baseUrl);
397
+ resolveAndComposePackages(
398
+ json.scopes[s],
399
+ outMap.scopes[resolvedScope] || (outMap.scopes[resolvedScope] = {}),
400
+ baseUrl,
401
+ parentMap
402
+ );
403
+ }
404
+
405
+ if (json.integrity) resolveAndComposeIntegrity(json.integrity, outMap.integrity, baseUrl);
406
+
407
+ return outMap;
162
408
  };
163
409
 
164
- const hasDocument = typeof document !== 'undefined';
165
-
166
- const noop = () => {};
167
-
168
- const chain = (a, b) =>
169
- function () {
170
- a.apply(this, arguments);
171
- b.apply(this, arguments);
172
- };
173
-
174
- const dynamicImport = (u, errUrl) => import(u);
175
-
176
- const defineValue = (obj, prop, value) =>
177
- Object.defineProperty(obj, prop, { writable: false, configurable: false, value });
178
-
179
- const optionsScript = hasDocument ? document.querySelector('script[type=esms-options]') : undefined;
180
-
181
- const esmsInitOptions = optionsScript ? JSON.parse(optionsScript.innerHTML) : {};
182
- Object.assign(esmsInitOptions, self.esmsInitOptions || {});
183
-
184
- const r$1 = esmsInitOptions.version;
185
- if (self.importShim || (r$1 && r$1 !== "2.4.0")) {
186
- return;
187
- }
188
-
189
- // shim mode is determined on initialization, no late shim mode
190
- const shimMode =
191
- esmsInitOptions.shimMode ||
192
- (hasDocument &&
193
- document.querySelectorAll('script[type=module-shim],script[type=importmap-shim],link[rel=modulepreload-shim]')
194
- .length > 0);
195
-
196
- let importHook,
197
- resolveHook,
198
- fetchHook = fetch,
199
- metaHook,
200
- tsTransform =
201
- esmsInitOptions.tsTransform ||
202
- (hasDocument && document.currentScript && document.currentScript.src.replace(/(\.\w+)?\.js$/, '-typescript.js')) ||
203
- './es-module-shims-typescript.js';
204
-
205
- const defaultFetchOpts = { credentials: 'same-origin' };
206
-
207
- const {
208
- revokeBlobURLs,
209
- noLoadEventRetriggers,
210
- enforceIntegrity,
211
- hotReload,
212
- hotReloadInterval = 100,
213
- nativePassthrough = !hotReload
214
- } = esmsInitOptions;
215
-
216
- const globalHook = name => (typeof name === 'string' ? self[name] : name);
217
-
218
- if (esmsInitOptions.onimport) importHook = globalHook(esmsInitOptions.onimport);
219
- if (esmsInitOptions.resolve) resolveHook = globalHook(esmsInitOptions.resolve);
220
- if (esmsInitOptions.fetch) fetchHook = globalHook(esmsInitOptions.fetch);
221
- if (esmsInitOptions.meta) metaHook = globalHook(esmsInitOptions.meta);
222
-
223
- if (hotReload) [importHook, resolveHook, metaHook] = initHotReload();
224
-
225
- const mapOverrides = esmsInitOptions.mapOverrides;
226
-
227
- let nonce = esmsInitOptions.nonce;
228
- if (!nonce && hasDocument) {
229
- const nonceElement = document.querySelector('script[nonce]');
230
- if (nonceElement) nonce = nonceElement.nonce || nonceElement.getAttribute('nonce');
231
- }
232
-
233
- const onerror = globalHook(esmsInitOptions.onerror || console.error.bind(console));
234
-
235
- const enable = Array.isArray(esmsInitOptions.polyfillEnable) ? esmsInitOptions.polyfillEnable : [];
236
- const enableAll = esmsInitOptions.polyfillEnable === 'all' || enable.includes('all');
237
- const wasmInstancePhaseEnabled =
238
- enable.includes('wasm-modules') || enable.includes('wasm-module-instances') || enableAll;
239
- const wasmSourcePhaseEnabled =
240
- enable.includes('wasm-modules') || enable.includes('wasm-module-sources') || enableAll;
241
- const deferPhaseEnabled = enable.includes('import-defer') || enableAll;
242
-
243
- const onpolyfill =
244
- esmsInitOptions.onpolyfill ?
245
- globalHook(esmsInitOptions.onpolyfill)
246
- : () => {
247
- console.log(`%c^^ Module error above is polyfilled and can be ignored ^^`, 'font-weight:900;color:#391');
248
- };
249
-
250
- const baseUrl =
251
- hasDocument ?
252
- document.baseURI
253
- : `${location.protocol}//${location.host}${
254
- location.pathname.includes('/') ?
255
- location.pathname.slice(0, location.pathname.lastIndexOf('/') + 1)
256
- : location.pathname
257
- }`;
258
-
259
- const createBlob = (source, type = 'text/javascript') => URL.createObjectURL(new Blob([source], { type }));
260
- let { skip } = esmsInitOptions;
261
- if (Array.isArray(skip)) {
262
- const l = skip.map(s => new URL(s, baseUrl).href);
263
- skip = s => l.some(i => (i[i.length - 1] === '/' && s.startsWith(i)) || s === i);
264
- } else if (typeof skip === 'string') {
265
- const r = new RegExp(skip);
266
- skip = s => r.test(s);
267
- } else if (skip instanceof RegExp) {
268
- skip = s => skip.test(s);
269
- }
270
-
271
- const dispatchError = error => self.dispatchEvent(Object.assign(new Event('error'), { error }));
272
-
273
- const throwError = err => {
274
- (self.reportError || dispatchError)(err), void onerror(err);
275
- };
276
-
277
- const fromParent = parent => (parent ? ` imported from ${parent}` : '');
410
+ const getMatch = (path, matchObj) => {
411
+ if (matchObj[path]) return path;
412
+ let sepIndex = path.length;
413
+ do {
414
+ const segment = path.slice(0, sepIndex + 1);
415
+ if (segment in matchObj) return segment;
416
+ } while ((sepIndex = path.lastIndexOf('/', sepIndex - 1)) !== -1);
417
+ };
278
418
 
279
- const backslashRegEx = /\\/g;
280
-
281
- const asURL = url => {
282
- try {
283
- if (url.indexOf(':') !== -1) return new URL(url).href;
284
- } catch (_) {}
285
- };
286
-
287
- const resolveUrl = (relUrl, parentUrl) =>
288
- resolveIfNotPlainOrUrl(relUrl, parentUrl) || asURL(relUrl) || resolveIfNotPlainOrUrl('./' + relUrl, parentUrl);
289
-
290
- const resolveIfNotPlainOrUrl = (relUrl, parentUrl) => {
291
- const hIdx = parentUrl.indexOf('#'),
292
- qIdx = parentUrl.indexOf('?');
293
- if (hIdx + qIdx > -2)
294
- parentUrl = parentUrl.slice(
295
- 0,
296
- hIdx === -1 ? qIdx
297
- : qIdx === -1 || qIdx > hIdx ? hIdx
298
- : qIdx
299
- );
300
- if (relUrl.indexOf('\\') !== -1) relUrl = relUrl.replace(backslashRegEx, '/');
301
- // protocol-relative
302
- if (relUrl[0] === '/' && relUrl[1] === '/') {
303
- return parentUrl.slice(0, parentUrl.indexOf(':') + 1) + relUrl;
304
- }
305
- // relative-url
306
- else if (
307
- (relUrl[0] === '.' &&
308
- (relUrl[1] === '/' ||
309
- (relUrl[1] === '.' && (relUrl[2] === '/' || (relUrl.length === 2 && (relUrl += '/')))) ||
310
- (relUrl.length === 1 && (relUrl += '/')))) ||
311
- relUrl[0] === '/'
312
- ) {
313
- const parentProtocol = parentUrl.slice(0, parentUrl.indexOf(':') + 1);
314
- if (parentProtocol === 'blob:') {
315
- throw new TypeError(
316
- `Failed to resolve module specifier "${relUrl}". Invalid relative url or base scheme isn't hierarchical.`
317
- );
318
- }
319
- // Disabled, but these cases will give inconsistent results for deep backtracking
320
- //if (parentUrl[parentProtocol.length] !== '/')
321
- // throw new Error('Cannot resolve');
322
- // read pathname from parent URL
323
- // pathname taken to be part after leading "/"
324
- let pathname;
325
- if (parentUrl[parentProtocol.length + 1] === '/') {
326
- // resolving to a :// so we need to read out the auth and host
327
- if (parentProtocol !== 'file:') {
328
- pathname = parentUrl.slice(parentProtocol.length + 2);
329
- pathname = pathname.slice(pathname.indexOf('/') + 1);
330
- } else {
331
- pathname = parentUrl.slice(8);
332
- }
333
- } else {
334
- // resolving to :/ so pathname is the /... part
335
- pathname = parentUrl.slice(parentProtocol.length + (parentUrl[parentProtocol.length] === '/'));
336
- }
337
-
338
- if (relUrl[0] === '/') return parentUrl.slice(0, parentUrl.length - pathname.length - 1) + relUrl;
339
-
340
- // join together and split for removal of .. and . segments
341
- // looping the string instead of anything fancy for perf reasons
342
- // '../../../../../z' resolved to 'x/y' is just 'z'
343
- const segmented = pathname.slice(0, pathname.lastIndexOf('/') + 1) + relUrl;
344
-
345
- const output = [];
346
- let segmentIndex = -1;
347
- for (let i = 0; i < segmented.length; i++) {
348
- // busy reading a segment - only terminate on '/'
349
- if (segmentIndex !== -1) {
350
- if (segmented[i] === '/') {
351
- output.push(segmented.slice(segmentIndex, i + 1));
352
- segmentIndex = -1;
353
- }
354
- continue;
355
- }
356
- // new segment - check if it is relative
357
- else if (segmented[i] === '.') {
358
- // ../ segment
359
- if (segmented[i + 1] === '.' && (segmented[i + 2] === '/' || i + 2 === segmented.length)) {
360
- output.pop();
361
- i += 2;
362
- continue;
363
- }
364
- // ./ segment
365
- else if (segmented[i + 1] === '/' || i + 1 === segmented.length) {
366
- i += 1;
367
- continue;
368
- }
369
- }
370
- // it is the start of a new segment
371
- while (segmented[i] === '/') i++;
372
- segmentIndex = i;
373
- }
374
- // finish reading out the last segment
375
- if (segmentIndex !== -1) output.push(segmented.slice(segmentIndex));
376
- return parentUrl.slice(0, parentUrl.length - pathname.length) + output.join('');
377
- }
378
- };
379
-
380
- const resolveAndComposeImportMap = (json, baseUrl, parentMap) => {
381
- const outMap = {
382
- imports: { ...parentMap.imports },
383
- scopes: { ...parentMap.scopes },
384
- integrity: { ...parentMap.integrity }
385
- };
386
-
387
- if (json.imports) resolveAndComposePackages(json.imports, outMap.imports, baseUrl, parentMap);
388
-
389
- if (json.scopes)
390
- for (let s in json.scopes) {
391
- const resolvedScope = resolveUrl(s, baseUrl);
392
- resolveAndComposePackages(
393
- json.scopes[s],
394
- outMap.scopes[resolvedScope] || (outMap.scopes[resolvedScope] = {}),
395
- baseUrl,
396
- parentMap
397
- );
398
- }
399
-
400
- if (json.integrity) resolveAndComposeIntegrity(json.integrity, outMap.integrity, baseUrl);
401
-
402
- return outMap;
403
- };
404
-
405
- const getMatch = (path, matchObj) => {
406
- if (matchObj[path]) return path;
407
- let sepIndex = path.length;
408
- do {
409
- const segment = path.slice(0, sepIndex + 1);
410
- if (segment in matchObj) return segment;
411
- } while ((sepIndex = path.lastIndexOf('/', sepIndex - 1)) !== -1);
412
- };
413
-
414
- const applyPackages = (id, packages) => {
415
- const pkgName = getMatch(id, packages);
416
- if (pkgName) {
417
- const pkg = packages[pkgName];
418
- if (pkg === null) return;
419
- return pkg + id.slice(pkgName.length);
420
- }
421
- };
422
-
423
- const resolveImportMap = (importMap, resolvedOrPlain, parentUrl) => {
424
- let scopeUrl = parentUrl && getMatch(parentUrl, importMap.scopes);
425
- while (scopeUrl) {
426
- const packageResolution = applyPackages(resolvedOrPlain, importMap.scopes[scopeUrl]);
427
- if (packageResolution) return packageResolution;
428
- scopeUrl = getMatch(scopeUrl.slice(0, scopeUrl.lastIndexOf('/')), importMap.scopes);
429
- }
430
- return applyPackages(resolvedOrPlain, importMap.imports) || (resolvedOrPlain.indexOf(':') !== -1 && resolvedOrPlain);
431
- };
432
-
433
- const resolveAndComposePackages = (packages, outPackages, baseUrl, parentMap) => {
434
- for (let p in packages) {
435
- const resolvedLhs = resolveIfNotPlainOrUrl(p, baseUrl) || p;
436
- if (
437
- (!shimMode || !mapOverrides) &&
438
- outPackages[resolvedLhs] &&
439
- outPackages[resolvedLhs] !== packages[resolvedLhs]
440
- ) {
441
- console.warn(
442
- `es-module-shims: Rejected map override "${resolvedLhs}" from ${outPackages[resolvedLhs]} to ${packages[resolvedLhs]}.`
443
- );
444
- continue;
445
- }
446
- let target = packages[p];
447
- if (typeof target !== 'string') continue;
448
- const mapped = resolveImportMap(parentMap, resolveIfNotPlainOrUrl(target, baseUrl) || target, baseUrl);
449
- if (mapped) {
450
- outPackages[resolvedLhs] = mapped;
451
- continue;
452
- }
453
- console.warn(`es-module-shims: Mapping "${p}" -> "${packages[p]}" does not resolve`);
454
- }
455
- };
456
-
457
- const resolveAndComposeIntegrity = (integrity, outIntegrity, baseUrl) => {
458
- for (let p in integrity) {
459
- const resolvedLhs = resolveIfNotPlainOrUrl(p, baseUrl) || p;
460
- if (
461
- (!shimMode || !mapOverrides) &&
462
- outIntegrity[resolvedLhs] &&
463
- outIntegrity[resolvedLhs] !== integrity[resolvedLhs]
464
- ) {
465
- console.warn(
466
- `es-module-shims: Rejected map integrity override "${resolvedLhs}" from ${outIntegrity[resolvedLhs]} to ${integrity[resolvedLhs]}.`
467
- );
468
- }
469
- outIntegrity[resolvedLhs] = integrity[p];
470
- }
419
+ const applyPackages = (id, packages) => {
420
+ const pkgName = getMatch(id, packages);
421
+ if (pkgName) {
422
+ const pkg = packages[pkgName];
423
+ if (pkg === null) return;
424
+ return pkg + id.slice(pkgName.length);
425
+ }
426
+ };
427
+
428
+ const resolveImportMap = (importMap, resolvedOrPlain, parentUrl) => {
429
+ let scopeUrl = parentUrl && getMatch(parentUrl, importMap.scopes);
430
+ while (scopeUrl) {
431
+ const packageResolution = applyPackages(resolvedOrPlain, importMap.scopes[scopeUrl]);
432
+ if (packageResolution) return packageResolution;
433
+ scopeUrl = getMatch(scopeUrl.slice(0, scopeUrl.lastIndexOf('/')), importMap.scopes);
434
+ }
435
+ return applyPackages(resolvedOrPlain, importMap.imports) || (resolvedOrPlain.indexOf(':') !== -1 && resolvedOrPlain);
436
+ };
437
+
438
+ const resolveAndComposePackages = (packages, outPackages, baseUrl, parentMap) => {
439
+ for (let p in packages) {
440
+ const resolvedLhs = resolveIfNotPlainOrUrl(p, baseUrl) || p;
441
+ if (
442
+ (!shimMode || !mapOverrides) &&
443
+ outPackages[resolvedLhs] &&
444
+ outPackages[resolvedLhs] !== packages[resolvedLhs]
445
+ ) {
446
+ console.warn(
447
+ `es-module-shims: Rejected map override "${resolvedLhs}" from ${outPackages[resolvedLhs]} to ${packages[resolvedLhs]}.`
448
+ );
449
+ continue;
450
+ }
451
+ let target = packages[p];
452
+ if (typeof target !== 'string') continue;
453
+ const mapped = resolveImportMap(parentMap, resolveIfNotPlainOrUrl(target, baseUrl) || target, baseUrl);
454
+ if (mapped) {
455
+ outPackages[resolvedLhs] = mapped;
456
+ continue;
457
+ }
458
+ console.warn(`es-module-shims: Mapping "${p}" -> "${packages[p]}" does not resolve`);
459
+ }
460
+ };
461
+
462
+ const resolveAndComposeIntegrity = (integrity, outIntegrity, baseUrl) => {
463
+ for (let p in integrity) {
464
+ const resolvedLhs = resolveIfNotPlainOrUrl(p, baseUrl) || p;
465
+ if (
466
+ (!shimMode || !mapOverrides) &&
467
+ outIntegrity[resolvedLhs] &&
468
+ outIntegrity[resolvedLhs] !== integrity[resolvedLhs]
469
+ ) {
470
+ console.warn(
471
+ `es-module-shims: Rejected map integrity override "${resolvedLhs}" from ${outIntegrity[resolvedLhs]} to ${integrity[resolvedLhs]}.`
472
+ );
473
+ }
474
+ outIntegrity[resolvedLhs] = integrity[p];
475
+ }
471
476
  };
472
477
 
473
478
  // support browsers without dynamic import support (eg Firefox 6x)
@@ -508,12 +513,13 @@
508
513
  )
509
514
  ]);
510
515
 
516
+ const msgTag = `s${version}`;
511
517
  return new Promise(resolve => {
512
518
  const iframe = document.createElement('iframe');
513
519
  iframe.style.display = 'none';
514
520
  iframe.setAttribute('nonce', nonce);
515
521
  function cb({ data }) {
516
- const isFeatureDetectionMessage = Array.isArray(data) && data[0] === 'esms';
522
+ const isFeatureDetectionMessage = Array.isArray(data) && data[0] === msgTag;
517
523
  if (!isFeatureDetectionMessage) return;
518
524
  [
519
525
  ,
@@ -543,7 +549,7 @@
543
549
  supportsImportMaps && wasmInstancePhaseEnabled ?
544
550
  `${wasmSourcePhaseEnabled ? 'sp.then(s=>s?' : ''}c(b(\`import"\${b(new Uint8Array(${JSON.stringify(wasmBytes)}),'application/wasm')\}"\`))${wasmSourcePhaseEnabled ? ':false)' : ''}`
545
551
  : 'false'
546
- }]).then(a=>parent.postMessage(['esms'].concat(a),'*'))<${''}/script>`;
552
+ }]).then(a=>parent.postMessage(['${msgTag}'].concat(a),'*'))<${''}/script>`;
547
553
 
548
554
  // Safari will call onload eagerly on head injection, but we don't want the Wechat
549
555
  // path to trigger before setting srcdoc, therefore we track the timing
@@ -686,6 +692,7 @@
686
692
  if (!shimMode) throw new Error('Unsupported in polyfill mode.');
687
693
  composedImportMap = resolveAndComposeImportMap(importMapIn, baseUrl, composedImportMap);
688
694
  };
695
+ importShim$1.version = version;
689
696
 
690
697
  const registry = (importShim$1._r = {});
691
698
  // Wasm caches
@@ -697,7 +704,6 @@
697
704
  const shimModeOptions = { ...esmsInitOptions, shimMode: true };
698
705
  if (optionsScript) optionsScript.innerHTML = JSON.stringify(shimModeOptions);
699
706
  self.esmsInitOptions = shimModeOptions;
700
- defineValue(self, '_d', undefined);
701
707
 
702
708
  const loadAll = async (load, seen) => {
703
709
  seen[load.u] = 1;
@@ -816,7 +822,8 @@
816
822
  // we mock import('./x.css', { with: { type: 'css' }}) support via an inline static reexport
817
823
  // because we can't syntactically pass through to dynamic import with a second argument
818
824
  if (sourceType === 'css' || sourceType === 'json') {
819
- source = `export{default}from'${url}'with{type:"${sourceType}"}`;
825
+ // Direct reexport for hot reloading skipped due to Firefox bug https://bugzilla.mozilla.org/show_bug.cgi?id=1965620
826
+ source = `import m from'${url}'with{type:"${sourceType}"};export default m;`;
820
827
  url += '?entry';
821
828
  }
822
829
 
@@ -1117,7 +1124,11 @@
1117
1124
  s += `if(h)h.accept(m=>({${obj}}=m))`;
1118
1125
  return { r, s, t: 'wasm' };
1119
1126
  } else if (jsonContentType.test(contentType))
1120
- return { r, s: `${hotPrefix}j=${await res.text()};export{j as default};if(h)h.accept(m=>j=m.default)`, t: 'json' };
1127
+ return {
1128
+ r,
1129
+ s: `${hotPrefix}j=${await res.text()};export{j as default};if(h)h.accept(m=>j=m.default)`,
1130
+ t: 'json'
1131
+ };
1121
1132
  else if (cssContentType.test(contentType)) {
1122
1133
  return {
1123
1134
  r,