es-module-shims 2.0.5 → 2.0.7

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,96 +1,96 @@
1
- /* ES Module Shims Wasm 2.0.5 */
1
+ /* ES Module Shims Wasm 2.0.7 */
2
2
  (function () {
3
3
 
4
- const hasDocument = typeof document !== 'undefined';
5
-
6
- const noop = () => {};
7
-
8
- const dynamicImport = (u, errUrl) => import(u);
9
-
10
- const optionsScript = hasDocument ? document.querySelector('script[type=esms-options]') : undefined;
11
-
12
- const esmsInitOptions = optionsScript ? JSON.parse(optionsScript.innerHTML) : {};
13
- Object.assign(esmsInitOptions, self.esmsInitOptions || {});
14
-
15
- // shim mode is determined on initialization, no late shim mode
16
- const shimMode =
17
- hasDocument ?
18
- esmsInitOptions.shimMode ||
19
- document.querySelectorAll('script[type=module-shim],script[type=importmap-shim],link[rel=modulepreload-shim]')
20
- .length > 0
21
- : true;
22
-
23
- const importHook = globalHook(shimMode && esmsInitOptions.onimport);
24
- const resolveHook = globalHook(shimMode && esmsInitOptions.resolve);
25
- let fetchHook = esmsInitOptions.fetch ? globalHook(esmsInitOptions.fetch) : fetch;
26
- const metaHook = esmsInitOptions.meta ? globalHook(shimMode && esmsInitOptions.meta) : noop;
27
- const tsTransform =
28
- esmsInitOptions.tsTransform ||
29
- (document.currentScript &&
30
- document.currentScript.src.replace(/\.js$/, '-typescript.js')) ||
31
- './es-module-shims-typescript.js';
32
-
33
- const mapOverrides = esmsInitOptions.mapOverrides;
34
-
35
- let nonce = esmsInitOptions.nonce;
36
- if (!nonce && hasDocument) {
37
- const nonceElement = document.querySelector('script[nonce]');
38
- if (nonceElement) nonce = nonceElement.nonce || nonceElement.getAttribute('nonce');
39
- }
40
-
41
- const onerror = globalHook(esmsInitOptions.onerror || noop);
42
-
43
- const { revokeBlobURLs, noLoadEventRetriggers, enforceIntegrity } = esmsInitOptions;
44
-
45
- function globalHook(name) {
46
- return typeof name === 'string' ? self[name] : name;
47
- }
48
-
49
- const enable = Array.isArray(esmsInitOptions.polyfillEnable) ? esmsInitOptions.polyfillEnable : [];
50
- const enableAll = esmsInitOptions.polyfillEnable === 'all' || enable.includes('all');
51
- const enableLatest = esmsInitOptions.polyfillEnable === 'latest' || enable.includes('latest');
52
- const cssModulesEnabled = enable.includes('css-modules') || enableAll || enableLatest;
53
- const jsonModulesEnabled = enable.includes('json-modules') || enableAll || enableLatest;
54
- const wasmModulesEnabled = enable.includes('wasm-modules') || enableAll;
55
- const sourcePhaseEnabled = enable.includes('source-phase') || enableAll;
56
- const typescriptEnabled = enable.includes('typescript') || enableAll;
57
-
58
- const onpolyfill =
59
- esmsInitOptions.onpolyfill ?
60
- globalHook(esmsInitOptions.onpolyfill)
61
- : () => {
62
- console.log(`%c^^ Module error above is polyfilled and can be ignored ^^`, 'font-weight:900;color:#391');
63
- };
64
-
65
- const baseUrl =
66
- hasDocument ?
67
- document.baseURI
68
- : `${location.protocol}//${location.host}${
69
- location.pathname.includes('/') ?
70
- location.pathname.slice(0, location.pathname.lastIndexOf('/') + 1)
71
- : location.pathname
72
- }`;
73
-
74
- const createBlob = (source, type = 'text/javascript') => URL.createObjectURL(new Blob([source], { type }));
75
- let { skip } = esmsInitOptions;
76
- if (Array.isArray(skip)) {
77
- const l = skip.map(s => new URL(s, baseUrl).href);
78
- skip = s => l.some(i => (i[i.length - 1] === '/' && s.startsWith(i)) || s === i);
79
- } else if (typeof skip === 'string') {
80
- const r = new RegExp(skip);
81
- skip = s => r.test(s);
82
- } else if (skip instanceof RegExp) {
83
- skip = s => skip.test(s);
84
- }
85
-
86
- const dispatchError = error => self.dispatchEvent(Object.assign(new Event('error'), { error }));
87
-
88
- const throwError = err => {
89
- (self.reportError || dispatchError)(err), void onerror(err);
90
- };
91
-
92
- function fromParent(parent) {
93
- return parent ? ` imported from ${parent}` : '';
4
+ const hasDocument = typeof document !== 'undefined';
5
+
6
+ const noop = () => {};
7
+
8
+ const dynamicImport = (u, errUrl) => import(u);
9
+
10
+ const optionsScript = hasDocument ? document.querySelector('script[type=esms-options]') : undefined;
11
+
12
+ const esmsInitOptions = optionsScript ? JSON.parse(optionsScript.innerHTML) : {};
13
+ Object.assign(esmsInitOptions, self.esmsInitOptions || {});
14
+
15
+ // shim mode is determined on initialization, no late shim mode
16
+ const shimMode =
17
+ hasDocument ?
18
+ esmsInitOptions.shimMode ||
19
+ document.querySelectorAll('script[type=module-shim],script[type=importmap-shim],link[rel=modulepreload-shim]')
20
+ .length > 0
21
+ : true;
22
+
23
+ const importHook = globalHook(shimMode && esmsInitOptions.onimport);
24
+ const resolveHook = globalHook(shimMode && esmsInitOptions.resolve);
25
+ let fetchHook = esmsInitOptions.fetch ? globalHook(esmsInitOptions.fetch) : fetch;
26
+ const metaHook = esmsInitOptions.meta ? globalHook(shimMode && esmsInitOptions.meta) : noop;
27
+ const tsTransform =
28
+ esmsInitOptions.tsTransform ||
29
+ (document.currentScript &&
30
+ document.currentScript.src.replace(/\.js$/, '-typescript.js')) ||
31
+ './es-module-shims-typescript.js';
32
+
33
+ const mapOverrides = esmsInitOptions.mapOverrides;
34
+
35
+ let nonce = esmsInitOptions.nonce;
36
+ if (!nonce && hasDocument) {
37
+ const nonceElement = document.querySelector('script[nonce]');
38
+ if (nonceElement) nonce = nonceElement.nonce || nonceElement.getAttribute('nonce');
39
+ }
40
+
41
+ const onerror = globalHook(esmsInitOptions.onerror || noop);
42
+
43
+ const { revokeBlobURLs, noLoadEventRetriggers, enforceIntegrity } = esmsInitOptions;
44
+
45
+ function globalHook(name) {
46
+ return typeof name === 'string' ? self[name] : name;
47
+ }
48
+
49
+ const enable = Array.isArray(esmsInitOptions.polyfillEnable) ? esmsInitOptions.polyfillEnable : [];
50
+ const enableAll = esmsInitOptions.polyfillEnable === 'all' || enable.includes('all');
51
+ const enableLatest = esmsInitOptions.polyfillEnable === 'latest' || enable.includes('latest');
52
+ const cssModulesEnabled = enable.includes('css-modules') || enableAll || enableLatest;
53
+ const jsonModulesEnabled = enable.includes('json-modules') || enableAll || enableLatest;
54
+ const wasmModulesEnabled = enable.includes('wasm-modules') || enableAll;
55
+ const sourcePhaseEnabled = enable.includes('source-phase') || enableAll;
56
+ const typescriptEnabled = enable.includes('typescript') || enableAll;
57
+
58
+ const onpolyfill =
59
+ esmsInitOptions.onpolyfill ?
60
+ globalHook(esmsInitOptions.onpolyfill)
61
+ : () => {
62
+ console.log(`%c^^ Module error above is polyfilled and can be ignored ^^`, 'font-weight:900;color:#391');
63
+ };
64
+
65
+ const baseUrl =
66
+ hasDocument ?
67
+ document.baseURI
68
+ : `${location.protocol}//${location.host}${
69
+ location.pathname.includes('/') ?
70
+ location.pathname.slice(0, location.pathname.lastIndexOf('/') + 1)
71
+ : location.pathname
72
+ }`;
73
+
74
+ const createBlob = (source, type = 'text/javascript') => URL.createObjectURL(new Blob([source], { type }));
75
+ let { skip } = esmsInitOptions;
76
+ if (Array.isArray(skip)) {
77
+ const l = skip.map(s => new URL(s, baseUrl).href);
78
+ skip = s => l.some(i => (i[i.length - 1] === '/' && s.startsWith(i)) || s === i);
79
+ } else if (typeof skip === 'string') {
80
+ const r = new RegExp(skip);
81
+ skip = s => r.test(s);
82
+ } else if (skip instanceof RegExp) {
83
+ skip = s => skip.test(s);
84
+ }
85
+
86
+ const dispatchError = error => self.dispatchEvent(Object.assign(new Event('error'), { error }));
87
+
88
+ const throwError = err => {
89
+ (self.reportError || dispatchError)(err), void onerror(err);
90
+ };
91
+
92
+ function fromParent(parent) {
93
+ return parent ? ` imported from ${parent}` : '';
94
94
  }
95
95
 
96
96
  const backslashRegEx = /\\/g;
@@ -453,17 +453,18 @@
453
453
  }
454
454
  // we mock import('./x.css', { with: { type: 'css' }}) support via an inline static reexport
455
455
  // because we can't syntactically pass through to dynamic import with a second argument in this libarary
456
- const url = await importHandler(id, opts, parentUrl);
457
- const source =
458
- typeof opts === 'object' && typeof opts.with === 'object' && typeof opts.with.type === 'string' ?
459
- `export{default}from'${url}'with{type:"${opts.with.type}"}`
460
- : null;
461
- return topLevelLoad(url, { credentials: 'same-origin' }, source);
456
+ let url = await importHandler(id, opts, parentUrl);
457
+ let source = null;
458
+ if (typeof opts === 'object' && typeof opts.with === 'object' && typeof opts.with.type === 'string') {
459
+ source = `export{default}from'${url}'with{type:"${opts.with.type}"}`;
460
+ url += '?entry';
461
+ }
462
+ return topLevelLoad(url, { credentials: 'same-origin' }, source, undefined, undefined);
462
463
  }
463
464
 
464
465
  // import.source()
465
466
  // (opts not currently supported as no use cases yet)
466
- if (sourcePhaseEnabled)
467
+ if (shimMode || sourcePhaseEnabled)
467
468
  importShim.source = async function importShimSource(specifier, opts, parentUrl) {
468
469
  if (typeof opts === 'string') {
469
470
  parentUrl = opts;
@@ -536,7 +537,12 @@
536
537
  (!multipleImportMaps || supportsMultipleImportMaps) &&
537
538
  !importMapSrc &&
538
539
  !typescriptEnabled;
539
- if (sourcePhaseEnabled && typeof WebAssembly !== 'undefined' && !Object.getPrototypeOf(WebAssembly.Module).name) {
540
+ if (
541
+ !shimMode &&
542
+ sourcePhaseEnabled &&
543
+ typeof WebAssembly !== 'undefined' &&
544
+ !Object.getPrototypeOf(WebAssembly.Module).name
545
+ ) {
540
546
  const s = Symbol();
541
547
  const brand = m =>
542
548
  Object.defineProperty(m, s, { writable: false, configurable: false, value: 'WebAssembly.Module' });
@@ -873,6 +879,10 @@
873
879
  }
874
880
 
875
881
  let esmsTsTransform;
882
+ async function initTs() {
883
+ const m = await import(tsTransform);
884
+ if (!esmsTsTransform) esmsTsTransform = m.transform;
885
+ }
876
886
 
877
887
  async function fetchModule(url, fetchOpts, parent) {
878
888
  const mapIntegrity = composedImportMap.integrity[url];
@@ -913,12 +923,12 @@
913
923
  )});export default s;`,
914
924
  t: 'css'
915
925
  };
916
- } else if ((typescriptEnabled && tsContentType.test(contentType)) || url.endsWith('.ts') || url.endsWith('.mts')) {
926
+ } else if (
927
+ (shimMode || typescriptEnabled) &&
928
+ (tsContentType.test(contentType) || url.endsWith('.ts') || url.endsWith('.mts'))
929
+ ) {
917
930
  const source = await res.text();
918
- // if we don't have a ts transform hook, try to load it
919
- if (!esmsTsTransform) {
920
- ({ transform: esmsTsTransform } = await import(tsTransform));
921
- }
931
+ if (!esmsTsTransform) await initTs();
922
932
  const transformed = esmsTsTransform(source, url);
923
933
  return { r, s: transformed || source, t: transformed ? 'ts' : 'js' };
924
934
  } else
@@ -1008,12 +1018,16 @@
1008
1018
  load.L = load.f.then(async () => {
1009
1019
  let childFetchOpts = fetchOpts;
1010
1020
  load.d = load.a[0]
1011
- .map(({ n, d, t }) => {
1021
+ .map(({ n, d, t, a }) => {
1012
1022
  const sourcePhase = t >= 4;
1013
1023
  if (sourcePhase) {
1014
- if (!sourcePhaseEnabled) throw featErr('source-phase');
1024
+ if (!shimMode && !sourcePhaseEnabled) throw featErr('source-phase');
1015
1025
  if (!supportsSourcePhase) load.n = true;
1016
1026
  }
1027
+ if (a > 0) {
1028
+ if (!shimMode && !cssModulesEnabled && !jsonModulesEnabled) throw featErr('css-modules / json-modules');
1029
+ if (!supportsCssType && !supportsJsonType) load.n = true;
1030
+ }
1017
1031
  if (d !== -1 || !n) return;
1018
1032
  const resolved = resolve(n, load.r || load.u);
1019
1033
  if (resolved.n) load.n = true;
@@ -1118,7 +1132,7 @@
1118
1132
  if (!firstImportMap && legacyAcceptingImportMaps) importMapPromise.then(() => (firstImportMap = composedImportMap));
1119
1133
  if (!legacyAcceptingImportMaps && !multipleImportMaps) {
1120
1134
  multipleImportMaps = true;
1121
- if (baselinePassthrough && !supportsMultipleImportMaps) {
1135
+ if (!shimMode && baselinePassthrough && !supportsMultipleImportMaps) {
1122
1136
  baselinePassthrough = false;
1123
1137
  if (hasDocument) attachMutationObserver();
1124
1138
  }
@@ -1128,6 +1142,20 @@
1128
1142
 
1129
1143
  function processScript(script, ready = readyStateCompleteCnt > 0) {
1130
1144
  if (epCheck(script, ready)) return;
1145
+ if (script.lang === 'ts' && !script.src) {
1146
+ const source = script.innerHTML;
1147
+ return initTs()
1148
+ .then(() =>
1149
+ topLevelLoad(
1150
+ baseUrl,
1151
+ getFetchOpts(script),
1152
+ esmsTsTransform(source, baseUrl) || source,
1153
+ undefined,
1154
+ undefined
1155
+ )
1156
+ )
1157
+ .catch(throwError);
1158
+ }
1131
1159
  // does this load block readystate complete
1132
1160
  const isBlockingReadyScript = script.getAttribute('async') === null && readyStateCompleteCnt > 0;
1133
1161
  // does this load block DOMContentLoaded
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "es-module-shims",
3
- "version": "2.0.5",
3
+ "version": "2.0.7",
4
4
  "description": "Shims for the latest ES module features",
5
5
  "main": "dist/es-module-shims.js",
6
6
  "exports": {
@@ -31,7 +31,7 @@
31
31
  "license": "MIT",
32
32
  "devDependencies": {
33
33
  "@rollup/plugin-replace": "^2.4.2",
34
- "amaro": "^0.1.8",
34
+ "amaro": "^0.3.0",
35
35
  "es-module-lexer": "1.6.0",
36
36
  "kleur": "^4.1.4",
37
37
  "mime-types": "^2.1.33",