es-module-shims 1.10.0 → 2.0.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 +148 -125
- package/dist/es-module-shims.debug.js +722 -583
- package/dist/es-module-shims.js +708 -576
- package/dist/es-module-shims.wasm.js +708 -576
- package/index.d.ts +1 -0
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -2,18 +2,15 @@
|
|
|
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 [91% of users](https://caniuse.com/import-maps) with native import maps support.
|
|
6
6
|
|
|
7
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).
|
|
8
8
|
|
|
9
9
|
The following modules features are polyfilled:
|
|
10
10
|
|
|
11
11
|
* [Import Maps](#import-maps) polyfill.
|
|
12
|
-
* Dynamic `import()` shimming when necessary in eg older Firefox versions.
|
|
13
|
-
* `import.meta` and `import.meta.url`.
|
|
14
12
|
* [JSON](#json-modules) and [CSS modules](#css-modules) with import assertions (when enabled).
|
|
15
13
|
* [Wasm modules](#wasm-modules) with support for Source Phase Imports (when enabled).
|
|
16
|
-
* [`<link rel="modulepreload">` is shimmed](#modulepreload) in browsers without import maps support.
|
|
17
14
|
|
|
18
15
|
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.
|
|
19
16
|
|
|
@@ -30,7 +27,7 @@ Because we are still using the native module loader the edge cases work out comp
|
|
|
30
27
|
Include ES Module Shims with a `async` attribute on the script, then include an import map and module scripts normally:
|
|
31
28
|
|
|
32
29
|
```html
|
|
33
|
-
<script async src="https://ga.jspm.io/npm:es-module-shims@
|
|
30
|
+
<script async src="https://ga.jspm.io/npm:es-module-shims@2.0.0/dist/es-module-shims.js"></script>
|
|
34
31
|
|
|
35
32
|
<!-- https://generator.jspm.io/#U2NhYGBkDM0rySzJSU1hKEpNTC5xMLTQM9Az0C1K1jMAAKFS5w0gAA -->
|
|
36
33
|
<script type="importmap">
|
|
@@ -65,9 +62,9 @@ This error is important - it means that the native browser loader didn't execute
|
|
|
65
62
|
at link time, and before execution time. And this is what allows the polyfill to be able to reexecute the modules and their dependencies
|
|
66
63
|
without risk of duplicate execution.
|
|
67
64
|
|
|
68
|
-
The ES Module Shims polyfill will analyze the browser to
|
|
69
|
-
|
|
70
|
-
|
|
65
|
+
The ES Module Shims polyfill will analyze the browser to check its fine-grained support for various import maps and modules features.
|
|
66
|
+
If it is deeemed to support a baseline set of features, and multiple import maps are not in use, the polyfill will do no further work. Otherwise, it
|
|
67
|
+
will analyze all module scripts on the page to see if any of them have static module syntax that would fail. If found, that graph will then be reexecuted through ES Module Shims using its internal rewriting of import statements to polyfill features.
|
|
71
68
|
|
|
72
69
|
When the polyfill kicks in another console log message is output(which can be disabled or customized via the [polyfill hook](#polyfill-hook)):
|
|
73
70
|
|
|
@@ -75,6 +72,9 @@ When the polyfill kicks in another console log message is output(which can be di
|
|
|
75
72
|
^^ Module error above is polyfilled and can be ignored ^^
|
|
76
73
|
```
|
|
77
74
|
|
|
75
|
+
The fetch options used by the polyfill are carefully followed per the spec. In older Firefox and Safari this fetch network cache is
|
|
76
|
+
not fully shared with the polyfill so separate entries can be seen in the network tab, network-level cache coalescing is still seen at the very least.
|
|
77
|
+
|
|
78
78
|
### Polyfill Edge Case: Dynamic Import
|
|
79
79
|
|
|
80
80
|
Only static link-time errors are polyfilled, not runtime errors.
|
|
@@ -127,45 +127,52 @@ If a static failure is not possible and dynamic import must be used, one alterna
|
|
|
127
127
|
|
|
128
128
|
When running in polyfill mode, it can be thought of that are effectively two loaders running on the page - the ES Module Shims polyfill loader, and the native loader.
|
|
129
129
|
|
|
130
|
-
|
|
130
|
+
Whenever possible, the polyfill loader will share native modules that can be correctly executed, with one exception per the previous section - modules which use dynamic import, which are imported as dependencies of modules which require polyfill features.
|
|
131
131
|
|
|
132
|
-
|
|
132
|
+
For example consider two shimmed modules, both of which use import maps:
|
|
133
133
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
"imports": {
|
|
138
|
-
"dep": "/dep.js"
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
</script>
|
|
142
|
-
<script type="module">
|
|
143
|
-
import '/dep.js';
|
|
144
|
-
</script>
|
|
145
|
-
<script type="module">
|
|
146
|
-
import 'dep';
|
|
147
|
-
</script>
|
|
134
|
+
`shim-a.js`
|
|
135
|
+
```js
|
|
136
|
+
import 'mapped-dep-a';
|
|
148
137
|
```
|
|
149
138
|
|
|
150
|
-
|
|
151
|
-
|
|
139
|
+
`shim-b.js`
|
|
140
|
+
```js
|
|
141
|
+
import 'mapped-dep-b';
|
|
152
142
|
```
|
|
153
143
|
|
|
154
|
-
|
|
144
|
+
where `mapped-dep-a` resolves to `/dep-a.js` and `mapped-dep-b` resolves to `/dep-b.js`, respectively containing:
|
|
155
145
|
|
|
156
|
-
|
|
146
|
+
`/dep-a.js`
|
|
147
|
+
```js
|
|
148
|
+
console.log('dep a');
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
`/dep-b.js`
|
|
152
|
+
```js
|
|
153
|
+
console.log('dep b');
|
|
154
|
+
import(expr);
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
While the shim modules are always loaded in the shim loader, the `dep-a.js` module is loaded from the native loader since it does not require any polyfilling.
|
|
158
|
+
|
|
159
|
+
On th other hand, `dep-b.js` is loaded from the shim loader as well _because it was loaded by a polyfilled parent graph and uses dynamic import_. Within the polyfill loader, the `import(expr)` is replaced with `importShim(expr)` to support import maps. This is in contrast to top-level native graphs which do not get shimmed per the previous section.
|
|
160
|
+
|
|
161
|
+
As a result, `import('/dep-a.js')` in the native loader is the same instance as the `dep-a.js` loaded by `shim-a.js`, but `dep-b` would be executed twice if passed into `import('/dep-b.js')` - the shim loader and native loader instances are separate, and `dep b` would be logged twice.
|
|
157
162
|
|
|
158
|
-
|
|
163
|
+
Note that this is the ONLY scenario in which instance sharing will not otherwise occur in the polyfill loader.
|
|
164
|
+
|
|
165
|
+
A workaround to this instance sharing case is to use the [`skip` option](#skip) to list modules which should always be loaded via the native loader (which also saves on analysis work time for performance):
|
|
159
166
|
|
|
160
167
|
```html
|
|
161
168
|
<script type="esms-options">
|
|
162
169
|
{
|
|
163
|
-
"skip": ["/dep.js"]
|
|
170
|
+
"skip": ["/dep-b.js"]
|
|
164
171
|
}
|
|
165
172
|
</script>
|
|
166
173
|
```
|
|
167
174
|
|
|
168
|
-
The above would then fully cause dependency module instance to be shared between ES Module Shims and the native loader, with the polyfill then logging `"
|
|
175
|
+
The above would then fully cause dependency module instance of dep-b to be shared between ES Module Shims and the native loader, with the polyfill then logging `"dep b"` only once.
|
|
169
176
|
|
|
170
177
|
#### No Shim Scripts
|
|
171
178
|
|
|
@@ -215,38 +222,31 @@ Works in all browsers with [baseline ES module support](https://caniuse.com/#fea
|
|
|
215
222
|
|
|
216
223
|
Browser Compatibility on baseline ES modules support **with** ES Module Shims:
|
|
217
224
|
|
|
218
|
-
| ES Modules Features | Chrome (
|
|
225
|
+
| ES Modules Features | Chrome (71+) | Firefox (60+) | Safari (10.1+) |
|
|
219
226
|
| ----------------------------------------------- | ------------------------------------ | ------------------------------------ | ------------------------------------ |
|
|
220
227
|
| [modulepreload](#modulepreload) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
|
|
221
|
-
| [Dynamic Import](#dynamic-import) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
|
|
222
|
-
| [import.meta.url](#importmetaurl) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
|
|
223
228
|
| [Import Maps](#import-maps) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
|
|
229
|
+
| [Import Map Integrity](#import-map-integrity) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
|
|
230
|
+
| [Multiple Import Maps](#multiple-import-maps) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
|
|
224
231
|
| [JSON Modules](#json-modules) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
|
|
225
|
-
| [CSS Modules](#css-modules) | :heavy_check_mark
|
|
232
|
+
| [CSS Modules](#css-modules) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
|
|
226
233
|
| [Wasm Modules](#wasm-modules) | 89+ | 89+ | 15+ |
|
|
227
|
-
| [import.meta.resolve](#resolve) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
|
|
228
|
-
| [Module Workers](#module-workers) (via wrapper) | 63+ | ~113+ | 15+ |
|
|
229
|
-
| Top-Level Await (unpolyfilled<sup>3</sup>) | 89+ | 89+ | 15+ |
|
|
230
|
-
|
|
231
|
-
* 1: _CSS module support requires a separate [Constructable Stylesheets polyfill](https://github.com/calebdwilliams/construct-style-sheets#readme)._
|
|
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._
|
|
233
234
|
|
|
234
235
|
Browser compatibility **without** ES Module Shims:
|
|
235
236
|
|
|
236
|
-
| ES Modules Features
|
|
237
|
-
|
|
|
238
|
-
| [modulepreload](#modulepreload)
|
|
239
|
-
| [
|
|
240
|
-
| [
|
|
241
|
-
| [Import
|
|
242
|
-
| [
|
|
243
|
-
| [
|
|
244
|
-
| [
|
|
245
|
-
| [
|
|
246
|
-
|
|
|
247
|
-
|
|
|
248
|
-
|
|
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.
|
|
237
|
+
| ES Modules Features | Chrome | Firefox | Safari |
|
|
238
|
+
| --------------------------------------------- | ------------------ | ------------------ | ------------------ |
|
|
239
|
+
| [modulepreload](#modulepreload) | 66+ | 115+ | 17.5+ |
|
|
240
|
+
| [import.meta.url](#importmetaurl) | ~76+ | ~67+ | ~12+ |
|
|
241
|
+
| [Import Maps](#import-maps) | 89+ | 108+ | 16.4+ |
|
|
242
|
+
| [Import Map Integrity](#import-map-integrity) | 127+ | :x: | :x: |
|
|
243
|
+
| [Multiple Import Maps](#multiple-import-maps) | Pending | :x: | :x: |
|
|
244
|
+
| [JSON Modules](#json-modules) | 123+ | :x: | 17.2+ |
|
|
245
|
+
| [CSS Modules](#css-modules) | 123+ | :x: | :x: |
|
|
246
|
+
| [Wasm Modules](#wasm-modules) | :x: | :x: | :x: |
|
|
247
|
+
| import.meta.resolve | 105+ | 106+ | 16.4+ |
|
|
248
|
+
| [Module Workers](#module-workers) | ~68+ | ~113+ | 15+ |
|
|
249
|
+
| Top-Level Await | 89+ | 89+ | 15+ |
|
|
250
250
|
|
|
251
251
|
### Import Maps
|
|
252
252
|
|
|
@@ -280,18 +280,6 @@ Using this polyfill we can write:
|
|
|
280
280
|
|
|
281
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.
|
|
282
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
|
-
|
|
287
|
-
#### Multiple Import Maps
|
|
288
|
-
|
|
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.
|
|
290
|
-
|
|
291
|
-
In polyfill mode, multiple import maps are therefore not supported.
|
|
292
|
-
|
|
293
|
-
In shim mode, support for multiple `importmap-shim` scripts follows the [import map extensions](https://github.com/guybedford/import-maps-extensions) proposal.
|
|
294
|
-
|
|
295
283
|
#### External Import Maps
|
|
296
284
|
|
|
297
285
|
External import maps (using a `"src"` attribute) are not currently supported in any native implementation.
|
|
@@ -300,9 +288,13 @@ In polyfill mode, external import maps are therefore not supported.
|
|
|
300
288
|
|
|
301
289
|
In shim mode, external import maps are fully supported.
|
|
302
290
|
|
|
303
|
-
|
|
291
|
+
### Multiple Import Maps
|
|
292
|
+
|
|
293
|
+
Multiple import maps have been recently implemented in Chromium in https://bugs.chromium.org/p/chromium/issues/detail?id=927119, including supporting dynamically loading import maps even after modules have been loaded.
|
|
304
294
|
|
|
305
|
-
|
|
295
|
+
In polyfill mode, multiple import maps are supported.
|
|
296
|
+
|
|
297
|
+
Support for dynamically injecting import maps with JavaScript via e.g.:
|
|
306
298
|
|
|
307
299
|
```js
|
|
308
300
|
document.body.appendChild(Object.assign(document.createElement('script'), {
|
|
@@ -311,11 +303,64 @@ document.body.appendChild(Object.assign(document.createElement('script'), {
|
|
|
311
303
|
}));
|
|
312
304
|
```
|
|
313
305
|
|
|
314
|
-
is
|
|
306
|
+
is also provided using mutation observers.
|
|
307
|
+
|
|
308
|
+
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
|
+
|
|
310
|
+
Therefore, imports that would otherwise be supported fine by the first map can't be polyfilled, for example:
|
|
311
|
+
|
|
312
|
+
```html
|
|
313
|
+
<script type="importmap">
|
|
314
|
+
{
|
|
315
|
+
"imports": {
|
|
316
|
+
"a": "/a.js"
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
</script>
|
|
320
|
+
<script type="importmap">
|
|
321
|
+
{
|
|
322
|
+
"scopes": {
|
|
323
|
+
"/": {
|
|
324
|
+
"a": "/b.js"
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
</script>
|
|
329
|
+
<script type="module">
|
|
330
|
+
import 'a';
|
|
331
|
+
</script>
|
|
332
|
+
```
|
|
315
333
|
|
|
316
|
-
|
|
334
|
+
In the above, browsers with single import maps support will resolve `/a.js` and the polyfill will not apply, while browsers without any import maps support will be polyfilled to resolve `/b.js`.
|
|
317
335
|
|
|
318
|
-
|
|
336
|
+
Instead, following the usual advice, make sure to design the app to either have static failures or not on all polyfill environments to get well-defined polyfill behaviour:
|
|
337
|
+
|
|
338
|
+
```html
|
|
339
|
+
<script type="importmap">
|
|
340
|
+
{
|
|
341
|
+
"imports": {
|
|
342
|
+
"a": "/a.js"
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
</script>
|
|
346
|
+
<script type="importmap">
|
|
347
|
+
{
|
|
348
|
+
"imports": {
|
|
349
|
+
"b": "/b.js"
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
</script>
|
|
353
|
+
<script type="module">
|
|
354
|
+
import 'a';
|
|
355
|
+
</script>
|
|
356
|
+
<script type="module">
|
|
357
|
+
import 'b';
|
|
358
|
+
</script>
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
The above will then correctly execute both `a` and `b`, with only the `b` importer being polyfilled.
|
|
362
|
+
|
|
363
|
+
Note that shimmed graphs will always support correct mappings - the above rules only apply to the initial polyfill engagement.
|
|
319
364
|
|
|
320
365
|
#### Reading current import map state
|
|
321
366
|
|
|
@@ -337,13 +382,9 @@ const importMap = { imports: {/*...*/}, scopes: {/*...*/} };
|
|
|
337
382
|
importShim.addImportMap(importMap);
|
|
338
383
|
```
|
|
339
384
|
|
|
385
|
+
### Shim Import
|
|
340
386
|
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
> Stability: Stable browser standard
|
|
344
|
-
|
|
345
|
-
Dynamic `import(...)` within any modules loaded will be rewritten as `importShim(...)` automatically
|
|
346
|
-
providing full support for all es-module-shims features through dynamic import.
|
|
387
|
+
Dynamic `import(...)` within any modules loaded will be rewritten as `importShim(...)` automatically providing full support for all es-module-shims features through dynamic import.
|
|
347
388
|
|
|
348
389
|
To load code dynamically (say from the browser console), `importShim` can be called similarly:
|
|
349
390
|
|
|
@@ -389,7 +430,9 @@ A full example of such a CSP workflow is provided below:
|
|
|
389
430
|
<script async src="es-module-shims.js"></script>
|
|
390
431
|
<script type="importmap" nonce="n0nce">
|
|
391
432
|
{
|
|
392
|
-
"
|
|
433
|
+
"imports": {
|
|
434
|
+
"pkg": "/pkg.js"
|
|
435
|
+
}
|
|
393
436
|
}
|
|
394
437
|
</script>
|
|
395
438
|
<script type="module" nonce="n0nce">
|
|
@@ -401,6 +444,28 @@ import pkg from 'pkg';
|
|
|
401
444
|
|
|
402
445
|
To use the Web Assembly / non-CSP build of ES Module Shims, this is available as a self-contained single file at `es-module-shims/wasm` or `es-module-shims/dist/es-module-shims.wasm.js` in the package folder.
|
|
403
446
|
|
|
447
|
+
#### Import Map Integrity
|
|
448
|
+
|
|
449
|
+
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:
|
|
450
|
+
|
|
451
|
+
```html
|
|
452
|
+
<script type="importmap">
|
|
453
|
+
{
|
|
454
|
+
"imports": {
|
|
455
|
+
"pkg": "/pkg.js"
|
|
456
|
+
},
|
|
457
|
+
"integrity": {
|
|
458
|
+
"/pkg.js": "sha384-..."
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
</script>
|
|
462
|
+
<script>
|
|
463
|
+
import "/pkg.js";
|
|
464
|
+
</script>
|
|
465
|
+
```
|
|
466
|
+
|
|
467
|
+
Note integrity can only be validated when in shim mode or when the polyfill is definitely engaging.
|
|
468
|
+
|
|
404
469
|
### JSON Modules
|
|
405
470
|
|
|
406
471
|
> Stability: WhatWG Standard, Single Browser Implementer
|
|
@@ -431,14 +496,6 @@ import sheet from 'https://site.com/sheet.css' with { type: 'css' };
|
|
|
431
496
|
</script>
|
|
432
497
|
```
|
|
433
498
|
|
|
434
|
-
To support the polyfill or shim of this feature, the [Constructable Stylesheets polyfill](https://github.com/calebdwilliams/construct-style-sheets#readme) must be separately included in browsers not supporting [Constructable Stylesheets](https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet/CSSStyleSheet) eg via:
|
|
435
|
-
|
|
436
|
-
```html
|
|
437
|
-
<script async src="https://unpkg.com/construct-style-sheets-polyfill@3.1.0/dist/adoptedStyleSheets.js"></script>
|
|
438
|
-
```
|
|
439
|
-
|
|
440
|
-
For more information see the [web.dev article](https://web.dev/css-module-scripts/).
|
|
441
|
-
|
|
442
499
|
In addition CSS modules need to be served with a valid CSS content type.
|
|
443
500
|
|
|
444
501
|
### Wasm Modules
|
|
@@ -474,27 +531,6 @@ const instance = await WebAssembly.instantiate(mod, { /* ...imports */ });
|
|
|
474
531
|
|
|
475
532
|
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.
|
|
476
533
|
|
|
477
|
-
### Resolve
|
|
478
|
-
|
|
479
|
-
> Stability: Draft HTML PR
|
|
480
|
-
|
|
481
|
-
`import.meta.resolve` provides a contextual resolver within modules. It is synchronous, changed from being formerly asynchronous due to following the [browser specification PR](https://github.com/whatwg/html/pull/5572).
|
|
482
|
-
|
|
483
|
-
The second argument to `import.meta.resolve` permits a custom parent URL scope for the resolution (not currently in the browser spec), which defaults to `import.meta.url`.
|
|
484
|
-
|
|
485
|
-
```js
|
|
486
|
-
// resolve a relative path to a module
|
|
487
|
-
var resolvedUrl = import.meta.resolve('./relative.js');
|
|
488
|
-
// resolve a dependency from a module
|
|
489
|
-
var resolvedUrl = import.meta.resolve('dep');
|
|
490
|
-
// resolve a path
|
|
491
|
-
var resolvedUrlPath = import.meta.resolve('dep/');
|
|
492
|
-
// resolve with a custom parent scope
|
|
493
|
-
var resolvedUrl = import.meta.resolve('dep', 'https://site.com/another/scope');
|
|
494
|
-
```
|
|
495
|
-
|
|
496
|
-
Node.js also implements a similar API, although it's in the process of shifting to a synchronous resolver.
|
|
497
|
-
|
|
498
534
|
### Module Workers
|
|
499
535
|
|
|
500
536
|
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+).
|
|
@@ -554,10 +590,8 @@ window.esmsInitOptions = {
|
|
|
554
590
|
polyfillEnable: ['css-modules', 'json-modules'], // default empty
|
|
555
591
|
// Custom CSP nonce
|
|
556
592
|
nonce: 'n0nce', // default is automatic detection
|
|
557
|
-
// Don't retrigger load events on module scripts (DOMContentLoaded, domready)
|
|
593
|
+
// Don't retrigger load events on module scripts (DOMContentLoaded, domready, window 'onload')
|
|
558
594
|
noLoadEventRetriggers: true, // default false
|
|
559
|
-
// Retrigger window 'load' event (will be combined into load event above on next major)
|
|
560
|
-
globalLoadEventRetrigger: true, // default false
|
|
561
595
|
// Skip source analysis of certain URLs for full native passthrough
|
|
562
596
|
skip: /^https:\/\/cdn\.com/, // defaults to null
|
|
563
597
|
// Clean up blob URLs after execution
|
|
@@ -643,7 +677,7 @@ This option can also be set to `true` to entirely disable the native passthrough
|
|
|
643
677
|
|
|
644
678
|
### Enforce Integrity
|
|
645
679
|
|
|
646
|
-
|
|
680
|
+
While integrity is always verified and validated when available, when enabled, `enforceIntegrity` will ensure that **all modules must have integrity defined** when loaded through ES Module Shims 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.
|
|
647
681
|
|
|
648
682
|
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:
|
|
649
683
|
|
|
@@ -684,7 +718,7 @@ Alternatively, add a `blob:` URL policy with the CSP build to get CSP compatibil
|
|
|
684
718
|
|
|
685
719
|
### No Load Event Retriggers
|
|
686
720
|
|
|
687
|
-
Because of the extra processing done by ES Module Shims it is possible for static module scripts to execute after the `DOMContentLoaded` or `
|
|
721
|
+
Because of the extra processing done by ES Module Shims it is possible for static module scripts to execute after the `DOMContentLoaded`, `readystatechange` or window `load` events they expect, which can cause missed attachment.
|
|
688
722
|
|
|
689
723
|
In addition, script elements will also have their load events refired when polyfilled.
|
|
690
724
|
|
|
@@ -695,20 +729,13 @@ In such a case, this double event firing can be disabled with the `noLoadEventRe
|
|
|
695
729
|
```js
|
|
696
730
|
<script type="esms-options">
|
|
697
731
|
{
|
|
698
|
-
// do not re-trigger DOM events (onreadystatechange, DOMContentLoaded)
|
|
732
|
+
// do not re-trigger DOM events (onreadystatechange, DOMContentLoaded, window 'onload')
|
|
699
733
|
"noLoadEventRetriggers": true
|
|
700
734
|
}
|
|
701
735
|
</script>
|
|
702
736
|
<script async src="es-module-shims.js"></script>
|
|
703
737
|
```
|
|
704
738
|
|
|
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
|
-
|
|
712
739
|
### Skip
|
|
713
740
|
|
|
714
741
|
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.
|
|
@@ -865,10 +892,6 @@ If the resolve hook should apply for all modules in the entire module graph, mak
|
|
|
865
892
|
</script>
|
|
866
893
|
```
|
|
867
894
|
|
|
868
|
-
Support for an asynchronous resolve hook has been deprecated as of 1.5.0 and will be removed in the next major.
|
|
869
|
-
|
|
870
|
-
Instead async work should be done with the import hook.
|
|
871
|
-
|
|
872
895
|
#### Meta Hook
|
|
873
896
|
|
|
874
897
|
The meta hook allows customizing the `import.meta` object in each module scope.
|