es-module-shims 2.6.1 → 2.6.2

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.
package/README.md CHANGED
@@ -31,7 +31,7 @@ Because we are still using the native module loader the edge cases work out comp
31
31
  Include ES Module Shims with a `async` attribute on the script, then include an import map and module scripts normally:
32
32
 
33
33
  ```html
34
- <script async src="https://ga.jspm.io/npm:es-module-shims@2.6.1/dist/es-module-shims.js"></script>
34
+ <script async src="https://ga.jspm.io/npm:es-module-shims@2.6.2/dist/es-module-shims.js"></script>
35
35
 
36
36
  <!-- https://generator.jspm.io/#U2NhYGBkDM0rySzJSU1hKEpNTC5xMLTQM9Az0C1K1jMAAKFS5w0gAA -->
37
37
  <script type="importmap">
@@ -1,9 +1,11 @@
1
- /** ES Module Shims @version 2.6.1 */
1
+ /** ES Module Shims @version 2.6.2 */
2
2
  (function () {
3
3
 
4
+ const self_ = typeof globalThis !== 'undefined' ? globalThis : self;
5
+
4
6
  let invalidate;
5
7
  const hotReload$1 = url => invalidate(new URL(url, baseUrl).href);
6
- const initHotReload = () => {
8
+ const initHotReload = (topLevelLoad, importShim) => {
7
9
  let _importHook = importHook,
8
10
  _resolveHook = resolveHook,
9
11
  _metaHook = metaHook;
@@ -149,21 +151,27 @@
149
151
  curInvalidationRoots = new Set();
150
152
  };
151
153
 
152
- return [
154
+ setHooks(
153
155
  _importHook ? chain(_importHook, hotImportHook) : hotImportHook,
154
156
  _resolveHook ?
155
157
  (id, parent, defaultResolve) =>
156
158
  hotResolveHook(id, parent, (id, parent) => _resolveHook(id, parent, defaultResolve))
157
159
  : hotResolveHook,
158
160
  _metaHook ? chain(_metaHook, hotMetaHook) : hotMetaHook
159
- ];
161
+ );
160
162
  };
161
163
 
162
164
  const hasDocument = typeof document !== 'undefined';
163
165
 
164
166
  const noop = () => {};
165
167
 
166
- const dynamicImport = (u, errUrl) => import(u);
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);
167
175
 
168
176
  const defineValue = (obj, prop, value) =>
169
177
  Object.defineProperty(obj, prop, { writable: false, configurable: false, value });
@@ -171,12 +179,12 @@
171
179
  const optionsScript = hasDocument ? document.querySelector('script[type=esms-options]') : undefined;
172
180
 
173
181
  const esmsInitOptions = optionsScript ? JSON.parse(optionsScript.innerHTML) : {};
174
- Object.assign(esmsInitOptions, self.esmsInitOptions || {});
182
+ Object.assign(esmsInitOptions, self_.esmsInitOptions || {});
175
183
 
176
- const version = "2.6.1";
184
+ const version = "2.6.2";
177
185
 
178
186
  const r$1 = esmsInitOptions.version;
179
- if (self.importShim || (r$1 && r$1 !== version)) {
187
+ if (self_.importShim || (r$1 && r$1 !== version)) {
180
188
  console.info(
181
189
  `es-module-shims: skipping initialization as ${r$1 ? `configured for ${r$1}` : 'another instance has already registered'}`
182
190
  );
@@ -186,9 +194,11 @@
186
194
  // shim mode is determined on initialization, no late shim mode
187
195
  const shimMode =
188
196
  esmsInitOptions.shimMode ||
189
- (hasDocument &&
197
+ (hasDocument ?
190
198
  document.querySelectorAll('script[type=module-shim],script[type=importmap-shim],link[rel=modulepreload-shim]')
191
- .length > 0);
199
+ .length > 0
200
+ // Without a document, shim mode is always true as we cannot polyfill
201
+ : true);
192
202
 
193
203
  let importHook,
194
204
  resolveHook,
@@ -202,7 +212,7 @@
202
212
 
203
213
  const defaultFetchOpts = { credentials: 'same-origin' };
204
214
 
205
- const globalHook = name => (typeof name === 'string' ? self[name] : name);
215
+ const globalHook = name => (typeof name === 'string' ? self_[name] : name);
206
216
 
207
217
  if (esmsInitOptions.onimport) importHook = globalHook(esmsInitOptions.onimport);
208
218
  if (esmsInitOptions.resolve) resolveHook = globalHook(esmsInitOptions.resolve);
@@ -220,7 +230,11 @@
220
230
  nativePassthrough = !hasCustomizationHooks && !hotReload
221
231
  } = esmsInitOptions;
222
232
 
223
- if (hotReload) [importHook, resolveHook, metaHook] = initHotReload();
233
+ const setHooks = (importHook_, resolveHook_, metaHook_) => (
234
+ (importHook = importHook_),
235
+ (resolveHook = resolveHook_),
236
+ (metaHook = metaHook_)
237
+ );
224
238
 
225
239
  const mapOverrides = esmsInitOptions.mapOverrides;
226
240
 
@@ -252,13 +266,14 @@
252
266
  };
253
267
 
254
268
  const baseUrl =
255
- hasDocument ?
256
- document.baseURI
257
- : `${location.protocol}//${location.host}${
269
+ hasDocument ? document.baseURI
270
+ : typeof location !== 'undefined' ?
271
+ `${location.protocol}//${location.host}${
258
272
  location.pathname.includes('/') ?
259
273
  location.pathname.slice(0, location.pathname.lastIndexOf('/') + 1)
260
274
  : location.pathname
261
- }`;
275
+ }`
276
+ : 'about:blank';
262
277
 
263
278
  const createBlob = (source, type = 'text/javascript') => URL.createObjectURL(new Blob([source], { type }));
264
279
  let { skip } = esmsInitOptions;
@@ -272,10 +287,10 @@
272
287
  skip = s => skip.test(s);
273
288
  }
274
289
 
275
- const dispatchError = error => self.dispatchEvent(Object.assign(new Event('error'), { error }));
290
+ const dispatchError = error => self_.dispatchEvent(Object.assign(new Event('error'), { error }));
276
291
 
277
292
  const throwError = err => {
278
- (self.reportError || dispatchError)(err);
293
+ (self_.reportError || dispatchError)(err);
279
294
  onerror(err);
280
295
  };
281
296
 
@@ -630,7 +645,7 @@
630
645
  };
631
646
 
632
647
  // import()
633
- async function importShim$1(id, opts, parentUrl) {
648
+ async function importShim(id, opts, parentUrl) {
634
649
  if (typeof opts === 'string') {
635
650
  parentUrl = opts;
636
651
  opts = undefined;
@@ -654,7 +669,7 @@
654
669
  // import.source()
655
670
  // (opts not currently supported as no use cases yet)
656
671
  if (shimMode || wasmSourcePhaseEnabled)
657
- importShim$1.source = async (id, opts, parentUrl) => {
672
+ importShim.source = async (id, opts, parentUrl) => {
658
673
  if (typeof opts === 'string') {
659
674
  parentUrl = opts;
660
675
  opts = undefined;
@@ -668,18 +683,17 @@
668
683
  await importMapPromise;
669
684
  const url = resolve(id, parentUrl || baseUrl).r;
670
685
  const load = getOrCreateLoad(url, defaultFetchOpts, undefined, undefined);
671
- if (firstPolyfillLoad && !shimMode && load.n && nativelyLoaded) {
672
- onpolyfill();
673
- firstPolyfillLoad = false;
674
- }
675
686
  await load.f;
676
- return importShim$1._s[load.r];
687
+ return importShim._s[load.r];
677
688
  };
678
689
 
679
690
  // import.defer() is just a proxy for import(), since we can't actually defer
680
- if (shimMode || deferPhaseEnabled) importShim$1.defer = importShim$1;
691
+ if (shimMode || deferPhaseEnabled) importShim.defer = importShim;
681
692
 
682
- if (hotReload) importShim$1.hotReload = hotReload$1;
693
+ if (hotReload) {
694
+ initHotReload(topLevelLoad, importShim);
695
+ importShim.hotReload = hotReload$1;
696
+ }
683
697
 
684
698
  const defaultResolve = (id, parentUrl) => {
685
699
  return (
@@ -696,24 +710,24 @@
696
710
  return resolve(id, `${parentUrl}`).r;
697
711
  };
698
712
 
699
- importShim$1.resolve = (id, parentUrl) => resolve(id, parentUrl).r;
700
- importShim$1.getImportMap = () => JSON.parse(JSON.stringify(composedImportMap));
701
- importShim$1.addImportMap = importMapIn => {
713
+ importShim.resolve = (id, parentUrl) => resolve(id, parentUrl).r;
714
+ importShim.getImportMap = () => JSON.parse(JSON.stringify(composedImportMap));
715
+ importShim.addImportMap = importMapIn => {
702
716
  if (!shimMode) throw new Error('Unsupported in polyfill mode.');
703
717
  composedImportMap = resolveAndComposeImportMap(importMapIn, baseUrl, composedImportMap);
704
718
  };
705
- importShim$1.version = version;
719
+ importShim.version = version;
706
720
 
707
- const registry = (importShim$1._r = {});
721
+ const registry = (importShim._r = {});
708
722
  // Wasm caches
709
- const sourceCache = (importShim$1._s = {});
710
- (importShim$1._i = new WeakMap());
723
+ const sourceCache = (importShim._s = {});
724
+ /* const instanceCache = */ importShim._i = new WeakMap();
711
725
 
712
726
  // Ensure this version is the only version
713
- defineValue(self, 'importShim', Object.freeze(importShim$1));
727
+ defineValue(self_, 'importShim', Object.freeze(importShim));
714
728
  const shimModeOptions = { ...esmsInitOptions, shimMode: true };
715
729
  if (optionsScript) optionsScript.innerHTML = JSON.stringify(shimModeOptions);
716
- self.esmsInitOptions = shimModeOptions;
730
+ self_.esmsInitOptions = shimModeOptions;
717
731
 
718
732
  const loadAll = async (load, seen) => {
719
733
  seen[load.u] = 1;
@@ -816,7 +830,7 @@
816
830
  let firstPolyfillLoad = true;
817
831
  let legacyAcceptingImportMaps = true;
818
832
 
819
- const topLevelLoad = async (
833
+ async function topLevelLoad(
820
834
  url,
821
835
  parentUrl,
822
836
  fetchOpts,
@@ -824,7 +838,7 @@
824
838
  nativelyLoaded,
825
839
  lastStaticLoadPromise,
826
840
  sourceType
827
- ) => {
841
+ ) {
828
842
  await initPromise;
829
843
  await importMapPromise;
830
844
  url = (await resolve(url, parentUrl)).r;
@@ -874,11 +888,11 @@
874
888
  if (load.s) (await dynamicImport(load.s, load.u)).u$_(module);
875
889
  revokeObjectURLs(Object.keys(seen));
876
890
  return module;
877
- };
891
+ }
878
892
 
879
893
  const revokeObjectURLs = registryKeys => {
880
894
  let curIdx = 0;
881
- const handler = self.requestIdleCallback || self.requestAnimationFrame || (fn => setTimeout(fn, 0));
895
+ const handler = self_.requestIdleCallback || self_.requestAnimationFrame || (fn => setTimeout(fn, 0));
882
896
  handler(cleanup);
883
897
  function cleanup() {
884
898
  for (const key of registryKeys.slice(curIdx, (curIdx += 100))) {
@@ -1075,10 +1089,11 @@
1075
1089
 
1076
1090
  const doFetch = async (url, fetchOpts, parent) => {
1077
1091
  if (enforceIntegrity && !fetchOpts.integrity) throw Error(`No integrity for ${url}${fromParent(parent)}.`);
1078
- const poolQueue = pushFetchPool();
1092
+ let res,
1093
+ poolQueue = pushFetchPool();
1079
1094
  if (poolQueue) await poolQueue;
1080
1095
  try {
1081
- var res = await fetchHook(url, fetchOpts);
1096
+ res = await fetchHook(url, fetchOpts);
1082
1097
  } catch (e) {
1083
1098
  e.message = `Unable to fetch ${url}${fromParent(parent)} - see network log for details.\n` + e.message;
1084
1099
  throw e;
@@ -1100,24 +1115,22 @@
1100
1115
  if (!esmsTsTransform) esmsTsTransform = m.transform;
1101
1116
  };
1102
1117
 
1103
- const contentTypeRegEx = /^(text|application)\/((x-)?javascript|wasm|json|css|typescript)(;|$)/;
1104
1118
  async function defaultSourceHook(url, fetchOpts, parent) {
1105
- const res = await doFetch(url, fetchOpts, parent);
1106
- let [, , t] = (res.headers.get('content-type') || '').match(contentTypeRegEx) || [];
1107
- if (!t) {
1108
- if (url.endsWith('.ts') || url.endsWith('.mts')) t = 'ts';
1109
- else
1110
- throw Error(
1111
- `Unsupported Content-Type "${contentType}" loading ${url}${fromParent(parent)}. Modules must be served with a valid MIME type like application/javascript.`
1112
- );
1119
+ let res = await doFetch(url, fetchOpts, parent),
1120
+ contentType,
1121
+ [, json, type, jsts] =
1122
+ (contentType = res.headers.get('content-type') || '').match(
1123
+ /^(?:[^/;]+\/(?:[^/+;]+\+)?(json)|(?:text|application)\/(?:x-)?((java|type)script|wasm|css))(?:;|$)/
1124
+ ) || [];
1125
+ if (!(type = json || (jsts ? jsts[0] + 's' : type || (/\.m?ts(\?|#|$)/.test(url) && 'ts')))) {
1126
+ throw Error(
1127
+ `Unsupported Content-Type "${contentType}" loading ${url}${fromParent(parent)}. Modules must be served with a valid MIME type like application/javascript.`
1128
+ );
1113
1129
  }
1114
1130
  return {
1115
1131
  url: res.url,
1116
- source: t === 'wasm' ? await WebAssembly.compileStreaming(res) : await res.text(),
1117
- type:
1118
- t[0] === 'x' || (t[0] === 'j' && t[1] === 'a') ? 'js'
1119
- : t[0] === 't' ? 'ts'
1120
- : t
1132
+ source: await (type > 'v' ? WebAssembly.compileStreaming(res) : res.text()),
1133
+ type
1121
1134
  };
1122
1135
  }
1123
1136
 
@@ -1151,7 +1164,7 @@
1151
1164
  }
1152
1165
  source += `if(h)h.accept(m=>({${obj}}=m))`;
1153
1166
  } else if (type === 'json') {
1154
- source = `${hotPrefix}j=${source};export{j as default};if(h)h.accept(m=>j=m.default)`;
1167
+ source = `${hotPrefix}j=JSON.parse(${JSON.stringify(source)});export{j as default};if(h)h.accept(m=>j=m.default)`;
1155
1168
  } else if (type === 'css') {
1156
1169
  source = `${hotPrefix}s=h&&h.data.s||new CSSStyleSheet();s.replaceSync(${JSON.stringify(
1157
1170
  source.replace(
@@ -1240,7 +1253,7 @@
1240
1253
 
1241
1254
  const linkLoad = (load, fetchOpts) => {
1242
1255
  if (load.L) return;
1243
- load.L = load.f.then(async () => {
1256
+ load.L = load.f.then(() => {
1244
1257
  let childFetchOpts = fetchOpts;
1245
1258
  load.d = load.a[0]
1246
1259
  .map(({ n, d, t, a, se }) => {
@@ -1,9 +1,11 @@
1
- /** ES Module Shims @version 2.6.1 */
1
+ /** ES Module Shims @version 2.6.2 */
2
2
  (function () {
3
3
 
4
+ const self_ = typeof globalThis !== 'undefined' ? globalThis : self;
5
+
4
6
  let invalidate;
5
7
  const hotReload$1 = url => invalidate(new URL(url, baseUrl).href);
6
- const initHotReload = () => {
8
+ const initHotReload = (topLevelLoad, importShim) => {
7
9
  let _importHook = importHook,
8
10
  _resolveHook = resolveHook,
9
11
  _metaHook = metaHook;
@@ -148,21 +150,27 @@
148
150
  curInvalidationRoots = new Set();
149
151
  };
150
152
 
151
- return [
153
+ setHooks(
152
154
  _importHook ? chain(_importHook, hotImportHook) : hotImportHook,
153
155
  _resolveHook ?
154
156
  (id, parent, defaultResolve) =>
155
157
  hotResolveHook(id, parent, (id, parent) => _resolveHook(id, parent, defaultResolve))
156
158
  : hotResolveHook,
157
159
  _metaHook ? chain(_metaHook, hotMetaHook) : hotMetaHook
158
- ];
160
+ );
159
161
  };
160
162
 
161
163
  const hasDocument = typeof document !== 'undefined';
162
164
 
163
165
  const noop = () => {};
164
166
 
165
- const dynamicImport = (u, errUrl) => import(u);
167
+ const chain = (a, b) =>
168
+ function () {
169
+ a.apply(this, arguments);
170
+ b.apply(this, arguments);
171
+ };
172
+
173
+ const dynamicImport = (u, _errUrl) => import(u);
166
174
 
167
175
  const defineValue = (obj, prop, value) =>
168
176
  Object.defineProperty(obj, prop, { writable: false, configurable: false, value });
@@ -170,21 +178,23 @@
170
178
  const optionsScript = hasDocument ? document.querySelector('script[type=esms-options]') : undefined;
171
179
 
172
180
  const esmsInitOptions = optionsScript ? JSON.parse(optionsScript.innerHTML) : {};
173
- Object.assign(esmsInitOptions, self.esmsInitOptions || {});
181
+ Object.assign(esmsInitOptions, self_.esmsInitOptions || {});
174
182
 
175
- const version = "2.6.1";
183
+ const version = "2.6.2";
176
184
 
177
185
  const r$1 = esmsInitOptions.version;
178
- if (self.importShim || (r$1 && r$1 !== version)) {
186
+ if (self_.importShim || (r$1 && r$1 !== version)) {
179
187
  return;
180
188
  }
181
189
 
182
190
  // shim mode is determined on initialization, no late shim mode
183
191
  const shimMode =
184
192
  esmsInitOptions.shimMode ||
185
- (hasDocument &&
193
+ (hasDocument ?
186
194
  document.querySelectorAll('script[type=module-shim],script[type=importmap-shim],link[rel=modulepreload-shim]')
187
- .length > 0);
195
+ .length > 0
196
+ // Without a document, shim mode is always true as we cannot polyfill
197
+ : true);
188
198
 
189
199
  let importHook,
190
200
  resolveHook,
@@ -198,7 +208,7 @@
198
208
 
199
209
  const defaultFetchOpts = { credentials: 'same-origin' };
200
210
 
201
- const globalHook = name => (typeof name === 'string' ? self[name] : name);
211
+ const globalHook = name => (typeof name === 'string' ? self_[name] : name);
202
212
 
203
213
  if (esmsInitOptions.onimport) importHook = globalHook(esmsInitOptions.onimport);
204
214
  if (esmsInitOptions.resolve) resolveHook = globalHook(esmsInitOptions.resolve);
@@ -216,7 +226,11 @@
216
226
  nativePassthrough = !hasCustomizationHooks && !hotReload
217
227
  } = esmsInitOptions;
218
228
 
219
- if (hotReload) [importHook, resolveHook, metaHook] = initHotReload();
229
+ const setHooks = (importHook_, resolveHook_, metaHook_) => (
230
+ (importHook = importHook_),
231
+ (resolveHook = resolveHook_),
232
+ (metaHook = metaHook_)
233
+ );
220
234
 
221
235
  const mapOverrides = esmsInitOptions.mapOverrides;
222
236
 
@@ -248,13 +262,14 @@
248
262
  };
249
263
 
250
264
  const baseUrl =
251
- hasDocument ?
252
- document.baseURI
253
- : `${location.protocol}//${location.host}${
265
+ hasDocument ? document.baseURI
266
+ : typeof location !== 'undefined' ?
267
+ `${location.protocol}//${location.host}${
254
268
  location.pathname.includes('/') ?
255
269
  location.pathname.slice(0, location.pathname.lastIndexOf('/') + 1)
256
270
  : location.pathname
257
- }`;
271
+ }`
272
+ : 'about:blank';
258
273
 
259
274
  const createBlob = (source, type = 'text/javascript') => URL.createObjectURL(new Blob([source], { type }));
260
275
  let { skip } = esmsInitOptions;
@@ -268,10 +283,10 @@
268
283
  skip = s => skip.test(s);
269
284
  }
270
285
 
271
- const dispatchError = error => self.dispatchEvent(Object.assign(new Event('error'), { error }));
286
+ const dispatchError = error => self_.dispatchEvent(Object.assign(new Event('error'), { error }));
272
287
 
273
288
  const throwError = err => {
274
- (self.reportError || dispatchError)(err);
289
+ (self_.reportError || dispatchError)(err);
275
290
  onerror(err);
276
291
  };
277
292
 
@@ -620,7 +635,7 @@
620
635
  };
621
636
 
622
637
  // import()
623
- async function importShim$1(id, opts, parentUrl) {
638
+ async function importShim(id, opts, parentUrl) {
624
639
  if (typeof opts === 'string') {
625
640
  parentUrl = opts;
626
641
  opts = undefined;
@@ -643,7 +658,7 @@
643
658
  // import.source()
644
659
  // (opts not currently supported as no use cases yet)
645
660
  if (shimMode || wasmSourcePhaseEnabled)
646
- importShim$1.source = async (id, opts, parentUrl) => {
661
+ importShim.source = async (id, opts, parentUrl) => {
647
662
  if (typeof opts === 'string') {
648
663
  parentUrl = opts;
649
664
  opts = undefined;
@@ -656,18 +671,17 @@
656
671
  await importMapPromise;
657
672
  const url = resolve(id, parentUrl || baseUrl).r;
658
673
  const load = getOrCreateLoad(url, defaultFetchOpts, undefined, undefined);
659
- if (firstPolyfillLoad && !shimMode && load.n && nativelyLoaded) {
660
- onpolyfill();
661
- firstPolyfillLoad = false;
662
- }
663
674
  await load.f;
664
- return importShim$1._s[load.r];
675
+ return importShim._s[load.r];
665
676
  };
666
677
 
667
678
  // import.defer() is just a proxy for import(), since we can't actually defer
668
- if (shimMode || deferPhaseEnabled) importShim$1.defer = importShim$1;
679
+ if (shimMode || deferPhaseEnabled) importShim.defer = importShim;
669
680
 
670
- if (hotReload) importShim$1.hotReload = hotReload$1;
681
+ if (hotReload) {
682
+ initHotReload(topLevelLoad, importShim);
683
+ importShim.hotReload = hotReload$1;
684
+ }
671
685
 
672
686
  const defaultResolve = (id, parentUrl) => {
673
687
  return (
@@ -684,24 +698,24 @@
684
698
  return resolve(id, `${parentUrl}`).r;
685
699
  };
686
700
 
687
- importShim$1.resolve = (id, parentUrl) => resolve(id, parentUrl).r;
688
- importShim$1.getImportMap = () => JSON.parse(JSON.stringify(composedImportMap));
689
- importShim$1.addImportMap = importMapIn => {
701
+ importShim.resolve = (id, parentUrl) => resolve(id, parentUrl).r;
702
+ importShim.getImportMap = () => JSON.parse(JSON.stringify(composedImportMap));
703
+ importShim.addImportMap = importMapIn => {
690
704
  if (!shimMode) throw new Error('Unsupported in polyfill mode.');
691
705
  composedImportMap = resolveAndComposeImportMap(importMapIn, baseUrl, composedImportMap);
692
706
  };
693
- importShim$1.version = version;
707
+ importShim.version = version;
694
708
 
695
- const registry = (importShim$1._r = {});
709
+ const registry = (importShim._r = {});
696
710
  // Wasm caches
697
- const sourceCache = (importShim$1._s = {});
698
- (importShim$1._i = new WeakMap());
711
+ const sourceCache = (importShim._s = {});
712
+ /* const instanceCache = */ importShim._i = new WeakMap();
699
713
 
700
714
  // Ensure this version is the only version
701
- defineValue(self, 'importShim', Object.freeze(importShim$1));
715
+ defineValue(self_, 'importShim', Object.freeze(importShim));
702
716
  const shimModeOptions = { ...esmsInitOptions, shimMode: true };
703
717
  if (optionsScript) optionsScript.innerHTML = JSON.stringify(shimModeOptions);
704
- self.esmsInitOptions = shimModeOptions;
718
+ self_.esmsInitOptions = shimModeOptions;
705
719
 
706
720
  const loadAll = async (load, seen) => {
707
721
  seen[load.u] = 1;
@@ -804,7 +818,7 @@
804
818
  let firstPolyfillLoad = true;
805
819
  let legacyAcceptingImportMaps = true;
806
820
 
807
- const topLevelLoad = async (
821
+ async function topLevelLoad(
808
822
  url,
809
823
  parentUrl,
810
824
  fetchOpts,
@@ -812,7 +826,7 @@
812
826
  nativelyLoaded,
813
827
  lastStaticLoadPromise,
814
828
  sourceType
815
- ) => {
829
+ ) {
816
830
  await initPromise;
817
831
  await importMapPromise;
818
832
  url = (await resolve(url, parentUrl)).r;
@@ -858,11 +872,11 @@
858
872
  if (load.s) (await dynamicImport(load.s, load.u)).u$_(module);
859
873
  revokeObjectURLs(Object.keys(seen));
860
874
  return module;
861
- };
875
+ }
862
876
 
863
877
  const revokeObjectURLs = registryKeys => {
864
878
  let curIdx = 0;
865
- const handler = self.requestIdleCallback || self.requestAnimationFrame || (fn => setTimeout(fn, 0));
879
+ const handler = self_.requestIdleCallback || self_.requestAnimationFrame || (fn => setTimeout(fn, 0));
866
880
  handler(cleanup);
867
881
  function cleanup() {
868
882
  for (const key of registryKeys.slice(curIdx, (curIdx += 100))) {
@@ -1057,10 +1071,11 @@
1057
1071
 
1058
1072
  const doFetch = async (url, fetchOpts, parent) => {
1059
1073
  if (enforceIntegrity && !fetchOpts.integrity) throw Error(`No integrity for ${url}${fromParent(parent)}.`);
1060
- const poolQueue = pushFetchPool();
1074
+ let res,
1075
+ poolQueue = pushFetchPool();
1061
1076
  if (poolQueue) await poolQueue;
1062
1077
  try {
1063
- var res = await fetchHook(url, fetchOpts);
1078
+ res = await fetchHook(url, fetchOpts);
1064
1079
  } catch (e) {
1065
1080
  e.message = `Unable to fetch ${url}${fromParent(parent)} - see network log for details.\n` + e.message;
1066
1081
  throw e;
@@ -1082,24 +1097,22 @@
1082
1097
  if (!esmsTsTransform) esmsTsTransform = m.transform;
1083
1098
  };
1084
1099
 
1085
- const contentTypeRegEx = /^(text|application)\/((x-)?javascript|wasm|json|css|typescript)(;|$)/;
1086
1100
  async function defaultSourceHook(url, fetchOpts, parent) {
1087
- const res = await doFetch(url, fetchOpts, parent);
1088
- let [, , t] = (res.headers.get('content-type') || '').match(contentTypeRegEx) || [];
1089
- if (!t) {
1090
- if (url.endsWith('.ts') || url.endsWith('.mts')) t = 'ts';
1091
- else
1092
- throw Error(
1093
- `Unsupported Content-Type "${contentType}" loading ${url}${fromParent(parent)}. Modules must be served with a valid MIME type like application/javascript.`
1094
- );
1101
+ let res = await doFetch(url, fetchOpts, parent),
1102
+ contentType,
1103
+ [, json, type, jsts] =
1104
+ (contentType = res.headers.get('content-type') || '').match(
1105
+ /^(?:[^/;]+\/(?:[^/+;]+\+)?(json)|(?:text|application)\/(?:x-)?((java|type)script|wasm|css))(?:;|$)/
1106
+ ) || [];
1107
+ if (!(type = json || (jsts ? jsts[0] + 's' : type || (/\.m?ts(\?|#|$)/.test(url) && 'ts')))) {
1108
+ throw Error(
1109
+ `Unsupported Content-Type "${contentType}" loading ${url}${fromParent(parent)}. Modules must be served with a valid MIME type like application/javascript.`
1110
+ );
1095
1111
  }
1096
1112
  return {
1097
1113
  url: res.url,
1098
- source: t === 'wasm' ? await WebAssembly.compileStreaming(res) : await res.text(),
1099
- type:
1100
- t[0] === 'x' || (t[0] === 'j' && t[1] === 'a') ? 'js'
1101
- : t[0] === 't' ? 'ts'
1102
- : t
1114
+ source: await (type > 'v' ? WebAssembly.compileStreaming(res) : res.text()),
1115
+ type
1103
1116
  };
1104
1117
  }
1105
1118
 
@@ -1133,7 +1146,7 @@
1133
1146
  }
1134
1147
  source += `if(h)h.accept(m=>({${obj}}=m))`;
1135
1148
  } else if (type === 'json') {
1136
- source = `${hotPrefix}j=${source};export{j as default};if(h)h.accept(m=>j=m.default)`;
1149
+ source = `${hotPrefix}j=JSON.parse(${JSON.stringify(source)});export{j as default};if(h)h.accept(m=>j=m.default)`;
1137
1150
  } else if (type === 'css') {
1138
1151
  source = `${hotPrefix}s=h&&h.data.s||new CSSStyleSheet();s.replaceSync(${JSON.stringify(
1139
1152
  source.replace(
@@ -1221,7 +1234,7 @@
1221
1234
 
1222
1235
  const linkLoad = (load, fetchOpts) => {
1223
1236
  if (load.L) return;
1224
- load.L = load.f.then(async () => {
1237
+ load.L = load.f.then(() => {
1225
1238
  let childFetchOpts = fetchOpts;
1226
1239
  load.d = load.a[0]
1227
1240
  .map(({ n, d, t, a, se }) => {
@@ -1,9 +1,11 @@
1
- /** ES Module Shims Wasm @version 2.6.1 */
1
+ /** ES Module Shims Wasm @version 2.6.2 */
2
2
  (function () {
3
3
 
4
+ const self_ = typeof globalThis !== 'undefined' ? globalThis : self;
5
+
4
6
  let invalidate;
5
7
  const hotReload$1 = url => invalidate(new URL(url, baseUrl).href);
6
- const initHotReload = () => {
8
+ const initHotReload = (topLevelLoad, importShim) => {
7
9
  let _importHook = importHook,
8
10
  _resolveHook = resolveHook,
9
11
  _metaHook = metaHook;
@@ -148,21 +150,27 @@
148
150
  curInvalidationRoots = new Set();
149
151
  };
150
152
 
151
- return [
153
+ setHooks(
152
154
  _importHook ? chain(_importHook, hotImportHook) : hotImportHook,
153
155
  _resolveHook ?
154
156
  (id, parent, defaultResolve) =>
155
157
  hotResolveHook(id, parent, (id, parent) => _resolveHook(id, parent, defaultResolve))
156
158
  : hotResolveHook,
157
159
  _metaHook ? chain(_metaHook, hotMetaHook) : hotMetaHook
158
- ];
160
+ );
159
161
  };
160
162
 
161
163
  const hasDocument = typeof document !== 'undefined';
162
164
 
163
165
  const noop = () => {};
164
166
 
165
- const dynamicImport = (u, errUrl) => import(u);
167
+ const chain = (a, b) =>
168
+ function () {
169
+ a.apply(this, arguments);
170
+ b.apply(this, arguments);
171
+ };
172
+
173
+ const dynamicImport = (u, _errUrl) => import(u);
166
174
 
167
175
  const defineValue = (obj, prop, value) =>
168
176
  Object.defineProperty(obj, prop, { writable: false, configurable: false, value });
@@ -170,21 +178,23 @@
170
178
  const optionsScript = hasDocument ? document.querySelector('script[type=esms-options]') : undefined;
171
179
 
172
180
  const esmsInitOptions = optionsScript ? JSON.parse(optionsScript.innerHTML) : {};
173
- Object.assign(esmsInitOptions, self.esmsInitOptions || {});
181
+ Object.assign(esmsInitOptions, self_.esmsInitOptions || {});
174
182
 
175
- const version = "2.6.1";
183
+ const version = "2.6.2";
176
184
 
177
185
  const r = esmsInitOptions.version;
178
- if (self.importShim || (r && r !== version)) {
186
+ if (self_.importShim || (r && r !== version)) {
179
187
  return;
180
188
  }
181
189
 
182
190
  // shim mode is determined on initialization, no late shim mode
183
191
  const shimMode =
184
192
  esmsInitOptions.shimMode ||
185
- (hasDocument &&
193
+ (hasDocument ?
186
194
  document.querySelectorAll('script[type=module-shim],script[type=importmap-shim],link[rel=modulepreload-shim]')
187
- .length > 0);
195
+ .length > 0
196
+ // Without a document, shim mode is always true as we cannot polyfill
197
+ : true);
188
198
 
189
199
  let importHook,
190
200
  resolveHook,
@@ -198,7 +208,7 @@
198
208
 
199
209
  const defaultFetchOpts = { credentials: 'same-origin' };
200
210
 
201
- const globalHook = name => (typeof name === 'string' ? self[name] : name);
211
+ const globalHook = name => (typeof name === 'string' ? self_[name] : name);
202
212
 
203
213
  if (esmsInitOptions.onimport) importHook = globalHook(esmsInitOptions.onimport);
204
214
  if (esmsInitOptions.resolve) resolveHook = globalHook(esmsInitOptions.resolve);
@@ -216,7 +226,11 @@
216
226
  nativePassthrough = !hasCustomizationHooks && !hotReload
217
227
  } = esmsInitOptions;
218
228
 
219
- if (hotReload) [importHook, resolveHook, metaHook] = initHotReload();
229
+ const setHooks = (importHook_, resolveHook_, metaHook_) => (
230
+ (importHook = importHook_),
231
+ (resolveHook = resolveHook_),
232
+ (metaHook = metaHook_)
233
+ );
220
234
 
221
235
  const mapOverrides = esmsInitOptions.mapOverrides;
222
236
 
@@ -248,13 +262,14 @@
248
262
  };
249
263
 
250
264
  const baseUrl =
251
- hasDocument ?
252
- document.baseURI
253
- : `${location.protocol}//${location.host}${
265
+ hasDocument ? document.baseURI
266
+ : typeof location !== 'undefined' ?
267
+ `${location.protocol}//${location.host}${
254
268
  location.pathname.includes('/') ?
255
269
  location.pathname.slice(0, location.pathname.lastIndexOf('/') + 1)
256
270
  : location.pathname
257
- }`;
271
+ }`
272
+ : 'about:blank';
258
273
 
259
274
  const createBlob = (source, type = 'text/javascript') => URL.createObjectURL(new Blob([source], { type }));
260
275
  let { skip } = esmsInitOptions;
@@ -268,10 +283,10 @@
268
283
  skip = s => skip.test(s);
269
284
  }
270
285
 
271
- const dispatchError = error => self.dispatchEvent(Object.assign(new Event('error'), { error }));
286
+ const dispatchError = error => self_.dispatchEvent(Object.assign(new Event('error'), { error }));
272
287
 
273
288
  const throwError = err => {
274
- (self.reportError || dispatchError)(err);
289
+ (self_.reportError || dispatchError)(err);
275
290
  onerror(err);
276
291
  };
277
292
 
@@ -620,7 +635,7 @@
620
635
  };
621
636
 
622
637
  // import()
623
- async function importShim$1(id, opts, parentUrl) {
638
+ async function importShim(id, opts, parentUrl) {
624
639
  if (typeof opts === 'string') {
625
640
  parentUrl = opts;
626
641
  opts = undefined;
@@ -643,7 +658,7 @@
643
658
  // import.source()
644
659
  // (opts not currently supported as no use cases yet)
645
660
  if (shimMode || wasmSourcePhaseEnabled)
646
- importShim$1.source = async (id, opts, parentUrl) => {
661
+ importShim.source = async (id, opts, parentUrl) => {
647
662
  if (typeof opts === 'string') {
648
663
  parentUrl = opts;
649
664
  opts = undefined;
@@ -656,18 +671,17 @@
656
671
  await importMapPromise;
657
672
  const url = resolve(id, parentUrl || baseUrl).r;
658
673
  const load = getOrCreateLoad(url, defaultFetchOpts, undefined, undefined);
659
- if (firstPolyfillLoad && !shimMode && load.n && nativelyLoaded) {
660
- onpolyfill();
661
- firstPolyfillLoad = false;
662
- }
663
674
  await load.f;
664
- return importShim$1._s[load.r];
675
+ return importShim._s[load.r];
665
676
  };
666
677
 
667
678
  // import.defer() is just a proxy for import(), since we can't actually defer
668
- if (shimMode || deferPhaseEnabled) importShim$1.defer = importShim$1;
679
+ if (shimMode || deferPhaseEnabled) importShim.defer = importShim;
669
680
 
670
- if (hotReload) importShim$1.hotReload = hotReload$1;
681
+ if (hotReload) {
682
+ initHotReload(topLevelLoad, importShim);
683
+ importShim.hotReload = hotReload$1;
684
+ }
671
685
 
672
686
  const defaultResolve = (id, parentUrl) => {
673
687
  return (
@@ -684,24 +698,24 @@
684
698
  return resolve(id, `${parentUrl}`).r;
685
699
  };
686
700
 
687
- importShim$1.resolve = (id, parentUrl) => resolve(id, parentUrl).r;
688
- importShim$1.getImportMap = () => JSON.parse(JSON.stringify(composedImportMap));
689
- importShim$1.addImportMap = importMapIn => {
701
+ importShim.resolve = (id, parentUrl) => resolve(id, parentUrl).r;
702
+ importShim.getImportMap = () => JSON.parse(JSON.stringify(composedImportMap));
703
+ importShim.addImportMap = importMapIn => {
690
704
  if (!shimMode) throw new Error('Unsupported in polyfill mode.');
691
705
  composedImportMap = resolveAndComposeImportMap(importMapIn, baseUrl, composedImportMap);
692
706
  };
693
- importShim$1.version = version;
707
+ importShim.version = version;
694
708
 
695
- const registry = (importShim$1._r = {});
709
+ const registry = (importShim._r = {});
696
710
  // Wasm caches
697
- const sourceCache = (importShim$1._s = {});
698
- (importShim$1._i = new WeakMap());
711
+ const sourceCache = (importShim._s = {});
712
+ /* const instanceCache = */ importShim._i = new WeakMap();
699
713
 
700
714
  // Ensure this version is the only version
701
- defineValue(self, 'importShim', Object.freeze(importShim$1));
715
+ defineValue(self_, 'importShim', Object.freeze(importShim));
702
716
  const shimModeOptions = { ...esmsInitOptions, shimMode: true };
703
717
  if (optionsScript) optionsScript.innerHTML = JSON.stringify(shimModeOptions);
704
- self.esmsInitOptions = shimModeOptions;
718
+ self_.esmsInitOptions = shimModeOptions;
705
719
 
706
720
  const loadAll = async (load, seen) => {
707
721
  seen[load.u] = 1;
@@ -804,7 +818,7 @@
804
818
  let firstPolyfillLoad = true;
805
819
  let legacyAcceptingImportMaps = true;
806
820
 
807
- const topLevelLoad = async (
821
+ async function topLevelLoad(
808
822
  url,
809
823
  parentUrl,
810
824
  fetchOpts,
@@ -812,7 +826,7 @@
812
826
  nativelyLoaded,
813
827
  lastStaticLoadPromise,
814
828
  sourceType
815
- ) => {
829
+ ) {
816
830
  await initPromise;
817
831
  await importMapPromise;
818
832
  url = (await resolve(url, parentUrl)).r;
@@ -858,11 +872,11 @@
858
872
  if (load.s) (await dynamicImport(load.s, load.u)).u$_(module);
859
873
  revokeObjectURLs(Object.keys(seen));
860
874
  return module;
861
- };
875
+ }
862
876
 
863
877
  const revokeObjectURLs = registryKeys => {
864
878
  let curIdx = 0;
865
- const handler = self.requestIdleCallback || self.requestAnimationFrame || (fn => setTimeout(fn, 0));
879
+ const handler = self_.requestIdleCallback || self_.requestAnimationFrame || (fn => setTimeout(fn, 0));
866
880
  handler(cleanup);
867
881
  function cleanup() {
868
882
  for (const key of registryKeys.slice(curIdx, (curIdx += 100))) {
@@ -1057,10 +1071,11 @@
1057
1071
 
1058
1072
  const doFetch = async (url, fetchOpts, parent) => {
1059
1073
  if (enforceIntegrity && !fetchOpts.integrity) throw Error(`No integrity for ${url}${fromParent(parent)}.`);
1060
- const poolQueue = pushFetchPool();
1074
+ let res,
1075
+ poolQueue = pushFetchPool();
1061
1076
  if (poolQueue) await poolQueue;
1062
1077
  try {
1063
- var res = await fetchHook(url, fetchOpts);
1078
+ res = await fetchHook(url, fetchOpts);
1064
1079
  } catch (e) {
1065
1080
  e.message = `Unable to fetch ${url}${fromParent(parent)} - see network log for details.\n` + e.message;
1066
1081
  throw e;
@@ -1082,24 +1097,22 @@
1082
1097
  if (!esmsTsTransform) esmsTsTransform = m.transform;
1083
1098
  };
1084
1099
 
1085
- const contentTypeRegEx = /^(text|application)\/((x-)?javascript|wasm|json|css|typescript)(;|$)/;
1086
1100
  async function defaultSourceHook(url, fetchOpts, parent) {
1087
- const res = await doFetch(url, fetchOpts, parent);
1088
- let [, , t] = (res.headers.get('content-type') || '').match(contentTypeRegEx) || [];
1089
- if (!t) {
1090
- if (url.endsWith('.ts') || url.endsWith('.mts')) t = 'ts';
1091
- else
1092
- throw Error(
1093
- `Unsupported Content-Type "${contentType}" loading ${url}${fromParent(parent)}. Modules must be served with a valid MIME type like application/javascript.`
1094
- );
1101
+ let res = await doFetch(url, fetchOpts, parent),
1102
+ contentType,
1103
+ [, json, type, jsts] =
1104
+ (contentType = res.headers.get('content-type') || '').match(
1105
+ /^(?:[^/;]+\/(?:[^/+;]+\+)?(json)|(?:text|application)\/(?:x-)?((java|type)script|wasm|css))(?:;|$)/
1106
+ ) || [];
1107
+ if (!(type = json || (jsts ? jsts[0] + 's' : type || (/\.m?ts(\?|#|$)/.test(url) && 'ts')))) {
1108
+ throw Error(
1109
+ `Unsupported Content-Type "${contentType}" loading ${url}${fromParent(parent)}. Modules must be served with a valid MIME type like application/javascript.`
1110
+ );
1095
1111
  }
1096
1112
  return {
1097
1113
  url: res.url,
1098
- source: t === 'wasm' ? await WebAssembly.compileStreaming(res) : await res.text(),
1099
- type:
1100
- t[0] === 'x' || (t[0] === 'j' && t[1] === 'a') ? 'js'
1101
- : t[0] === 't' ? 'ts'
1102
- : t
1114
+ source: await (type > 'v' ? WebAssembly.compileStreaming(res) : res.text()),
1115
+ type
1103
1116
  };
1104
1117
  }
1105
1118
 
@@ -1133,7 +1146,7 @@
1133
1146
  }
1134
1147
  source += `if(h)h.accept(m=>({${obj}}=m))`;
1135
1148
  } else if (type === 'json') {
1136
- source = `${hotPrefix}j=${source};export{j as default};if(h)h.accept(m=>j=m.default)`;
1149
+ source = `${hotPrefix}j=JSON.parse(${JSON.stringify(source)});export{j as default};if(h)h.accept(m=>j=m.default)`;
1137
1150
  } else if (type === 'css') {
1138
1151
  source = `${hotPrefix}s=h&&h.data.s||new CSSStyleSheet();s.replaceSync(${JSON.stringify(
1139
1152
  source.replace(
@@ -1221,7 +1234,7 @@
1221
1234
 
1222
1235
  const linkLoad = (load, fetchOpts) => {
1223
1236
  if (load.L) return;
1224
- load.L = load.f.then(async () => {
1237
+ load.L = load.f.then(() => {
1225
1238
  let childFetchOpts = fetchOpts;
1226
1239
  load.d = load.a[0]
1227
1240
  .map(({ n, d, t, a, se }) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "es-module-shims",
3
- "version": "2.6.1",
3
+ "version": "2.6.2",
4
4
  "description": "Shims for the latest ES module features",
5
5
  "main": "dist/es-module-shims.js",
6
6
  "exports": {
@@ -32,6 +32,7 @@
32
32
  "devDependencies": {
33
33
  "@rollup/plugin-replace": "^2.4.2",
34
34
  "amaro": "0.5.3",
35
+ "chomp": "^0.2.23",
35
36
  "es-module-lexer": "1.7.0",
36
37
  "kleur": "^4.1.4",
37
38
  "mime-types": "^2.1.33",
@@ -50,8 +51,8 @@
50
51
  "test": "test"
51
52
  },
52
53
  "scripts": {
53
- "build": "npm install -g chomp ; chomp build",
54
- "test": "npm install -g chomp ; chomp test"
54
+ "build": "chomp build",
55
+ "test": "chomp test"
55
56
  },
56
57
  "repository": {
57
58
  "type": "git",