index.d.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. interface ESMSInitOptions {
  2. /**
  3. * Enable Shim Mode
  4. */
  5. shimMode?: boolean;
  6. /**
  7. * Enable polyfill features.
  8. * Currently supports ['css-modules', 'json-modules']
  9. */
  10. polyfillEnable?: string[];
  11. /**
  12. * Nonce for CSP build
  13. */
  14. nonce?: boolean;
  15. /**
  16. * Disable retriggering of document readystate
  17. */
  18. noLoadEventRetriggers: true,
  19. /**
  20. * #### Skip Processing Stability
  21. *
  22. * > Non-spec feature
  23. *
  24. * When loading modules that you know will only use baseline modules
  25. * features, it is possible to set a rule to explicitly opt-out modules
  26. * from rewriting. This improves performance because those modules then do
  27. * not need to be processed or transformed at all, so that only local
  28. * application code is handled and not library code.
  29. *
  30. * This can be configured by setting the importShim.skip URL regular
  31. * expression:
  32. *
  33. * ```js
  34. * importShim.skip = /^https:\/\/cdn\.com/;
  35. * ```
  36. *
  37. * By default, this expression supports jspm.dev, dev.jspm.io and
  38. * cdn.pika.dev.
  39. */
  40. skip: RegExp;
  41. /**
  42. * #### Error hook
  43. *
  44. * Register a callback for any ES Module Shims module errors.
  45. *
  46. */
  47. onerror: (e: any) => any;
  48. /**
  49. * #### Resolve Hook
  50. *
  51. * Only supported in Shim Mode.
  52. *
  53. * Provide a custom resolver function.
  54. */
  55. resolve: (id: string, parentUrl: string, resolve: (id: string, parentUrl: string) => string) => string | Promise<string>;
  56. /**
  57. * #### Fetch Hook
  58. *
  59. * Only supported in Shim Mode.
  60. *
  61. * > Stability: Non-spec feature
  62. *
  63. * This is provided as a convenience feature since the pipeline handles
  64. * the same data URL rewriting and circular handling of the module graph
  65. * that applies when trying to implement any module transform system.
  66. *
  67. * The ES Module Shims fetch hook can be used to implement transform
  68. * plugins.
  69. *
  70. * For example:
  71. *
  72. * ```js
  73. * importShim.fetch = async function (url) {
  74. * const response = await fetch(url);
  75. * if (response.url.endsWith('.ts')) {
  76. * const source = await response.body();
  77. * const transformed = tsCompile(source);
  78. * return new Response(new Blob([transformed], { type: 'application/javascript' }));
  79. * }
  80. * return response;
  81. * };
  82. * ```
  83. *
  84. * Because the dependency analysis applies by ES Module Shims takes care
  85. * of ensuring all dependencies run through the same fetch hook, the above
  86. * is all that is needed to implement custom plugins.
  87. *
  88. * Streaming support is also provided, for example here is a hook with
  89. * streaming support for JSON:
  90. *
  91. * ```js
  92. * importShim.fetch = async function (url) {
  93. * const response = await fetch(url);
  94. * if (!response.ok)
  95. * throw new Error(`${response.status} ${response.statusText} ${response.url}`);
  96. * const contentType = response.headers.get('content-type');
  97. * if (!/^application\/json($|;)/.test(contentType))
  98. * return response;
  99. * const reader = response.body.getReader();
  100. * return new Response(new ReadableStream({
  101. * async start (controller) {
  102. * let done, value;
  103. * controller.enqueue(new Uint8Array([...'export default '].map(c => c.charCodeAt(0))));
  104. * while (({ done, value } = await reader.read()) && !done) {
  105. * controller.enqueue(value);
  106. * }
  107. * controller.close();
  108. * }
  109. * }), {
  110. * status: 200,
  111. * headers: {
  112. * "Content-Type": "application/javascript"
  113. * }
  114. * });
  115. * }
  116. * ```
  117. */
  118. fetch: (input: RequestInfo, init?: RequestInit) => Promise<Response>;
  119. /**
  120. * #### Revoke Blob URLs
  121. *
  122. * Set to *true* to cleanup blob URLs from memory after execution.
  123. * Can cost some compute time for large loads.
  124. *
  125. */
  126. revokeBlobURLs: boolean;
  127. }
  128. /**
  129. * Dynamic import(...) within any modules loaded will be rewritten as
  130. * importShim(...) automatically providing full support for all es-module-shims
  131. * features through dynamic import.
  132. *
  133. * To load code dynamically (say from the browser console), importShim can be
  134. * called similarly:
  135. *
  136. * ```js
  137. * importShim('/path/to/module.js').then(x => console.log(x));
  138. * ```
  139. */
  140. declare function importShim<Default, Exports extends object>(
  141. specifier: string,
  142. parentUrl?: string
  143. ): Promise<{ default: Default } & Exports>;
  144. interface Window {
  145. esmsInitOptions?: ESMSInitOptions;
  146. importShim: typeof importShim;
  147. }