es-module-shims 1.3.0 → 1.3.4

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/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ES Module Shims 1.3.4 (2021/12/02)
2
+ * Support source mapping normalization with trailing comments (https://github.com/guybedford/es-module-shims/pull/229)
3
+
4
+ ES Module Shims 1.3.3 (2021/11/30))
5
+ * Remove use of document.write (https://github.com/guybedford/es-module-shims/pull/227, @lewisl9029)
6
+
7
+ ES Module Shims 1.3.2 (2021/11/16)
8
+ * Fix CSP nonce detection (https://github.com/guybedford/es-module-shims/pull/223, @MathiasWP)
9
+
10
+ ES Module Shims 1.3.1 (2021/11/04)
11
+ * Feature: Support "async" attribute ordering on polyfill and shim scripts (https://github.com/guybedford/es-module-shims/pull/221)
12
+ * Fix: Sourcemap regex fix (https://github.com/guybedford/es-module-shims/pull/218)
13
+ * Fix: Polyfill duplicate instance bug (https://github.com/guybedford/es-module-shims/pull/220)
14
+
1
15
  ES Module Shims 1.3.0 (2021/10/14)
2
16
  * Feature: Shim mode function to get import map, dynamic map overrides now throw (https://github.com/guybedford/es-module-shims/pull/213, @lewisl9029)
3
17
  * Feature: Upgrade to es-module-lexer@0.9.3 with import / export strings support (https://github.com/guybedford/es-module-shims/pull/210)
package/README.md CHANGED
@@ -47,8 +47,8 @@ ES Module Shims is designed for production performance. A [comprehensive benchma
47
47
 
48
48
  Benchmark summary:
49
49
 
50
- * [ES Module Shims Chrome Passthrough](bench/README.md#chrome-passthrough-performance) results in ~5ms extra initialization time over native for ES Module Shims fetching, execution and initialization, and on a slow connection the additional non-blocking bandwidth cost of its 10KB compressed download as expected.
51
- * [ES Module Shims Polyfilling](bench/README.md#native-v-polyfill-performance) is on average 1.4x - 1.5x slower than native module loading, and up to 1.8x slower on slow networks (most likely due to the browser preloader), both for cached and uncached loads, and this result scales linearly up to 10MB and 20k modules loaded executing on the fastest connection in just over 2 seconds in Firefox.
50
+ * [ES Module Shims Chrome Passthrough](bench/README.md#chrome-passthrough-performance) (for [70% of users](https://caniuse.com/import-maps)) results in ~5ms extra initialization time over native for ES Module Shims fetching, execution and initialization, and on a slow connection the additional non-blocking bandwidth cost of its 10KB compressed download as expected.
51
+ * [ES Module Shims Polyfilling](bench/README.md#native-v-polyfill-performance) (for the remaining [30% of users](https://caniuse.com/import-maps)) is on average 1.4x - 1.5x slower than native module loading, and up to 1.8x slower on slow networks (most likely due to the browser preloader), both for cached and uncached loads, and this result scales linearly up to 10MB and 20k modules loaded executing on the fastest connection in just over 2 seconds in Firefox.
52
52
  * [Very large import maps](bench/README.md#large-import-maps-performance) (100s of entries) cost only a few extra milliseconds upfront for the additional loading cost.
53
53
 
54
54
  ### Polyfill Mode
@@ -85,7 +85,7 @@ If using CSS modules or JSON modules, since these features are relatively new, t
85
85
 
86
86
  ```html
87
87
  <script>
88
- window.esmsInitOptions = { enable: ['css-modules', 'json-modules'] }
88
+ window.esmsInitOptions = { polyfillEnable: ['css-modules', 'json-modules'] }
89
89
  </script>
90
90
  ```
91
91
 
@@ -120,10 +120,12 @@ Works in all browsers with [baseline ES module support](https://caniuse.com/#fea
120
120
  | [JSON Modules](#json-modules) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
121
121
  | [CSS Modules](#css-modules) | :heavy_check_mark:<sup>3</sup> | :heavy_check_mark:<sup>3</sup> | :heavy_check_mark:<sup>3</sup> | :heavy_check_mark:<sup>3</sup> |
122
122
  | [import.meta.resolve](#resolve) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
123
+ | [Top-Level Await](#tla) | :heavy_check_mark: 89+ | :heavy_check_mark: 89+ | :x:<sup>4</sup> | :x:<sup>4</sup> |
123
124
 
124
125
  * 1: _The Edge parallel execution ordering bug is corrected by ES Module Shims with an execution chain inlining approach._
125
126
  * 2: _Module worker support cannot be implemented without dynamic import support in web workers._
126
127
  * 3: _CSS module support requires a separate [Constructable Stylesheets polyfill](https://github.com/calebdwilliams/construct-style-sheets#readme)._
128
+ * 4: _Top-level await support is possible for ES Module Shims to implement, with the feature request tracking in https://github.com/guybedford/es-module-shims/issues/5._
127
129
 
128
130
  #### Current browser compatibility of modules features without ES module shims:
129
131
 
@@ -138,6 +140,7 @@ Works in all browsers with [baseline ES module support](https://caniuse.com/#fea
138
140
  | [JSON Modules](#json-modules) | :heavy_check_mark: 91+ | :x: | :x: | :x: |
139
141
  | [CSS Modules](#css-modules) | :heavy_check_mark: 95+ | :x: | :x: | :x: |
140
142
  | [import.meta.resolve](#resolve) | :x: | :x: | :x: | :x: |
143
+ | [Top-Level Await](#tla) | :heavy_check_mark: 89+ | :heavy_check_mark: 89+ | :x: | :x: |
141
144
 
142
145
  * 1: _Edge executes parallel dependencies in non-deterministic order. ([ChakraCore bug](https://github.com/microsoft/ChakraCore/issues/6261))._
143
146
  * ~: _Indicates the exact first version support has not yet been determined (PR's welcome!)._
@@ -1,4 +1,4 @@
1
- /* ES Module Shims 1.3.0 */
1
+ /* ES Module Shims 1.3.4 */
2
2
  (function () {
3
3
 
4
4
  const edge = navigator.userAgent.match(/Edge\/\d\d\.\d+$/);
@@ -211,7 +211,7 @@
211
211
  if (!nonce) {
212
212
  const nonceElement = document.querySelector('script[nonce]');
213
213
  if (nonceElement)
214
- nonce = nonceElement.getAttribute('nonce');
214
+ nonce = nonceElement.nonce || nonceElement.getAttribute('nonce');
215
215
  }
216
216
 
217
217
  const onerror = globalHook(esmsInitOptions$1.onerror || noop);
@@ -298,8 +298,7 @@
298
298
  const iframe = document.createElement('iframe');
299
299
  iframe.style.display = 'none';
300
300
  document.head.appendChild(iframe);
301
- // we use document.write here because eg Weixin built-in browser doesn't support setting srcdoc
302
- iframe.contentWindow.document.write(`<script type=importmap nonce="${nonce}">{"imports":{"x":"data:text/javascript,"}}<${''}/script><script nonce="${nonce}">import('x').then(()=>1,()=>0).then(v=>parent._$s(v))<${''}/script>`);
301
+ iframe.src = createBlob(`<script type=importmap nonce="${nonce}">{"imports":{"x":"data:text/javascript,"}}<${''}/script><script nonce="${nonce}">import('x').then(()=>1,()=>0).then(v=>parent._$s(v))<${''}/script>`, 'text/html');
303
302
  })
304
303
  ]);
305
304
  });
@@ -404,7 +403,7 @@
404
403
  if (revokeBlobURLs) revokeObjectURLs(Object.keys(seen));
405
404
  return module;
406
405
  }
407
- const module = await dynamicImport((shimMode || load.n) ? load.b : load.u, { errUrl: load.u });
406
+ const module = await dynamicImport(!shimMode && !load.n && nativelyLoaded ? load.u : load.b, { errUrl: load.u });
408
407
  // if the top-level load is a shell, run its update function
409
408
  if (load.s)
410
409
  (await dynamicImport(load.s)).u$_(module);
@@ -530,9 +529,9 @@
530
529
  resolvedSource += source.slice(lastIndex);
531
530
  }
532
531
 
533
- resolvedSource = resolvedSource.replace(/\/\/# sourceMappingURL=(.*)\s*$/, (match, url) => match.replace(url, () => new URL(url, load.r)));
532
+ // ; and // trailer support added for Ruby 7 source maps compatibility
534
533
  let hasSourceURL = false;
535
- resolvedSource = resolvedSource.replace(/\/\/# sourceURL=(.*)\s*$/, (match, url) => (hasSourceURL = true, match.replace(url, () => new URL(url, load.r))));
534
+ resolvedSource = resolvedSource.replace(sourceMapURLRegEx, (match, isMapping, url) => (hasSourceURL = !isMapping, match.replace(url, () => new URL(url, load.r))));
536
535
  if (!hasSourceURL)
537
536
  resolvedSource += '\n//# sourceURL=' + load.r;
538
537
 
@@ -540,6 +539,8 @@
540
539
  load.S = undefined;
541
540
  }
542
541
 
542
+ const sourceMapURLRegEx = /\n\/\/# source(Mapping)?URL=([^\n]+)\s*((;|\/\/[^#][^\n]+)\s*)*$/;
543
+
543
544
  const jsContentType = /^(text|application)\/(x-)?javascript(;|$)/;
544
545
  const jsonContentType = /^(text|application)\/json(;|$)/;
545
546
  const cssContentType = /^(text|application)\/css(;|$)/;
@@ -760,11 +761,12 @@
760
761
  const isDomContentLoadedScript = domContentLoadedCnt > 0;
761
762
  if (isReadyScript) readyStateCompleteCnt++;
762
763
  if (isDomContentLoadedScript) domContentLoadedCnt++;
763
- const loadPromise = topLevelLoad(script.src || `${baseUrl}?${id++}`, getFetchOpts(script), !script.src && script.innerHTML, !shimMode, isReadyScript && lastStaticLoadPromise).catch(e => {
764
+ const blocks = script.getAttribute('async') === null && isReadyScript;
765
+ const loadPromise = topLevelLoad(script.src || `${baseUrl}?${id++}`, getFetchOpts(script), !script.src && script.innerHTML, !shimMode, blocks && lastStaticLoadPromise).catch(e => {
764
766
  setTimeout(() => { throw e });
765
767
  onerror(e);
766
768
  });
767
- if (isReadyScript)
769
+ if (blocks)
768
770
  lastStaticLoadPromise = loadPromise.then(readyStateCompleteCheck);
769
771
  if (isDomContentLoadedScript)
770
772
  loadPromise.then(domContentLoadedCheck);
@@ -1,4 +1,4 @@
1
- /* ES Module Shims Wasm 1.3.0 */
1
+ /* ES Module Shims Wasm 1.3.4 */
2
2
  (function () {
3
3
 
4
4
  const edge = navigator.userAgent.match(/Edge\/\d\d\.\d+$/);
@@ -211,7 +211,7 @@
211
211
  if (!nonce) {
212
212
  const nonceElement = document.querySelector('script[nonce]');
213
213
  if (nonceElement)
214
- nonce = nonceElement.getAttribute('nonce');
214
+ nonce = nonceElement.nonce || nonceElement.getAttribute('nonce');
215
215
  }
216
216
 
217
217
  const onerror = globalHook(esmsInitOptions$1.onerror || noop);
@@ -295,8 +295,7 @@
295
295
  const iframe = document.createElement('iframe');
296
296
  iframe.style.display = 'none';
297
297
  document.head.appendChild(iframe);
298
- // we use document.write here because eg Weixin built-in browser doesn't support setting srcdoc
299
- iframe.contentWindow.document.write(`<script type=importmap nonce="${nonce}">{"imports":{"x":"data:text/javascript,"}}<${''}/script><script nonce="${nonce}">import('x').then(()=>1,()=>0).then(v=>parent._$s(v))<${''}/script>`);
298
+ iframe.src = createBlob(`<script type=importmap nonce="${nonce}">{"imports":{"x":"data:text/javascript,"}}<${''}/script><script nonce="${nonce}">import('x').then(()=>1,()=>0).then(v=>parent._$s(v))<${''}/script>`, 'text/html');
300
299
  })
301
300
  ]);
302
301
  });
@@ -402,7 +401,7 @@
402
401
  if (revokeBlobURLs) revokeObjectURLs(Object.keys(seen));
403
402
  return module;
404
403
  }
405
- const module = await dynamicImport((shimMode || load.n) ? load.b : load.u, { errUrl: load.u });
404
+ const module = await dynamicImport(!shimMode && !load.n && nativelyLoaded ? load.u : load.b, { errUrl: load.u });
406
405
  // if the top-level load is a shell, run its update function
407
406
  if (load.s)
408
407
  (await dynamicImport(load.s)).u$_(module);
@@ -528,9 +527,9 @@
528
527
  resolvedSource += source.slice(lastIndex);
529
528
  }
530
529
 
531
- resolvedSource = resolvedSource.replace(/\/\/# sourceMappingURL=(.*)\s*$/, (match, url) => match.replace(url, () => new URL(url, load.r)));
530
+ // ; and // trailer support added for Ruby 7 source maps compatibility
532
531
  let hasSourceURL = false;
533
- resolvedSource = resolvedSource.replace(/\/\/# sourceURL=(.*)\s*$/, (match, url) => (hasSourceURL = true, match.replace(url, () => new URL(url, load.r))));
532
+ resolvedSource = resolvedSource.replace(sourceMapURLRegEx, (match, isMapping, url) => (hasSourceURL = !isMapping, match.replace(url, () => new URL(url, load.r))));
534
533
  if (!hasSourceURL)
535
534
  resolvedSource += '\n//# sourceURL=' + load.r;
536
535
 
@@ -538,6 +537,8 @@
538
537
  load.S = undefined;
539
538
  }
540
539
 
540
+ const sourceMapURLRegEx = /\n\/\/# source(Mapping)?URL=([^\n]+)\s*((;|\/\/[^#][^\n]+)\s*)*$/;
541
+
541
542
  const jsContentType = /^(text|application)\/(x-)?javascript(;|$)/;
542
543
  const jsonContentType = /^(text|application)\/json(;|$)/;
543
544
  const cssContentType = /^(text|application)\/css(;|$)/;
@@ -758,11 +759,12 @@
758
759
  const isDomContentLoadedScript = domContentLoadedCnt > 0;
759
760
  if (isReadyScript) readyStateCompleteCnt++;
760
761
  if (isDomContentLoadedScript) domContentLoadedCnt++;
761
- const loadPromise = topLevelLoad(script.src || `${baseUrl}?${id++}`, getFetchOpts(script), !script.src && script.innerHTML, !shimMode, isReadyScript && lastStaticLoadPromise).catch(e => {
762
+ const blocks = script.getAttribute('async') === null && isReadyScript;
763
+ const loadPromise = topLevelLoad(script.src || `${baseUrl}?${id++}`, getFetchOpts(script), !script.src && script.innerHTML, !shimMode, blocks && lastStaticLoadPromise).catch(e => {
762
764
  setTimeout(() => { throw e });
763
765
  onerror(e);
764
766
  });
765
- if (isReadyScript)
767
+ if (blocks)
766
768
  lastStaticLoadPromise = loadPromise.then(readyStateCompleteCheck);
767
769
  if (isDomContentLoadedScript)
768
770
  loadPromise.then(domContentLoadedCheck);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "es-module-shims",
3
- "version": "1.3.0",
3
+ "version": "1.3.4",
4
4
  "description": "Shims for the latest ES module features",
5
5
  "main": "dist/es-module-shims.js",
6
6
  "exports": {
@@ -11,12 +11,13 @@
11
11
  "build": "rollup -c",
12
12
  "bench": "bash bench/bench.sh",
13
13
  "bench:clear": "rm -rf bench/results",
14
- "footprint": "npm run build && cat dist/es-module-shims.js | brotli | wc -c",
15
- "footprint:csp": "npm run build && cat dist/es-module-shims.csp.js | brotli | wc -c",
14
+ "footprint": "npm run build ; cat dist/es-module-shims.js | brotli | wc -c",
15
+ "footprint:csp": "npm run build ; cat dist/es-module-shims.csp.js | brotli | wc -c",
16
16
  "prepublishOnly": "npm run build",
17
- "test": "npm run test:test-base-href && npm run test:test-csp && npm run test:test-early-module-load && npm run test:test-polyfill && npm run test:test-polyfill-wasm && npm run test:test-preload-case && npm run test:test-revoke-blob-urls && npm run test:test-shim",
17
+ "test": "npm run test:test-base-href ; npm run test:test-csp ; npm run test:test-dom-order ; npm run test:test-early-module-load ; npm run test:test-polyfill ; npm run test:test-polyfill-wasm ; npm run test:test-preload-case ; npm run test:test-revoke-blob-urls ; npm run test:test-shim",
18
18
  "test:test-base-href": "cross-env TEST_NAME=test-base-href node test/server.mjs",
19
19
  "test:test-csp": "cross-env TEST_NAME=test-csp node test/server.mjs",
20
+ "test:test-dom-order": "cross-env TEST_NAME=test-dom-order node test/server.mjs",
20
21
  "test:test-early-module-load": "cross-env TEST_NAME=test-early-module-load node test/server.mjs",
21
22
  "test:test-polyfill": "cross-env TEST_NAME=test-polyfill node test/server.mjs",
22
23
  "test:test-polyfill-wasm": "cross-env TEST_NAME=test-polyfill-wasm node test/server.mjs",