@jsenv/core 27.0.0-alpha.6 → 27.0.0-alpha.62

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.
Files changed (150) hide show
  1. package/dist/event_source_client.js +549 -0
  2. package/dist/event_source_client.js.map +188 -0
  3. package/dist/html_supervisor_installer.js +1236 -0
  4. package/dist/html_supervisor_installer.js.map +337 -0
  5. package/dist/html_supervisor_setup.js +95 -0
  6. package/dist/html_supervisor_setup.js.map +57 -0
  7. package/dist/import_meta_hot.js +86 -0
  8. package/dist/import_meta_hot.js.map +42 -0
  9. package/main.js +8 -1
  10. package/package.json +36 -29
  11. package/readme.md +6 -14
  12. package/src/build/build.js +961 -559
  13. package/src/build/build_urls_generator.js +48 -23
  14. package/src/build/graph_utils.js +31 -0
  15. package/src/build/{inject_version_mappings.js → inject_global_version_mappings.js} +33 -15
  16. package/src/build/inject_service_worker_urls.js +79 -0
  17. package/src/build/resync_ressource_hints.js +68 -0
  18. package/src/build/start_build_server.js +200 -0
  19. package/src/dev/plugins/explorer/jsenv_plugin_explorer.js +2 -2
  20. package/src/dev/plugins/toolbar/jsenv_plugin_toolbar.js +3 -1
  21. package/src/dev/start_dev_server.js +150 -34
  22. package/src/execute/execute.js +33 -6
  23. package/src/execute/run.js +19 -56
  24. package/src/execute/runtimes/browsers/from_playwright.js +207 -147
  25. package/src/execute/runtimes/node/controllable_file.mjs +26 -10
  26. package/src/execute/runtimes/node/node_execution_performance.js +67 -0
  27. package/src/execute/runtimes/node/node_process.js +280 -39
  28. package/src/jsenv_root_directory_url.js +1 -0
  29. package/src/omega/{runtime_support/default_runtime_support.js → compat/default_runtime_compat.js} +3 -5
  30. package/src/omega/{runtime_support/features_compatibility.js → compat/features_compats.js} +66 -4
  31. package/src/omega/compat/runtime_compat.js +50 -0
  32. package/src/omega/errors.js +51 -58
  33. package/src/omega/fetched_content_compliance.js +24 -0
  34. package/src/omega/file_url_converter.js +8 -50
  35. package/src/omega/kitchen.js +475 -308
  36. package/src/omega/omega_server.js +2 -3
  37. package/src/omega/server/file_service.js +57 -26
  38. package/src/omega/server/user_agent.js +4 -2
  39. package/src/omega/url_graph/url_graph_load.js +22 -7
  40. package/src/omega/url_graph/url_graph_report.js +94 -51
  41. package/src/omega/url_graph/url_info_transformations.js +26 -9
  42. package/src/omega/url_graph.js +80 -16
  43. package/src/omega/web_workers.js +42 -0
  44. package/src/{dev/plugins/autoreload → plugins/autoreload/dev_sse}/client/autoreload_preference.js +0 -0
  45. package/src/{dev/plugins/autoreload → plugins/autoreload/dev_sse}/client/event_source_client.js +19 -12
  46. package/src/{dev/plugins/autoreload → plugins/autoreload/dev_sse}/client/reload.js +0 -0
  47. package/src/{dev/plugins/autoreload → plugins/autoreload/dev_sse}/client/url_helpers.js +0 -0
  48. package/src/plugins/autoreload/dev_sse/jsenv_plugin_dev_sse_client.js +46 -0
  49. package/src/plugins/autoreload/dev_sse/jsenv_plugin_dev_sse_server.js +204 -0
  50. package/src/plugins/autoreload/jsenv_plugin_autoreload.js +27 -0
  51. package/src/plugins/autoreload/jsenv_plugin_hmr.js +35 -0
  52. package/src/plugins/bundling/css/bundle_css.js +140 -0
  53. package/src/plugins/bundling/js_classic_workers/bundle_js_classic_workers.js +13 -0
  54. package/src/plugins/bundling/js_module/bundle_js_module.js +309 -0
  55. package/src/plugins/bundling/jsenv_plugin_bundling.js +54 -0
  56. package/src/plugins/cache_control/jsenv_plugin_cache_control.js +34 -0
  57. package/src/{omega/core_plugins → plugins}/commonjs_globals/jsenv_plugin_commonjs_globals.js +54 -41
  58. package/src/plugins/file_urls/jsenv_plugin_file_urls.js +66 -0
  59. package/src/{omega/core_plugins → plugins}/filesystem_magic/jsenv_plugin_filesystem_magic.js +8 -5
  60. package/src/{omega/core_plugins → plugins}/html_supervisor/client/error_in_document.js +0 -0
  61. package/src/{omega/core_plugins → plugins}/html_supervisor/client/error_in_notification.js +0 -0
  62. package/src/plugins/html_supervisor/client/html_supervisor_installer.js +242 -0
  63. package/src/plugins/html_supervisor/client/html_supervisor_setup.js +79 -0
  64. package/src/{omega/core_plugins → plugins}/html_supervisor/client/perf_browser.js +0 -0
  65. package/src/{omega/core_plugins → plugins}/html_supervisor/client/uneval_exception.js +0 -0
  66. package/src/{omega/core_plugins → plugins}/html_supervisor/jsenv_plugin_html_supervisor.js +83 -61
  67. package/src/plugins/http_urls/jsenv_plugin_http_urls.js +12 -0
  68. package/src/{dev/plugins/autoreload → plugins/import_meta_hot}/babel_plugin_metadata_import_meta_hot.js +4 -5
  69. package/src/{dev/plugins/autoreload → plugins/import_meta_hot}/client/import_meta_hot.js +3 -1
  70. package/src/{dev/plugins/autoreload → plugins/import_meta_hot}/html_hot_dependencies.js +7 -4
  71. package/src/plugins/import_meta_hot/jsenv_plugin_import_meta_hot.js +105 -0
  72. package/src/{omega/core_plugins → plugins}/import_meta_scenarios/jsenv_plugin_import_meta_scenarios.js +33 -8
  73. package/src/plugins/import_meta_url/client/import_meta_url_browser.js +52 -0
  74. package/src/plugins/import_meta_url/client/import_meta_url_commonjs.mjs +9 -0
  75. package/src/{omega/core_plugins → plugins}/importmap/jsenv_plugin_importmap.js +39 -33
  76. package/src/plugins/inject_globals/jsenv_plugin_inject_globals.js +67 -0
  77. package/src/{omega/core_plugins → plugins}/inline/client/inline_content.js +0 -0
  78. package/src/{omega/core_plugins → plugins}/inline/jsenv_plugin_data_urls.js +18 -14
  79. package/src/{omega/core_plugins/inline/jsenv_plugin_js_and_css_inside_html.js → plugins/inline/jsenv_plugin_html_inline_content.js} +65 -44
  80. package/src/plugins/inline/jsenv_plugin_inline.js +36 -0
  81. package/src/{omega/core_plugins → plugins}/inline/jsenv_plugin_inline_query_param.js +6 -6
  82. package/src/plugins/inline/jsenv_plugin_js_inline_content.js +297 -0
  83. package/src/plugins/leading_slash/jsenv_plugin_leading_slash.js +13 -0
  84. package/src/plugins/minification/css/minify_css.js +9 -0
  85. package/src/plugins/minification/html/minify_html.js +15 -0
  86. package/src/{build/plugins/minify_js/jsenv_plugin_minify_js.js → plugins/minification/js/minify_js.js} +6 -22
  87. package/src/plugins/minification/jsenv_plugin_minification.js +78 -0
  88. package/src/plugins/minification/json/minify_json.js +8 -0
  89. package/src/plugins/node_esm_resolution/jsenv_plugin_node_esm_resolution.js +146 -0
  90. package/src/{omega → plugins}/plugin_controller.js +42 -11
  91. package/src/plugins/plugins.js +92 -0
  92. package/src/plugins/transpilation/as_js_classic/client/s.js +874 -0
  93. package/src/plugins/transpilation/as_js_classic/client/s.js.md +1 -0
  94. package/src/plugins/transpilation/as_js_classic/helpers/babel_plugin_transform_import_meta_url.js +47 -0
  95. package/src/plugins/transpilation/as_js_classic/helpers/systemjs_old.js +43 -0
  96. package/src/plugins/transpilation/as_js_classic/jsenv_plugin_as_js_classic.js +199 -0
  97. package/src/plugins/transpilation/as_js_classic/jsenv_plugin_script_type_module_as_classic.js +270 -0
  98. package/src/plugins/transpilation/as_js_classic/jsenv_plugin_workers_type_module_as_classic.js +55 -0
  99. package/src/{omega/core_plugins → plugins/transpilation}/babel/global_this/babel_plugin_global_this_as_jsenv_import.js +0 -0
  100. package/src/{omega/core_plugins → plugins/transpilation}/babel/global_this/client/global_this.js +0 -0
  101. package/src/{omega/core_plugins → plugins/transpilation}/babel/helpers/babel_plugin_babel_helpers_as_jsenv_imports.js +0 -0
  102. package/src/{omega/core_plugins → plugins/transpilation}/babel/helpers/babel_plugin_structure.js +12 -19
  103. package/src/{omega/core_plugins → plugins/transpilation}/babel/helpers/babel_plugins_compatibility.js +0 -0
  104. package/src/{omega/core_plugins → plugins/transpilation}/babel/jsenv_plugin_babel.js +45 -27
  105. package/src/{omega/core_plugins → plugins/transpilation}/babel/new_stylesheet/babel_plugin_new_stylesheet_as_jsenv_import.js +30 -6
  106. package/src/{omega/core_plugins → plugins/transpilation}/babel/new_stylesheet/client/.eslintrc.cjs +0 -0
  107. package/src/{omega/core_plugins → plugins/transpilation}/babel/new_stylesheet/client/new_stylesheet.js +0 -0
  108. package/src/{omega/core_plugins → plugins/transpilation}/babel/regenerator_runtime/babel_plugin_regenerator_runtime_as_jsenv_import.js +0 -0
  109. package/src/{omega/core_plugins → plugins/transpilation}/babel/regenerator_runtime/client/regenerator_runtime.js +0 -0
  110. package/src/plugins/transpilation/css_parcel/jsenv_plugin_css_parcel.js +18 -0
  111. package/src/plugins/transpilation/fetch_original_url_info.js +30 -0
  112. package/src/plugins/transpilation/import_assertions/jsenv_plugin_import_assertions.js +181 -0
  113. package/src/plugins/transpilation/jsenv_plugin_top_level_await.js +80 -0
  114. package/src/plugins/transpilation/jsenv_plugin_transpilation.js +44 -0
  115. package/src/plugins/url_analysis/css/css_urls.js +49 -0
  116. package/src/plugins/url_analysis/html/html_urls.js +269 -0
  117. package/src/plugins/url_analysis/js/js_urls.js +67 -0
  118. package/src/plugins/url_analysis/jsenv_plugin_url_analysis.js +18 -0
  119. package/src/plugins/url_analysis/webmanifest/webmanifest_urls.js +17 -0
  120. package/src/{omega/core_plugins → plugins}/url_resolution/jsenv_plugin_url_resolution.js +12 -5
  121. package/src/plugins/url_version/jsenv_plugin_url_version.js +28 -0
  122. package/src/test/execute_plan.js +38 -19
  123. package/src/test/execute_test_plan.js +25 -8
  124. package/src/test/logs_file_execution.js +10 -12
  125. package/src/build/plugins/bundle_js_module/jsenv_plugin_bundle_js_module.js +0 -225
  126. package/src/build/plugins/minify_html/jsenv_plugin_minify_html.js +0 -30
  127. package/src/dev/plugins/autoreload/client/event_source_connection.js +0 -195
  128. package/src/dev/plugins/autoreload/jsenv_plugin_autoreload.js +0 -374
  129. package/src/dev/plugins/autoreload/sse_service.js +0 -149
  130. package/src/execute/runtimes/node/controlled_process.js +0 -316
  131. package/src/omega/core_plugins/file_urls/jsenv_plugin_file_urls.js +0 -67
  132. package/src/omega/core_plugins/html_supervisor/client/html_supervisor_installer.js +0 -168
  133. package/src/omega/core_plugins/html_supervisor/client/html_supervisor_setup.js +0 -77
  134. package/src/omega/core_plugins/import_assertions/helpers/babel_plugin_metadata_import_assertions.js +0 -98
  135. package/src/omega/core_plugins/import_assertions/helpers/json_module.js +0 -12
  136. package/src/omega/core_plugins/import_assertions/helpers/text_module.js +0 -6
  137. package/src/omega/core_plugins/import_assertions/jsenv_plugin_import_assertions.js +0 -211
  138. package/src/omega/core_plugins/inline/jsenv_plugin_inline.js +0 -13
  139. package/src/omega/core_plugins/inline/jsenv_plugin_new_inline_content.js +0 -210
  140. package/src/omega/core_plugins/leading_slash/jsenv_plugin_leading_slash.js +0 -12
  141. package/src/omega/core_plugins/node_esm_resolution/jsenv_plugin_node_esm_resolution.js +0 -77
  142. package/src/omega/core_plugins/url_version/jsenv_plugin_url_version.js +0 -50
  143. package/src/omega/core_plugins.js +0 -39
  144. package/src/omega/runtime_support/runtime_support.js +0 -20
  145. package/src/omega/url_graph/url_graph_sort.js +0 -29
  146. package/src/omega/url_mentions/css_url_mentions.js +0 -63
  147. package/src/omega/url_mentions/html_url_mentions.js +0 -185
  148. package/src/omega/url_mentions/js_module_url_mentions.js +0 -91
  149. package/src/omega/url_mentions/parse_url_mentions.js +0 -37
  150. package/src/omega/url_mentions/worker_classic_url_mentions.js +0 -37
@@ -0,0 +1,549 @@
1
+ import { urlHotMetas } from "./import_meta_hot.js";
2
+
3
+ /* eslint-env browser */
4
+ const createEventSourceConnection = (eventSourceUrl, events = {}, {
5
+ retryMaxAttempt = Infinity,
6
+ retryAllocatedMs = Infinity,
7
+ lastEventId
8
+ } = {}) => {
9
+ const {
10
+ EventSource
11
+ } = window;
12
+
13
+ if (typeof EventSource !== "function") {
14
+ return () => {};
15
+ }
16
+
17
+ const eventSourceOrigin = new URL(eventSourceUrl).origin;
18
+ Object.keys(events).forEach(eventName => {
19
+ const eventCallback = events[eventName];
20
+
21
+ events[eventName] = e => {
22
+ if (e.origin === eventSourceOrigin) {
23
+ if (e.lastEventId) {
24
+ lastEventId = e.lastEventId;
25
+ }
26
+
27
+ eventCallback(e);
28
+ }
29
+ };
30
+ });
31
+ const status = {
32
+ value: "default",
33
+ goTo: value => {
34
+ if (value === status.value) {
35
+ return;
36
+ }
37
+
38
+ status.value = value;
39
+ status.onchange();
40
+ },
41
+ onchange: () => {}
42
+ };
43
+
44
+ let _disconnect = () => {};
45
+
46
+ const attemptConnection = url => {
47
+ const eventSource = new EventSource(url, {
48
+ withCredentials: true
49
+ });
50
+
51
+ _disconnect = () => {
52
+ if (status.value !== "connecting" && status.value !== "connected") {
53
+ console.warn("disconnect() ignored because connection is ".concat(status.value));
54
+ return;
55
+ }
56
+
57
+ eventSource.onerror = undefined;
58
+ eventSource.close();
59
+ Object.keys(events).forEach(eventName => {
60
+ eventSource.removeEventListener(eventName, events[eventName]);
61
+ });
62
+ status.goTo("disconnected");
63
+ };
64
+
65
+ let retryCount = 0;
66
+ let firstRetryMs = Date.now();
67
+
68
+ eventSource.onerror = errorEvent => {
69
+ if (errorEvent.target.readyState === EventSource.CONNECTING) {
70
+ if (retryCount > retryMaxAttempt) {
71
+ console.info("could not connect after ".concat(retryMaxAttempt, " attempt"));
72
+
73
+ _disconnect();
74
+
75
+ return;
76
+ }
77
+
78
+ if (retryCount === 0) {
79
+ firstRetryMs = Date.now();
80
+ } else {
81
+ const allRetryDuration = Date.now() - firstRetryMs;
82
+
83
+ if (retryAllocatedMs && allRetryDuration > retryAllocatedMs) {
84
+ console.info("could not connect in less than ".concat(retryAllocatedMs, " ms"));
85
+
86
+ _disconnect();
87
+
88
+ return;
89
+ }
90
+ }
91
+
92
+ retryCount++;
93
+ status.goTo("connecting");
94
+ return;
95
+ }
96
+
97
+ if (errorEvent.target.readyState === EventSource.CLOSED) {
98
+ _disconnect();
99
+
100
+ return;
101
+ }
102
+ };
103
+
104
+ eventSource.onopen = () => {
105
+ status.goTo("connected");
106
+ };
107
+
108
+ Object.keys(events).forEach(eventName => {
109
+ eventSource.addEventListener(eventName, events[eventName]);
110
+ });
111
+
112
+ if (!events.hasOwnProperty("welcome")) {
113
+ eventSource.addEventListener("welcome", e => {
114
+ if (e.origin === eventSourceOrigin && e.lastEventId) {
115
+ lastEventId = e.lastEventId;
116
+ }
117
+ });
118
+ }
119
+
120
+ status.goTo("connecting");
121
+ };
122
+
123
+ let connect = () => {
124
+ attemptConnection(eventSourceUrl);
125
+
126
+ connect = () => {
127
+ attemptConnection(lastEventId ? addLastEventIdIntoUrlSearchParams(eventSourceUrl, lastEventId) : eventSourceUrl);
128
+ };
129
+ };
130
+
131
+ const removePageUnloadListener = listenPageUnload(() => {
132
+ if (status.value === "connecting" || status.value === "connected") {
133
+ _disconnect();
134
+ }
135
+ });
136
+
137
+ const destroy = () => {
138
+ removePageUnloadListener();
139
+
140
+ _disconnect();
141
+ };
142
+
143
+ return {
144
+ status,
145
+ connect,
146
+ disconnect: () => _disconnect(),
147
+ destroy
148
+ };
149
+ };
150
+
151
+ const addLastEventIdIntoUrlSearchParams = (url, lastEventId) => {
152
+ if (url.indexOf("?") === -1) {
153
+ url += "?";
154
+ } else {
155
+ url += "&";
156
+ }
157
+
158
+ return "".concat(url, "last-event-id=").concat(encodeURIComponent(lastEventId));
159
+ }; // const listenPageMightFreeze = (callback) => {
160
+ // const removePageHideListener = listenEvent(window, "pagehide", (pageHideEvent) => {
161
+ // if (pageHideEvent.persisted === true) {
162
+ // callback(pageHideEvent)
163
+ // }
164
+ // })
165
+ // return removePageHideListener
166
+ // }
167
+ // const listenPageFreeze = (callback) => {
168
+ // const removeFreezeListener = listenEvent(document, "freeze", (freezeEvent) => {
169
+ // callback(freezeEvent)
170
+ // })
171
+ // return removeFreezeListener
172
+ // }
173
+ // const listenPageIsRestored = (callback) => {
174
+ // const removeResumeListener = listenEvent(document, "resume", (resumeEvent) => {
175
+ // removePageshowListener()
176
+ // callback(resumeEvent)
177
+ // })
178
+ // const removePageshowListener = listenEvent(window, "pageshow", (pageshowEvent) => {
179
+ // if (pageshowEvent.persisted === true) {
180
+ // removePageshowListener()
181
+ // removeResumeListener()
182
+ // callback(pageshowEvent)
183
+ // }
184
+ // })
185
+ // return () => {
186
+ // removeResumeListener()
187
+ // removePageshowListener()
188
+ // }
189
+ // }
190
+
191
+
192
+ const listenPageUnload = callback => {
193
+ const removePageHideListener = listenEvent(window, "pagehide", pageHideEvent => {
194
+ if (pageHideEvent.persisted !== true) {
195
+ callback(pageHideEvent);
196
+ }
197
+ });
198
+ return removePageHideListener;
199
+ };
200
+
201
+ const listenEvent = (emitter, event, callback) => {
202
+ emitter.addEventListener(event, callback);
203
+ return () => {
204
+ emitter.removeEventListener(event, callback);
205
+ };
206
+ };
207
+
208
+ const isAutoreloadEnabled = () => {
209
+ const value = window.localStorage.getItem("autoreload");
210
+
211
+ if (value === "0") {
212
+ return false;
213
+ }
214
+
215
+ return true;
216
+ };
217
+ const setAutoreloadPreference = value => {
218
+ window.localStorage.setItem("autoreload", value ? "1" : "0");
219
+ };
220
+
221
+ const compareTwoUrlPaths = (url, otherUrl) => {
222
+ if (url === otherUrl) {
223
+ return true;
224
+ }
225
+
226
+ const urlObject = new URL(url);
227
+ const otherUrlObject = new URL(otherUrl);
228
+ return urlObject.origin === otherUrlObject.origin && urlObject.pathname === otherUrlObject.pathname;
229
+ };
230
+ const injectQuery = (url, query) => {
231
+ const urlObject = new URL(url);
232
+ const {
233
+ searchParams
234
+ } = urlObject;
235
+ Object.keys(query).forEach(key => {
236
+ searchParams.set(key, query[key]);
237
+ });
238
+ return String(urlObject);
239
+ };
240
+
241
+ const htmlAttributeSrcSet = {
242
+ parse: srcset => {
243
+ const srcCandidates = [];
244
+ srcset.split(",").forEach(set => {
245
+ const [specifier, descriptor] = set.trim().split(" ");
246
+ srcCandidates.push({
247
+ specifier,
248
+ descriptor
249
+ });
250
+ });
251
+ return srcCandidates;
252
+ },
253
+ stringify: srcCandidates => {
254
+ const srcset = srcCandidates.map(({
255
+ specifier,
256
+ descriptor
257
+ }) => "".concat(specifier, " ").concat(descriptor)).join(", ");
258
+ return srcset;
259
+ }
260
+ };
261
+
262
+ const reloadHtmlPage = () => {
263
+ window.location.reload(true);
264
+ }; // This function can consider everything as hot reloadable:
265
+ // - no need to check [hot-accept]and [hot-decline] attributes for instance
266
+ // This is because if something should full reload, we receive "full_reload"
267
+ // from server and this function is not called
268
+
269
+ const reloadDOMNodesUsingUrl = urlToReload => {
270
+ const mutations = [];
271
+
272
+ const shouldReloadUrl = urlCandidate => {
273
+ return compareTwoUrlPaths(urlCandidate, urlToReload);
274
+ };
275
+
276
+ const visitNodeAttributeAsUrl = (node, attributeName) => {
277
+ let attribute = node[attributeName];
278
+
279
+ if (!attribute) {
280
+ return;
281
+ }
282
+
283
+ if (SVGAnimatedString && attribute instanceof SVGAnimatedString) {
284
+ attribute = attribute.animVal;
285
+ }
286
+
287
+ if (!shouldReloadUrl(attribute)) {
288
+ return;
289
+ }
290
+
291
+ mutations.push(() => {
292
+ node[attributeName] = injectQuery(attribute, {
293
+ hmr: Date.now()
294
+ });
295
+ });
296
+ };
297
+
298
+ Array.from(document.querySelectorAll("link[rel=\"stylesheet\"]")).forEach(link => {
299
+ visitNodeAttributeAsUrl(link, "href");
300
+ });
301
+ Array.from(document.querySelectorAll("link[rel=\"icon\"]")).forEach(link => {
302
+ visitNodeAttributeAsUrl(link, "href");
303
+ });
304
+ Array.from(document.querySelector("script")).forEach(script => {
305
+ visitNodeAttributeAsUrl(script, "src");
306
+ }); // There is no real need to update a.href because the ressource will be fetched when clicked.
307
+ // But in a scenario where the ressource was already visited and is in browser cache, adding
308
+ // the dynamic query param ensure the cache is invalidated
309
+
310
+ Array.from(document.querySelectorAll("a")).forEach(a => {
311
+ visitNodeAttributeAsUrl(a, "href");
312
+ }); // About iframes:
313
+ // - By default iframe itself and everything inside trigger a parent page full-reload
314
+ // - Adding [hot-accept] on the iframe means parent page won't reload when iframe full/hot reload
315
+ // In that case and if there is code in the iframe and parent doing post message communication:
316
+ // you must put import.meta.hot.decline() for code involved in communication.
317
+ // (both in parent and iframe)
318
+
319
+ Array.from(document.querySelectorAll("img")).forEach(img => {
320
+ visitNodeAttributeAsUrl(img, "src");
321
+ const srcset = img.srcset;
322
+
323
+ if (srcset) {
324
+ const srcCandidates = htmlAttributeSrcSet.parse(srcset);
325
+ srcCandidates.forEach(srcCandidate => {
326
+ const url = new URL(srcCandidate.specifier, "".concat(window.location.href));
327
+
328
+ if (shouldReloadUrl(url)) {
329
+ srcCandidate.specifier = injectQuery(url, {
330
+ hmr: Date.now()
331
+ });
332
+ }
333
+ });
334
+ mutations.push(() => {
335
+ img.srcset = htmlAttributeSrcSet.stringify(srcCandidates);
336
+ });
337
+ }
338
+ });
339
+ Array.from(document.querySelectorAll("source")).forEach(source => {
340
+ visitNodeAttributeAsUrl(source, "src");
341
+ }); // svg image tag
342
+
343
+ Array.from(document.querySelectorAll("image")).forEach(image => {
344
+ visitNodeAttributeAsUrl(image, "href");
345
+ }); // svg use
346
+
347
+ Array.from(document.querySelectorAll("use")).forEach(use => {
348
+ visitNodeAttributeAsUrl(use, "href");
349
+ });
350
+ mutations.forEach(mutation => {
351
+ mutation();
352
+ });
353
+ };
354
+ const reloadJsImport = async url => {
355
+ const urlWithHmr = injectQuery(url, {
356
+ hmr: Date.now()
357
+ });
358
+ const namespace = await import(urlWithHmr);
359
+ return namespace;
360
+ };
361
+
362
+ const reloadMessages = [];
363
+ const reloadMessagesSignal = {
364
+ onchange: () => {}
365
+ };
366
+ let pendingCallbacks = [];
367
+ let running = false;
368
+
369
+ const addToHotQueue = async callback => {
370
+ pendingCallbacks.push(callback);
371
+ dequeue();
372
+ };
373
+
374
+ const dequeue = async () => {
375
+ if (running) {
376
+ return;
377
+ }
378
+
379
+ const callbacks = pendingCallbacks.slice();
380
+ pendingCallbacks = [];
381
+ running = true;
382
+
383
+ try {
384
+ await callbacks.reduce(async (previous, callback) => {
385
+ await previous;
386
+ await callback();
387
+ }, Promise.resolve());
388
+ } finally {
389
+ running = false;
390
+
391
+ if (pendingCallbacks.length) {
392
+ dequeue();
393
+ }
394
+ }
395
+ };
396
+
397
+ const applyReloadMessageEffects = async () => {
398
+ const someEffectIsFullReload = reloadMessages.some(reloadMessage => reloadMessage.type === "full");
399
+
400
+ if (someEffectIsFullReload) {
401
+ reloadHtmlPage();
402
+ return;
403
+ }
404
+
405
+ const onApplied = reloadMessage => {
406
+ const index = reloadMessages.indexOf(reloadMessage);
407
+ reloadMessages.splice(index, 1);
408
+ reloadMessagesSignal.onchange();
409
+ };
410
+
411
+ const setReloadMessagePromise = (reloadMessage, promise) => {
412
+ reloadMessage.status = "pending";
413
+ promise.then(() => {
414
+ onApplied(reloadMessage);
415
+ }, e => {
416
+ // TODO: reuse error display from html supervisor
417
+ console.error(e);
418
+ console.error("[hmr] Failed to reload after ".concat(reloadMessage.reason, ".\nThis could be due to syntax errors or importing non-existent modules (see errors above)"));
419
+ reloadMessage.status = "failed";
420
+ reloadMessagesSignal.onchange();
421
+ });
422
+ };
423
+
424
+ reloadMessages.forEach(reloadMessage => {
425
+ if (reloadMessage.type === "hot") {
426
+ const promise = addToHotQueue(() => {
427
+ return applyHotReload(reloadMessage);
428
+ });
429
+ setReloadMessagePromise(reloadMessage, promise);
430
+ } else {
431
+ setReloadMessagePromise(reloadMessage, Promise.resolve());
432
+ }
433
+ });
434
+ reloadMessagesSignal.onchange(); // reload status is "pending"
435
+ };
436
+
437
+ const applyHotReload = async ({
438
+ hotInstructions
439
+ }) => {
440
+ await hotInstructions.reduce(async (previous, {
441
+ type,
442
+ boundary,
443
+ acceptedBy
444
+ }) => {
445
+ await previous;
446
+ const urlToFetch = new URL(boundary, "".concat(window.location.origin, "/")).href;
447
+ const urlHotMeta = urlHotMetas[urlToFetch]; // TODO: we should return when there is no url hot meta because
448
+ // it means code was not executed (code splitting with dynamic import)
449
+ // if (!urlHotMeta) {return }
450
+
451
+ if (type === "prune") {
452
+ console.groupCollapsed("[jsenv] prune: ".concat(boundary, " (inside ").concat(acceptedBy, ")"));
453
+ } else if (acceptedBy === boundary) {
454
+ console.groupCollapsed("[jsenv] hot reloading: ".concat(boundary));
455
+ } else {
456
+ console.groupCollapsed("[jsenv] hot reloading: ".concat(acceptedBy, " inside ").concat(boundary));
457
+ }
458
+
459
+ if (urlHotMeta && urlHotMeta.disposeCallback) {
460
+ console.log("call dispose callback");
461
+ await urlHotMeta.disposeCallback();
462
+ }
463
+
464
+ if (type === "prune") {
465
+ delete urlHotMetas[urlToFetch];
466
+ console.log("cleanup pruned url");
467
+ console.groupEnd();
468
+ return null;
469
+ }
470
+
471
+ if (type === "js_module") {
472
+ console.log("importing js module");
473
+ const namespace = await reloadJsImport(urlToFetch);
474
+
475
+ if (urlHotMeta && urlHotMeta.acceptCallback) {
476
+ await urlHotMeta.acceptCallback(namespace);
477
+ }
478
+
479
+ console.log("js module import done");
480
+ console.groupEnd();
481
+ return namespace;
482
+ }
483
+
484
+ if (type === "html") {
485
+ if (!compareTwoUrlPaths(urlToFetch, window.location.href)) {
486
+ // we are not in that HTML page
487
+ return null;
488
+ }
489
+
490
+ console.log("reloading url");
491
+ const urlToReload = new URL(acceptedBy, "".concat(window.location.origin, "/")).href;
492
+ reloadDOMNodesUsingUrl(urlToReload);
493
+ console.log("url reloaded");
494
+ console.groupEnd();
495
+ return null;
496
+ }
497
+
498
+ console.warn("unknown update type: \"".concat(type, "\""));
499
+ return null;
500
+ }, Promise.resolve());
501
+ };
502
+
503
+ const addReloadMessage = reloadMessage => {
504
+ reloadMessages.push(reloadMessage);
505
+
506
+ if (isAutoreloadEnabled()) {
507
+ applyReloadMessageEffects();
508
+ } else {
509
+ reloadMessagesSignal.onchange();
510
+ }
511
+ };
512
+
513
+ const eventsourceConnection = createEventSourceConnection(document.location.href, {
514
+ reload: ({
515
+ data
516
+ }) => {
517
+ const reloadMessage = JSON.parse(data);
518
+ addReloadMessage(reloadMessage);
519
+ }
520
+ }, {
521
+ retryMaxAttempt: Infinity,
522
+ retryAllocatedMs: 20 * 1000
523
+ });
524
+ const {
525
+ status,
526
+ connect,
527
+ disconnect
528
+ } = eventsourceConnection;
529
+ connect();
530
+ window.__jsenv_event_source_client__ = {
531
+ status,
532
+ connect,
533
+ disconnect,
534
+ isAutoreloadEnabled,
535
+ setAutoreloadPreference,
536
+ urlHotMetas,
537
+ reloadMessages,
538
+ reloadMessagesSignal,
539
+ applyReloadMessageEffects,
540
+ addReloadMessage
541
+ }; // const findHotMetaUrl = (originalFileRelativeUrl) => {
542
+ // return Object.keys(urlHotMetas).find((compileUrl) => {
543
+ // return (
544
+ // parseCompiledUrl(compileUrl).fileRelativeUrl === originalFileRelativeUrl
545
+ // )
546
+ // })
547
+ // }
548
+
549
+ //# sourceMappingURL=event_source_client.js.map