es-module-shims 2.5.0 → 2.6.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 Wasm @version 2.5.0 */
2
- (function (exports) {
1
+ /** ES Module Shims Wasm @version 2.6.0 */
2
+ (function () {
3
3
 
4
4
  let invalidate;
5
5
  const hotReload$1 = url => invalidate(new URL(url, baseUrl).href);
@@ -23,9 +23,7 @@
23
23
  hotState.e = typeof source === 'string' ? source : true;
24
24
  hotState.t = sourceType;
25
25
  };
26
- const hotMetaHook = (metaObj, url) => {
27
- metaObj.hot = new Hot(url);
28
- };
26
+ const hotMetaHook = (metaObj, url) => (metaObj.hot = new Hot(url));
29
27
 
30
28
  const Hot = class Hot {
31
29
  constructor(url) {
@@ -50,10 +48,9 @@
50
48
  }
51
49
  invalidate() {
52
50
  const hotState = getHotState(this.url);
53
- hotState.a = null;
54
- hotState.A = true;
51
+ hotState.a = hotState.A = null;
55
52
  const seen = [this.url];
56
- for (const p of hotState.p) invalidate(p, this.url, seen);
53
+ hotState.p.forEach(p => invalidate(p, this.url, seen));
57
54
  }
58
55
  };
59
56
 
@@ -93,25 +90,23 @@
93
90
  });
94
91
 
95
92
  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);
93
+ const hotState = hotRegistry[url];
94
+ if (!hotState || seen.includes(url)) return false;
95
+ seen.push(url);
96
+ hotState.A = false;
97
+ if (
98
+ fromUrl &&
99
+ hotState.a &&
100
+ hotState.a.some(([d]) => d && (typeof d === 'string' ? d === fromUrl : d.includes(fromUrl)))
101
+ ) {
102
+ curInvalidationRoots.add(fromUrl);
103
+ } else {
104
+ if (hotState.e || hotState.a) curInvalidationRoots.add(url);
105
+ hotState.v++;
106
+ if (!hotState.a) hotState.p.forEach(p => invalidate(p, url, seen));
114
107
  }
108
+ if (!curInvalidationInterval) curInvalidationInterval = setTimeout(handleInvalidations, hotReloadInterval);
109
+ return true;
115
110
  };
116
111
 
117
112
  const handleInvalidations = () => {
@@ -129,26 +124,25 @@
129
124
  hotState.t
130
125
  ).then(m => {
131
126
  if (hotState.a) {
132
- hotState.a.every(([d, c]) => d === null && !earlyRoots.has(c) && c(m));
127
+ hotState.a.forEach(([d, c]) => d === null && !earlyRoots.has(c) && c(m));
133
128
  // unload should be the latest unload handler from the just loaded module
134
129
  if (hotState.u) {
135
130
  hotState.u(hotState.d);
136
131
  hotState.u = null;
137
132
  }
138
133
  }
139
- for (const parent of hotState.p) {
140
- const hotState = hotRegistry[parent];
134
+ hotState.p.forEach(p => {
135
+ const hotState = hotRegistry[p];
141
136
  if (hotState && hotState.a)
142
- hotState.a.every(async ([d, c]) => {
143
- return (
137
+ hotState.a.forEach(
138
+ async ([d, c]) =>
144
139
  d &&
145
140
  !earlyRoots.has(c) &&
146
141
  (typeof d === 'string' ?
147
142
  d === root && c(m)
148
143
  : c(await Promise.all(d.map(d => (earlyRoots.push(c), importShim(toVersioned(d, getHotState(d))))))))
149
- );
150
- });
151
- }
144
+ );
145
+ });
152
146
  }, throwError);
153
147
  }
154
148
  curInvalidationRoots = new Set();
@@ -164,121 +158,122 @@
164
158
  ];
165
159
  };
166
160
 
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);
175
- };
176
-
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 = esmsInitOptions.version;
190
- if (self.importShim || (r && r !== 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
-
161
+ const hasDocument = typeof document !== 'undefined';
162
+
163
+ const noop = () => {};
164
+
165
+ const dynamicImport = (u, errUrl) => import(u);
166
+
167
+ const defineValue = (obj, prop, value) =>
168
+ Object.defineProperty(obj, prop, { writable: false, configurable: false, value });
169
+
170
+ const optionsScript = hasDocument ? document.querySelector('script[type=esms-options]') : undefined;
171
+
172
+ const esmsInitOptions = optionsScript ? JSON.parse(optionsScript.innerHTML) : {};
173
+ Object.assign(esmsInitOptions, self.esmsInitOptions || {});
174
+
175
+ const version = "2.6.0";
176
+
177
+ const r = esmsInitOptions.version;
178
+ if (self.importShim || (r && r !== version)) {
179
+ return;
180
+ }
181
+
182
+ // shim mode is determined on initialization, no late shim mode
183
+ const shimMode =
184
+ esmsInitOptions.shimMode ||
185
+ (hasDocument &&
186
+ document.querySelectorAll('script[type=module-shim],script[type=importmap-shim],link[rel=modulepreload-shim]')
187
+ .length > 0);
188
+
189
+ let importHook,
190
+ resolveHook,
191
+ fetchHook = fetch,
192
+ sourceHook,
193
+ metaHook,
194
+ tsTransform =
195
+ esmsInitOptions.tsTransform ||
196
+ (hasDocument && document.currentScript && document.currentScript.src.replace(/(\.\w+)?\.js$/, '-typescript.js')) ||
197
+ './es-module-shims-typescript.js';
198
+
199
+ const defaultFetchOpts = { credentials: 'same-origin' };
200
+
201
+ const globalHook = name => (typeof name === 'string' ? self[name] : name);
202
+
203
+ if (esmsInitOptions.onimport) importHook = globalHook(esmsInitOptions.onimport);
204
+ if (esmsInitOptions.resolve) resolveHook = globalHook(esmsInitOptions.resolve);
205
+ if (esmsInitOptions.fetch) fetchHook = globalHook(esmsInitOptions.fetch);
206
+ if (esmsInitOptions.source) sourceHook = globalHook(esmsInitOptions.source);
207
+ if (esmsInitOptions.meta) metaHook = globalHook(esmsInitOptions.meta);
208
+
209
+ const hasCustomizationHooks = importHook || resolveHook || fetchHook !== fetch || sourceHook || metaHook;
210
+
211
+ const {
212
+ noLoadEventRetriggers,
213
+ enforceIntegrity,
214
+ hotReload,
215
+ hotReloadInterval = 100,
216
+ nativePassthrough = !hasCustomizationHooks && !hotReload
217
+ } = esmsInitOptions;
218
+
219
+ if (hotReload) [importHook, resolveHook, metaHook] = initHotReload();
220
+
221
+ const mapOverrides = esmsInitOptions.mapOverrides;
222
+
223
+ let nonce = esmsInitOptions.nonce;
224
+ if (!nonce && hasDocument) {
225
+ const nonceElement = document.querySelector('script[nonce]');
226
+ if (nonceElement) nonce = nonceElement.nonce || nonceElement.getAttribute('nonce');
227
+ }
228
+
229
+ const onerror = globalHook(esmsInitOptions.onerror || console.error.bind(console));
230
+
231
+ const enable = Array.isArray(esmsInitOptions.polyfillEnable) ? esmsInitOptions.polyfillEnable : [];
232
+ const disable = Array.isArray(esmsInitOptions.polyfillDisable) ? esmsInitOptions.polyfillDisable : [];
233
+
234
+ const enableAll = esmsInitOptions.polyfillEnable === 'all' || enable.includes('all');
235
+ const wasmInstancePhaseEnabled =
236
+ enable.includes('wasm-modules') || enable.includes('wasm-module-instances') || enableAll;
237
+ const wasmSourcePhaseEnabled =
238
+ enable.includes('wasm-modules') || enable.includes('wasm-module-sources') || enableAll;
239
+ const deferPhaseEnabled = enable.includes('import-defer') || enableAll;
240
+ const cssModulesEnabled = !disable.includes('css-modules');
241
+ const jsonModulesEnabled = !disable.includes('json-modules');
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
+
282
277
  const fromParent = parent => (parent ? ` imported from ${parent}` : '');
283
278
 
284
279
  const backslashRegEx = /\\/g;
@@ -538,13 +533,15 @@
538
533
 
539
534
  // Feature checking with careful avoidance of unnecessary work - all gated on initial import map supports check. CSS gates on JSON feature check, Wasm instance phase gates on wasm source phase check.
540
535
  const importMapTest = `<script nonce=${nonce || ''}>b=(s,type='text/javascript')=>URL.createObjectURL(new Blob([s],{type}));c=u=>import(u).then(()=>true,()=>false);i=innerText=>document.head.appendChild(Object.assign(document.createElement('script'),{type:'importmap',nonce:"${nonce}",innerText}));i(\`{"imports":{"x":"\${b('')}"}}\`);i(\`{"imports":{"y":"\${b('')}"}}\`);cm=${
541
- supportsImportMaps ? `c(b(\`import"\${b('{}','text/json')}"with{type:"json"}\`))` : 'false'
536
+ supportsImportMaps && jsonModulesEnabled ? `c(b(\`import"\${b('{}','text/json')}"with{type:"json"}\`))` : 'false'
542
537
  };sp=${
543
538
  supportsImportMaps && wasmSourcePhaseEnabled ?
544
539
  `c(b(\`import source x from "\${b(new Uint8Array(${JSON.stringify(wasmBytes)}),'application/wasm')\}"\`))`
545
540
  : 'false'
546
541
  };Promise.all([${supportsImportMaps ? 'true' : "c('x')"},${supportsImportMaps ? "c('y')" : false},cm,${
547
- supportsImportMaps ? `cm.then(s=>s?c(b(\`import"\${b('','text/css')\}"with{type:"css"}\`)):false)` : 'false'
542
+ supportsImportMaps && cssModulesEnabled ?
543
+ `cm.then(s=>s?c(b(\`import"\${b('','text/css')\}"with{type:"css"}\`)):false)`
544
+ : 'false'
548
545
  },sp,${
549
546
  supportsImportMaps && wasmInstancePhaseEnabled ?
550
547
  `${wasmSourcePhaseEnabled ? 'sp.then(s=>s?' : ''}c(b(\`import"\${b(new Uint8Array(${JSON.stringify(wasmBytes)}),'application/wasm')\}"\`))${wasmSourcePhaseEnabled ? ':false)' : ''}`
@@ -628,7 +625,7 @@
628
625
  opts = undefined;
629
626
  }
630
627
  await initPromise; // needed for shim check
631
- if (shimMode || !baselinePassthrough) {
628
+ if (shimMode || !baselineSupport) {
632
629
  if (hasDocument) processScriptsAndPreloads();
633
630
  legacyAcceptingImportMaps = false;
634
631
  }
@@ -651,7 +648,7 @@
651
648
  opts = undefined;
652
649
  }
653
650
  await initPromise; // needed for shim check
654
- if (shimMode || !baselinePassthrough) {
651
+ if (shimMode || !baselineSupport) {
655
652
  if (hasDocument) processScriptsAndPreloads();
656
653
  legacyAcceptingImportMaps = false;
657
654
  }
@@ -722,19 +719,19 @@
722
719
  let firstImportMap = null;
723
720
  // To support polyfilling multiple import maps, we separately track the composed import map from the first import map
724
721
  let composedImportMap = { imports: {}, scopes: {}, integrity: {} };
725
- let baselinePassthrough;
722
+ let baselineSupport;
726
723
 
727
724
  const initPromise = featureDetectionPromise.then(() => {
728
- baselinePassthrough =
729
- esmsInitOptions.polyfillEnable !== true &&
725
+ baselineSupport =
730
726
  supportsImportMaps &&
731
- supportsJsonType &&
732
- supportsCssType &&
727
+ (!jsonModulesEnabled || supportsJsonType) &&
728
+ (!cssModulesEnabled || supportsCssType) &&
733
729
  (!wasmInstancePhaseEnabled || supportsWasmInstancePhase) &&
734
730
  (!wasmSourcePhaseEnabled || supportsWasmSourcePhase) &&
735
731
  !deferPhaseEnabled &&
736
732
  (!multipleImportMaps || supportsMultipleImportMaps) &&
737
- !importMapSrc;
733
+ !importMapSrc &&
734
+ !hasCustomizationHooks;
738
735
  if (!shimMode && typeof WebAssembly !== 'undefined') {
739
736
  if (wasmSourcePhaseEnabled && !Object.getPrototypeOf(WebAssembly.Module).name) {
740
737
  const s = Symbol();
@@ -766,7 +763,7 @@
766
763
  const supports = HTMLScriptElement.supports || (type => type === 'classic' || type === 'module');
767
764
  HTMLScriptElement.supports = type => type === 'importmap' || supports(type);
768
765
  }
769
- if (shimMode || !baselinePassthrough) {
766
+ if (shimMode || !baselineSupport) {
770
767
  attachMutationObserver();
771
768
  if (document.readyState === 'complete') {
772
769
  readyStateCompleteCheck();
@@ -829,7 +826,7 @@
829
826
 
830
827
  if (importHook) await importHook(url, typeof fetchOpts !== 'string' ? fetchOpts : {}, parentUrl, source, sourceType);
831
828
  // early analysis opt-out - no need to even fetch if we have feature support
832
- if (!shimMode && baselinePassthrough && nativePassthrough && sourceType !== 'ts') {
829
+ if (!shimMode && baselineSupport && nativePassthrough && sourceType !== 'ts') {
833
830
  // for polyfill case, only dynamic import needs a return value here, and dynamic import will never pass nativelyLoaded
834
831
  if (nativelyLoaded) return null;
835
832
  await lastStaticLoadPromise;
@@ -858,7 +855,7 @@
858
855
  : import(load.u));
859
856
  // if the top-level load is a shell, run its update function
860
857
  if (load.s) (await dynamicImport(load.s, load.u)).u$_(module);
861
- if (revokeBlobURLs) revokeObjectURLs(Object.keys(seen));
858
+ revokeObjectURLs(Object.keys(seen));
862
859
  return module;
863
860
  };
864
861
 
@@ -1043,13 +1040,6 @@
1043
1040
 
1044
1041
  const sourceURLCommentPrefix = '\n//# sourceURL=';
1045
1042
  const sourceMapURLCommentPrefix = '\n//# sourceMappingURL=';
1046
-
1047
- const jsContentType = /^(text|application)\/(x-)?javascript(;|$)/;
1048
- const wasmContentType = /^application\/wasm(;|$)/;
1049
- const jsonContentType = /^(text|application)\/json(;|$)/;
1050
- const cssContentType = /^(text|application)\/css(;|$)/;
1051
- const tsContentType = /^application\/typescript(;|$)|/;
1052
-
1053
1043
  const cssUrlRegEx = /url\(\s*(?:(["'])((?:\\.|[^\n\\"'])+)\1|((?:\\.|[^\s,"'()\\])+))\s*\)/g;
1054
1044
 
1055
1045
  // restrict in-flight fetches to a pool of 100
@@ -1090,77 +1080,73 @@
1090
1080
  if (!esmsTsTransform) esmsTsTransform = m.transform;
1091
1081
  };
1092
1082
 
1083
+ const contentTypeRegEx = /^(text|application)\/((x-)?javascript|wasm|json|css|typescript)(;|$)/;
1084
+ async function defaultSourceHook(url, fetchOpts, parent) {
1085
+ const res = await doFetch(url, fetchOpts, parent);
1086
+ let [, , t] = (res.headers.get('content-type') || '').match(contentTypeRegEx) || [];
1087
+ if (!t) {
1088
+ if (url.endsWith('.ts') || url.endsWith('.mts')) t = 'ts';
1089
+ else
1090
+ throw Error(
1091
+ `Unsupported Content-Type "${contentType}" loading ${url}${fromParent(parent)}. Modules must be served with a valid MIME type like application/javascript.`
1092
+ );
1093
+ }
1094
+ return {
1095
+ url: res.url,
1096
+ source: t === 'wasm' ? await WebAssembly.compileStreaming(res) : await res.text(),
1097
+ type:
1098
+ t[0] === 'x' || (t[0] === 'j' && t[1] === 'a') ? 'js'
1099
+ : t[0] === 't' ? 'ts'
1100
+ : t
1101
+ };
1102
+ }
1103
+
1093
1104
  const hotPrefix = 'var h=import.meta.hot,';
1094
- const fetchModule = async (url, fetchOpts, parent) => {
1095
- const mapIntegrity = composedImportMap.integrity[url];
1096
- const res = await doFetch(
1097
- url,
1098
- mapIntegrity && !fetchOpts.integrity ? { ...fetchOpts, integrity: mapIntegrity } : fetchOpts,
1099
- parent
1100
- );
1101
- const r = res.url;
1102
- const contentType = res.headers.get('content-type');
1103
- if (jsContentType.test(contentType)) return { r, s: await res.text(), t: 'js' };
1104
- else if (wasmContentType.test(contentType)) {
1105
- const wasmModule = await (sourceCache[r] || (sourceCache[r] = WebAssembly.compileStreaming(res)));
1106
- const exports = WebAssembly.Module.exports(wasmModule);
1107
- sourceCache[r] = wasmModule;
1108
- const rStr = urlJsString(r);
1109
- let s = `import*as $_ns from${rStr};`,
1110
- i = 0,
1105
+ const fetchModule = async (reqUrl, fetchOpts, parent) => {
1106
+ const mapIntegrity = composedImportMap.integrity[reqUrl];
1107
+ fetchOpts = mapIntegrity && !fetchOpts.integrity ? { ...fetchOpts, integrity: mapIntegrity } : fetchOpts;
1108
+ let {
1109
+ url = reqUrl,
1110
+ source,
1111
+ type
1112
+ } = (await (sourceHook || defaultSourceHook)(reqUrl, fetchOpts, parent, defaultSourceHook)) || {};
1113
+ if (type === 'wasm') {
1114
+ const exports = WebAssembly.Module.exports((sourceCache[url] = source));
1115
+ const imports = WebAssembly.Module.imports(source);
1116
+ const rStr = urlJsString(url);
1117
+ source = `import*as $_ns from${rStr};`;
1118
+ let i = 0,
1111
1119
  obj = '';
1112
- for (const { module, kind } of WebAssembly.Module.imports(wasmModule)) {
1120
+ for (const { module, kind } of imports) {
1113
1121
  const specifier = urlJsString(module);
1114
- s += `import*as impt${i} from${specifier};\n`;
1122
+ source += `import*as impt${i} from${specifier};\n`;
1115
1123
  obj += `${specifier}:${kind === 'global' ? `importShim._i.get(impt${i})||impt${i++}` : `impt${i++}`},`;
1116
1124
  }
1117
- s += `${hotPrefix}i=await WebAssembly.instantiate(importShim._s[${rStr}],{${obj}});importShim._i.set($_ns,i);`;
1125
+ source += `${hotPrefix}i=await WebAssembly.instantiate(importShim._s[${rStr}],{${obj}});importShim._i.set($_ns,i);`;
1118
1126
  obj = '';
1119
1127
  for (const { name, kind } of exports) {
1120
- s += `export let ${name}=i.exports['${name}'];`;
1121
- if (kind === 'global') s += `try{${name}=${name}.value}catch{${name}=undefined}`;
1128
+ source += `export let ${name}=i.exports['${name}'];`;
1129
+ if (kind === 'global') source += `try{${name}=${name}.value}catch{${name}=undefined}`;
1122
1130
  obj += `${name},`;
1123
1131
  }
1124
- s += `if(h)h.accept(m=>({${obj}}=m))`;
1125
- return { r, s, t: 'wasm' };
1126
- } else if (jsonContentType.test(contentType))
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
- };
1132
- else if (cssContentType.test(contentType)) {
1133
- return {
1134
- r,
1135
- s: `${hotPrefix}s=h&&h.data.s||new CSSStyleSheet();s.replaceSync(${JSON.stringify(
1136
- (await res.text()).replace(
1137
- cssUrlRegEx,
1138
- (_match, quotes = '', relUrl1, relUrl2) => `url(${quotes}${resolveUrl(relUrl1 || relUrl2, url)}${quotes})`
1139
- )
1140
- )});if(h){h.data.s=s;h.accept(()=>{})}export default s`,
1141
- t: 'css'
1142
- };
1143
- } else if (tsContentType.test(contentType) || url.endsWith('.ts') || url.endsWith('.mts')) {
1144
- const source = await res.text();
1132
+ source += `if(h)h.accept(m=>({${obj}}=m))`;
1133
+ } else if (type === 'json') {
1134
+ source = `${hotPrefix}j=${source};export{j as default};if(h)h.accept(m=>j=m.default)`;
1135
+ } else if (type === 'css') {
1136
+ source = `${hotPrefix}s=h&&h.data.s||new CSSStyleSheet();s.replaceSync(${JSON.stringify(
1137
+ source.replace(
1138
+ cssUrlRegEx,
1139
+ (_match, quotes = '', relUrl1, relUrl2) => `url(${quotes}${resolveUrl(relUrl1 || relUrl2, url)}${quotes})`
1140
+ )
1141
+ )});if(h){h.data.s=s;h.accept(()=>{})}export default s`;
1142
+ } else if (type === 'ts') {
1145
1143
  if (!esmsTsTransform) await initTs();
1146
1144
  const transformed = esmsTsTransform(source, url);
1147
1145
  // even if the TypeScript is valid JavaScript, unless it was a top-level inline source, it wasn't served with
1148
1146
  // a valid JS MIME here, so we must still polyfill it
1149
- return { r, s: transformed === undefined ? source : transformed, t: 'ts' };
1150
- } else
1151
- throw Error(
1152
- `Unsupported Content-Type "${contentType}" loading ${url}${fromParent(parent)}. Modules must be served with a valid MIME type like application/javascript.`
1153
- );
1154
- };
1155
-
1156
- const isUnsupportedType = type => {
1157
- if (type === 'wasm' && !wasmInstancePhaseEnabled && !wasmSourcePhaseEnabled) throw featErr(`wasm-modules`);
1158
- return (
1159
- (type === 'css' && !supportsCssType) ||
1160
- (type === 'json' && !supportsJsonType) ||
1161
- (type === 'wasm' && !supportsWasmInstancePhase && !supportsWasmSourcePhase) ||
1162
- type === 'ts'
1163
- );
1147
+ source = transformed === undefined ? source : transformed;
1148
+ }
1149
+ return { url, source, type };
1164
1150
  };
1165
1151
 
1166
1152
  const getOrCreateLoad = (url, fetchOpts, parent, source) => {
@@ -1202,8 +1188,16 @@
1202
1188
  load.f = (async () => {
1203
1189
  if (load.S === undefined) {
1204
1190
  // preload fetch options override fetch options (race)
1205
- ({ r: load.r, s: load.S, t: load.t } = await (fetchCache[url] || fetchModule(url, fetchOpts, parent)));
1206
- if (!load.n && load.t !== 'js' && !shimMode && isUnsupportedType(load.t)) {
1191
+ ({ url: load.r, source: load.S, type: load.t } = await (fetchCache[url] || fetchModule(url, fetchOpts, parent)));
1192
+ if (
1193
+ !load.n &&
1194
+ load.t !== 'js' &&
1195
+ !shimMode &&
1196
+ ((load.t === 'css' && !supportsCssType) ||
1197
+ (load.t === 'json' && !supportsJsonType) ||
1198
+ (load.t === 'wasm' && !supportsWasmInstancePhase && !supportsWasmSourcePhase) ||
1199
+ load.t === 'ts')
1200
+ ) {
1207
1201
  load.n = true;
1208
1202
  }
1209
1203
  }
@@ -1250,7 +1244,7 @@
1250
1244
  }
1251
1245
  if (d !== -1 || !n) return;
1252
1246
  const resolved = resolve(n, load.r || load.u);
1253
- if (resolved.n) load.n = true;
1247
+ if (resolved.n || hasCustomizationHooks) load.n = true;
1254
1248
  if (d >= 0 || resolved.N) load.N = true;
1255
1249
  if (d !== -1) return;
1256
1250
  if (skip && skip(resolved.r) && !sourcePhase) return { l: { b: resolved.r }, s: false };
@@ -1301,14 +1295,14 @@
1301
1295
  domContentLoaded = true;
1302
1296
  domContentLoadedCnt--;
1303
1297
  }
1304
- if (--domContentLoadedCnt === 0 && !noLoadEventRetriggers && (shimMode || !baselinePassthrough)) {
1298
+ if (--domContentLoadedCnt === 0 && !noLoadEventRetriggers && (shimMode || !baselineSupport)) {
1305
1299
  document.removeEventListener('DOMContentLoaded', domContentLoadedEvent);
1306
1300
  document.dispatchEvent(new Event('DOMContentLoaded'));
1307
1301
  }
1308
1302
  };
1309
1303
  let loadCnt = 1;
1310
1304
  const loadCheck = () => {
1311
- if (--loadCnt === 0 && !noLoadEventRetriggers && (shimMode || !baselinePassthrough)) {
1305
+ if (--loadCnt === 0 && !noLoadEventRetriggers && (shimMode || !baselineSupport)) {
1312
1306
  window.removeEventListener('load', loadEvent);
1313
1307
  window.dispatchEvent(new Event('load'));
1314
1308
  }
@@ -1342,7 +1336,7 @@
1342
1336
  const readyStateCompleteCheck = () => {
1343
1337
  if (--readyStateCompleteCnt === 0) {
1344
1338
  domContentLoadedCheck();
1345
- if (!noLoadEventRetriggers && (shimMode || !baselinePassthrough)) {
1339
+ if (!noLoadEventRetriggers && (shimMode || !baselineSupport)) {
1346
1340
  document.removeEventListener('readystatechange', readyListener);
1347
1341
  document.dispatchEvent(new Event('readystatechange'));
1348
1342
  }
@@ -1379,8 +1373,8 @@
1379
1373
  if (!firstImportMap && legacyAcceptingImportMaps) importMapPromise.then(() => (firstImportMap = composedImportMap));
1380
1374
  if (!legacyAcceptingImportMaps && !multipleImportMaps) {
1381
1375
  multipleImportMaps = true;
1382
- if (!shimMode && baselinePassthrough && !supportsMultipleImportMaps) {
1383
- baselinePassthrough = false;
1376
+ if (!shimMode && baselineSupport && !supportsMultipleImportMaps) {
1377
+ baselineSupport = false;
1384
1378
  if (hasDocument) attachMutationObserver();
1385
1379
  }
1386
1380
  }
@@ -1440,14 +1434,11 @@
1440
1434
  const fetchCache = {};
1441
1435
  const processPreload = link => {
1442
1436
  link.ep = true;
1443
- if (fetchCache[link.href]) return;
1444
- fetchCache[link.href] = fetchModule(link.href, getFetchOpts(link));
1437
+ initPromise.then(() => {
1438
+ if (baselineSupport && !shimMode) return;
1439
+ if (fetchCache[link.href]) return;
1440
+ fetchCache[link.href] = fetchModule(link.href, getFetchOpts(link));
1441
+ });
1445
1442
  };
1446
1443
 
1447
- exports.topLevelLoad = topLevelLoad;
1448
-
1449
- Object.defineProperty(exports, '__esModule', { value: true });
1450
-
1451
- return exports;
1452
-
1453
- })({});
1444
+ })();