es-module-shims 2.3.0 → 2.4.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,5 +1,5 @@
1
- /** ES Module Shims 2.3.0 */
2
- (function () {
1
+ /** ES Module Shims @version 2.4.0 */
2
+ (function (exports) {
3
3
 
4
4
  let invalidate;
5
5
  const hotReload$1 = url => invalidate(new URL(url, baseUrl).href);
@@ -17,8 +17,10 @@
17
17
  if (!parents.includes(originalParent)) parents.push(originalParent);
18
18
  return toVersioned(url);
19
19
  };
20
- const hotImportHook = url => {
21
- getHotState(url).e = true;
20
+ const hotImportHook = (url, _, __, source, sourceType) => {
21
+ const hotState = getHotState(url);
22
+ hotState.e = typeof source === 'string' ? source : true;
23
+ hotState.t = sourceType;
22
24
  };
23
25
  const hotMetaHook = (metaObj, url) => {
24
26
  metaObj.hot = new Hot(url);
@@ -79,12 +81,14 @@
79
81
  A: true,
80
82
  // unload callback
81
83
  u: null,
82
- // entry point
84
+ // entry point or inline script source
83
85
  e: false,
84
86
  // hot data
85
87
  d: {},
86
88
  // parents
87
- p: []
89
+ p: [],
90
+ // source type
91
+ t: undefined
88
92
  });
89
93
 
90
94
  invalidate = (url, fromUrl, seen = []) => {
@@ -112,7 +116,15 @@
112
116
  const earlyRoots = new Set();
113
117
  for (const root of curInvalidationRoots) {
114
118
  const hotState = hotRegistry[root];
115
- importShim(toVersioned(root)).then(m => {
119
+ topLevelLoad(
120
+ toVersioned(root),
121
+ baseUrl,
122
+ defaultFetchOpts,
123
+ typeof hotState.e === 'string' ? hotState.e : undefined,
124
+ false,
125
+ undefined,
126
+ hotState.t
127
+ ).then(m => {
116
128
  if (hotState.a) {
117
129
  hotState.a.every(([d, c]) => d === null && !earlyRoots.has(c) && c(m));
118
130
  // unload should be the latest unload handler from the just loaded module
@@ -134,330 +146,332 @@
134
146
  );
135
147
  });
136
148
  }
137
- }, noop);
149
+ }, throwError);
138
150
  }
139
151
  curInvalidationRoots = new Set();
140
152
  }, hotReloadInterval);
141
153
  };
142
154
 
143
155
  return [
144
- _importHook ?
145
- (url, opts, parentUrl) => {
146
- _importHook(url, opts, parentUrl);
147
- hotImportHook(url);
148
- }
149
- : hotImportHook,
156
+ _importHook ? chain(_importHook, hotImportHook) : hotImportHook,
150
157
  _resolveHook ?
151
158
  (id, parent, defaultResolve) =>
152
159
  hotResolveHook(id, parent, (id, parent) => _resolveHook(id, parent, defaultResolve))
153
160
  : hotResolveHook,
154
- _metaHook ?
155
- (metaObj, url) => {
156
- _metaHook(metaObj, url);
157
- hotMetaHook(metaObj, url);
158
- }
159
- : hotMetaHook
161
+ _metaHook ? chain(_metaHook, hotMetaHook) : hotMetaHook
160
162
  ];
161
163
  };
162
164
 
163
- if (self.importShim) {
164
- console.info(
165
- `es-module-shims: skipping initialization as importShim was already registered by another polyfill instance`
166
- );
167
- return;
168
- }
169
-
170
- const hasDocument = typeof document !== 'undefined';
171
-
172
- const noop = () => {};
173
-
174
- const dynamicImport = (u, errUrl) => import(u);
175
-
176
- const optionsScript = hasDocument ? document.querySelector('script[type=esms-options]') : undefined;
177
-
178
- const esmsInitOptions = optionsScript ? JSON.parse(optionsScript.innerHTML) : {};
179
- Object.assign(esmsInitOptions, self.esmsInitOptions || {});
180
-
181
- // shim mode is determined on initialization, no late shim mode
182
- const shimMode =
183
- esmsInitOptions.shimMode ||
184
- (hasDocument &&
185
- document.querySelectorAll('script[type=module-shim],script[type=importmap-shim],link[rel=modulepreload-shim]')
186
- .length > 0);
187
-
188
- let importHook,
189
- resolveHook,
190
- fetchHook = fetch,
191
- metaHook = noop,
192
- tsTransform =
193
- (hasDocument && document.currentScript && document.currentScript.src.replace(/(\.\w+)?\.js$/, '-typescript.js')) ||
194
- './es-module-shims-typescript.js';
195
-
196
- const {
197
- revokeBlobURLs,
198
- noLoadEventRetriggers,
199
- enforceIntegrity,
200
- hotReload,
201
- hotReloadInterval = 100,
202
- nativePassthrough = !hotReload
203
- } = esmsInitOptions;
204
-
205
- const globalHook = name => (typeof name === 'string' ? self[name] : name);
206
-
207
- if (esmsInitOptions.onimport) importHook = globalHook(esmsInitOptions.onimport);
208
- if (esmsInitOptions.resolve) resolveHook = globalHook(esmsInitOptions.resolve);
209
- if (esmsInitOptions.fetch) fetchHook = globalHook(esmsInitOptions.fetch);
210
- if (esmsInitOptions.meta) metaHook = globalHook(esmsInitOptions.meta);
211
- if (esmsInitOptions.tsTransform) tsTransform = globalHook(esmsInitOptions.tsTransform);
212
-
213
- if (hotReload) [importHook, resolveHook, metaHook] = initHotReload();
214
-
215
- const mapOverrides = esmsInitOptions.mapOverrides;
216
-
217
- let nonce = esmsInitOptions.nonce;
218
- if (!nonce && hasDocument) {
219
- const nonceElement = document.querySelector('script[nonce]');
220
- if (nonceElement) nonce = nonceElement.nonce || nonceElement.getAttribute('nonce');
221
- }
222
-
223
- const onerror = globalHook(esmsInitOptions.onerror || noop);
224
-
225
- const enable = Array.isArray(esmsInitOptions.polyfillEnable) ? esmsInitOptions.polyfillEnable : [];
226
- const enableAll = esmsInitOptions.polyfillEnable === 'all' || enable.includes('all');
227
- const wasmInstancePhaseEnabled =
228
- enable.includes('wasm-modules') || enable.includes('wasm-module-instances') || enableAll;
229
- const wasmSourcePhaseEnabled =
230
- enable.includes('wasm-modules') || enable.includes('wasm-module-sources') || enableAll;
231
- const deferPhaseEnabled = enable.includes('import-defer') || enableAll;
232
-
233
- const onpolyfill =
234
- esmsInitOptions.onpolyfill ?
235
- globalHook(esmsInitOptions.onpolyfill)
236
- : () => {
237
- console.log(`%c^^ Module error above is polyfilled and can be ignored ^^`, 'font-weight:900;color:#391');
238
- };
239
-
240
- const baseUrl =
241
- hasDocument ?
242
- document.baseURI
243
- : `${location.protocol}//${location.host}${
244
- location.pathname.includes('/') ?
245
- location.pathname.slice(0, location.pathname.lastIndexOf('/') + 1)
246
- : location.pathname
247
- }`;
248
-
249
- const createBlob = (source, type = 'text/javascript') => URL.createObjectURL(new Blob([source], { type }));
250
- let { skip } = esmsInitOptions;
251
- if (Array.isArray(skip)) {
252
- const l = skip.map(s => new URL(s, baseUrl).href);
253
- skip = s => l.some(i => (i[i.length - 1] === '/' && s.startsWith(i)) || s === i);
254
- } else if (typeof skip === 'string') {
255
- const r = new RegExp(skip);
256
- skip = s => r.test(s);
257
- } else if (skip instanceof RegExp) {
258
- skip = s => skip.test(s);
259
- }
260
-
261
- const dispatchError = error => self.dispatchEvent(Object.assign(new Event('error'), { error }));
262
-
263
- const throwError = err => {
264
- (self.reportError || dispatchError)(err), void onerror(err);
265
- };
266
-
165
+ const hasDocument = typeof document !== 'undefined';
166
+
167
+ const noop = () => {};
168
+
169
+ const chain = (a, b) =>
170
+ function () {
171
+ a.apply(this, arguments);
172
+ b.apply(this, arguments);
173
+ };
174
+
175
+ const dynamicImport = (u, errUrl) => import(u);
176
+
177
+ const defineValue = (obj, prop, value) =>
178
+ Object.defineProperty(obj, prop, { writable: false, configurable: false, value });
179
+
180
+ const optionsScript = hasDocument ? document.querySelector('script[type=esms-options]') : undefined;
181
+
182
+ const esmsInitOptions = optionsScript ? JSON.parse(optionsScript.innerHTML) : {};
183
+ Object.assign(esmsInitOptions, self.esmsInitOptions || {});
184
+
185
+ const r$1 = esmsInitOptions.version;
186
+ if (self.importShim || (r$1 && r$1 !== "2.4.0")) {
187
+ console.info(
188
+ `es-module-shims: skipping initialization as ${r$1 ? `configured for ${r$1}` : 'another instance has already registered'}`
189
+ );
190
+ return;
191
+ }
192
+
193
+ // shim mode is determined on initialization, no late shim mode
194
+ const shimMode =
195
+ esmsInitOptions.shimMode ||
196
+ (hasDocument &&
197
+ document.querySelectorAll('script[type=module-shim],script[type=importmap-shim],link[rel=modulepreload-shim]')
198
+ .length > 0);
199
+
200
+ let importHook,
201
+ resolveHook,
202
+ fetchHook = fetch,
203
+ metaHook,
204
+ tsTransform =
205
+ esmsInitOptions.tsTransform ||
206
+ (hasDocument && document.currentScript && document.currentScript.src.replace(/(\.\w+)?\.js$/, '-typescript.js')) ||
207
+ './es-module-shims-typescript.js';
208
+
209
+ const defaultFetchOpts = { credentials: 'same-origin' };
210
+
211
+ const {
212
+ revokeBlobURLs,
213
+ noLoadEventRetriggers,
214
+ enforceIntegrity,
215
+ hotReload,
216
+ hotReloadInterval = 100,
217
+ nativePassthrough = !hotReload
218
+ } = esmsInitOptions;
219
+
220
+ const globalHook = name => (typeof name === 'string' ? self[name] : name);
221
+
222
+ if (esmsInitOptions.onimport) importHook = globalHook(esmsInitOptions.onimport);
223
+ if (esmsInitOptions.resolve) resolveHook = globalHook(esmsInitOptions.resolve);
224
+ if (esmsInitOptions.fetch) fetchHook = globalHook(esmsInitOptions.fetch);
225
+ if (esmsInitOptions.meta) metaHook = globalHook(esmsInitOptions.meta);
226
+
227
+ if (hotReload) [importHook, resolveHook, metaHook] = initHotReload();
228
+
229
+ const mapOverrides = esmsInitOptions.mapOverrides;
230
+
231
+ let nonce = esmsInitOptions.nonce;
232
+ if (!nonce && hasDocument) {
233
+ const nonceElement = document.querySelector('script[nonce]');
234
+ if (nonceElement) nonce = nonceElement.nonce || nonceElement.getAttribute('nonce');
235
+ }
236
+
237
+ const onerror = globalHook(esmsInitOptions.onerror || console.error.bind(console));
238
+
239
+ const enable = Array.isArray(esmsInitOptions.polyfillEnable) ? esmsInitOptions.polyfillEnable : [];
240
+ const enableAll = esmsInitOptions.polyfillEnable === 'all' || enable.includes('all');
241
+ const wasmInstancePhaseEnabled =
242
+ enable.includes('wasm-modules') || enable.includes('wasm-module-instances') || enableAll;
243
+ const wasmSourcePhaseEnabled =
244
+ enable.includes('wasm-modules') || enable.includes('wasm-module-sources') || enableAll;
245
+ const deferPhaseEnabled = enable.includes('import-defer') || enableAll;
246
+
247
+ const onpolyfill =
248
+ esmsInitOptions.onpolyfill ?
249
+ globalHook(esmsInitOptions.onpolyfill)
250
+ : () => {
251
+ console.log(`%c^^ Module error above is polyfilled and can be ignored ^^`, 'font-weight:900;color:#391');
252
+ };
253
+
254
+ const baseUrl =
255
+ hasDocument ?
256
+ document.baseURI
257
+ : `${location.protocol}//${location.host}${
258
+ location.pathname.includes('/') ?
259
+ location.pathname.slice(0, location.pathname.lastIndexOf('/') + 1)
260
+ : location.pathname
261
+ }`;
262
+
263
+ const createBlob = (source, type = 'text/javascript') => URL.createObjectURL(new Blob([source], { type }));
264
+ let { skip } = esmsInitOptions;
265
+ if (Array.isArray(skip)) {
266
+ const l = skip.map(s => new URL(s, baseUrl).href);
267
+ skip = s => l.some(i => (i[i.length - 1] === '/' && s.startsWith(i)) || s === i);
268
+ } else if (typeof skip === 'string') {
269
+ const r = new RegExp(skip);
270
+ skip = s => r.test(s);
271
+ } else if (skip instanceof RegExp) {
272
+ skip = s => skip.test(s);
273
+ }
274
+
275
+ const dispatchError = error => self.dispatchEvent(Object.assign(new Event('error'), { error }));
276
+
277
+ const throwError = err => {
278
+ (self.reportError || dispatchError)(err), void onerror(err);
279
+ };
280
+
267
281
  const fromParent = parent => (parent ? ` imported from ${parent}` : '');
268
282
 
269
- const backslashRegEx = /\\/g;
270
-
271
- const asURL = url => {
272
- try {
273
- if (url.indexOf(':') !== -1) return new URL(url).href;
274
- } catch (_) {}
275
- };
276
-
277
- const resolveUrl = (relUrl, parentUrl) =>
278
- resolveIfNotPlainOrUrl(relUrl, parentUrl) || asURL(relUrl) || resolveIfNotPlainOrUrl('./' + relUrl, parentUrl);
279
-
280
- const resolveIfNotPlainOrUrl = (relUrl, parentUrl) => {
281
- const hIdx = parentUrl.indexOf('#'),
282
- qIdx = parentUrl.indexOf('?');
283
- if (hIdx + qIdx > -2)
284
- parentUrl = parentUrl.slice(
285
- 0,
286
- hIdx === -1 ? qIdx
287
- : qIdx === -1 || qIdx > hIdx ? hIdx
288
- : qIdx
289
- );
290
- if (relUrl.indexOf('\\') !== -1) relUrl = relUrl.replace(backslashRegEx, '/');
291
- // protocol-relative
292
- if (relUrl[0] === '/' && relUrl[1] === '/') {
293
- return parentUrl.slice(0, parentUrl.indexOf(':') + 1) + relUrl;
294
- }
295
- // relative-url
296
- else if (
297
- (relUrl[0] === '.' &&
298
- (relUrl[1] === '/' ||
299
- (relUrl[1] === '.' && (relUrl[2] === '/' || (relUrl.length === 2 && (relUrl += '/')))) ||
300
- (relUrl.length === 1 && (relUrl += '/')))) ||
301
- relUrl[0] === '/'
302
- ) {
303
- const parentProtocol = parentUrl.slice(0, parentUrl.indexOf(':') + 1);
304
- if (parentProtocol === 'blob:') {
305
- throw new TypeError(
306
- `Failed to resolve module specifier "${relUrl}". Invalid relative url or base scheme isn't hierarchical.`
307
- );
308
- }
309
- // Disabled, but these cases will give inconsistent results for deep backtracking
310
- //if (parentUrl[parentProtocol.length] !== '/')
311
- // throw new Error('Cannot resolve');
312
- // read pathname from parent URL
313
- // pathname taken to be part after leading "/"
314
- let pathname;
315
- if (parentUrl[parentProtocol.length + 1] === '/') {
316
- // resolving to a :// so we need to read out the auth and host
317
- if (parentProtocol !== 'file:') {
318
- pathname = parentUrl.slice(parentProtocol.length + 2);
319
- pathname = pathname.slice(pathname.indexOf('/') + 1);
320
- } else {
321
- pathname = parentUrl.slice(8);
322
- }
323
- } else {
324
- // resolving to :/ so pathname is the /... part
325
- pathname = parentUrl.slice(parentProtocol.length + (parentUrl[parentProtocol.length] === '/'));
326
- }
327
-
328
- if (relUrl[0] === '/') return parentUrl.slice(0, parentUrl.length - pathname.length - 1) + relUrl;
329
-
330
- // join together and split for removal of .. and . segments
331
- // looping the string instead of anything fancy for perf reasons
332
- // '../../../../../z' resolved to 'x/y' is just 'z'
333
- const segmented = pathname.slice(0, pathname.lastIndexOf('/') + 1) + relUrl;
334
-
335
- const output = [];
336
- let segmentIndex = -1;
337
- for (let i = 0; i < segmented.length; i++) {
338
- // busy reading a segment - only terminate on '/'
339
- if (segmentIndex !== -1) {
340
- if (segmented[i] === '/') {
341
- output.push(segmented.slice(segmentIndex, i + 1));
342
- segmentIndex = -1;
343
- }
344
- continue;
345
- }
346
- // new segment - check if it is relative
347
- else if (segmented[i] === '.') {
348
- // ../ segment
349
- if (segmented[i + 1] === '.' && (segmented[i + 2] === '/' || i + 2 === segmented.length)) {
350
- output.pop();
351
- i += 2;
352
- continue;
353
- }
354
- // ./ segment
355
- else if (segmented[i + 1] === '/' || i + 1 === segmented.length) {
356
- i += 1;
357
- continue;
358
- }
359
- }
360
- // it is the start of a new segment
361
- while (segmented[i] === '/') i++;
362
- segmentIndex = i;
363
- }
364
- // finish reading out the last segment
365
- if (segmentIndex !== -1) output.push(segmented.slice(segmentIndex));
366
- return parentUrl.slice(0, parentUrl.length - pathname.length) + output.join('');
367
- }
368
- };
369
-
370
- const resolveAndComposeImportMap = (json, baseUrl, parentMap) => {
371
- const outMap = {
372
- imports: Object.assign({}, parentMap.imports),
373
- scopes: Object.assign({}, parentMap.scopes),
374
- integrity: Object.assign({}, parentMap.integrity)
375
- };
376
-
377
- if (json.imports) resolveAndComposePackages(json.imports, outMap.imports, baseUrl, parentMap);
378
-
379
- if (json.scopes)
380
- for (let s in json.scopes) {
381
- const resolvedScope = resolveUrl(s, baseUrl);
382
- resolveAndComposePackages(
383
- json.scopes[s],
384
- outMap.scopes[resolvedScope] || (outMap.scopes[resolvedScope] = {}),
385
- baseUrl,
386
- parentMap
387
- );
388
- }
389
-
390
- if (json.integrity) resolveAndComposeIntegrity(json.integrity, outMap.integrity, baseUrl);
391
-
392
- return outMap;
393
- };
394
-
395
- const getMatch = (path, matchObj) => {
396
- if (matchObj[path]) return path;
397
- let sepIndex = path.length;
398
- do {
399
- const segment = path.slice(0, sepIndex + 1);
400
- if (segment in matchObj) return segment;
401
- } while ((sepIndex = path.lastIndexOf('/', sepIndex - 1)) !== -1);
402
- };
403
-
404
- const applyPackages = (id, packages) => {
405
- const pkgName = getMatch(id, packages);
406
- if (pkgName) {
407
- const pkg = packages[pkgName];
408
- if (pkg === null) return;
409
- return pkg + id.slice(pkgName.length);
410
- }
411
- };
412
-
413
- const resolveImportMap = (importMap, resolvedOrPlain, parentUrl) => {
414
- let scopeUrl = parentUrl && getMatch(parentUrl, importMap.scopes);
415
- while (scopeUrl) {
416
- const packageResolution = applyPackages(resolvedOrPlain, importMap.scopes[scopeUrl]);
417
- if (packageResolution) return packageResolution;
418
- scopeUrl = getMatch(scopeUrl.slice(0, scopeUrl.lastIndexOf('/')), importMap.scopes);
419
- }
420
- return applyPackages(resolvedOrPlain, importMap.imports) || (resolvedOrPlain.indexOf(':') !== -1 && resolvedOrPlain);
421
- };
422
-
423
- const resolveAndComposePackages = (packages, outPackages, baseUrl, parentMap) => {
424
- for (let p in packages) {
425
- const resolvedLhs = resolveIfNotPlainOrUrl(p, baseUrl) || p;
426
- if (
427
- (!shimMode || !mapOverrides) &&
428
- outPackages[resolvedLhs] &&
429
- outPackages[resolvedLhs] !== packages[resolvedLhs]
430
- ) {
431
- console.warn(
432
- `es-module-shims: Rejected map override "${resolvedLhs}" from ${outPackages[resolvedLhs]} to ${packages[resolvedLhs]}.`
433
- );
434
- continue;
435
- }
436
- let target = packages[p];
437
- if (typeof target !== 'string') continue;
438
- const mapped = resolveImportMap(parentMap, resolveIfNotPlainOrUrl(target, baseUrl) || target, baseUrl);
439
- if (mapped) {
440
- outPackages[resolvedLhs] = mapped;
441
- continue;
442
- }
443
- console.warn(`es-module-shims: Mapping "${p}" -> "${packages[p]}" does not resolve`);
444
- }
445
- };
446
-
447
- const resolveAndComposeIntegrity = (integrity, outIntegrity, baseUrl) => {
448
- for (let p in integrity) {
449
- const resolvedLhs = resolveIfNotPlainOrUrl(p, baseUrl) || p;
450
- if (
451
- (!shimMode || !mapOverrides) &&
452
- outIntegrity[resolvedLhs] &&
453
- outIntegrity[resolvedLhs] !== integrity[resolvedLhs]
454
- ) {
455
- console.warn(
456
- `es-module-shims: Rejected map integrity override "${resolvedLhs}" from ${outIntegrity[resolvedLhs]} to ${integrity[resolvedLhs]}.`
457
- );
458
- }
459
- outIntegrity[resolvedLhs] = integrity[p];
460
- }
283
+ const backslashRegEx = /\\/g;
284
+
285
+ const asURL = url => {
286
+ try {
287
+ if (url.indexOf(':') !== -1) return new URL(url).href;
288
+ } catch (_) {}
289
+ };
290
+
291
+ const resolveUrl = (relUrl, parentUrl) =>
292
+ resolveIfNotPlainOrUrl(relUrl, parentUrl) || asURL(relUrl) || resolveIfNotPlainOrUrl('./' + relUrl, parentUrl);
293
+
294
+ const resolveIfNotPlainOrUrl = (relUrl, parentUrl) => {
295
+ const hIdx = parentUrl.indexOf('#'),
296
+ qIdx = parentUrl.indexOf('?');
297
+ if (hIdx + qIdx > -2)
298
+ parentUrl = parentUrl.slice(
299
+ 0,
300
+ hIdx === -1 ? qIdx
301
+ : qIdx === -1 || qIdx > hIdx ? hIdx
302
+ : qIdx
303
+ );
304
+ if (relUrl.indexOf('\\') !== -1) relUrl = relUrl.replace(backslashRegEx, '/');
305
+ // protocol-relative
306
+ if (relUrl[0] === '/' && relUrl[1] === '/') {
307
+ return parentUrl.slice(0, parentUrl.indexOf(':') + 1) + relUrl;
308
+ }
309
+ // relative-url
310
+ else if (
311
+ (relUrl[0] === '.' &&
312
+ (relUrl[1] === '/' ||
313
+ (relUrl[1] === '.' && (relUrl[2] === '/' || (relUrl.length === 2 && (relUrl += '/')))) ||
314
+ (relUrl.length === 1 && (relUrl += '/')))) ||
315
+ relUrl[0] === '/'
316
+ ) {
317
+ const parentProtocol = parentUrl.slice(0, parentUrl.indexOf(':') + 1);
318
+ if (parentProtocol === 'blob:') {
319
+ throw new TypeError(
320
+ `Failed to resolve module specifier "${relUrl}". Invalid relative url or base scheme isn't hierarchical.`
321
+ );
322
+ }
323
+ // Disabled, but these cases will give inconsistent results for deep backtracking
324
+ //if (parentUrl[parentProtocol.length] !== '/')
325
+ // throw new Error('Cannot resolve');
326
+ // read pathname from parent URL
327
+ // pathname taken to be part after leading "/"
328
+ let pathname;
329
+ if (parentUrl[parentProtocol.length + 1] === '/') {
330
+ // resolving to a :// so we need to read out the auth and host
331
+ if (parentProtocol !== 'file:') {
332
+ pathname = parentUrl.slice(parentProtocol.length + 2);
333
+ pathname = pathname.slice(pathname.indexOf('/') + 1);
334
+ } else {
335
+ pathname = parentUrl.slice(8);
336
+ }
337
+ } else {
338
+ // resolving to :/ so pathname is the /... part
339
+ pathname = parentUrl.slice(parentProtocol.length + (parentUrl[parentProtocol.length] === '/'));
340
+ }
341
+
342
+ if (relUrl[0] === '/') return parentUrl.slice(0, parentUrl.length - pathname.length - 1) + relUrl;
343
+
344
+ // join together and split for removal of .. and . segments
345
+ // looping the string instead of anything fancy for perf reasons
346
+ // '../../../../../z' resolved to 'x/y' is just 'z'
347
+ const segmented = pathname.slice(0, pathname.lastIndexOf('/') + 1) + relUrl;
348
+
349
+ const output = [];
350
+ let segmentIndex = -1;
351
+ for (let i = 0; i < segmented.length; i++) {
352
+ // busy reading a segment - only terminate on '/'
353
+ if (segmentIndex !== -1) {
354
+ if (segmented[i] === '/') {
355
+ output.push(segmented.slice(segmentIndex, i + 1));
356
+ segmentIndex = -1;
357
+ }
358
+ continue;
359
+ }
360
+ // new segment - check if it is relative
361
+ else if (segmented[i] === '.') {
362
+ // ../ segment
363
+ if (segmented[i + 1] === '.' && (segmented[i + 2] === '/' || i + 2 === segmented.length)) {
364
+ output.pop();
365
+ i += 2;
366
+ continue;
367
+ }
368
+ // ./ segment
369
+ else if (segmented[i + 1] === '/' || i + 1 === segmented.length) {
370
+ i += 1;
371
+ continue;
372
+ }
373
+ }
374
+ // it is the start of a new segment
375
+ while (segmented[i] === '/') i++;
376
+ segmentIndex = i;
377
+ }
378
+ // finish reading out the last segment
379
+ if (segmentIndex !== -1) output.push(segmented.slice(segmentIndex));
380
+ return parentUrl.slice(0, parentUrl.length - pathname.length) + output.join('');
381
+ }
382
+ };
383
+
384
+ const resolveAndComposeImportMap = (json, baseUrl, parentMap) => {
385
+ const outMap = {
386
+ imports: { ...parentMap.imports },
387
+ scopes: { ...parentMap.scopes },
388
+ integrity: { ...parentMap.integrity }
389
+ };
390
+
391
+ if (json.imports) resolveAndComposePackages(json.imports, outMap.imports, baseUrl, parentMap);
392
+
393
+ if (json.scopes)
394
+ for (let s in json.scopes) {
395
+ const resolvedScope = resolveUrl(s, baseUrl);
396
+ resolveAndComposePackages(
397
+ json.scopes[s],
398
+ outMap.scopes[resolvedScope] || (outMap.scopes[resolvedScope] = {}),
399
+ baseUrl,
400
+ parentMap
401
+ );
402
+ }
403
+
404
+ if (json.integrity) resolveAndComposeIntegrity(json.integrity, outMap.integrity, baseUrl);
405
+
406
+ return outMap;
407
+ };
408
+
409
+ const getMatch = (path, matchObj) => {
410
+ if (matchObj[path]) return path;
411
+ let sepIndex = path.length;
412
+ do {
413
+ const segment = path.slice(0, sepIndex + 1);
414
+ if (segment in matchObj) return segment;
415
+ } while ((sepIndex = path.lastIndexOf('/', sepIndex - 1)) !== -1);
416
+ };
417
+
418
+ const applyPackages = (id, packages) => {
419
+ const pkgName = getMatch(id, packages);
420
+ if (pkgName) {
421
+ const pkg = packages[pkgName];
422
+ if (pkg === null) return;
423
+ return pkg + id.slice(pkgName.length);
424
+ }
425
+ };
426
+
427
+ const resolveImportMap = (importMap, resolvedOrPlain, parentUrl) => {
428
+ let scopeUrl = parentUrl && getMatch(parentUrl, importMap.scopes);
429
+ while (scopeUrl) {
430
+ const packageResolution = applyPackages(resolvedOrPlain, importMap.scopes[scopeUrl]);
431
+ if (packageResolution) return packageResolution;
432
+ scopeUrl = getMatch(scopeUrl.slice(0, scopeUrl.lastIndexOf('/')), importMap.scopes);
433
+ }
434
+ return applyPackages(resolvedOrPlain, importMap.imports) || (resolvedOrPlain.indexOf(':') !== -1 && resolvedOrPlain);
435
+ };
436
+
437
+ const resolveAndComposePackages = (packages, outPackages, baseUrl, parentMap) => {
438
+ for (let p in packages) {
439
+ const resolvedLhs = resolveIfNotPlainOrUrl(p, baseUrl) || p;
440
+ if (
441
+ (!shimMode || !mapOverrides) &&
442
+ outPackages[resolvedLhs] &&
443
+ outPackages[resolvedLhs] !== packages[resolvedLhs]
444
+ ) {
445
+ console.warn(
446
+ `es-module-shims: Rejected map override "${resolvedLhs}" from ${outPackages[resolvedLhs]} to ${packages[resolvedLhs]}.`
447
+ );
448
+ continue;
449
+ }
450
+ let target = packages[p];
451
+ if (typeof target !== 'string') continue;
452
+ const mapped = resolveImportMap(parentMap, resolveIfNotPlainOrUrl(target, baseUrl) || target, baseUrl);
453
+ if (mapped) {
454
+ outPackages[resolvedLhs] = mapped;
455
+ continue;
456
+ }
457
+ console.warn(`es-module-shims: Mapping "${p}" -> "${packages[p]}" does not resolve`);
458
+ }
459
+ };
460
+
461
+ const resolveAndComposeIntegrity = (integrity, outIntegrity, baseUrl) => {
462
+ for (let p in integrity) {
463
+ const resolvedLhs = resolveIfNotPlainOrUrl(p, baseUrl) || p;
464
+ if (
465
+ (!shimMode || !mapOverrides) &&
466
+ outIntegrity[resolvedLhs] &&
467
+ outIntegrity[resolvedLhs] !== integrity[resolvedLhs]
468
+ ) {
469
+ console.warn(
470
+ `es-module-shims: Rejected map integrity override "${resolvedLhs}" from ${outIntegrity[resolvedLhs]} to ${integrity[resolvedLhs]}.`
471
+ );
472
+ }
473
+ outIntegrity[resolvedLhs] = integrity[p];
474
+ }
461
475
  };
462
476
 
463
477
  // support browsers without dynamic import support (eg Firefox 6x)
@@ -630,15 +644,7 @@
630
644
  sourceType = opts.with.type;
631
645
  }
632
646
  }
633
- return topLevelLoad(
634
- id,
635
- parentUrl || baseUrl,
636
- { credentials: 'same-origin' },
637
- undefined,
638
- undefined,
639
- undefined,
640
- sourceType
641
- );
647
+ return topLevelLoad(id, parentUrl || baseUrl, defaultFetchOpts, undefined, undefined, undefined, sourceType);
642
648
  }
643
649
 
644
650
  // import.source()
@@ -657,7 +663,7 @@
657
663
  }
658
664
  await importMapPromise;
659
665
  const url = resolve(id, parentUrl || baseUrl).r;
660
- const load = getOrCreateLoad(url, { credentials: 'same-origin' }, undefined, undefined);
666
+ const load = getOrCreateLoad(url, defaultFetchOpts, undefined, undefined);
661
667
  if (firstPolyfillLoad && !shimMode && load.n && nativelyLoaded) {
662
668
  onpolyfill();
663
669
  firstPolyfillLoad = false;
@@ -671,8 +677,6 @@
671
677
 
672
678
  if (hotReload) importShim$1.hotReload = hotReload$1;
673
679
 
674
- self.importShim = importShim$1;
675
-
676
680
  const defaultResolve = (id, parentUrl) => {
677
681
  return (
678
682
  resolveImportMap(composedImportMap, resolveIfNotPlainOrUrl(id, parentUrl) || id, parentUrl) ||
@@ -700,6 +704,13 @@
700
704
  const sourceCache = (importShim$1._s = {});
701
705
  (importShim$1._i = new WeakMap());
702
706
 
707
+ // Ensure this version is the only version
708
+ defineValue(self, 'importShim', Object.freeze(importShim$1));
709
+ const shimModeOptions = { ...esmsInitOptions, shimMode: true };
710
+ if (optionsScript) optionsScript.innerHTML = JSON.stringify(shimModeOptions);
711
+ self.esmsInitOptions = shimModeOptions;
712
+ defineValue(self, '_d', undefined);
713
+
703
714
  const loadAll = async (load, seen) => {
704
715
  seen[load.u] = 1;
705
716
  await load.L;
@@ -733,8 +744,7 @@
733
744
  if (!shimMode && typeof WebAssembly !== 'undefined') {
734
745
  if (wasmSourcePhaseEnabled && !Object.getPrototypeOf(WebAssembly.Module).name) {
735
746
  const s = Symbol();
736
- const brand = m =>
737
- Object.defineProperty(m, s, { writable: false, configurable: false, value: 'WebAssembly.Module' });
747
+ const brand = m => defineValue(m, s, 'WebAssembly.Module');
738
748
  class AbstractModuleSource {
739
749
  get [Symbol.toStringTag]() {
740
750
  if (this[s]) return this[s];
@@ -802,7 +812,15 @@
802
812
  let firstPolyfillLoad = true;
803
813
  let legacyAcceptingImportMaps = true;
804
814
 
805
- const topLevelLoad = async (url, parentUrl, fetchOpts, source, nativelyLoaded, lastStaticLoadPromise, sourceType) => {
815
+ const topLevelLoad = async (
816
+ url,
817
+ parentUrl,
818
+ fetchOpts,
819
+ source,
820
+ nativelyLoaded,
821
+ lastStaticLoadPromise,
822
+ sourceType
823
+ ) => {
806
824
  await initPromise;
807
825
  await importMapPromise;
808
826
  url = (await resolve(url, parentUrl)).r;
@@ -814,7 +832,7 @@
814
832
  url += '?entry';
815
833
  }
816
834
 
817
- if (importHook) await importHook(url, typeof fetchOpts !== 'string' ? fetchOpts : {}, parentUrl);
835
+ if (importHook) await importHook(url, typeof fetchOpts !== 'string' ? fetchOpts : {}, parentUrl, source, sourceType);
818
836
  // early analysis opt-out - no need to even fetch if we have feature support
819
837
  if (!shimMode && baselinePassthrough && nativePassthrough && sourceType !== 'ts') {
820
838
  console.info(`es-module-shims: early exit for ${url} due to baseline modules support`);
@@ -1088,7 +1106,7 @@
1088
1106
  const mapIntegrity = composedImportMap.integrity[url];
1089
1107
  const res = await doFetch(
1090
1108
  url,
1091
- mapIntegrity && !fetchOpts.integrity ? Object.assign({}, fetchOpts, { integrity: mapIntegrity }) : fetchOpts,
1109
+ mapIntegrity && !fetchOpts.integrity ? { ...fetchOpts, integrity: mapIntegrity } : fetchOpts,
1092
1110
  parent
1093
1111
  );
1094
1112
  const r = res.url;
@@ -1155,8 +1173,8 @@
1155
1173
  const getOrCreateLoad = (url, fetchOpts, parent, source) => {
1156
1174
  if (source && registry[url]) {
1157
1175
  let i = 0;
1158
- while (registry[url + '?' + ++i]);
1159
- url += '?' + i;
1176
+ while (registry[url + '#' + ++i]);
1177
+ url += '#' + i;
1160
1178
  }
1161
1179
  let load = registry[url];
1162
1180
  if (load) return load;
@@ -1243,7 +1261,7 @@
1243
1261
  if (d >= 0 || resolved.N) load.N = true;
1244
1262
  if (d !== -1) return;
1245
1263
  if (skip && skip(resolved.r) && !sourcePhase) return { l: { b: resolved.r }, s: false };
1246
- if (childFetchOpts.integrity) childFetchOpts = Object.assign({}, childFetchOpts, { integrity: undefined });
1264
+ if (childFetchOpts.integrity) childFetchOpts = { ...childFetchOpts, integrity: undefined };
1247
1265
  const child = { l: getOrCreateLoad(resolved.r, childFetchOpts, load.r, source), s: sourcePhase };
1248
1266
  // assertion case -> inline the CSS / JSON URL directly
1249
1267
  if (source === '') child.l.b = child.l.u;
@@ -1421,7 +1439,7 @@
1421
1439
  !script.src ? script.innerHTML : undefined,
1422
1440
  !shimMode,
1423
1441
  isBlockingReadyScript && lastStaticLoadPromise,
1424
- 'ts'
1442
+ ts ? 'ts' : undefined
1425
1443
  ).catch(throwError);
1426
1444
  }
1427
1445
  if (!noLoadEventRetriggers) loadPromise.then(() => script.dispatchEvent(new Event('load')));
@@ -1439,4 +1457,10 @@
1439
1457
  fetchCache[link.href] = fetchModule(link.href, getFetchOpts(link));
1440
1458
  };
1441
1459
 
1442
- })();
1460
+ exports.topLevelLoad = topLevelLoad;
1461
+
1462
+ Object.defineProperty(exports, '__esModule', { value: true });
1463
+
1464
+ return exports;
1465
+
1466
+ })({});