es-module-shims 2.1.2 → 2.2.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,7 +12,7 @@ The following modules features are polyfilled:
12
12
  * [JSON](#json-modules) and [CSS modules](#css-modules) with import assertions when enabled.
13
13
  * [Wasm modules](#wasm-modules) with support for Source Phase Imports when enabled.
14
14
  * [Import defer](#import-defer) via syntax stripping to allow usage in modern browsers with a polyfill fallback when enabled.
15
- * [TypeScript](#typescript-type-stripping) type stripping when enabled.
15
+ * [TypeScript](#typescript-type-stripping) type stripping.
16
16
 
17
17
  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
18
 
@@ -29,7 +29,7 @@ Because we are still using the native module loader the edge cases work out comp
29
29
  Include ES Module Shims with a `async` attribute on the script, then include an import map and module scripts normally:
30
30
 
31
31
  ```html
32
- <script async src="https://ga.jspm.io/npm:es-module-shims@2.1.2/dist/es-module-shims.js"></script>
32
+ <script async src="https://ga.jspm.io/npm:es-module-shims@2.2.0/dist/es-module-shims.js"></script>
33
33
 
34
34
  <!-- https://generator.jspm.io/#U2NhYGBkDM0rySzJSU1hKEpNTC5xMLTQM9Az0C1K1jMAAKFS5w0gAA -->
35
35
  <script type="importmap">
@@ -188,22 +188,20 @@ If the polyfill is analyzing or applying to a module script that doesn't need to
188
188
 
189
189
  ### Polyfill Features
190
190
 
191
- If using more modern features like CSS Modules or JSON Modules, these need to be manually enabled via the [`polyfillEnable` init option](#polyfill-enable-option) to raise the native baseline from just checking import maps to also checking that browsers support these features:
191
+ If using more modern features like [Import Defer](#import-defer) or [Wasm Modules](#wasm-modules), these need to be manually enabled via the [`polyfillEnable` init option](#polyfill-enable-option) to raise the native baseline from just checking import maps to also checking that browsers support these features:
192
192
 
193
193
  ```html
194
194
  <script>
195
- window.esmsInitOptions = { polyfillEnable: ['css-modules', 'json-modules', 'wasm-modules', 'typescript'] }
195
+ window.esmsInitOptions = { polyfillEnable: ['wasm-module-sources', 'import-defer'] }
196
196
  </script>
197
197
  ```
198
198
 
199
- The above polyfill options correspond to `polyfillEnable: 'all'`.
200
-
201
- Alternatively options can be set via the `esms-options` script type:
199
+ Alternatively options can be set via the `esms-options` script type JSON:
202
200
 
203
201
  ```html
204
202
  <script type="esms-options">
205
203
  {
206
- "polyfillEnable": "all"
204
+ "polyfillEnable": ["wasm-module-sources", "import-defer"]
207
205
  }
208
206
  </script>
209
207
  ```
@@ -254,7 +252,7 @@ Browser compatibility **without** ES Module Shims:
254
252
  | [modulepreload](#modulepreload) | 66+ | 115+ | 17.5+ |
255
253
  | [Import Maps](#import-maps) | 89+ | 108+ | 16.4+ |
256
254
  | [Import Map Integrity](#import-map-integrity) | 127+ | :x: | :x: |
257
- | [Multiple Import Maps](#multiple-import-maps) | Pending | :x: | :x: |
255
+ | [Multiple Import Maps](#multiple-import-maps) | 135+ | :x: | :x: |
258
256
  | [JSON Modules](#json-modules) | 123+ | :x: | 17.2+ |
259
257
  | [CSS Modules](#css-modules) | 123+ | :x: | :x: |
260
258
  | [Wasm Modules](#wasm-modules) | Pending | :x: | :x: |
@@ -483,11 +481,13 @@ Note integrity can only be validated when in shim mode or when the polyfill is d
483
481
 
484
482
  ### JSON Modules
485
483
 
486
- > Stability: WhatWG Standard, Single Browser Implementer
484
+ > Stability: WhatWG Standard, Stable, Multiple Browser Implementers
487
485
 
488
- In shim mode, JSON modules are always supported. In polyfill mode, JSON modules require the `polyfillEnable: ['json-modules']` [init option](#polyfill-enable-option).
486
+ JSON modules are now enabled by default in ES Module Shims with 85% browser support for native where the polyfill won't engage at all.
489
487
 
490
- JSON Modules are currently supported in Chrome when using them via an import assertion:
488
+ In shim mode, JSON modules are always supported.
489
+
490
+ JSON Modules are supported in Chrome and Safari when using them via an import assertion:
491
491
 
492
492
  ```html
493
493
  <script type="module">
@@ -501,9 +501,9 @@ In addition JSON modules need to be served with a valid JSON content type.
501
501
 
502
502
  > Stability: WhatWG Standard, Single Browser Implementer
503
503
 
504
- In shim mode, CSS modules are always supported. In polyfill mode, CSS modules require the `polyfillEnable: ['css-modules']` [init option](#polyfill-enable-option).
504
+ CSS imports are fully supported in both polyfill and shim mode with native passthrough applying in Chrome.
505
505
 
506
- CSS Modules are currently supported in Chrome when using them via an import assertion:
506
+ Use them by adding the `type: 'css'` import assertion when importing a CSS file:
507
507
 
508
508
  ```html
509
509
  <script type="module">
@@ -511,7 +511,7 @@ import sheet from 'https://site.com/sheet.css' with { type: 'css' };
511
511
  </script>
512
512
  ```
513
513
 
514
- In addition CSS modules need to be served with a valid CSS content type.
514
+ In addition CSS files need to be served with a valid CSS content type.
515
515
 
516
516
  ### Wasm Modules
517
517
 
@@ -519,13 +519,11 @@ In addition CSS modules need to be served with a valid CSS content type.
519
519
 
520
520
  Implements the [WebAssembly ESM Integration](https://github.com/WebAssembly/esm-integration) spec, including support for source phase imports.
521
521
 
522
- In shim mode, Wasm modules are always supported. In polyfill mode, Wasm modules require the `polyfillEnable: ['wasm-modules']` [init option](#polyfill-enable-option).
522
+ In shim mode, Wasm modules are always supported. In polyfill mode, Wasm modules require the `polyfillEnable: ['wasm-module-sources']` (or `'wasm-modules-instances'` for instance imports) [init option](#polyfill-enable-option).
523
523
 
524
524
  WebAssembly module exports are made available as module exports and WebAssembly module imports will be resolved using the browser module loader.
525
525
 
526
- By default Wasm support will look for both source phase syntax as well as instance Wasm imports, resulting in full analysis of the module graph when either is unsupported.
527
-
528
- If only using Wasm modules in the source phase set `polyfillEnable: ['wasm-module-sources']` [init option](#polyfill-enable-option) to ensure full native passthrough without the extra code analysis when Chrome ships the source phase.
526
+ If only using Wasm modules in both the instance and source phase set `polyfillEnable: ['wasm-modules']` [init option](#polyfill-enable-option) to enable both modes.
529
527
 
530
528
  When enabling the source phase feature either way, `WebAssembly.Module` is also polyfilled to extend from `AbstractModuleSource` per the source phase proposal.
531
529
 
@@ -546,7 +544,7 @@ const instance = await WebAssembly.instantiate(mod, { /* ...imports */ });
546
544
  </script>
547
545
  ```
548
546
 
549
- 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.
547
+ 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 safer than normal 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.
550
548
 
551
549
  ### Import Defer
552
550
 
@@ -558,22 +556,65 @@ The polyfill is simply just a defer syntax stripping, allowing environments that
558
556
 
559
557
  ### TypeScript Type Stripping
560
558
 
561
- 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.
559
+ TypeScript type stripping is supported like [in Node.js](https://nodejs.org/api/typescript.html).
562
560
 
563
- 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.
561
+ To trigger TypeScript type stripping, make sure to either use shim mode, or to add the non-standard `lang="ts"` option to the top-level evaluator. This ensures that the polyfill processing will engage on this graph, while usually module graphs are not processed at all in browsers with full native support.
564
562
 
565
- Example:
563
+ Modules will then be interpreted as TypeScript when one of the following applied:
564
+
565
+ * It is served with the `application/typescript` MIME type.
566
+ * It ends in `.ts` or `.mts` and is not served with a valid module script MIME type.
567
+ * Or, if it is an inline `lang="ts"` module script.
568
+
569
+ For example the following are all valid TypeScript usage patterns:
570
+
571
+ Shim mode - no `lang=ts` is necessary:
566
572
 
567
573
  ```html
568
- <script async src="https://ga.jspm.io/npm:es-module-shims@2.1.2/dist/es-module-shims.js"></script>
569
- <script type="esms-options">
574
+ <script type="module-shim" src="app.ts">
575
+ ```
576
+
577
+ Polyfill mode via `lang=ts`:
578
+
579
+ ```html
580
+ <script type="module" lang="ts" src="app.ts"></script>
581
+ ```
582
+
583
+ Inline TypeScript with `lang=ts`:
584
+
585
+ ```html
586
+ <script type="module" lang="ts">
587
+ import './dep.ts';
588
+ const ts: boolean = true;
589
+ console.log('TypeScript!');
590
+ </script>
591
+ ```
592
+
593
+ Indirect static import to a TypeScript module via `lang="ts"`:
594
+
595
+ ```html
596
+ <script type="importmap">
570
597
  {
571
- "polyfillEnable": "all"
598
+ "imports": {
599
+ "app": "/app.mts"
600
+ }
572
601
  }
573
602
  </script>
574
- <script type="module" src="test.ts"></script>
603
+ <script type="module" lang="ts">
604
+ import 'app';
605
+ </script>
606
+ ```
607
+
608
+ Loading TypeScript dynamically:
609
+
610
+ ```html
611
+ <script type="module" lang="ts">
612
+ importShim('./app.ts', { lang: 'ts' });
613
+ </script>
575
614
  ```
576
615
 
616
+ When processing a TypeScript module, the `es-module-shims-typescript.js` extension must be available as a sibling asset to `es-module-shims.js`. It will then be loaded on-demand to provide the type stripping processing.
617
+
577
618
  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.
578
619
 
579
620
  ### Module Workers
@@ -632,7 +673,7 @@ window.esmsInitOptions = {
632
673
  // Enable Shim Mode
633
674
  shimMode: true, // default false
634
675
  // Enable newer modules features
635
- polyfillEnable: ['css-modules', 'json-modules'], // default empty
676
+ polyfillEnable: ['wasm-module-sources'], // default empty
636
677
  // Custom CSP nonce
637
678
  nonce: 'n0nce', // default is automatic detection
638
679
  // Don't retrigger load events on module scripts (DOMContentLoaded, domready, window 'onload')
@@ -671,7 +712,7 @@ window.esmsInitOptions = {
671
712
  <script type="esms-options">
672
713
  {
673
714
  "shimMode": true,
674
- "polyfillEnable": ["css-modules", "json-modules"],
715
+ "polyfillEnable": ["wasm-module-sources"],
675
716
  "nonce": "n0nce",
676
717
  "onpolyfill": "polyfill"
677
718
  }
@@ -700,14 +741,14 @@ DOM `load` events are fired for all `"module-shim"` scripts both for success and
700
741
 
701
742
  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.
702
743
 
703
- This options supports `"css-modules"`, `"json-modules"`, `"wasm-modules"`, `"wasm-module-sources"`, `"wasm-module-instances"` and `"import-defer"`.
744
+ This options supports `"wasm-modules"`, `"wasm-module-sources"`, `"wasm-module-instances"` and `"import-defer"`.
704
745
 
705
- In adddition, the `"all"` option will enable all features and the `"latest"` option will implement the latest supported browser features (currently `"css-modules"` and `"json-modules"`).
746
+ In adddition, the `"all"` option will enable all features.
706
747
 
707
748
  ```html
708
749
  <script type="esms-options">
709
750
  {
710
- "polyfillEnable": ["latest", "typescript"]
751
+ "polyfillEnable": ["all"]
711
752
  }
712
753
  </script>
713
754
  ```