es-module-shims 2.5.1 → 2.6.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/index.d.ts CHANGED
@@ -1,6 +1,10 @@
1
1
  interface ESMSInitOptions {
2
2
  /**
3
- * Enable Shim Mode
3
+ * Enable Shim Mode, where only
4
+ * <script type="module-shim"></script>
5
+ * and
6
+ * <script type="importmap-shim"></script>
7
+ * are supported and the native loader is not used directly (no {@link nativePassthrough}).
4
8
  */
5
9
  shimMode?: boolean;
6
10
 
@@ -16,21 +20,59 @@ interface ESMSInitOptions {
16
20
 
17
21
  /**
18
22
  * Enable polyfill features.
19
- *
23
+ *
20
24
  * Currently supports:
21
25
  * - 'wasm-modules': Both 'wasm-module-sources' and 'wasm-module-instances'
22
26
  * - 'wasm-module-sources': Support for Wasm source phase imports (import source mod from './mod.wasm')
23
27
  * - 'wasm-module-instances': Support for Wasm instance phase imports (import * as mod from './mod.wasm')
24
28
  * - 'import-defer': Support for import defer syntax (import defer * as ns from './foo.js')
25
29
  */
26
- polyfillEnable?: Array<'wasm-modules' | 'wasm-module-instances' | 'wasm-module-sources' | 'import-defer'>;
30
+ polyfillEnable?: Array<'wasm-modules' | 'wasm-module-instances' | 'wasm-module-sources' | 'import-defer' | 'all'>;
27
31
 
28
32
  /**
29
- * Version
30
- *
33
+ * Disable polyfill features:
34
+ * - 'css-modules': CSS Module imports of the form `import styles from './source.css' with { type: 'css' }`
35
+ * - 'json-modules': JSON Module imports of the form `import styles from './source.css' with { type: 'css' }`
36
+ *
37
+ * These features are enabled by default but only supported in Chrome 123+, so that disabling these features
38
+ * allows ES Module Shims to avoid unnecessary feature detections and module analysis,
39
+ * so this will ensure {@link nativePassthrough} for Chrome 89+, Firefox 108+, Safari 16.4+
40
+ */
41
+ polyfillDisable?: Array<'css-modules' | 'json-modules'>;
42
+
43
+ /**
44
+ * @default true
45
+ *
46
+ * By default, native passthrough is enabled so that modules will be entirely loaded through the native loader
47
+ * when it is known to be safe to do so. This happens when:
48
+ *
49
+ * 1. Polyfill mode is enabled (which is by default, unless shim mode is explicitly enabled)
50
+ * 2. No hot reloading or hooks options are in use
51
+ * 3. Then either of:
52
+ * a) The browser supports the minimum baseline features expected by ES Module Shims
53
+ * (either by default, or as customized via polyfillEnable / polyfillDisable)
54
+ * b) Or if the browser does not support the minimum baseline features, ES Module Shims
55
+ * will analyze the entire subgraph of modules and still use passthrough after that analysis
56
+ * if every module down the graph is known to be natively supported without causing a static error on load.
57
+ *
58
+ * Setting this option to false can avoid some potential double fetching on the polyfill path when modules are
59
+ * analyzed for support, since the native loader cache is not always shared with the fetch cache in some browser.
60
+ *
61
+ * In addition dynamic import() not being polyfilled can also be worked around by using this option since it will
62
+ * replace all dynamic import() expressions with an importShim() expression by using the polyfill loader instead
63
+ * of the native loader.
64
+ *
65
+ * This option is therefore serving two purposes - firstly to easily disable internally in the case of hot reloading
66
+ * and hooks, and secondly to allow this kind customization in the case of dynamic import or performance cases.
67
+ */
68
+ nativePassthrough?: boolean;
69
+
70
+ /**
71
+ * #### Version
72
+ *
31
73
  * Useful when there are multiple instances of ES Module Shims for whatever reason
32
74
  * interacting in an application.
33
- *
75
+ *
34
76
  * When set, ES Module Shims will early exit if its own version is not the expected version.
35
77
  * Note that this feature is only supported from version 2.4.0 upwards.
36
78
  */
@@ -45,30 +87,39 @@ interface ESMSInitOptions {
45
87
  enforceIntegrity?: boolean;
46
88
 
47
89
  /**
48
- * Nonce for CSP build
90
+ * Nonce for CSP compatibility
91
+ *
92
+ * When set, feature detection scripts that run in a separate feature detection
93
+ * iframe will use this nonce to ensure CSP compatibility.
49
94
  */
50
95
  nonce?: boolean;
51
96
 
52
97
  /**
53
- * Disable retriggering of document readystate and DOMContentLoaded
98
+ * Disable retriggering of document readystate, DOMContentLoaded and the window load event.
99
+ *
100
+ * ES Module Shims will re-trigger these events because normally <script type="module"></script>
101
+ * runs under static defer semantics and results in delaying these events until it completes.
102
+ *
103
+ * When modules are polyfilled, the top-level load will first complete and the events will trigger
104
+ * before the modules have run, which may result in missing attachments.
105
+ *
106
+ * By retriggering these events we ensure later attachment.
107
+ *
108
+ * The consequence of this approach is that it does assume that attachments are idempotent, and in
109
+ * where that is not the case this can result in duplicate DOM injections or attachments.
110
+ *
111
+ * This option can therefore be used to disable this feature when those problems arise.
54
112
  */
55
113
  noLoadEventRetriggers?: boolean;
56
114
 
57
115
  /**
58
- * Enable retriggering of the Window / global 'load' event
59
- */
60
- globalLoadEventRetrigger?: boolean;
61
-
62
- /**
63
- * #### Skip Processing Stability
64
- *
65
- * > Non-spec feature
66
116
  *
67
117
  * When loading modules that you know will only use baseline modules
68
118
  * features, it is possible to set a rule to explicitly opt-out modules
69
- * from rewriting. This improves performance because those modules then do
70
- * not need to be processed or transformed at all, so that only local
71
- * application code is handled and not library code.
119
+ * from any analysis and rewriting, effectively forcing {@link nativePassthrough}
120
+ * only for these modules and their dependencies. This improves performance because
121
+ * those modules then do not need to be processed or transformed at all, so that
122
+ * e.g. only local application code is handled and not library code.
72
123
  *
73
124
  * This can be configured by setting the importShim.skip URL regular
74
125
  * expression:
@@ -77,10 +128,8 @@ interface ESMSInitOptions {
77
128
  * importShim.skip = /^https:\/\/cdn\.com/;
78
129
  * ```
79
130
  *
80
- * By default, this expression supports jspm.dev, dev.jspm.io and
81
- * cdn.pika.dev.
82
131
  */
83
- skip?: RegExp | string[] | string
132
+ skip?: RegExp | string[] | string;
84
133
 
85
134
  /**
86
135
  * #### Error hook
@@ -105,11 +154,7 @@ interface ESMSInitOptions {
105
154
  *
106
155
  * Provide a custom resolver function.
107
156
  */
108
- resolve?: (
109
- id: string,
110
- parentUrl: string,
111
- resolve: (id: string, parentUrl: string) => string
112
- ) => string | Promise<string>;
157
+ resolve?: (id: string, parentUrl: string, parentResolve: (id: string, parentUrl: string) => string) => string;
113
158
 
114
159
  /**
115
160
  * #### Fetch Hook
@@ -172,15 +217,94 @@ interface ESMSInitOptions {
172
217
  * });
173
218
  * }
174
219
  * ```
220
+ *
221
+ * @deprecated use the {@link source} hook instead.
175
222
  */
176
223
  fetch?: (input: RequestInfo, init?: RequestInit) => Promise<Response>;
177
224
 
225
+ /**
226
+ * #### Source Hook
227
+ *
228
+ * Source hook used to obtain the module source for a given URL.
229
+ * Can be used to implement virtual modules.
230
+ *
231
+ * Note that only valid fetch scheme URLs are supported - http(s): / data: / blob: / file:
232
+ *
233
+ * https: or file: is therefore recommended for virtual module paths.
234
+ *
235
+ * @example
236
+ * ```
237
+ * <script>
238
+ * const virtualFs = {
239
+ * 'index.js': `import './src/dep.js';`,
240
+ * 'src/dep.js': 'console.log("virtual source execution!")'
241
+ * };
242
+ * esmsInitOptions = {
243
+ * source (url, fetchOpts, parent, defaultSourceHook) {
244
+ * // Only virtualize sources under the URL file:///virtual-pkg/ i.e. via
245
+ * // `import 'file:///virtual-pkg/index.js'`.
246
+ * if (!url.startsWith('file:///virtual-pkg/')) return defaultSourceHook(url, fetchOpts, parent);
247
+ *
248
+ * // Strip the query string prefix for hot reloading workflow support
249
+ * const versionQueryParam = url.match(/\?v=\d+$/);
250
+ * if (versionQueryParam) url = url.slice(0, -versionQueryParam[0].length);
251
+ *
252
+ * // Lookup the virtual source from the virtual filesystem and return if found
253
+ * const virtualSource = virtualFs[url.slice('file:///virtual-pkg/'.length)];
254
+ * if (!virtualSource) throw new Error(`Virtual module ${url} not found, imported from ${parent}`);
255
+ * return {
256
+ * type: 'js',
257
+ * source: virtualSource
258
+ * };
259
+ * }
260
+ * };
261
+ * </script>
262
+ * ```
263
+ *
264
+ * For support in hot reloading workflows, note that the ?v={Number} version query
265
+ * string suffix will be passed so needs to be checked and removed if applicable.
266
+ *
267
+ * For JS, CSS, JSON and TypeScript, it provides the source text string.
268
+ * For WebAssembly, it provides the compiled WebAssembly.Module record.
269
+ *
270
+ * The default implementation uses globalThis.fetch to obtain the response and then
271
+ * the 'content-type' MIME type header to infer the module type, per HTML semantics.
272
+ *
273
+ * URL may be returned differently from the request URL because responseURL
274
+ * is allowed to be distinct from requestURL in the module system even thoough
275
+ * requestURL is used as the registry key only.
276
+ *
277
+ * @param url The URL of the module source being requested
278
+ * @param fetchOpts Any custom fetch options (including integrity)
279
+ * @param parent The parent importer URL for debug messages
280
+ * @param defaultSourceHook The default source hook, to be used as the fallback
281
+ */
282
+ source?: (
283
+ url: string,
284
+ fetchOpts: RequestInit,
285
+ parent: string,
286
+ defaultSourceHook: (
287
+ url,
288
+ fetchOptions,
289
+ parent
290
+ ) => Promise<{
291
+ url: string;
292
+ type: 'js' | 'wasm' | 'css' | 'json' | 'ts';
293
+ source: string | WebAssembly.Module;
294
+ }>
295
+ ) => Promise<{
296
+ url?: string | undefined;
297
+ type: 'js' | 'wasm' | 'css' | 'json' | 'ts';
298
+ source: string | WebAssembly.Module;
299
+ }>;
300
+
178
301
  /**
179
302
  * #### Revoke Blob URLs
180
303
  *
181
304
  * Set to *true* to cleanup blob URLs from memory after execution.
182
305
  * Can cost some compute time for large loads.
183
306
  *
307
+ * @deprecated this option is now on by default and will be removed in a future version.
184
308
  */
185
309
  revokeBlobURLs?: boolean;
186
310
 
@@ -193,11 +317,13 @@ interface ESMSInitOptions {
193
317
  mapOverrides?: boolean;
194
318
 
195
319
  /**
196
- * #### Meta hook
197
- *
198
- * Register a callback for import.meta construction.
199
- *
200
- */
320
+ * #### Meta hook
321
+ *
322
+ * Register a callback for import.meta construction.
323
+ *
324
+ * @param meta The import.meta object to mutate
325
+ * @param url The URL of the module
326
+ */
201
327
  meta?: (meta: any, url: string) => void;
202
328
 
203
329
  /**
@@ -205,8 +331,10 @@ interface ESMSInitOptions {
205
331
  *
206
332
  * Register a callback for top-level imports.
207
333
  *
334
+ * @param url The top-level module being imported
335
+ * @param fetchOptions the {@link RequestInit} fetch options to used, to also be used for dependencies as well
208
336
  */
209
- onimport?: (url: string, options: any, parentUrl: string) => void;
337
+ onimport?: (url: string, fetchOptions: RequestInit, parentUrl: string) => void;
210
338
  }
211
339
 
212
340
  interface ImportMap {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "es-module-shims",
3
- "version": "2.5.1",
3
+ "version": "2.6.1",
4
4
  "description": "Shims for the latest ES module features",
5
5
  "main": "dist/es-module-shims.js",
6
6
  "exports": {