es-module-shims 2.5.1 → 2.6.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 +88 -15
- package/dist/es-module-shims.debug.js +1004 -1002
- package/dist/es-module-shims.js +985 -983
- package/dist/es-module-shims.wasm.js +985 -983
- package/index.d.ts +161 -33
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -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@2.
|
|
33
|
+
<script async src="https://ga.jspm.io/npm:es-module-shims@2.6.0/dist/es-module-shims.js"></script>
|
|
34
34
|
|
|
35
35
|
<!-- https://generator.jspm.io/#U2NhYGBkDM0rySzJSU1hKEpNTC5xMLTQM9Az0C1K1jMAAKFS5w0gAA -->
|
|
36
36
|
<script type="importmap">
|
|
@@ -686,7 +686,8 @@ const worker = new Worker(getWorkerScriptURL('myEsModule.js'));
|
|
|
686
686
|
|
|
687
687
|
Provide a `esmsInitOptions` on the global scope before `es-module-shims` is loaded to configure various aspects of the module loading process:
|
|
688
688
|
|
|
689
|
-
* [polyfillEnable](#polyfill-enable)
|
|
689
|
+
* [polyfillEnable](#polyfill-enable-option)
|
|
690
|
+
* [polyfillDisable](#polyfill-disable-option)
|
|
690
691
|
* [enforceIntegrity](#enforce-integrity)
|
|
691
692
|
* [fetch](#fetch-hook)
|
|
692
693
|
* [mapOverrides](#overriding-import-map-entries)
|
|
@@ -697,7 +698,7 @@ Provide a `esmsInitOptions` on the global scope before `es-module-shims` is load
|
|
|
697
698
|
* [onerror](#error-hook)
|
|
698
699
|
* [onpolyfill](#polyfill-hook)
|
|
699
700
|
* [resolve](#resolve-hook)
|
|
700
|
-
* [
|
|
701
|
+
* [source](#source-hook)
|
|
701
702
|
* [shimMode](#shim-mode-option)
|
|
702
703
|
* [skip](#skip)
|
|
703
704
|
* [version](#version)
|
|
@@ -709,14 +710,14 @@ window.esmsInitOptions = {
|
|
|
709
710
|
shimMode: true, // default false
|
|
710
711
|
// Enable newer modules features
|
|
711
712
|
polyfillEnable: ['wasm-module-sources'], // default empty
|
|
713
|
+
// Disable features that are unused
|
|
714
|
+
polyfillDisable: ['css-modules'], // default empty
|
|
712
715
|
// Custom CSP nonce
|
|
713
716
|
nonce: 'n0nce', // default is automatic detection
|
|
714
717
|
// Don't retrigger load events on module scripts (DOMContentLoaded, domready, window 'onload')
|
|
715
718
|
noLoadEventRetriggers: true, // default false
|
|
716
719
|
// Skip source analysis of certain URLs for full native passthrough
|
|
717
720
|
skip: /^https:\/\/cdn\.com/, // defaults to null
|
|
718
|
-
// Clean up blob URLs after execution
|
|
719
|
-
revokeBlobURLs: true, // default false
|
|
720
721
|
// Secure mode to not support loading modules without integrity
|
|
721
722
|
// (integrity is always verified even when disabled though)
|
|
722
723
|
enforceIntegrity: true, // default false
|
|
@@ -733,6 +734,8 @@ window.esmsInitOptions = {
|
|
|
733
734
|
onpolyfill: () => {}, // default logs to the console
|
|
734
735
|
// Hook all module resolutions
|
|
735
736
|
resolve: (id, parentUrl, resolve) => resolve(id, parentUrl), // default is spec resolution
|
|
737
|
+
// Hook module source loading
|
|
738
|
+
source: (url, options, parentUrl, defaultSourceHook) => ({ type: 'js', source: 'export var p = 5' }),
|
|
736
739
|
// Hook source fetch function
|
|
737
740
|
fetch: (url, options) => fetch(url, options), // default is native
|
|
738
741
|
// Hook import.meta construction
|
|
@@ -793,15 +796,27 @@ In adddition, the `"all"` option will enable all features.
|
|
|
793
796
|
</script>
|
|
794
797
|
```
|
|
795
798
|
|
|
796
|
-
The
|
|
799
|
+
The reason the `polyfillEnable` option is needed is because ES Module Shims implements a performance optimization where if a browser supports modern modules features to an expected baseline of import maps support, it will skip all polyfill source analysis resulting in full native passthrough performance.
|
|
797
800
|
|
|
798
|
-
|
|
801
|
+
### Pollyfill Disable Option
|
|
799
802
|
|
|
800
|
-
|
|
803
|
+
Conversely to `polyfillEnable` it can be beneficial to dissable unused features where excluding those features from the baseline allows avoiding unnecessary feature detections or unnecessary analysis of module graphs.
|
|
804
|
+
|
|
805
|
+
This option effectively lowers the baseline support allowing wider passthrough cases.
|
|
801
806
|
|
|
802
|
-
|
|
807
|
+
The supported options currently are just `css-modules` and `json-modules`.
|
|
808
|
+
|
|
809
|
+
For example:
|
|
810
|
+
|
|
811
|
+
```html
|
|
812
|
+
<script type="esms-options">
|
|
813
|
+
{
|
|
814
|
+
"polyfillDisable": ["css-modules", "json-modules"]
|
|
815
|
+
}
|
|
816
|
+
</script>
|
|
817
|
+
```
|
|
803
818
|
|
|
804
|
-
|
|
819
|
+
will disable the CSS and JSON feature detections, and lower the baseline passthrough modules support from Chrome 123, Firefox 138 and Safari 17.2 down to Chrome 64, Safari 11.1 and Firefox 67.
|
|
805
820
|
|
|
806
821
|
### Enforce Integrity
|
|
807
822
|
|
|
@@ -988,6 +1003,8 @@ Overriding this hook with an empty function will disable the default polyfill lo
|
|
|
988
1003
|
|
|
989
1004
|
In the above, running in latest Chromium browsers, nothing will be logged, while running in an older browser that does not support newer features like import maps the console log will be output.
|
|
990
1005
|
|
|
1006
|
+
> As this hook does not affect execution, it is recommended for use in polyfill mode.
|
|
1007
|
+
|
|
991
1008
|
#### Error hook
|
|
992
1009
|
|
|
993
1010
|
You can provide a function to handle errors during the module loading process by providing an `onerror` option:
|
|
@@ -1001,6 +1018,8 @@ You can provide a function to handle errors during the module loading process by
|
|
|
1001
1018
|
<script async src="es-module-shims.js"></script>
|
|
1002
1019
|
```
|
|
1003
1020
|
|
|
1021
|
+
> As this hook does not affect execution, it is recommended for use in polyfill mode.
|
|
1022
|
+
|
|
1004
1023
|
#### Import Hook
|
|
1005
1024
|
|
|
1006
1025
|
The import hook is supported for both shim and polyfill modes and provides an async hook which can ensure any necessary work is done before a top-level module import or dynamic `import()` starts further processing.
|
|
@@ -1018,15 +1037,12 @@ The import hook is supported for both shim and polyfill modes and provides an as
|
|
|
1018
1037
|
</script>
|
|
1019
1038
|
```
|
|
1020
1039
|
|
|
1040
|
+
> It is usually recommended to use shim mode with module customization hooks. When the resolve hook is enabled in polyfill mode (or any other hooks), native passthrough will be disabled and modules may be executed twice if they correctly execute without the hook.
|
|
1041
|
+
|
|
1021
1042
|
#### Resolve Hook
|
|
1022
1043
|
|
|
1023
1044
|
The resolve hook is supported for both shim and polyfill modes and allows full customization of the resolver, while still having access to the original resolve function.
|
|
1024
1045
|
|
|
1025
|
-
Note that in polyfill mode the resolve hook may not be called for all modules when native passthrough is occurring and that it still will not affect
|
|
1026
|
-
the native passthrough executions.
|
|
1027
|
-
|
|
1028
|
-
If the resolve hook should apply for all modules in the entire module graph, make sure to set `polyfillEnable: true` to [disable the baseline support analysis opt-out](#baseline-support-analysis-opt-out).
|
|
1029
|
-
|
|
1030
1046
|
```js
|
|
1031
1047
|
<script>
|
|
1032
1048
|
window.esmsInitOptions = {
|
|
@@ -1042,6 +1058,59 @@ If the resolve hook should apply for all modules in the entire module graph, mak
|
|
|
1042
1058
|
</script>
|
|
1043
1059
|
```
|
|
1044
1060
|
|
|
1061
|
+
> It is usually recommended to use shim mode with module customization hooks. When the resolve hook is enabled in polyfill mode (or any other hooks), native passthrough will be disabled and modules may be executed twice if they correctly execute without the hook.
|
|
1062
|
+
|
|
1063
|
+
#### Source Hook
|
|
1064
|
+
|
|
1065
|
+
The source hook used to obtain the module source for a given URL, allowing for the implementation
|
|
1066
|
+
of virtual module sources.
|
|
1067
|
+
|
|
1068
|
+
Note that only valid fetch scheme URLs are supported - `http:` / `https:` / `data:` / `blob:` / `file:`.
|
|
1069
|
+
`https:` or `file:` is therefore recommended for virtual module paths.
|
|
1070
|
+
|
|
1071
|
+
For example:
|
|
1072
|
+
|
|
1073
|
+
```html
|
|
1074
|
+
<script>
|
|
1075
|
+
const virtualFs = {
|
|
1076
|
+
'index.js': `import './src/dep.js';`,
|
|
1077
|
+
'src/dep.js': 'console.log("virtual source execution!")'
|
|
1078
|
+
};
|
|
1079
|
+
esmsInitOptions = {
|
|
1080
|
+
source (url, fetchOpts, parent, defaultSourceHook) {
|
|
1081
|
+
// Only virtualize sources under the URL file:///virtual-pkg/ (i.e. via
|
|
1082
|
+
// `import 'file:///virtual-pkg/index.js'`.
|
|
1083
|
+
if (!url.startsWith('file:///virtual-pkg/')) return defaultSourceHook(url, fetchOpts, parent);
|
|
1084
|
+
|
|
1085
|
+
// Strip the query string prefix for hot reloading workflow support
|
|
1086
|
+
const versionQueryParam = url.match(/\?v=\d+$/);
|
|
1087
|
+
if (versionQueryParam) url = url.slice(0, -versionQueryParam[0].length);
|
|
1088
|
+
|
|
1089
|
+
// Lookup the virtual source from the virtual filesystem and return if found
|
|
1090
|
+
const virtualSource = virtualFs[url.slice('file:///virtual-pkg/'.length)];
|
|
1091
|
+
if (!virtualSource) throw new Error(`Virtual module ${url} not found, imported from ${parent}`);
|
|
1092
|
+
return {
|
|
1093
|
+
type: 'js',
|
|
1094
|
+
source: virtualSource
|
|
1095
|
+
};
|
|
1096
|
+
}
|
|
1097
|
+
};
|
|
1098
|
+
</script>
|
|
1099
|
+
```
|
|
1100
|
+
|
|
1101
|
+
For support in hot reloading workflows, note that the ?v={Number} version query
|
|
1102
|
+
string suffix will be passed so needs to be checked and removed if applicable.
|
|
1103
|
+
|
|
1104
|
+
For JS, CSS, JSON and TypeScript, it provides the source text string.
|
|
1105
|
+
For WebAssembly, it provides the compiled WebAssembly.Module record.
|
|
1106
|
+
|
|
1107
|
+
The default implementation uses globalThis.fetch to obtain the response and then
|
|
1108
|
+
the 'content-type' MIME type header to infer the module type, per HTML semantics.
|
|
1109
|
+
|
|
1110
|
+
URL may be returned differently from the request URL because responseURL
|
|
1111
|
+
is allowed to be distinct from requestURL in the module system even thoough
|
|
1112
|
+
requestURL is used as the registry key only.
|
|
1113
|
+
|
|
1045
1114
|
#### Meta Hook
|
|
1046
1115
|
|
|
1047
1116
|
The meta hook allows customizing the `import.meta` object in each module scope.
|
|
@@ -1068,6 +1137,8 @@ import assert from 'assert';
|
|
|
1068
1137
|
assert.ok(import.meta.custom.startsWith('custom value'));
|
|
1069
1138
|
```
|
|
1070
1139
|
|
|
1140
|
+
> It is usually recommended to use shim mode with module customization hooks. When the resolve hook is enabled in polyfill mode (or any other hooks), native passthrough will be disabled and modules may be executed twice if they correctly execute without the hook.
|
|
1141
|
+
|
|
1071
1142
|
#### Fetch Hook
|
|
1072
1143
|
|
|
1073
1144
|
The fetch hook is supported for shim mode only.
|
|
@@ -1125,6 +1196,8 @@ window.esmsInitOptions = {
|
|
|
1125
1196
|
}
|
|
1126
1197
|
```
|
|
1127
1198
|
|
|
1199
|
+
> It is usually recommended to use shim mode with module customization hooks. When the resolve hook is enabled in polyfill mode (or any other hooks), native passthrough will be disabled and modules may be executed twice if they correctly execute without the hook.
|
|
1200
|
+
|
|
1128
1201
|
## Implementation Details
|
|
1129
1202
|
|
|
1130
1203
|
### Import Rewriting
|