es-module-shims 1.8.3 → 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 +37 -17
- package/dist/es-module-shims.debug.js +161 -86
- package/dist/es-module-shims.js +159 -84
- package/dist/es-module-shims.wasm.js +159 -84
- package/package.json +2 -3
package/README.md
CHANGED
|
@@ -12,7 +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) when enabled.
|
|
15
|
+
* [Wasm modules](#wasm-modules) with support for Source Phase Imports (when enabled).
|
|
16
16
|
* [`<link rel="modulepreload">` is shimmed](#modulepreload) in browsers without import maps support.
|
|
17
17
|
|
|
18
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.
|
|
@@ -30,7 +30,7 @@ Because we are still using the native module loader the edge cases work out comp
|
|
|
30
30
|
Include ES Module Shims with a `async` attribute on the script, then include an import map and module scripts normally:
|
|
31
31
|
|
|
32
32
|
```html
|
|
33
|
-
<script async src="https://ga.jspm.io/npm:es-module-shims@1.
|
|
33
|
+
<script async src="https://ga.jspm.io/npm:es-module-shims@1.9.0/dist/es-module-shims.js"></script>
|
|
34
34
|
|
|
35
35
|
<!-- https://generator.jspm.io/#U2NhYGBkDM0rySzJSU1hKEpNTC5xMLTQM9Az0C1K1jMAAKFS5w0gAA -->
|
|
36
36
|
<script type="importmap">
|
|
@@ -72,7 +72,7 @@ If one is found, it will then be reexecuted through ES Module Shims using its in
|
|
|
72
72
|
When the polyfill kicks in another console log message is output(which can be disabled or customized via the [polyfill hook](#polyfill-hook)):
|
|
73
73
|
|
|
74
74
|
```
|
|
75
|
-
^^ Module
|
|
75
|
+
^^ Module error above is polyfilled and can be ignored ^^
|
|
76
76
|
```
|
|
77
77
|
|
|
78
78
|
### Polyfill Edge Case: Dynamic Import
|
|
@@ -225,12 +225,11 @@ Browser Compatibility on baseline ES modules support **with** ES Module Shims:
|
|
|
225
225
|
| [CSS Modules](#css-modules) | :heavy_check_mark:<sup>1</sup> | :heavy_check_mark:<sup>1</sup> | :heavy_check_mark:<sup>1</sup> |
|
|
226
226
|
| [Wasm Modules](#wasm-modules) | 89+ | 89+ | 15+ |
|
|
227
227
|
| [import.meta.resolve](#resolve) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
|
|
228
|
-
| [Module Workers](#module-workers) (via wrapper) | 63+ |
|
|
228
|
+
| [Module Workers](#module-workers) (via wrapper) | 63+ | ~113+ | 15+ |
|
|
229
229
|
| Top-Level Await (unpolyfilled<sup>3</sup>) | 89+ | 89+ | 15+ |
|
|
230
230
|
|
|
231
231
|
* 1: _CSS module support requires a separate [Constructable Stylesheets polyfill](https://github.com/calebdwilliams/construct-style-sheets#readme)._
|
|
232
|
-
* 2:
|
|
233
|
-
* 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._
|
|
234
233
|
|
|
235
234
|
Browser compatibility **without** ES Module Shims:
|
|
236
235
|
|
|
@@ -244,7 +243,7 @@ Browser compatibility **without** ES Module Shims:
|
|
|
244
243
|
| [CSS Modules](#css-modules) | 95+ | :x: | :x: |
|
|
245
244
|
| [Wasm Modules](#wasm-modules) | :x: | :x: | :x: |
|
|
246
245
|
| [import.meta.resolve](#resolve) | :x: | :x: | :x: |
|
|
247
|
-
| [Module Workers](#module-workers) | ~68+ |
|
|
246
|
+
| [Module Workers](#module-workers) | ~68+ | ~113+ | 15+ |
|
|
248
247
|
| Top-Level Await | 89+ | 89+ | 15+ |
|
|
249
248
|
|
|
250
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.
|
|
@@ -405,14 +404,12 @@ JSON Modules are currently supported in Chrome when using them via an import ass
|
|
|
405
404
|
|
|
406
405
|
```html
|
|
407
406
|
<script type="module">
|
|
408
|
-
import json from 'https://site.com/data.json'
|
|
407
|
+
import json from 'https://site.com/data.json' with { type: 'json' };
|
|
409
408
|
</script>
|
|
410
409
|
```
|
|
411
410
|
|
|
412
411
|
In addition JSON modules need to be served with a valid JSON content type.
|
|
413
412
|
|
|
414
|
-
Checks for assertion failures are not currently included.
|
|
415
|
-
|
|
416
413
|
### CSS Modules
|
|
417
414
|
|
|
418
415
|
> Stability: WhatWG Standard, Single Browser Implementer
|
|
@@ -423,7 +420,7 @@ CSS Modules are currently supported in Chrome when using them via an import asse
|
|
|
423
420
|
|
|
424
421
|
```html
|
|
425
422
|
<script type="module">
|
|
426
|
-
import sheet from 'https://site.com/sheet.css'
|
|
423
|
+
import sheet from 'https://site.com/sheet.css' with { type: 'css' };
|
|
427
424
|
</script>
|
|
428
425
|
```
|
|
429
426
|
|
|
@@ -437,18 +434,20 @@ For more information see the [web.dev article](https://web.dev/css-module-script
|
|
|
437
434
|
|
|
438
435
|
In addition CSS modules need to be served with a valid CSS content type.
|
|
439
436
|
|
|
440
|
-
Checks for assertion failures are not currently included.
|
|
441
|
-
|
|
442
437
|
### Wasm Modules
|
|
443
438
|
|
|
444
439
|
> Stability: WebAssembly Standard, Unimplemented
|
|
445
440
|
|
|
446
|
-
Implements the [WebAssembly ESM Integration](https://github.com/WebAssembly/esm-integration) spec
|
|
441
|
+
Implements the [WebAssembly ESM Integration](https://github.com/WebAssembly/esm-integration) spec, including support for source phase imports.
|
|
447
442
|
|
|
448
443
|
In shim mode, Wasm modules are always supported. In polyfill mode, Wasm modules require the `polyfillEnable: ['wasm-modules']` [init option](#polyfill-enable-option).
|
|
449
444
|
|
|
450
445
|
WebAssembly module exports are made available as module exports and WebAssembly module imports will be resolved using the browser module loader.
|
|
451
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
|
+
|
|
452
451
|
WebAssembly modules require native top-level await support to be polyfilled, see the [compatibility table](#browser-support) above.
|
|
453
452
|
|
|
454
453
|
```html
|
|
@@ -457,6 +456,15 @@ import { fn } from './app.wasm';
|
|
|
457
456
|
</script>
|
|
458
457
|
```
|
|
459
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
|
+
|
|
460
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.
|
|
461
469
|
|
|
462
470
|
### Resolve
|
|
@@ -482,7 +490,7 @@ Node.js also implements a similar API, although it's in the process of shifting
|
|
|
482
490
|
|
|
483
491
|
### Module Workers
|
|
484
492
|
|
|
485
|
-
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+).
|
|
486
494
|
|
|
487
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:
|
|
488
496
|
|
|
@@ -520,6 +528,7 @@ Provide a `esmsInitOptions` on the global scope before `es-module-shims` is load
|
|
|
520
528
|
* [mapOverrides](#overriding-import-map-entries)
|
|
521
529
|
* [modulepreload](#modulepreload)
|
|
522
530
|
* [noLoadEventRetriggers](#no-load-event-retriggers)
|
|
531
|
+
* [globalLoadEventRetrigger](#global-load-event-retrigger)
|
|
523
532
|
* [nonce](#nonce)
|
|
524
533
|
* [onerror](#error-hook)
|
|
525
534
|
* [onpolyfill](#polyfill-hook)
|
|
@@ -538,8 +547,10 @@ window.esmsInitOptions = {
|
|
|
538
547
|
polyfillEnable: ['css-modules', 'json-modules'], // default empty
|
|
539
548
|
// Custom CSP nonce
|
|
540
549
|
nonce: 'n0nce', // default is automatic detection
|
|
541
|
-
// Don't retrigger load events on module scripts
|
|
550
|
+
// Don't retrigger load events on module scripts (DOMContentLoaded, domready)
|
|
542
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
|
|
543
554
|
// Skip source analysis of certain URLs for full native passthrough
|
|
544
555
|
skip: /^https:\/\/cdn\.com/, // defaults to null
|
|
545
556
|
// Clean up blob URLs after execution
|
|
@@ -603,7 +614,7 @@ DOM `load` events are fired for all `"module-shim"` scripts both for success and
|
|
|
603
614
|
|
|
604
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.
|
|
605
616
|
|
|
606
|
-
|
|
617
|
+
This options supports `"css-modules"`, `"json-modules"`, `"wasm-modules"`, `"source-phase"`.
|
|
607
618
|
|
|
608
619
|
```html
|
|
609
620
|
<script type="esms-options">
|
|
@@ -661,6 +672,8 @@ Alternatively, add a `blob:` URL policy with the CSP build to get CSP compatibil
|
|
|
661
672
|
|
|
662
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.
|
|
663
674
|
|
|
675
|
+
In addition, script elements will also have their load events refired when polyfilled.
|
|
676
|
+
|
|
664
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.
|
|
665
678
|
|
|
666
679
|
In such a case, this double event firing can be disabled with the `noLoadEventRetriggers` option:
|
|
@@ -675,6 +688,13 @@ In such a case, this double event firing can be disabled with the `noLoadEventRe
|
|
|
675
688
|
<script async src="es-module-shims.js"></script>
|
|
676
689
|
```
|
|
677
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
|
+
|
|
678
698
|
### Skip
|
|
679
699
|
|
|
680
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.
|