es-module-shims 1.7.2 → 1.8.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.
package/README.md CHANGED
@@ -29,7 +29,7 @@ Because we are still using the native module loader the edge cases work out comp
29
29
  Include ES Module Shims with a `async` attribute on the script, then include an import map and module scripts normally:
30
30
 
31
31
  ```html
32
- <script async src="https://ga.jspm.io/npm:es-module-shims@1.7.2/dist/es-module-shims.js"></script>
32
+ <script async src="https://ga.jspm.io/npm:es-module-shims@1.8.0/dist/es-module-shims.js"></script>
33
33
 
34
34
  <!-- https://generator.jspm.io/#U2NhYGBkDM0rySzJSU1hKEpNTC5xMLTQM9Az0C1K1jMAAKFS5w0gAA -->
35
35
  <script type="importmap">
@@ -1,4 +1,4 @@
1
- /* ES Module Shims DEBUG BUILD 1.7.2 */
1
+ /* ES Module Shims DEBUG BUILD 1.8.0 */
2
2
  (function () {
3
3
 
4
4
  const hasWindow = typeof window !== 'undefined';
@@ -463,6 +463,7 @@
463
463
  };
464
464
 
465
465
  const registry = importShim._r = {};
466
+ importShim._w = {};
466
467
 
467
468
  async function loadAll (load, seen) {
468
469
  if (load.b || seen[load.u])
@@ -603,90 +604,112 @@
603
604
  // edge doesnt execute sibling in order, so we fix this up by ensuring all previous executions are explicit dependencies
604
605
  let resolvedSource = edge && lastLoad ? `import '${lastLoad}';` : '';
605
606
 
606
- if (!imports.length) {
607
- resolvedSource += source;
608
- }
609
- else {
610
- // once all deps have loaded we can inline the dependency resolution blobs
611
- // and define this blob
612
- let lastIndex = 0, depIndex = 0, dynamicImportEndStack = [];
613
- function pushStringTo (originalIndex) {
614
- while (dynamicImportEndStack[dynamicImportEndStack.length - 1] < originalIndex) {
615
- const dynamicImportEnd = dynamicImportEndStack.pop();
616
- resolvedSource += `${source.slice(lastIndex, dynamicImportEnd)}, ${urlJsString(load.r)}`;
617
- lastIndex = dynamicImportEnd;
618
- }
619
- resolvedSource += source.slice(lastIndex, originalIndex);
620
- lastIndex = originalIndex;
607
+ // once all deps have loaded we can inline the dependency resolution blobs
608
+ // and define this blob
609
+ let lastIndex = 0, depIndex = 0, dynamicImportEndStack = [];
610
+ function pushStringTo (originalIndex) {
611
+ while (dynamicImportEndStack[dynamicImportEndStack.length - 1] < originalIndex) {
612
+ const dynamicImportEnd = dynamicImportEndStack.pop();
613
+ resolvedSource += `${source.slice(lastIndex, dynamicImportEnd)}, ${urlJsString(load.r)}`;
614
+ lastIndex = dynamicImportEnd;
621
615
  }
622
- for (const { s: start, ss: statementStart, se: statementEnd, d: dynamicImportIndex } of imports) {
623
- // dependency source replacements
624
- if (dynamicImportIndex === -1) {
625
- let depLoad = load.d[depIndex++], blobUrl = depLoad.b, cycleShell = !blobUrl;
626
- if (cycleShell) {
627
- // circular shell creation
628
- if (!(blobUrl = depLoad.s)) {
629
- blobUrl = depLoad.s = createBlob(`export function u$_(m){${
630
- depLoad.a[1].map(({ s, e }, i) => {
631
- const q = depLoad.S[s] === '"' || depLoad.S[s] === "'";
632
- return `e$_${i}=m${q ? `[` : '.'}${depLoad.S.slice(s, e)}${q ? `]` : ''}`;
633
- }).join(',')
634
- }}${
635
- depLoad.a[1].length ? `let ${depLoad.a[1].map((_, i) => `e$_${i}`).join(',')};` : ''
636
- }export {${
637
- depLoad.a[1].map(({ s, e }, i) => `e$_${i} as ${depLoad.S.slice(s, e)}`).join(',')
638
- }}\n//# sourceURL=${depLoad.r}?cycle`);
639
- }
640
- }
641
-
642
- pushStringTo(start - 1);
643
- resolvedSource += `/*${source.slice(start - 1, statementEnd)}*/${urlJsString(blobUrl)}`;
616
+ resolvedSource += source.slice(lastIndex, originalIndex);
617
+ lastIndex = originalIndex;
618
+ }
644
619
 
645
- // circular shell execution
646
- if (!cycleShell && depLoad.s) {
647
- resolvedSource += `;import*as m$_${depIndex} from'${depLoad.b}';import{u$_ as u$_${depIndex}}from'${depLoad.s}';u$_${depIndex}(m$_${depIndex})`;
648
- depLoad.s = undefined;
620
+ for (const { s: start, ss: statementStart, se: statementEnd, d: dynamicImportIndex } of imports) {
621
+ // dependency source replacements
622
+ if (dynamicImportIndex === -1) {
623
+ let depLoad = load.d[depIndex++], blobUrl = depLoad.b, cycleShell = !blobUrl;
624
+ if (cycleShell) {
625
+ // circular shell creation
626
+ if (!(blobUrl = depLoad.s)) {
627
+ blobUrl = depLoad.s = createBlob(`export function u$_(m){${
628
+ depLoad.a[1].map(({ s, e }, i) => {
629
+ const q = depLoad.S[s] === '"' || depLoad.S[s] === "'";
630
+ return `e$_${i}=m${q ? `[` : '.'}${depLoad.S.slice(s, e)}${q ? `]` : ''}`;
631
+ }).join(',')
632
+ }}${
633
+ depLoad.a[1].length ? `let ${depLoad.a[1].map((_, i) => `e$_${i}`).join(',')};` : ''
634
+ }export {${
635
+ depLoad.a[1].map(({ s, e }, i) => `e$_${i} as ${depLoad.S.slice(s, e)}`).join(',')
636
+ }}\n//# sourceURL=${depLoad.r}?cycle`);
649
637
  }
650
- lastIndex = statementEnd;
651
- }
652
- // import.meta
653
- else if (dynamicImportIndex === -2) {
654
- load.m = { url: load.r, resolve: metaResolve };
655
- metaHook(load.m, load.u);
656
- pushStringTo(start);
657
- resolvedSource += `importShim._r[${urlJsString(load.u)}].m`;
658
- lastIndex = statementEnd;
659
638
  }
660
- // dynamic import
661
- else {
662
- pushStringTo(statementStart + 6);
663
- resolvedSource += `Shim(`;
664
- dynamicImportEndStack.push(statementEnd - 1);
665
- lastIndex = start;
639
+
640
+ pushStringTo(start - 1);
641
+ resolvedSource += `/*${source.slice(start - 1, statementEnd)}*/${urlJsString(blobUrl)}`;
642
+
643
+ // circular shell execution
644
+ if (!cycleShell && depLoad.s) {
645
+ resolvedSource += `;import*as m$_${depIndex} from'${depLoad.b}';import{u$_ as u$_${depIndex}}from'${depLoad.s}';u$_${depIndex}(m$_${depIndex})`;
646
+ depLoad.s = undefined;
666
647
  }
648
+ lastIndex = statementEnd;
649
+ }
650
+ // import.meta
651
+ else if (dynamicImportIndex === -2) {
652
+ load.m = { url: load.r, resolve: metaResolve };
653
+ metaHook(load.m, load.u);
654
+ pushStringTo(start);
655
+ resolvedSource += `importShim._r[${urlJsString(load.u)}].m`;
656
+ lastIndex = statementEnd;
667
657
  }
658
+ // dynamic import
659
+ else {
660
+ pushStringTo(statementStart + 6);
661
+ resolvedSource += `Shim(`;
662
+ dynamicImportEndStack.push(statementEnd - 1);
663
+ lastIndex = start;
664
+ }
665
+ }
666
+
667
+ // support progressive cycle binding updates (try statement avoids tdz errors)
668
+ if (load.s)
669
+ resolvedSource += `\n;import{u$_}from'${load.s}';try{u$_({${exports.filter(e => e.ln).map(({ s, e, ln }) => `${source.slice(s, e)}:${ln}`).join(',')}})}catch(_){};\n`;
670
+
671
+ function pushSourceURL (commentPrefix, commentStart) {
672
+ const urlStart = commentStart + commentPrefix.length;
673
+ const commentEnd = source.indexOf('\n', urlStart);
674
+ const urlEnd = commentEnd !== -1 ? commentEnd : source.length;
675
+ pushStringTo(urlStart);
676
+ resolvedSource += new URL(source.slice(urlStart, urlEnd), load.r).href;
677
+ lastIndex = urlEnd;
678
+ }
668
679
 
669
- // support progressive cycle binding updates (try statement avoids tdz errors)
670
- if (load.s)
671
- resolvedSource += `\n;import{u$_}from'${load.s}';try{u$_({${exports.filter(e => e.ln).map(({ s, e, ln }) => `${source.slice(s, e)}:${ln}`).join(',')}})}catch(_){};\n`;
680
+ let sourceURLCommentStart = source.lastIndexOf(sourceURLCommentPrefix);
681
+ let sourceMapURLCommentStart = source.lastIndexOf(sourceMapURLCommentPrefix);
672
682
 
673
- pushStringTo(source.length);
683
+ // ignore sourceMap comments before already spliced code
684
+ if (sourceURLCommentStart < lastIndex) sourceURLCommentStart = -1;
685
+ if (sourceMapURLCommentStart < lastIndex) sourceMapURLCommentStart = -1;
686
+
687
+ // sourceURL first / only
688
+ if (sourceURLCommentStart !== -1 && (sourceMapURLCommentStart === -1 || sourceMapURLCommentStart > sourceURLCommentStart)) {
689
+ pushSourceURL(sourceURLCommentPrefix, sourceURLCommentStart);
690
+ }
691
+ // sourceMappingURL
692
+ if (sourceMapURLCommentStart !== -1) {
693
+ pushSourceURL(sourceMapURLCommentPrefix, sourceMapURLCommentStart);
694
+ // sourceURL last
695
+ if (sourceURLCommentStart !== -1 && (sourceURLCommentStart > sourceMapURLCommentStart))
696
+ pushSourceURL(sourceURLCommentPrefix, sourceURLCommentStart);
674
697
  }
675
698
 
676
- let hasSourceURL = false;
677
- resolvedSource = resolvedSource.replace(sourceMapURLRegEx, (match, isMapping, url) => (hasSourceURL = !isMapping, match.replace(url, () => new URL(url, load.r))));
678
- if (!hasSourceURL)
679
- resolvedSource += '\n//# sourceURL=' + load.r;
699
+ pushStringTo(source.length);
700
+
701
+ if (sourceURLCommentStart === -1)
702
+ resolvedSource += sourceURLCommentPrefix + load.r;
680
703
 
681
704
  load.b = lastLoad = createBlob(resolvedSource);
682
705
  load.S = undefined;
683
706
  }
684
707
 
685
- // ; and // trailer support added for Ruby on Rails 7 source maps compatibility
686
- // https://github.com/guybedford/es-module-shims/issues/228
687
- const sourceMapURLRegEx = /\n\/\/# source(Mapping)?URL=([^\n]+)\s*((;|\/\/[^#][^\n]*)\s*)*$/;
708
+ const sourceURLCommentPrefix = '\n//# sourceURL=';
709
+ const sourceMapURLCommentPrefix = '\n//# sourceMappingURL=';
688
710
 
689
711
  const jsContentType = /^(text|application)\/(x-)?javascript(;|$)/;
712
+ const wasmContentType = /^(application)\/wasm(;|$)/;
690
713
  const jsonContentType = /^(text|application)\/json(;|$)/;
691
714
  const cssContentType = /^(text|application)\/css(;|$)/;
692
715
 
@@ -730,6 +753,21 @@
730
753
  const contentType = res.headers.get('content-type');
731
754
  if (jsContentType.test(contentType))
732
755
  return { r: res.url, s: await res.text(), t: 'js' };
756
+ else if (wasmContentType.test(contentType)) {
757
+ const module = importShim._w[url] = await WebAssembly.compileStreaming(res);
758
+ let s = '', i = 0, importObj = '';
759
+ for (const impt of WebAssembly.Module.imports(module)) {
760
+ s += `import * as impt${i} from '${impt.module}';\n`;
761
+ importObj += `'${impt.module}':impt${i++},`;
762
+ }
763
+ i = 0;
764
+ s += `const instance = await WebAssembly.instantiate(importShim._w['${url}'], {${importObj}});\n`;
765
+ for (const expt of WebAssembly.Module.exports(module)) {
766
+ s += `const expt${i} = instance['${expt.name}'];\n`;
767
+ s += `export { expt${i++} as "${expt.name}" };\n`;
768
+ }
769
+ return { r: res.url, s, t: 'wasm' };
770
+ }
733
771
  else if (jsonContentType.test(contentType))
734
772
  return { r: res.url, s: `export default ${await res.text()}`, t: 'json' };
735
773
  else if (cssContentType.test(contentType)) {
@@ -1,4 +1,4 @@
1
- /* ES Module Shims 1.7.2 */
1
+ /* ES Module Shims 1.8.0 */
2
2
  (function () {
3
3
 
4
4
  const hasWindow = typeof window !== 'undefined';
@@ -458,6 +458,7 @@
458
458
  };
459
459
 
460
460
  const registry = importShim._r = {};
461
+ importShim._w = {};
461
462
 
462
463
  async function loadAll (load, seen) {
463
464
  if (load.b || seen[load.u])
@@ -596,90 +597,112 @@
596
597
  // edge doesnt execute sibling in order, so we fix this up by ensuring all previous executions are explicit dependencies
597
598
  let resolvedSource = edge && lastLoad ? `import '${lastLoad}';` : '';
598
599
 
599
- if (!imports.length) {
600
- resolvedSource += source;
601
- }
602
- else {
603
- // once all deps have loaded we can inline the dependency resolution blobs
604
- // and define this blob
605
- let lastIndex = 0, depIndex = 0, dynamicImportEndStack = [];
606
- function pushStringTo (originalIndex) {
607
- while (dynamicImportEndStack[dynamicImportEndStack.length - 1] < originalIndex) {
608
- const dynamicImportEnd = dynamicImportEndStack.pop();
609
- resolvedSource += `${source.slice(lastIndex, dynamicImportEnd)}, ${urlJsString(load.r)}`;
610
- lastIndex = dynamicImportEnd;
611
- }
612
- resolvedSource += source.slice(lastIndex, originalIndex);
613
- lastIndex = originalIndex;
600
+ // once all deps have loaded we can inline the dependency resolution blobs
601
+ // and define this blob
602
+ let lastIndex = 0, depIndex = 0, dynamicImportEndStack = [];
603
+ function pushStringTo (originalIndex) {
604
+ while (dynamicImportEndStack[dynamicImportEndStack.length - 1] < originalIndex) {
605
+ const dynamicImportEnd = dynamicImportEndStack.pop();
606
+ resolvedSource += `${source.slice(lastIndex, dynamicImportEnd)}, ${urlJsString(load.r)}`;
607
+ lastIndex = dynamicImportEnd;
614
608
  }
615
- for (const { s: start, ss: statementStart, se: statementEnd, d: dynamicImportIndex } of imports) {
616
- // dependency source replacements
617
- if (dynamicImportIndex === -1) {
618
- let depLoad = load.d[depIndex++], blobUrl = depLoad.b, cycleShell = !blobUrl;
619
- if (cycleShell) {
620
- // circular shell creation
621
- if (!(blobUrl = depLoad.s)) {
622
- blobUrl = depLoad.s = createBlob(`export function u$_(m){${
623
- depLoad.a[1].map(({ s, e }, i) => {
624
- const q = depLoad.S[s] === '"' || depLoad.S[s] === "'";
625
- return `e$_${i}=m${q ? `[` : '.'}${depLoad.S.slice(s, e)}${q ? `]` : ''}`;
626
- }).join(',')
627
- }}${
628
- depLoad.a[1].length ? `let ${depLoad.a[1].map((_, i) => `e$_${i}`).join(',')};` : ''
629
- }export {${
630
- depLoad.a[1].map(({ s, e }, i) => `e$_${i} as ${depLoad.S.slice(s, e)}`).join(',')
631
- }}\n//# sourceURL=${depLoad.r}?cycle`);
632
- }
633
- }
634
-
635
- pushStringTo(start - 1);
636
- resolvedSource += `/*${source.slice(start - 1, statementEnd)}*/${urlJsString(blobUrl)}`;
609
+ resolvedSource += source.slice(lastIndex, originalIndex);
610
+ lastIndex = originalIndex;
611
+ }
637
612
 
638
- // circular shell execution
639
- if (!cycleShell && depLoad.s) {
640
- resolvedSource += `;import*as m$_${depIndex} from'${depLoad.b}';import{u$_ as u$_${depIndex}}from'${depLoad.s}';u$_${depIndex}(m$_${depIndex})`;
641
- depLoad.s = undefined;
613
+ for (const { s: start, ss: statementStart, se: statementEnd, d: dynamicImportIndex } of imports) {
614
+ // dependency source replacements
615
+ if (dynamicImportIndex === -1) {
616
+ let depLoad = load.d[depIndex++], blobUrl = depLoad.b, cycleShell = !blobUrl;
617
+ if (cycleShell) {
618
+ // circular shell creation
619
+ if (!(blobUrl = depLoad.s)) {
620
+ blobUrl = depLoad.s = createBlob(`export function u$_(m){${
621
+ depLoad.a[1].map(({ s, e }, i) => {
622
+ const q = depLoad.S[s] === '"' || depLoad.S[s] === "'";
623
+ return `e$_${i}=m${q ? `[` : '.'}${depLoad.S.slice(s, e)}${q ? `]` : ''}`;
624
+ }).join(',')
625
+ }}${
626
+ depLoad.a[1].length ? `let ${depLoad.a[1].map((_, i) => `e$_${i}`).join(',')};` : ''
627
+ }export {${
628
+ depLoad.a[1].map(({ s, e }, i) => `e$_${i} as ${depLoad.S.slice(s, e)}`).join(',')
629
+ }}\n//# sourceURL=${depLoad.r}?cycle`);
642
630
  }
643
- lastIndex = statementEnd;
644
- }
645
- // import.meta
646
- else if (dynamicImportIndex === -2) {
647
- load.m = { url: load.r, resolve: metaResolve };
648
- metaHook(load.m, load.u);
649
- pushStringTo(start);
650
- resolvedSource += `importShim._r[${urlJsString(load.u)}].m`;
651
- lastIndex = statementEnd;
652
631
  }
653
- // dynamic import
654
- else {
655
- pushStringTo(statementStart + 6);
656
- resolvedSource += `Shim(`;
657
- dynamicImportEndStack.push(statementEnd - 1);
658
- lastIndex = start;
632
+
633
+ pushStringTo(start - 1);
634
+ resolvedSource += `/*${source.slice(start - 1, statementEnd)}*/${urlJsString(blobUrl)}`;
635
+
636
+ // circular shell execution
637
+ if (!cycleShell && depLoad.s) {
638
+ resolvedSource += `;import*as m$_${depIndex} from'${depLoad.b}';import{u$_ as u$_${depIndex}}from'${depLoad.s}';u$_${depIndex}(m$_${depIndex})`;
639
+ depLoad.s = undefined;
659
640
  }
641
+ lastIndex = statementEnd;
642
+ }
643
+ // import.meta
644
+ else if (dynamicImportIndex === -2) {
645
+ load.m = { url: load.r, resolve: metaResolve };
646
+ metaHook(load.m, load.u);
647
+ pushStringTo(start);
648
+ resolvedSource += `importShim._r[${urlJsString(load.u)}].m`;
649
+ lastIndex = statementEnd;
660
650
  }
651
+ // dynamic import
652
+ else {
653
+ pushStringTo(statementStart + 6);
654
+ resolvedSource += `Shim(`;
655
+ dynamicImportEndStack.push(statementEnd - 1);
656
+ lastIndex = start;
657
+ }
658
+ }
659
+
660
+ // support progressive cycle binding updates (try statement avoids tdz errors)
661
+ if (load.s)
662
+ resolvedSource += `\n;import{u$_}from'${load.s}';try{u$_({${exports.filter(e => e.ln).map(({ s, e, ln }) => `${source.slice(s, e)}:${ln}`).join(',')}})}catch(_){};\n`;
663
+
664
+ function pushSourceURL (commentPrefix, commentStart) {
665
+ const urlStart = commentStart + commentPrefix.length;
666
+ const commentEnd = source.indexOf('\n', urlStart);
667
+ const urlEnd = commentEnd !== -1 ? commentEnd : source.length;
668
+ pushStringTo(urlStart);
669
+ resolvedSource += new URL(source.slice(urlStart, urlEnd), load.r).href;
670
+ lastIndex = urlEnd;
671
+ }
661
672
 
662
- // support progressive cycle binding updates (try statement avoids tdz errors)
663
- if (load.s)
664
- resolvedSource += `\n;import{u$_}from'${load.s}';try{u$_({${exports.filter(e => e.ln).map(({ s, e, ln }) => `${source.slice(s, e)}:${ln}`).join(',')}})}catch(_){};\n`;
673
+ let sourceURLCommentStart = source.lastIndexOf(sourceURLCommentPrefix);
674
+ let sourceMapURLCommentStart = source.lastIndexOf(sourceMapURLCommentPrefix);
665
675
 
666
- pushStringTo(source.length);
676
+ // ignore sourceMap comments before already spliced code
677
+ if (sourceURLCommentStart < lastIndex) sourceURLCommentStart = -1;
678
+ if (sourceMapURLCommentStart < lastIndex) sourceMapURLCommentStart = -1;
679
+
680
+ // sourceURL first / only
681
+ if (sourceURLCommentStart !== -1 && (sourceMapURLCommentStart === -1 || sourceMapURLCommentStart > sourceURLCommentStart)) {
682
+ pushSourceURL(sourceURLCommentPrefix, sourceURLCommentStart);
683
+ }
684
+ // sourceMappingURL
685
+ if (sourceMapURLCommentStart !== -1) {
686
+ pushSourceURL(sourceMapURLCommentPrefix, sourceMapURLCommentStart);
687
+ // sourceURL last
688
+ if (sourceURLCommentStart !== -1 && (sourceURLCommentStart > sourceMapURLCommentStart))
689
+ pushSourceURL(sourceURLCommentPrefix, sourceURLCommentStart);
667
690
  }
668
691
 
669
- let hasSourceURL = false;
670
- resolvedSource = resolvedSource.replace(sourceMapURLRegEx, (match, isMapping, url) => (hasSourceURL = !isMapping, match.replace(url, () => new URL(url, load.r))));
671
- if (!hasSourceURL)
672
- resolvedSource += '\n//# sourceURL=' + load.r;
692
+ pushStringTo(source.length);
693
+
694
+ if (sourceURLCommentStart === -1)
695
+ resolvedSource += sourceURLCommentPrefix + load.r;
673
696
 
674
697
  load.b = lastLoad = createBlob(resolvedSource);
675
698
  load.S = undefined;
676
699
  }
677
700
 
678
- // ; and // trailer support added for Ruby on Rails 7 source maps compatibility
679
- // https://github.com/guybedford/es-module-shims/issues/228
680
- const sourceMapURLRegEx = /\n\/\/# source(Mapping)?URL=([^\n]+)\s*((;|\/\/[^#][^\n]*)\s*)*$/;
701
+ const sourceURLCommentPrefix = '\n//# sourceURL=';
702
+ const sourceMapURLCommentPrefix = '\n//# sourceMappingURL=';
681
703
 
682
704
  const jsContentType = /^(text|application)\/(x-)?javascript(;|$)/;
705
+ const wasmContentType = /^(application)\/wasm(;|$)/;
683
706
  const jsonContentType = /^(text|application)\/json(;|$)/;
684
707
  const cssContentType = /^(text|application)\/css(;|$)/;
685
708
 
@@ -723,6 +746,21 @@
723
746
  const contentType = res.headers.get('content-type');
724
747
  if (jsContentType.test(contentType))
725
748
  return { r: res.url, s: await res.text(), t: 'js' };
749
+ else if (wasmContentType.test(contentType)) {
750
+ const module = importShim._w[url] = await WebAssembly.compileStreaming(res);
751
+ let s = '', i = 0, importObj = '';
752
+ for (const impt of WebAssembly.Module.imports(module)) {
753
+ s += `import * as impt${i} from '${impt.module}';\n`;
754
+ importObj += `'${impt.module}':impt${i++},`;
755
+ }
756
+ i = 0;
757
+ s += `const instance = await WebAssembly.instantiate(importShim._w['${url}'], {${importObj}});\n`;
758
+ for (const expt of WebAssembly.Module.exports(module)) {
759
+ s += `const expt${i} = instance['${expt.name}'];\n`;
760
+ s += `export { expt${i++} as "${expt.name}" };\n`;
761
+ }
762
+ return { r: res.url, s, t: 'wasm' };
763
+ }
726
764
  else if (jsonContentType.test(contentType))
727
765
  return { r: res.url, s: `export default ${await res.text()}`, t: 'json' };
728
766
  else if (cssContentType.test(contentType)) {
@@ -1,4 +1,4 @@
1
- /* ES Module Shims Wasm 1.7.2 */
1
+ /* ES Module Shims Wasm 1.8.0 */
2
2
  (function () {
3
3
 
4
4
  const hasWindow = typeof window !== 'undefined';
@@ -458,6 +458,7 @@
458
458
  };
459
459
 
460
460
  const registry = importShim._r = {};
461
+ importShim._w = {};
461
462
 
462
463
  async function loadAll (load, seen) {
463
464
  if (load.b || seen[load.u])
@@ -596,90 +597,112 @@
596
597
  // edge doesnt execute sibling in order, so we fix this up by ensuring all previous executions are explicit dependencies
597
598
  let resolvedSource = edge && lastLoad ? `import '${lastLoad}';` : '';
598
599
 
599
- if (!imports.length) {
600
- resolvedSource += source;
601
- }
602
- else {
603
- // once all deps have loaded we can inline the dependency resolution blobs
604
- // and define this blob
605
- let lastIndex = 0, depIndex = 0, dynamicImportEndStack = [];
606
- function pushStringTo (originalIndex) {
607
- while (dynamicImportEndStack[dynamicImportEndStack.length - 1] < originalIndex) {
608
- const dynamicImportEnd = dynamicImportEndStack.pop();
609
- resolvedSource += `${source.slice(lastIndex, dynamicImportEnd)}, ${urlJsString(load.r)}`;
610
- lastIndex = dynamicImportEnd;
611
- }
612
- resolvedSource += source.slice(lastIndex, originalIndex);
613
- lastIndex = originalIndex;
600
+ // once all deps have loaded we can inline the dependency resolution blobs
601
+ // and define this blob
602
+ let lastIndex = 0, depIndex = 0, dynamicImportEndStack = [];
603
+ function pushStringTo (originalIndex) {
604
+ while (dynamicImportEndStack[dynamicImportEndStack.length - 1] < originalIndex) {
605
+ const dynamicImportEnd = dynamicImportEndStack.pop();
606
+ resolvedSource += `${source.slice(lastIndex, dynamicImportEnd)}, ${urlJsString(load.r)}`;
607
+ lastIndex = dynamicImportEnd;
614
608
  }
615
- for (const { s: start, ss: statementStart, se: statementEnd, d: dynamicImportIndex } of imports) {
616
- // dependency source replacements
617
- if (dynamicImportIndex === -1) {
618
- let depLoad = load.d[depIndex++], blobUrl = depLoad.b, cycleShell = !blobUrl;
619
- if (cycleShell) {
620
- // circular shell creation
621
- if (!(blobUrl = depLoad.s)) {
622
- blobUrl = depLoad.s = createBlob(`export function u$_(m){${
623
- depLoad.a[1].map(({ s, e }, i) => {
624
- const q = depLoad.S[s] === '"' || depLoad.S[s] === "'";
625
- return `e$_${i}=m${q ? `[` : '.'}${depLoad.S.slice(s, e)}${q ? `]` : ''}`;
626
- }).join(',')
627
- }}${
628
- depLoad.a[1].length ? `let ${depLoad.a[1].map((_, i) => `e$_${i}`).join(',')};` : ''
629
- }export {${
630
- depLoad.a[1].map(({ s, e }, i) => `e$_${i} as ${depLoad.S.slice(s, e)}`).join(',')
631
- }}\n//# sourceURL=${depLoad.r}?cycle`);
632
- }
633
- }
634
-
635
- pushStringTo(start - 1);
636
- resolvedSource += `/*${source.slice(start - 1, statementEnd)}*/${urlJsString(blobUrl)}`;
609
+ resolvedSource += source.slice(lastIndex, originalIndex);
610
+ lastIndex = originalIndex;
611
+ }
637
612
 
638
- // circular shell execution
639
- if (!cycleShell && depLoad.s) {
640
- resolvedSource += `;import*as m$_${depIndex} from'${depLoad.b}';import{u$_ as u$_${depIndex}}from'${depLoad.s}';u$_${depIndex}(m$_${depIndex})`;
641
- depLoad.s = undefined;
613
+ for (const { s: start, ss: statementStart, se: statementEnd, d: dynamicImportIndex } of imports) {
614
+ // dependency source replacements
615
+ if (dynamicImportIndex === -1) {
616
+ let depLoad = load.d[depIndex++], blobUrl = depLoad.b, cycleShell = !blobUrl;
617
+ if (cycleShell) {
618
+ // circular shell creation
619
+ if (!(blobUrl = depLoad.s)) {
620
+ blobUrl = depLoad.s = createBlob(`export function u$_(m){${
621
+ depLoad.a[1].map(({ s, e }, i) => {
622
+ const q = depLoad.S[s] === '"' || depLoad.S[s] === "'";
623
+ return `e$_${i}=m${q ? `[` : '.'}${depLoad.S.slice(s, e)}${q ? `]` : ''}`;
624
+ }).join(',')
625
+ }}${
626
+ depLoad.a[1].length ? `let ${depLoad.a[1].map((_, i) => `e$_${i}`).join(',')};` : ''
627
+ }export {${
628
+ depLoad.a[1].map(({ s, e }, i) => `e$_${i} as ${depLoad.S.slice(s, e)}`).join(',')
629
+ }}\n//# sourceURL=${depLoad.r}?cycle`);
642
630
  }
643
- lastIndex = statementEnd;
644
- }
645
- // import.meta
646
- else if (dynamicImportIndex === -2) {
647
- load.m = { url: load.r, resolve: metaResolve };
648
- metaHook(load.m, load.u);
649
- pushStringTo(start);
650
- resolvedSource += `importShim._r[${urlJsString(load.u)}].m`;
651
- lastIndex = statementEnd;
652
631
  }
653
- // dynamic import
654
- else {
655
- pushStringTo(statementStart + 6);
656
- resolvedSource += `Shim(`;
657
- dynamicImportEndStack.push(statementEnd - 1);
658
- lastIndex = start;
632
+
633
+ pushStringTo(start - 1);
634
+ resolvedSource += `/*${source.slice(start - 1, statementEnd)}*/${urlJsString(blobUrl)}`;
635
+
636
+ // circular shell execution
637
+ if (!cycleShell && depLoad.s) {
638
+ resolvedSource += `;import*as m$_${depIndex} from'${depLoad.b}';import{u$_ as u$_${depIndex}}from'${depLoad.s}';u$_${depIndex}(m$_${depIndex})`;
639
+ depLoad.s = undefined;
659
640
  }
641
+ lastIndex = statementEnd;
642
+ }
643
+ // import.meta
644
+ else if (dynamicImportIndex === -2) {
645
+ load.m = { url: load.r, resolve: metaResolve };
646
+ metaHook(load.m, load.u);
647
+ pushStringTo(start);
648
+ resolvedSource += `importShim._r[${urlJsString(load.u)}].m`;
649
+ lastIndex = statementEnd;
660
650
  }
651
+ // dynamic import
652
+ else {
653
+ pushStringTo(statementStart + 6);
654
+ resolvedSource += `Shim(`;
655
+ dynamicImportEndStack.push(statementEnd - 1);
656
+ lastIndex = start;
657
+ }
658
+ }
659
+
660
+ // support progressive cycle binding updates (try statement avoids tdz errors)
661
+ if (load.s)
662
+ resolvedSource += `\n;import{u$_}from'${load.s}';try{u$_({${exports.filter(e => e.ln).map(({ s, e, ln }) => `${source.slice(s, e)}:${ln}`).join(',')}})}catch(_){};\n`;
663
+
664
+ function pushSourceURL (commentPrefix, commentStart) {
665
+ const urlStart = commentStart + commentPrefix.length;
666
+ const commentEnd = source.indexOf('\n', urlStart);
667
+ const urlEnd = commentEnd !== -1 ? commentEnd : source.length;
668
+ pushStringTo(urlStart);
669
+ resolvedSource += new URL(source.slice(urlStart, urlEnd), load.r).href;
670
+ lastIndex = urlEnd;
671
+ }
661
672
 
662
- // support progressive cycle binding updates (try statement avoids tdz errors)
663
- if (load.s)
664
- resolvedSource += `\n;import{u$_}from'${load.s}';try{u$_({${exports.filter(e => e.ln).map(({ s, e, ln }) => `${source.slice(s, e)}:${ln}`).join(',')}})}catch(_){};\n`;
673
+ let sourceURLCommentStart = source.lastIndexOf(sourceURLCommentPrefix);
674
+ let sourceMapURLCommentStart = source.lastIndexOf(sourceMapURLCommentPrefix);
665
675
 
666
- pushStringTo(source.length);
676
+ // ignore sourceMap comments before already spliced code
677
+ if (sourceURLCommentStart < lastIndex) sourceURLCommentStart = -1;
678
+ if (sourceMapURLCommentStart < lastIndex) sourceMapURLCommentStart = -1;
679
+
680
+ // sourceURL first / only
681
+ if (sourceURLCommentStart !== -1 && (sourceMapURLCommentStart === -1 || sourceMapURLCommentStart > sourceURLCommentStart)) {
682
+ pushSourceURL(sourceURLCommentPrefix, sourceURLCommentStart);
683
+ }
684
+ // sourceMappingURL
685
+ if (sourceMapURLCommentStart !== -1) {
686
+ pushSourceURL(sourceMapURLCommentPrefix, sourceMapURLCommentStart);
687
+ // sourceURL last
688
+ if (sourceURLCommentStart !== -1 && (sourceURLCommentStart > sourceMapURLCommentStart))
689
+ pushSourceURL(sourceURLCommentPrefix, sourceURLCommentStart);
667
690
  }
668
691
 
669
- let hasSourceURL = false;
670
- resolvedSource = resolvedSource.replace(sourceMapURLRegEx, (match, isMapping, url) => (hasSourceURL = !isMapping, match.replace(url, () => new URL(url, load.r))));
671
- if (!hasSourceURL)
672
- resolvedSource += '\n//# sourceURL=' + load.r;
692
+ pushStringTo(source.length);
693
+
694
+ if (sourceURLCommentStart === -1)
695
+ resolvedSource += sourceURLCommentPrefix + load.r;
673
696
 
674
697
  load.b = lastLoad = createBlob(resolvedSource);
675
698
  load.S = undefined;
676
699
  }
677
700
 
678
- // ; and // trailer support added for Ruby on Rails 7 source maps compatibility
679
- // https://github.com/guybedford/es-module-shims/issues/228
680
- const sourceMapURLRegEx = /\n\/\/# source(Mapping)?URL=([^\n]+)\s*((;|\/\/[^#][^\n]*)\s*)*$/;
701
+ const sourceURLCommentPrefix = '\n//# sourceURL=';
702
+ const sourceMapURLCommentPrefix = '\n//# sourceMappingURL=';
681
703
 
682
704
  const jsContentType = /^(text|application)\/(x-)?javascript(;|$)/;
705
+ const wasmContentType = /^(application)\/wasm(;|$)/;
683
706
  const jsonContentType = /^(text|application)\/json(;|$)/;
684
707
  const cssContentType = /^(text|application)\/css(;|$)/;
685
708
 
@@ -723,6 +746,21 @@
723
746
  const contentType = res.headers.get('content-type');
724
747
  if (jsContentType.test(contentType))
725
748
  return { r: res.url, s: await res.text(), t: 'js' };
749
+ else if (wasmContentType.test(contentType)) {
750
+ const module = importShim._w[url] = await WebAssembly.compileStreaming(res);
751
+ let s = '', i = 0, importObj = '';
752
+ for (const impt of WebAssembly.Module.imports(module)) {
753
+ s += `import * as impt${i} from '${impt.module}';\n`;
754
+ importObj += `'${impt.module}':impt${i++},`;
755
+ }
756
+ i = 0;
757
+ s += `const instance = await WebAssembly.instantiate(importShim._w['${url}'], {${importObj}});\n`;
758
+ for (const expt of WebAssembly.Module.exports(module)) {
759
+ s += `const expt${i} = instance['${expt.name}'];\n`;
760
+ s += `export { expt${i++} as "${expt.name}" };\n`;
761
+ }
762
+ return { r: res.url, s, t: 'wasm' };
763
+ }
726
764
  else if (jsonContentType.test(contentType))
727
765
  return { r: res.url, s: `export default ${await res.text()}`, t: 'json' };
728
766
  else if (cssContentType.test(contentType)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "es-module-shims",
3
- "version": "1.7.2",
3
+ "version": "1.8.0",
4
4
  "description": "Shims for the latest ES module features",
5
5
  "main": "dist/es-module-shims.js",
6
6
  "exports": {