es-module-shims 2.0.2 → 2.0.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/README.md +44 -12
- package/dist/es-module-shims-typescript.js +546 -0
- package/dist/es-module-shims.debug.js +136 -174
- package/dist/es-module-shims.js +126 -166
- package/dist/es-module-shims.wasm.js +125 -165
- package/package.json +8 -4
package/README.md
CHANGED
|
@@ -2,15 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
Polyfills import maps and other ES Modules features on top of the baseline native ESM support in browsers.
|
|
4
4
|
|
|
5
|
-
With import maps now supported by all major browsers, ES Module Shims entirely bypasses processing for the [
|
|
5
|
+
With import maps now supported by all major browsers, ES Module Shims entirely bypasses processing for the [94% of users](https://caniuse.com/import-maps) with native import maps support.
|
|
6
6
|
|
|
7
|
-
For the remaining users, the highly performant (see [benchmarks](#benchmarks)) production and [CSP-compatible](#csp-support) shim kicks in to rewrite module specifiers driven by the [Web Assembly ES Module Lexer](https://github.com/guybedford/es-module-lexer).
|
|
7
|
+
For the remaining ~4% of users, the highly performant (see [benchmarks](#benchmarks)) production and [CSP-compatible](#csp-support) shim kicks in to rewrite module specifiers driven by the [Web Assembly ES Module Lexer](https://github.com/guybedford/es-module-lexer).
|
|
8
8
|
|
|
9
9
|
The following modules features are polyfilled:
|
|
10
10
|
|
|
11
11
|
* [Import Maps](#import-maps) polyfill.
|
|
12
|
-
* [JSON](#json-modules) and [CSS modules](#css-modules) with import assertions
|
|
13
|
-
* [Wasm modules](#wasm-modules) with support for Source Phase Imports
|
|
12
|
+
* [JSON](#json-modules) and [CSS modules](#css-modules) with import assertions when enabled.
|
|
13
|
+
* [Wasm modules](#wasm-modules) with support for Source Phase Imports when enabled.
|
|
14
|
+
* [TypeScript](#typescript) type stripping when enabled.
|
|
14
15
|
|
|
15
16
|
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.
|
|
16
17
|
|
|
@@ -27,7 +28,7 @@ Because we are still using the native module loader the edge cases work out comp
|
|
|
27
28
|
Include ES Module Shims with a `async` attribute on the script, then include an import map and module scripts normally:
|
|
28
29
|
|
|
29
30
|
```html
|
|
30
|
-
<script async src="https://ga.jspm.io/npm:es-module-shims@2.0.
|
|
31
|
+
<script async src="https://ga.jspm.io/npm:es-module-shims@2.0.4/dist/es-module-shims.js"></script>
|
|
31
32
|
|
|
32
33
|
<!-- https://generator.jspm.io/#U2NhYGBkDM0rySzJSU1hKEpNTC5xMLTQM9Az0C1K1jMAAKFS5w0gAA -->
|
|
33
34
|
<script type="importmap">
|
|
@@ -190,7 +191,19 @@ If using more modern features like CSS Modules or JSON Modules, these need to be
|
|
|
190
191
|
|
|
191
192
|
```html
|
|
192
193
|
<script>
|
|
193
|
-
window.esmsInitOptions = { polyfillEnable: ['css-modules', 'json-modules', 'wasm-modules'] }
|
|
194
|
+
window.esmsInitOptions = { polyfillEnable: ['css-modules', 'json-modules', 'wasm-modules', 'typescript'] }
|
|
195
|
+
</script>
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
The above polyfill options correspond to `polyfillEnable: 'all'`.
|
|
199
|
+
|
|
200
|
+
Alternatively options can be set via the `esms-options` script type:
|
|
201
|
+
|
|
202
|
+
```html
|
|
203
|
+
<script type="esms-options">
|
|
204
|
+
{
|
|
205
|
+
"polyfillEnable": "all"
|
|
206
|
+
}
|
|
194
207
|
</script>
|
|
195
208
|
```
|
|
196
209
|
|
|
@@ -210,8 +223,8 @@ ES Module Shims is designed for production performance. A [comprehensive benchma
|
|
|
210
223
|
|
|
211
224
|
Benchmark summary:
|
|
212
225
|
|
|
213
|
-
* [ES Module Shims Chrome Passthrough](bench/README.md#chrome-passthrough-performance) (for [
|
|
214
|
-
* [ES Module Shims Polyfilling](bench/README.md#native-v-polyfill-performance) (for the remaining [
|
|
226
|
+
* [ES Module Shims Chrome Passthrough](bench/README.md#chrome-passthrough-performance) (for [94% 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.
|
|
227
|
+
* [ES Module Shims Polyfilling](bench/README.md#native-v-polyfill-performance) (for the remaining [3% 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.
|
|
215
228
|
* [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.
|
|
216
229
|
|
|
217
230
|
## Features
|
|
@@ -222,7 +235,7 @@ Works in all browsers with [baseline ES module support](https://caniuse.com/#fea
|
|
|
222
235
|
|
|
223
236
|
Browser Compatibility on baseline ES modules support **with** ES Module Shims:
|
|
224
237
|
|
|
225
|
-
| ES Modules Features | Chrome (
|
|
238
|
+
| ES Modules Features | Chrome (63+) | Firefox (67+) | Safari (11.1+) |
|
|
226
239
|
| ----------------------------------------------- | ------------------------------------ | ------------------------------------ | ------------------------------------ |
|
|
227
240
|
| [modulepreload](#modulepreload) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
|
|
228
241
|
| [Import Maps](#import-maps) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
|
|
@@ -297,13 +310,13 @@ In polyfill mode, multiple import maps are supported.
|
|
|
297
310
|
Support for dynamically injecting import maps with JavaScript via e.g.:
|
|
298
311
|
|
|
299
312
|
```js
|
|
300
|
-
document.
|
|
313
|
+
document.head.appendChild(Object.assign(document.createElement('script'), {
|
|
301
314
|
type: 'importmap',
|
|
302
315
|
innerHTML: JSON.stringify({ imports: { x: './y.js' } }),
|
|
303
316
|
}));
|
|
304
317
|
```
|
|
305
318
|
|
|
306
|
-
is also provided using mutation observers.
|
|
319
|
+
is also provided using mutation observers. Note, only `document.head` appending of scripts, importmaps and preloads is supported - we do not observe body mutations to `document.body` for performance reasons.
|
|
307
320
|
|
|
308
321
|
The caveat for multiple import map support polyfill support in browsers that only support a single import map is per the usual "polyfill rule" for es-module-shims - only those top-level graphs with static import feailures can be polyfilled.
|
|
309
322
|
|
|
@@ -531,6 +544,26 @@ const instance = await WebAssembly.instantiate(mod, { /* ...imports */ });
|
|
|
531
544
|
|
|
532
545
|
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.
|
|
533
546
|
|
|
547
|
+
### TypeScript Type Stripping
|
|
548
|
+
|
|
549
|
+
Node.js recently [added support for automatically executing TypeScript with type stripping](https://nodejs.org/api/typescript.html). We support the exact same approach in ES Module Shims.
|
|
550
|
+
|
|
551
|
+
Once enabled, the separate `es-module-shims-typescript.js` extension must be available as a sibling asset to `es-module-shims.js` and will then be loaded on demand when a `.ts`, `.mts` file is loaded or when a file is served with the `application/typescript` MIME type.
|
|
552
|
+
|
|
553
|
+
Example:
|
|
554
|
+
|
|
555
|
+
```html
|
|
556
|
+
<script async src="https://ga.jspm.io/npm:es-module-shims@2.0.4/dist/es-module-shims.js"></script>
|
|
557
|
+
<script type="esms-options">
|
|
558
|
+
{
|
|
559
|
+
"polyfillEnable": "all"
|
|
560
|
+
}
|
|
561
|
+
</script>
|
|
562
|
+
<script type="module" src="test.ts"></script>
|
|
563
|
+
```
|
|
564
|
+
|
|
565
|
+
Note that runtime TypeScript features such as enums are not supported, and type only imports should be used where possible, per the Node.js guidance for TypeScript.
|
|
566
|
+
|
|
534
567
|
### Module Workers
|
|
535
568
|
|
|
536
569
|
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+).
|
|
@@ -575,7 +608,6 @@ Provide a `esmsInitOptions` on the global scope before `es-module-shims` is load
|
|
|
575
608
|
* [nonce](#nonce)
|
|
576
609
|
* [onerror](#error-hook)
|
|
577
610
|
* [onpolyfill](#polyfill-hook)
|
|
578
|
-
* [polyfillEnable](#polyfill-enable-option)
|
|
579
611
|
* [resolve](#resolve-hook)
|
|
580
612
|
* [revokeBlobURLs](#revoke-blob-urls)
|
|
581
613
|
* [shimMode](#shim-mode-option)
|