es-module-shims 1.8.3 → 1.10.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 +54 -20
- package/dist/es-module-shims.debug.js +179 -90
- package/dist/es-module-shims.js +177 -88
- package/dist/es-module-shims.wasm.js +177 -88
- package/index.d.ts +9 -4
- 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.10.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.
|
|
@@ -267,6 +266,9 @@ Using this polyfill we can write:
|
|
|
267
266
|
"/": {
|
|
268
267
|
"test-dep": "/test-dep.js"
|
|
269
268
|
}
|
|
269
|
+
},
|
|
270
|
+
"integrity": {
|
|
271
|
+
"/test.js": "sha386-..."
|
|
270
272
|
}
|
|
271
273
|
}
|
|
272
274
|
</script>
|
|
@@ -278,6 +280,10 @@ Using this polyfill we can write:
|
|
|
278
280
|
|
|
279
281
|
All modules are still loaded with the native browser module loader, but with their specifiers rewritten then executed as Blob URLs, so there is a relatively minimal overhead to using a polyfill approach like this.
|
|
280
282
|
|
|
283
|
+
#### Integrity
|
|
284
|
+
|
|
285
|
+
The `"integrity"` field for import maps is supported when possible, throwing an error in es-module-shims when the integrity does not match the expected value.
|
|
286
|
+
|
|
281
287
|
#### Multiple Import Maps
|
|
282
288
|
|
|
283
289
|
Multiple import maps are not currently supported in any native implementation, Chromium support is currently being tracked in https://bugs.chromium.org/p/chromium/issues/detail?id=927119.
|
|
@@ -405,14 +411,12 @@ JSON Modules are currently supported in Chrome when using them via an import ass
|
|
|
405
411
|
|
|
406
412
|
```html
|
|
407
413
|
<script type="module">
|
|
408
|
-
import json from 'https://site.com/data.json'
|
|
414
|
+
import json from 'https://site.com/data.json' with { type: 'json' };
|
|
409
415
|
</script>
|
|
410
416
|
```
|
|
411
417
|
|
|
412
418
|
In addition JSON modules need to be served with a valid JSON content type.
|
|
413
419
|
|
|
414
|
-
Checks for assertion failures are not currently included.
|
|
415
|
-
|
|
416
420
|
### CSS Modules
|
|
417
421
|
|
|
418
422
|
> Stability: WhatWG Standard, Single Browser Implementer
|
|
@@ -423,7 +427,7 @@ CSS Modules are currently supported in Chrome when using them via an import asse
|
|
|
423
427
|
|
|
424
428
|
```html
|
|
425
429
|
<script type="module">
|
|
426
|
-
import sheet from 'https://site.com/sheet.css'
|
|
430
|
+
import sheet from 'https://site.com/sheet.css' with { type: 'css' };
|
|
427
431
|
</script>
|
|
428
432
|
```
|
|
429
433
|
|
|
@@ -437,18 +441,20 @@ For more information see the [web.dev article](https://web.dev/css-module-script
|
|
|
437
441
|
|
|
438
442
|
In addition CSS modules need to be served with a valid CSS content type.
|
|
439
443
|
|
|
440
|
-
Checks for assertion failures are not currently included.
|
|
441
|
-
|
|
442
444
|
### Wasm Modules
|
|
443
445
|
|
|
444
446
|
> Stability: WebAssembly Standard, Unimplemented
|
|
445
447
|
|
|
446
|
-
Implements the [WebAssembly ESM Integration](https://github.com/WebAssembly/esm-integration) spec
|
|
448
|
+
Implements the [WebAssembly ESM Integration](https://github.com/WebAssembly/esm-integration) spec, including support for source phase imports.
|
|
447
449
|
|
|
448
450
|
In shim mode, Wasm modules are always supported. In polyfill mode, Wasm modules require the `polyfillEnable: ['wasm-modules']` [init option](#polyfill-enable-option).
|
|
449
451
|
|
|
450
452
|
WebAssembly module exports are made available as module exports and WebAssembly module imports will be resolved using the browser module loader.
|
|
451
453
|
|
|
454
|
+
When using the source phase import form, this must be enabled separately via the `polyfillEnable: ['wasm-modules', 'source-phase']` [init option](#polyfill-enable-option) to support source imports to WebAssembly modules.
|
|
455
|
+
|
|
456
|
+
When enabling `'source-phase'`, `WebAssembly.Module` is also polyfilled to extend from `AbstractModuleSource` per the source phase proposal.
|
|
457
|
+
|
|
452
458
|
WebAssembly modules require native top-level await support to be polyfilled, see the [compatibility table](#browser-support) above.
|
|
453
459
|
|
|
454
460
|
```html
|
|
@@ -457,6 +463,15 @@ import { fn } from './app.wasm';
|
|
|
457
463
|
</script>
|
|
458
464
|
```
|
|
459
465
|
|
|
466
|
+
And for the source phase:
|
|
467
|
+
|
|
468
|
+
```html
|
|
469
|
+
<script type="module">
|
|
470
|
+
import source mod from './app.wasm';
|
|
471
|
+
const instance = await WebAssembly.instantiate(mod, { /* ...imports */ });
|
|
472
|
+
</script>
|
|
473
|
+
```
|
|
474
|
+
|
|
460
475
|
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
476
|
|
|
462
477
|
### Resolve
|
|
@@ -482,7 +497,7 @@ Node.js also implements a similar API, although it's in the process of shifting
|
|
|
482
497
|
|
|
483
498
|
### Module Workers
|
|
484
499
|
|
|
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+).
|
|
500
|
+
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
501
|
|
|
487
502
|
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
503
|
|
|
@@ -520,6 +535,7 @@ Provide a `esmsInitOptions` on the global scope before `es-module-shims` is load
|
|
|
520
535
|
* [mapOverrides](#overriding-import-map-entries)
|
|
521
536
|
* [modulepreload](#modulepreload)
|
|
522
537
|
* [noLoadEventRetriggers](#no-load-event-retriggers)
|
|
538
|
+
* [globalLoadEventRetrigger](#global-load-event-retrigger)
|
|
523
539
|
* [nonce](#nonce)
|
|
524
540
|
* [onerror](#error-hook)
|
|
525
541
|
* [onpolyfill](#polyfill-hook)
|
|
@@ -538,8 +554,10 @@ window.esmsInitOptions = {
|
|
|
538
554
|
polyfillEnable: ['css-modules', 'json-modules'], // default empty
|
|
539
555
|
// Custom CSP nonce
|
|
540
556
|
nonce: 'n0nce', // default is automatic detection
|
|
541
|
-
// Don't retrigger load events on module scripts
|
|
557
|
+
// Don't retrigger load events on module scripts (DOMContentLoaded, domready)
|
|
542
558
|
noLoadEventRetriggers: true, // default false
|
|
559
|
+
// Retrigger window 'load' event (will be combined into load event above on next major)
|
|
560
|
+
globalLoadEventRetrigger: true, // default false
|
|
543
561
|
// Skip source analysis of certain URLs for full native passthrough
|
|
544
562
|
skip: /^https:\/\/cdn\.com/, // defaults to null
|
|
545
563
|
// Clean up blob URLs after execution
|
|
@@ -603,7 +621,7 @@ DOM `load` events are fired for all `"module-shim"` scripts both for success and
|
|
|
603
621
|
|
|
604
622
|
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
623
|
|
|
606
|
-
|
|
624
|
+
This options supports `"css-modules"`, `"json-modules"`, `"wasm-modules"`, `"source-phase"`.
|
|
607
625
|
|
|
608
626
|
```html
|
|
609
627
|
<script type="esms-options">
|
|
@@ -625,17 +643,24 @@ This option can also be set to `true` to entirely disable the native passthrough
|
|
|
625
643
|
|
|
626
644
|
### Enforce Integrity
|
|
627
645
|
|
|
628
|
-
When enabled, `enforceIntegrity` will ensure that all modules loaded through ES Module Shims must have integrity defined either on a `<link rel="modulepreload" integrity="...">` or
|
|
629
|
-
a `<link rel="modulepreload-shim" integrity="...">` preload tag in shim mode. Modules without integrity will throw at fetch time.
|
|
646
|
+
When enabled, `enforceIntegrity` will ensure that all modules loaded through ES Module Shims must have integrity defined either on a `<link rel="modulepreload" integrity="...">`, a `<link rel="modulepreload-shim" integrity="...">` preload tag in shim mode, or the `"integrity"` field in the import map. Modules without integrity will throw at fetch time.
|
|
630
647
|
|
|
631
|
-
For example in the following, only the listed `app.js` and `
|
|
648
|
+
For example in the following, only the listed `app.js`, `dep.js` and `another.js` modules will be able to execute with the provided integrity:
|
|
632
649
|
|
|
633
650
|
```html
|
|
651
|
+
<script type="importmap">
|
|
652
|
+
{
|
|
653
|
+
"integrity": {
|
|
654
|
+
"/another.js": "sha384-..."
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
</script>
|
|
634
658
|
<script type="esms-options">{ "enforceIntegrity": true }</script>
|
|
635
659
|
<link rel="modulepreload-shim" href="/app.js" integrity="sha384-..." />\
|
|
636
660
|
<link rel="modulepreload-shim" href="/dep.js" integrity="sha384-..." />
|
|
637
661
|
<script type="module-shim">
|
|
638
662
|
import '/app.js';
|
|
663
|
+
import '/another.js';
|
|
639
664
|
</script>
|
|
640
665
|
```
|
|
641
666
|
|
|
@@ -661,6 +686,8 @@ Alternatively, add a `blob:` URL policy with the CSP build to get CSP compatibil
|
|
|
661
686
|
|
|
662
687
|
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
688
|
|
|
689
|
+
In addition, script elements will also have their load events refired when polyfilled.
|
|
690
|
+
|
|
664
691
|
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
692
|
|
|
666
693
|
In such a case, this double event firing can be disabled with the `noLoadEventRetriggers` option:
|
|
@@ -675,6 +702,13 @@ In such a case, this double event firing can be disabled with the `noLoadEventRe
|
|
|
675
702
|
<script async src="es-module-shims.js"></script>
|
|
676
703
|
```
|
|
677
704
|
|
|
705
|
+
### Global Load Event Retrigger
|
|
706
|
+
|
|
707
|
+
In ES Module Shims 1.x, load event retriggers only apply to `DOMContentLoaded` and `readystatechange` and not to the window `load` event.
|
|
708
|
+
To enable the window / worker `'load'` event, set `globalLoadEventRetrigger: true`.
|
|
709
|
+
|
|
710
|
+
In the next major version, this will be the default for load events, at which point only `noLoadEventRetriggers` will remain.
|
|
711
|
+
|
|
678
712
|
### Skip
|
|
679
713
|
|
|
680
714
|
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.
|