es-module-shims 1.8.2 → 1.9.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
@@ -12,6 +12,7 @@ The following modules features are polyfilled:
12
12
  * Dynamic `import()` shimming when necessary in eg older Firefox versions.
13
13
  * `import.meta` and `import.meta.url`.
14
14
  * [JSON](#json-modules) and [CSS modules](#css-modules) with import assertions (when enabled).
15
+ * [Wasm modules](#wasm-modules) with support for Source Phase Imports (when enabled).
15
16
  * [`<link rel="modulepreload">` is shimmed](#modulepreload) in browsers without import maps support.
16
17
 
17
18
  When running in shim mode, module rewriting is applied for all users and custom [resolve](#resolve-hook) and [fetch](#fetch-hook) hooks can be implemented allowing for custom resolution and streaming in-browser transform workflows.
@@ -29,7 +30,7 @@ Because we are still using the native module loader the edge cases work out comp
29
30
  Include ES Module Shims with a `async` attribute on the script, then include an import map and module scripts normally:
30
31
 
31
32
  ```html
32
- <script async src="https://ga.jspm.io/npm:es-module-shims@1.8.2/dist/es-module-shims.js"></script>
33
+ <script async src="https://ga.jspm.io/npm:es-module-shims@1.9.0/dist/es-module-shims.js"></script>
33
34
 
34
35
  <!-- https://generator.jspm.io/#U2NhYGBkDM0rySzJSU1hKEpNTC5xMLTQM9Az0C1K1jMAAKFS5w0gAA -->
35
36
  <script type="importmap">
@@ -71,7 +72,7 @@ If one is found, it will then be reexecuted through ES Module Shims using its in
71
72
  When the polyfill kicks in another console log message is output(which can be disabled or customized via the [polyfill hook](#polyfill-hook)):
72
73
 
73
74
  ```
74
- ^^ Module TypeError above is polyfilled and can be ignored ^^
75
+ ^^ Module error above is polyfilled and can be ignored ^^
75
76
  ```
76
77
 
77
78
  ### Polyfill Edge Case: Dynamic Import
@@ -182,7 +183,7 @@ If using more modern features like CSS Modules or JSON Modules, these need to be
182
183
 
183
184
  ```html
184
185
  <script>
185
- window.esmsInitOptions = { polyfillEnable: ['css-modules', 'json-modules'] }
186
+ window.esmsInitOptions = { polyfillEnable: ['css-modules', 'json-modules', 'wasm-modules'] }
186
187
  </script>
187
188
  ```
188
189
 
@@ -222,13 +223,13 @@ Browser Compatibility on baseline ES modules support **with** ES Module Shims:
222
223
  | [Import Maps](#import-maps) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
223
224
  | [JSON Modules](#json-modules) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
224
225
  | [CSS Modules](#css-modules) | :heavy_check_mark:<sup>1</sup> | :heavy_check_mark:<sup>1</sup> | :heavy_check_mark:<sup>1</sup> |
226
+ | [Wasm Modules](#wasm-modules) | 89+ | 89+ | 15+ |
225
227
  | [import.meta.resolve](#resolve) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
226
- | [Module Workers](#module-workers) (via wrapper) | 63+ | :x:<sup>2</sup> | 15+ |
228
+ | [Module Workers](#module-workers) (via wrapper) | 63+ | ~113+ | 15+ |
227
229
  | Top-Level Await (unpolyfilled<sup>3</sup>) | 89+ | 89+ | 15+ |
228
230
 
229
231
  * 1: _CSS module support requires a separate [Constructable Stylesheets polyfill](https://github.com/calebdwilliams/construct-style-sheets#readme)._
230
- * 2: _Module worker support cannot yet be implemented in Firefox due to no dynamic import support in web workers._
231
- * 3: _Top-level await support is not currently polyfilled but is possible for ES Module Shims to implement for intermediate browser versions, with the feature request tracking in https://github.com/guybedford/es-module-shims/issues/5. The compatibility gap with native modules is currently < 5% of users so it may not even be necessary._
232
+ * 2: _Top-level await support is not currently polyfilled but is possible for ES Module Shims to implement for intermediate browser versions, with the feature request tracking in https://github.com/guybedford/es-module-shims/issues/5. The compatibility gap with native modules is currently < 5% of users so it may not even be necessary._
232
233
 
233
234
  Browser compatibility **without** ES Module Shims:
234
235
 
@@ -240,8 +241,9 @@ Browser compatibility **without** ES Module Shims:
240
241
  | [Import Maps](#import-maps) | 89+ | 108+ | 16.4+ |
241
242
  | [JSON Modules](#json-modules) | 91+ | :x: | :x: |
242
243
  | [CSS Modules](#css-modules) | 95+ | :x: | :x: |
244
+ | [Wasm Modules](#wasm-modules) | :x: | :x: | :x: |
243
245
  | [import.meta.resolve](#resolve) | :x: | :x: | :x: |
244
- | [Module Workers](#module-workers) | ~68+ | :x: | :x: |
246
+ | [Module Workers](#module-workers) | ~68+ | ~113+ | 15+ |
245
247
  | Top-Level Await | 89+ | 89+ | 15+ |
246
248
 
247
249
  * ❕<sup>1</sup>: On module redirects, Safari returns the request URL in `import.meta.url` instead of the response URL as per the spec.
@@ -402,14 +404,12 @@ JSON Modules are currently supported in Chrome when using them via an import ass
402
404
 
403
405
  ```html
404
406
  <script type="module">
405
- import json from 'https://site.com/data.json' assert { type: 'json' };
407
+ import json from 'https://site.com/data.json' with { type: 'json' };
406
408
  </script>
407
409
  ```
408
410
 
409
411
  In addition JSON modules need to be served with a valid JSON content type.
410
412
 
411
- Checks for assertion failures are not currently included.
412
-
413
413
  ### CSS Modules
414
414
 
415
415
  > Stability: WhatWG Standard, Single Browser Implementer
@@ -420,7 +420,7 @@ CSS Modules are currently supported in Chrome when using them via an import asse
420
420
 
421
421
  ```html
422
422
  <script type="module">
423
- import sheet from 'https://site.com/sheet.css' assert { type: 'css' };
423
+ import sheet from 'https://site.com/sheet.css' with { type: 'css' };
424
424
  </script>
425
425
  ```
426
426
 
@@ -434,7 +434,38 @@ For more information see the [web.dev article](https://web.dev/css-module-script
434
434
 
435
435
  In addition CSS modules need to be served with a valid CSS content type.
436
436
 
437
- Checks for assertion failures are not currently included.
437
+ ### Wasm Modules
438
+
439
+ > Stability: WebAssembly Standard, Unimplemented
440
+
441
+ Implements the [WebAssembly ESM Integration](https://github.com/WebAssembly/esm-integration) spec, including support for source phase imports.
442
+
443
+ In shim mode, Wasm modules are always supported. In polyfill mode, Wasm modules require the `polyfillEnable: ['wasm-modules']` [init option](#polyfill-enable-option).
444
+
445
+ WebAssembly module exports are made available as module exports and WebAssembly module imports will be resolved using the browser module loader.
446
+
447
+ When using the source phase import form, this must be enabled separately via the `polyfillEnabe: ['wasm-modules', 'source-phase']` [init option](#polyfill-enable-option) to support source imports to WebAssembly modules.
448
+
449
+ When enabling `'source-phase'`, `WebAssembly.Module` is also polyfilled to extend from `AbstractModuleSource` per the source phase proposal.
450
+
451
+ WebAssembly modules require native top-level await support to be polyfilled, see the [compatibility table](#browser-support) above.
452
+
453
+ ```html
454
+ <script type="module">
455
+ import { fn } from './app.wasm';
456
+ </script>
457
+ ```
458
+
459
+ And for the source phase:
460
+
461
+ ```html
462
+ <script type="module">
463
+ import source mod from './app.wasm';
464
+ const instance = await WebAssembly.instantiate(mod, { /* ...imports */ });
465
+ </script>
466
+ ```
467
+
468
+ If using CSP, make sure to add `'unsafe-wasm-eval'` to `script-src` which is needed when the shim or polyfill engages, note this policy is much much safer than eval due to the Wasm secure sandbox. See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src#unsafe_webassembly_execution.
438
469
 
439
470
  ### Resolve
440
471
 
@@ -459,7 +490,7 @@ Node.js also implements a similar API, although it's in the process of shifting
459
490
 
460
491
  ### Module Workers
461
492
 
462
- ES Module Shims can be used in module workers in browsers that provide dynamic import in worker environments, which at the moment are Chrome(80+), Edge(80+) and Safari(15+).
493
+ ES Module Shims can be used in module workers in browsers that provide dynamic import in worker environments, which at the moment are Chrome(80+), Edge(80+), Firefox(~113+) and Safari(15+).
463
494
 
464
495
  By default, when there is no DOM present, ES Module Shims will switch into shim mode. An example of ES Module Shims usage through shim mode in web workers is provided below:
465
496
 
@@ -497,6 +528,7 @@ Provide a `esmsInitOptions` on the global scope before `es-module-shims` is load
497
528
  * [mapOverrides](#overriding-import-map-entries)
498
529
  * [modulepreload](#modulepreload)
499
530
  * [noLoadEventRetriggers](#no-load-event-retriggers)
531
+ * [globalLoadEventRetrigger](#global-load-event-retrigger)
500
532
  * [nonce](#nonce)
501
533
  * [onerror](#error-hook)
502
534
  * [onpolyfill](#polyfill-hook)
@@ -515,8 +547,10 @@ window.esmsInitOptions = {
515
547
  polyfillEnable: ['css-modules', 'json-modules'], // default empty
516
548
  // Custom CSP nonce
517
549
  nonce: 'n0nce', // default is automatic detection
518
- // Don't retrigger load events on module scripts
550
+ // Don't retrigger load events on module scripts (DOMContentLoaded, domready)
519
551
  noLoadEventRetriggers: true, // default false
552
+ // Retrigger window 'load' event (will be combined into load event above on next major)
553
+ globalLoadEventRetrigger: true, // default false
520
554
  // Skip source analysis of certain URLs for full native passthrough
521
555
  skip: /^https:\/\/cdn\.com/, // defaults to null
522
556
  // Clean up blob URLs after execution
@@ -580,7 +614,7 @@ DOM `load` events are fired for all `"module-shim"` scripts both for success and
580
614
 
581
615
  The `polyfillEnable` option allows enabling polyfill features which are newer and would otherwise result in unnecessary polyfilling in modern browsers that haven't yet updated.
582
616
 
583
- Currently this option supports just `"css-modules"` and `"json-modules"`.
617
+ This options supports `"css-modules"`, `"json-modules"`, `"wasm-modules"`, `"source-phase"`.
584
618
 
585
619
  ```html
586
620
  <script type="esms-options">
@@ -638,6 +672,8 @@ Alternatively, add a `blob:` URL policy with the CSP build to get CSP compatibil
638
672
 
639
673
  Because of the extra processing done by ES Module Shims it is possible for static module scripts to execute after the `DOMContentLoaded` or `readystatechange` events they expect, which can cause missed attachment.
640
674
 
675
+ In addition, script elements will also have their load events refired when polyfilled.
676
+
641
677
  In order to ensure libraries that rely on these event still behave correctly, ES Module Shims will always double trigger these events that would normally have executed before the document ready state transition to completion, once all the static module scripts in the page have been completely executed through ES module shims.
642
678
 
643
679
  In such a case, this double event firing can be disabled with the `noLoadEventRetriggers` option:
@@ -652,6 +688,13 @@ In such a case, this double event firing can be disabled with the `noLoadEventRe
652
688
  <script async src="es-module-shims.js"></script>
653
689
  ```
654
690
 
691
+ ### Global Load Event Retrigger
692
+
693
+ In ES Module Shims 1.x, load event retriggers only apply to `DOMContentLoaded` and `readystatechange` and not to the window `load` event.
694
+ To enable the window / worker `'load'` event, set `globalLoadEventRetrigger: true`.
695
+
696
+ In the next major version, this will be the default for load events, at which point only `noLoadEventRetriggers` will remain.
697
+
655
698
  ### Skip
656
699
 
657
700
  When loading modules that you know will only use baseline modules features, it is possible to set a rule to explicitly opt-out modules from being polyfilled to always load and be referenced through the native loader only. This enables instance sharing with the native loader and also improves performance because those modules then do not need to be processed or transformed at all, so that only local application code is handled and not library code.