es-module-shims 2.2.0 → 2.3.1

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
@@ -9,10 +9,11 @@ For the remaining ~4% of users, the highly performant (see [benchmarks](#benchma
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 when enabled.
13
- * [Wasm modules](#wasm-modules) with support for Source Phase Imports when enabled.
14
- * [Import defer](#import-defer) via syntax stripping to allow usage in modern browsers with a polyfill fallback when enabled.
12
+ * [JSON](#json-modules) and [CSS modules](#css-modules) polyfill with import assertions.
13
+ * [Wasm modules](#wasm-modules) with support for Source Phase Imports, when enabled.
14
+ * [Import defer](#import-defer) via syntax stripping to allow usage in modern browsers with a polyfill fallback, when enabled.
15
15
  * [TypeScript](#typescript-type-stripping) type stripping.
16
+ * [Hot Reloading](#hot-reloading) with a Vite-style `import.meta.hot` API.
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.
18
19
 
@@ -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@2.2.0/dist/es-module-shims.js"></script>
33
+ <script async src="https://ga.jspm.io/npm:es-module-shims@2.3.1/dist/es-module-shims.js"></script>
33
34
 
34
35
  <!-- https://generator.jspm.io/#U2NhYGBkDM0rySzJSU1hKEpNTC5xMLTQM9Az0C1K1jMAAKFS5w0gAA -->
35
36
  <script type="importmap">
@@ -222,7 +223,7 @@ ES Module Shims is designed for production performance. A [comprehensive benchma
222
223
 
223
224
  Benchmark summary:
224
225
 
225
- * [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.
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 13KB compressed download as expected.
226
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.
227
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.
228
229
 
@@ -617,6 +618,39 @@ When processing a TypeScript module, the `es-module-shims-typescript.js` extensi
617
618
 
618
619
  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.
619
620
 
621
+ ### Hot Reloading
622
+
623
+ Hot reloading is supported in both shim and polyfill modes, when explicitly enabled. To enable hot-reloading, enable the `hotReload` init option:
624
+
625
+ test.html
626
+ ```html
627
+ <script type="esms-options">
628
+ {
629
+ "hotReload": true,
630
+ "hotReloadInterval": 100
631
+ }
632
+ </script>
633
+ <script async src="es-module-shims.js"></script>
634
+ <script type="module" src="/app.js"></script>
635
+ ```
636
+
637
+ In polyfill mode, all modules will be initialized twice when hot reloading to reinitiate with `import.meta.hot`. To avoid this, shim mode is recommended for hot reloading workflows whenever possible.
638
+
639
+ The `hotReloadInterval` option can also be configured which is the interval at which hot reload events are batched together as a single reload operation, with the default of `100`.
640
+
641
+ When enabled, [native passthrough](#native-passthrough) will be automatically disabled, and all modules will be provided with the `import.meta.hot` API fully supporting [Vite's `import.meta.hot`](https://vite.dev/guide/api-hmr), supporting the following methods except for the event handlers:
642
+
643
+ * `hot.accept(cb)`: Accept a hot update.
644
+ * `hot.accept(dep, cb)`: Accept a hot update of a dependency specifier string.
645
+ * `hot.accept(deps, cb)`: Accept a hot update of a list of dependency specifier strings.
646
+ * `hot.dispose(cb)`: Provide a dispose function for when this module is expected to be accepted by others.
647
+ * `hot.data`: Shared data object between hot reload instances.
648
+ * `hot.invalidate()`: Fully invalidate the current module, without accepting. Can be called within accept itself.
649
+
650
+ To trigger a hot reload, call the `importShim.hotReload(url)` API with the URL of the module that has changed. All of CSS, JSON, Wasm and TypeScript imports are supported in hot reloading.
651
+
652
+ This hot reload API can then be attached to a Web Socket, Server Side Events emitter or any other event source to provide a native hot reloading development environment.
653
+
620
654
  ### Module Workers
621
655
 
622
656
  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+).
@@ -656,6 +690,7 @@ Provide a `esmsInitOptions` on the global scope before `es-module-shims` is load
656
690
  * [enforceIntegrity](#enforce-integrity)
657
691
  * [fetch](#fetch-hook)
658
692
  * [mapOverrides](#overriding-import-map-entries)
693
+ * [nativePassthrough](#native-passthrough)
659
694
  * [modulepreload](#modulepreload)
660
695
  * [noLoadEventRetriggers](#no-load-event-retriggers)
661
696
  * [globalLoadEventRetrigger](#global-load-event-retrigger)
@@ -687,6 +722,9 @@ window.esmsInitOptions = {
687
722
  enforceIntegrity: true, // default false
688
723
  // Permit overrides to import maps
689
724
  mapOverrides: true, // default false
725
+ // Whether es-module-shims will defer to the native module loader whenever it can
726
+ // Automatically disabled in hot reloading workflows
727
+ nativePassthrough: false, // default true
690
728
 
691
729
  // -- Hooks --
692
730
  // Module load error
@@ -700,7 +738,7 @@ window.esmsInitOptions = {
700
738
  // Hook import.meta construction
701
739
  meta: (meta, url) => void // default is noop
702
740
  // Hook top-level imports
703
- onimport: (url, options, parentUrl) => void // default is noop
741
+ onimport: (url, options, parentUrl, source) => void // default is noop
704
742
  }
705
743
  </script>
706
744
  <script async src="es-module-shims.js"></script>
@@ -904,6 +942,17 @@ document.body.appendChild(Object.assign(document.createElement('script'), {
904
942
 
905
943
  This can be useful for HMR workflows.
906
944
 
945
+ ### Native Passthrough
946
+
947
+ By default, ES Module Shims will always use the native passthrough of calling the native `import()` mechanism to load module graphs, when it has analyzed that that module graph is fully supported in the current environment. This way, the native module registry is fully shared between polyfilled and non-polyfilled graphs,
948
+ and we lean into the native support as much as popssible.
949
+
950
+ In Shim mode, ES Module Shims never uses native pass through regardless of this option - it only applies to polyfill mode.
951
+
952
+ This option is enabled by default, so by turning it off it is possible to ensure that ES Module Shims is rewriting and processing all sources itself.
953
+
954
+ Native passthrough is automatically disabled when using [hot reloading](#hot-reloading).
955
+
907
956
  ### Hooks
908
957
 
909
958
  #### Polyfill hook
@@ -949,8 +998,11 @@ The import hook is supported for both shim and polyfill modes and provides an as
949
998
  ```js
950
999
  <script>
951
1000
  window.esmsInitOptions = {
952
- onimport: function (url, options, parentUrl) {
953
- console.log(`Top-level import for ${url}`);
1001
+ onimport: function (url, options, parentUrl, source) {
1002
+ if (source !== undefined)
1003
+ console.log(`Top-level inline script with source: ${source}`);
1004
+ else
1005
+ console.log(`Top-level URL import (static or dynamic) for ${url}`);
954
1006
  }
955
1007
  }
956
1008
  </script>