@jsenv/core 27.0.0-alpha.4 → 27.0.0-alpha.42

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 (141) 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 +1145 -0
  4. package/dist/html_supervisor_installer.js.map +322 -0
  5. package/dist/html_supervisor_setup.js +92 -0
  6. package/dist/html_supervisor_setup.js.map +57 -0
  7. package/main.js +8 -1
  8. package/package.json +24 -24
  9. package/readme.md +4 -12
  10. package/src/build/build.js +731 -433
  11. package/src/build/build_urls_generator.js +55 -21
  12. package/src/build/graph_utils.js +31 -0
  13. package/src/build/{inject_version_mappings.js → inject_global_version_mappings.js} +33 -15
  14. package/src/build/inject_service_worker_urls.js +79 -0
  15. package/src/build/resync_ressource_hints.js +68 -0
  16. package/src/build/start_build_server.js +205 -0
  17. package/src/dev/plugins/explorer/jsenv_plugin_explorer.js +2 -2
  18. package/src/dev/plugins/toolbar/jsenv_plugin_toolbar.js +3 -1
  19. package/src/dev/start_dev_server.js +58 -26
  20. package/src/execute/execute.js +30 -4
  21. package/src/execute/run.js +19 -56
  22. package/src/execute/runtimes/browsers/from_playwright.js +201 -147
  23. package/src/execute/runtimes/node/controllable_file.mjs +26 -10
  24. package/src/execute/runtimes/node/node_execution_performance.js +67 -0
  25. package/src/execute/runtimes/node/node_process.js +280 -39
  26. package/src/jsenv_root_directory_url.js +1 -0
  27. package/src/omega/{runtime_support/default_runtime_support.js → compat/default_runtime_compat.js} +3 -5
  28. package/src/omega/{runtime_support/features_compatibility.js → compat/features_compats.js} +66 -4
  29. package/src/omega/compat/runtime_compat.js +50 -0
  30. package/src/omega/errors.js +51 -58
  31. package/src/omega/fetched_content_compliance.js +24 -0
  32. package/src/omega/file_url_converter.js +8 -50
  33. package/src/omega/kitchen.js +414 -285
  34. package/src/omega/server/file_service.js +21 -22
  35. package/src/omega/url_graph/url_graph_load.js +14 -7
  36. package/src/omega/url_graph/url_graph_report.js +17 -15
  37. package/src/omega/url_graph/url_info_transformations.js +17 -5
  38. package/src/omega/url_graph.js +33 -10
  39. package/src/omega/web_workers.js +42 -0
  40. package/src/{dev/plugins/autoreload → plugins/autoreload/dev_sse}/client/autoreload_preference.js +0 -0
  41. package/src/{dev/plugins/autoreload → plugins/autoreload/dev_sse}/client/event_source_client.js +2 -2
  42. package/src/{dev/plugins/autoreload → plugins/autoreload/dev_sse}/client/reload.js +0 -0
  43. package/src/{dev/plugins/autoreload → plugins/autoreload/dev_sse}/client/url_helpers.js +0 -0
  44. package/src/plugins/autoreload/dev_sse/jsenv_plugin_dev_sse_client.js +46 -0
  45. package/src/{dev/plugins/autoreload/jsenv_plugin_autoreload.js → plugins/autoreload/dev_sse/jsenv_plugin_dev_sse_server.js} +31 -172
  46. package/src/plugins/autoreload/jsenv_plugin_autoreload.js +27 -0
  47. package/src/plugins/autoreload/jsenv_plugin_hmr.js +35 -0
  48. package/src/plugins/bundling/css/bundle_css.js +21 -0
  49. package/src/plugins/bundling/js_classic_workers/bundle_js_classic_workers.js +13 -0
  50. package/src/{build/plugins/bundle_js_module/jsenv_plugin_bundle_js_module.js → plugins/bundling/js_module/bundle_js_module.js} +120 -79
  51. package/src/plugins/bundling/jsenv_plugin_bundling.js +51 -0
  52. package/src/{omega/core_plugins → plugins}/commonjs_globals/jsenv_plugin_commonjs_globals.js +54 -41
  53. package/src/plugins/file_urls/jsenv_plugin_file_urls.js +66 -0
  54. package/src/{omega/core_plugins → plugins}/filesystem_magic/jsenv_plugin_filesystem_magic.js +8 -5
  55. package/src/{omega/core_plugins → plugins}/html_supervisor/client/error_in_document.js +0 -0
  56. package/src/{omega/core_plugins → plugins}/html_supervisor/client/error_in_notification.js +0 -0
  57. package/src/{omega/core_plugins → plugins}/html_supervisor/client/html_supervisor_installer.js +3 -2
  58. package/src/{omega/core_plugins → plugins}/html_supervisor/client/html_supervisor_setup.js +0 -0
  59. package/src/{omega/core_plugins → plugins}/html_supervisor/client/perf_browser.js +0 -0
  60. package/src/{omega/core_plugins → plugins}/html_supervisor/client/uneval_exception.js +0 -0
  61. package/src/{omega/core_plugins → plugins}/html_supervisor/jsenv_plugin_html_supervisor.js +52 -55
  62. package/src/plugins/http_urls/jsenv_plugin_http_urls.js +12 -0
  63. package/src/{dev/plugins/autoreload → plugins/import_meta_hot}/babel_plugin_metadata_import_meta_hot.js +4 -5
  64. package/src/{dev/plugins/autoreload → plugins/import_meta_hot}/client/import_meta_hot.js +3 -1
  65. package/src/{dev/plugins/autoreload → plugins/import_meta_hot}/html_hot_dependencies.js +2 -2
  66. package/src/plugins/import_meta_hot/jsenv_plugin_import_meta_hot.js +101 -0
  67. package/src/{omega/core_plugins → plugins}/import_meta_scenarios/jsenv_plugin_import_meta_scenarios.js +33 -8
  68. package/src/plugins/import_meta_url/client/import_meta_url_browser.js +52 -0
  69. package/src/plugins/import_meta_url/client/import_meta_url_commonjs.mjs +9 -0
  70. package/src/{omega/core_plugins → plugins}/importmap/jsenv_plugin_importmap.js +37 -31
  71. package/src/plugins/inject_globals/jsenv_plugin_inject_globals.js +67 -0
  72. package/src/{omega/core_plugins → plugins}/inline/client/inline_content.js +0 -0
  73. package/src/{omega/core_plugins → plugins}/inline/jsenv_plugin_data_urls.js +18 -14
  74. package/src/{omega/core_plugins/inline/jsenv_plugin_js_and_css_inside_html.js → plugins/inline/jsenv_plugin_html_inline_content.js} +61 -40
  75. package/src/plugins/inline/jsenv_plugin_inline.js +36 -0
  76. package/src/{omega/core_plugins → plugins}/inline/jsenv_plugin_inline_query_param.js +6 -6
  77. package/src/plugins/inline/jsenv_plugin_js_inline_content.js +296 -0
  78. package/src/plugins/leading_slash/jsenv_plugin_leading_slash.js +13 -0
  79. package/src/plugins/minification/css/minify_css.js +9 -0
  80. package/src/plugins/minification/html/minify_html.js +15 -0
  81. package/src/{build/plugins/minify_js/jsenv_plugin_minify_js.js → plugins/minification/js/minify_js.js} +6 -22
  82. package/src/plugins/minification/jsenv_plugin_minification.js +78 -0
  83. package/src/plugins/minification/json/minify_json.js +8 -0
  84. package/src/plugins/node_esm_resolution/jsenv_plugin_node_esm_resolution.js +146 -0
  85. package/src/{omega → plugins}/plugin_controller.js +39 -11
  86. package/src/plugins/plugins.js +89 -0
  87. package/src/plugins/transpilation/as_js_classic/client/s.js +808 -0
  88. package/src/plugins/transpilation/as_js_classic/client/s.js.md +1 -0
  89. package/src/plugins/transpilation/as_js_classic/helpers/babel_plugin_transform_import_meta_url.js +47 -0
  90. package/src/plugins/transpilation/as_js_classic/helpers/systemjs_old.js +43 -0
  91. package/src/plugins/transpilation/as_js_classic/jsenv_plugin_as_js_classic.js +183 -0
  92. package/src/plugins/transpilation/as_js_classic/jsenv_plugin_script_type_module_as_classic.js +256 -0
  93. package/src/plugins/transpilation/as_js_classic/jsenv_plugin_workers_type_module_as_classic.js +93 -0
  94. package/src/{omega/core_plugins → plugins/transpilation}/babel/global_this/babel_plugin_global_this_as_jsenv_import.js +0 -0
  95. package/src/{omega/core_plugins → plugins/transpilation}/babel/global_this/client/global_this.js +0 -0
  96. package/src/{omega/core_plugins → plugins/transpilation}/babel/helpers/babel_plugin_babel_helpers_as_jsenv_imports.js +0 -0
  97. package/src/{omega/core_plugins → plugins/transpilation}/babel/helpers/babel_plugin_structure.js +4 -22
  98. package/src/{omega/core_plugins → plugins/transpilation}/babel/helpers/babel_plugins_compatibility.js +0 -0
  99. package/src/{omega/core_plugins → plugins/transpilation}/babel/jsenv_plugin_babel.js +37 -21
  100. package/src/{omega/core_plugins → plugins/transpilation}/babel/new_stylesheet/babel_plugin_new_stylesheet_as_jsenv_import.js +30 -6
  101. package/src/{omega/core_plugins → plugins/transpilation}/babel/new_stylesheet/client/.eslintrc.cjs +0 -0
  102. package/src/{omega/core_plugins → plugins/transpilation}/babel/new_stylesheet/client/new_stylesheet.js +0 -0
  103. package/src/{omega/core_plugins → plugins/transpilation}/babel/regenerator_runtime/babel_plugin_regenerator_runtime_as_jsenv_import.js +0 -0
  104. package/src/{omega/core_plugins → plugins/transpilation}/babel/regenerator_runtime/client/regenerator_runtime.js +0 -0
  105. package/src/plugins/transpilation/css_parcel/jsenv_plugin_css_parcel.js +18 -0
  106. package/src/plugins/transpilation/fetch_original_url_info.js +30 -0
  107. package/src/plugins/transpilation/import_assertions/jsenv_plugin_import_assertions.js +200 -0
  108. package/src/plugins/transpilation/jsenv_plugin_top_level_await.js +70 -0
  109. package/src/plugins/transpilation/jsenv_plugin_transpilation.js +44 -0
  110. package/src/plugins/url_references/css/css_urls.js +49 -0
  111. package/src/plugins/url_references/html/html_urls.js +273 -0
  112. package/src/plugins/url_references/js/js_urls.js +43 -0
  113. package/src/plugins/url_references/jsenv_plugin_imports_analysis.js +46 -0
  114. package/src/plugins/url_references/jsenv_plugin_url_analysis.js +18 -0
  115. package/src/plugins/url_references/jsenv_plugin_url_references.js +6 -0
  116. package/src/plugins/url_references/webmanifest/webmanifest_urls.js +17 -0
  117. package/src/{omega/core_plugins → plugins}/url_resolution/jsenv_plugin_url_resolution.js +12 -5
  118. package/src/{omega/core_plugins → plugins}/url_version/jsenv_plugin_url_version.js +12 -15
  119. package/src/test/execute_plan.js +28 -11
  120. package/src/test/execute_test_plan.js +23 -8
  121. package/src/test/logs_file_execution.js +8 -7
  122. package/src/build/plugins/minify_html/jsenv_plugin_minify_html.js +0 -30
  123. package/src/dev/plugins/autoreload/client/event_source_connection.js +0 -195
  124. package/src/dev/plugins/autoreload/sse_service.js +0 -149
  125. package/src/execute/runtimes/node/controlled_process.js +0 -316
  126. package/src/omega/core_plugins/file_urls/jsenv_plugin_file_urls.js +0 -67
  127. package/src/omega/core_plugins/import_assertions/helpers/babel_plugin_metadata_import_assertions.js +0 -98
  128. package/src/omega/core_plugins/import_assertions/helpers/json_module.js +0 -12
  129. package/src/omega/core_plugins/import_assertions/helpers/text_module.js +0 -6
  130. package/src/omega/core_plugins/import_assertions/jsenv_plugin_import_assertions.js +0 -211
  131. package/src/omega/core_plugins/inline/jsenv_plugin_inline.js +0 -13
  132. package/src/omega/core_plugins/inline/jsenv_plugin_new_inline_content.js +0 -210
  133. package/src/omega/core_plugins/leading_slash/jsenv_plugin_leading_slash.js +0 -12
  134. package/src/omega/core_plugins/node_esm_resolution/jsenv_plugin_node_esm_resolution.js +0 -77
  135. package/src/omega/core_plugins.js +0 -39
  136. package/src/omega/runtime_support/runtime_support.js +0 -20
  137. package/src/omega/url_mentions/css_url_mentions.js +0 -63
  138. package/src/omega/url_mentions/html_url_mentions.js +0 -185
  139. package/src/omega/url_mentions/js_module_url_mentions.js +0 -91
  140. package/src/omega/url_mentions/parse_url_mentions.js +0 -37
  141. package/src/omega/url_mentions/worker_classic_url_mentions.js +0 -37
@@ -4,24 +4,33 @@ import { memoizeByUrl } from "@jsenv/utils/memoize/memoize_by_url.js"
4
4
 
5
5
  export const createBuilUrlsGenerator = ({ buildDirectoryUrl }) => {
6
6
  const cache = {}
7
- const generate = memoizeByUrl((url, urlInfo, parentUrlInfo) => {
8
- const directoryPath = determineDirectoryPath(urlInfo, parentUrlInfo)
9
- let name = urlToFilename(url)
10
- const { searchParams } = new URL(url)
11
- if (
12
- searchParams.has("css_module") ||
13
- searchParams.has("json_module") ||
14
- searchParams.has("text_module")
15
- ) {
16
- name = `${name}.js`
7
+
8
+ const getUrlName = (url, urlInfo) => {
9
+ if (!urlInfo) {
10
+ return urlToFilename(url)
11
+ }
12
+ if (urlInfo.filename) {
13
+ return urlInfo.filename
17
14
  }
15
+ return urlToFilename(url)
16
+ }
17
+
18
+ const generate = memoizeByUrl((url, { urlInfo, parentUrlInfo }) => {
19
+ const directoryPath = determineDirectoryPath({
20
+ urlInfo,
21
+ parentUrlInfo,
22
+ })
18
23
  let names = cache[directoryPath]
19
24
  if (!names) {
20
25
  names = []
21
26
  cache[directoryPath] = names
22
27
  }
23
-
24
- let nameCandidate = name
28
+ const urlObject = new URL(url)
29
+ let { search, hash } = urlObject
30
+ let name = getUrlName(url, urlInfo)
31
+ let [basename, extension] = splitFileExtension(name)
32
+ extension = extensionMappings[extension] || extension
33
+ let nameCandidate = `${basename}${extension}` // reconstruct name in case extension was normalized
25
34
  let integer = 1
26
35
  // eslint-disable-next-line no-constant-condition
27
36
  while (true) {
@@ -30,9 +39,9 @@ export const createBuilUrlsGenerator = ({ buildDirectoryUrl }) => {
30
39
  break
31
40
  }
32
41
  integer++
33
- nameCandidate = `${name}${integer}`
42
+ nameCandidate = `${basename}${integer}${extension}`
34
43
  }
35
- return `${buildDirectoryUrl}${directoryPath}${nameCandidate}`
44
+ return `${buildDirectoryUrl}${directoryPath}${nameCandidate}${search}${hash}`
36
45
  })
37
46
 
38
47
  return {
@@ -40,12 +49,37 @@ export const createBuilUrlsGenerator = ({ buildDirectoryUrl }) => {
40
49
  }
41
50
  }
42
51
 
43
- const determineDirectoryPath = (urlInfo, parentUrlInfo) => {
52
+ // It's best to generate files with an extension representing what is inside the file
53
+ // and after build js files contains solely js (js or typescript is gone).
54
+ // This way a static file server is already configured to server the correct content-type
55
+ // (otherwise one would have to configure that ".jsx" is "text/javascript")
56
+ // To keep in mind: if you have "user.jsx" and "user.js" AND both file are not bundled
57
+ // you end up with "dist/js/user.js" and "dist/js/user2.js"
58
+ const extensionMappings = {
59
+ ".ts": ".js",
60
+ ".jsx": ".js",
61
+ ".tsx": ".js",
62
+ }
63
+
64
+ const splitFileExtension = (filename) => {
65
+ const dotLastIndex = filename.lastIndexOf(".")
66
+ if (dotLastIndex === -1) {
67
+ return [filename, ""]
68
+ }
69
+ return [filename.slice(0, dotLastIndex), filename.slice(dotLastIndex)]
70
+ }
71
+
72
+ const determineDirectoryPath = ({ urlInfo, parentUrlInfo }) => {
44
73
  if (urlInfo.isInline) {
45
- const parentDirectoryPath = determineDirectoryPath(parentUrlInfo)
74
+ const parentDirectoryPath = determineDirectoryPath({
75
+ urlInfo: parentUrlInfo,
76
+ })
46
77
  return parentDirectoryPath
47
78
  }
48
- if (urlInfo.data.isEntryPoint) {
79
+ if (urlInfo.data.isEntryPoint || urlInfo.data.isWebWorkerEntryPoint) {
80
+ return ""
81
+ }
82
+ if (urlInfo.type === "importmap") {
49
83
  return ""
50
84
  }
51
85
  if (urlInfo.type === "html") {
@@ -55,10 +89,10 @@ const determineDirectoryPath = (urlInfo, parentUrlInfo) => {
55
89
  return "css/"
56
90
  }
57
91
  if (urlInfo.type === "js_module" || urlInfo.type === "js_classic") {
58
- if (urlInfo.subtype === "service_worker") {
59
- return ""
60
- }
61
92
  return "js/"
62
93
  }
63
- return "assets/"
94
+ if (urlInfo.type === "json") {
95
+ return "json/"
96
+ }
97
+ return "other/"
64
98
  }
@@ -0,0 +1,31 @@
1
+ export const GRAPH = {
2
+ map: (graph, callback) => {
3
+ return Object.keys(graph.urlInfos).map((url) => {
4
+ return callback(graph.urlInfos[url])
5
+ })
6
+ },
7
+
8
+ forEach: (graph, callback) => {
9
+ Object.keys(graph.urlInfos).forEach((url) => {
10
+ callback(graph.urlInfos[url], url)
11
+ })
12
+ },
13
+
14
+ filter: (graph, callback) => {
15
+ const urlInfos = []
16
+ Object.keys(graph.urlInfos).forEach((url) => {
17
+ const urlInfo = graph.urlInfos[url]
18
+ if (callback(urlInfo)) {
19
+ urlInfos.push(urlInfo)
20
+ }
21
+ })
22
+ return urlInfos
23
+ },
24
+
25
+ find: (graph, callback) => {
26
+ const urlFound = Object.keys(graph.urlInfos).find((url) => {
27
+ return callback(graph.urlInfos[url])
28
+ })
29
+ return graph.urlInfos[urlFound]
30
+ },
31
+ }
@@ -8,10 +8,27 @@ import {
8
8
  } from "@jsenv/utils/html_ast/html_ast.js"
9
9
  import { createMagicSource } from "@jsenv/utils/sourcemap/magic_source.js"
10
10
 
11
- export const injectVersionMappings = async (
12
- urlInfo,
13
- { kitchen, versionMappings },
14
- ) => {
11
+ import { GRAPH } from "./graph_utils.js"
12
+
13
+ export const injectGlobalVersionMapping = async ({
14
+ finalGraphKitchen,
15
+ finalGraph,
16
+ versionMappings,
17
+ }) => {
18
+ await Promise.all(
19
+ GRAPH.map(finalGraph, async (urlInfo) => {
20
+ if (urlInfo.data.isEntryPoint || urlInfo.data.isWebWorkerEntryPoint) {
21
+ await injectVersionMappings({
22
+ urlInfo,
23
+ kitchen: finalGraphKitchen,
24
+ versionMappings,
25
+ })
26
+ }
27
+ }),
28
+ )
29
+ }
30
+
31
+ const injectVersionMappings = async ({ urlInfo, kitchen, versionMappings }) => {
15
32
  const injector = injectors[urlInfo.type]
16
33
  if (injector) {
17
34
  const { content, sourcemap } = injector(urlInfo, { versionMappings })
@@ -22,6 +39,12 @@ export const injectVersionMappings = async (
22
39
  }
23
40
  }
24
41
 
42
+ const jsInjector = (urlInfo, { versionMappings }) => {
43
+ const magicSource = createMagicSource(urlInfo.content)
44
+ magicSource.prepend(generateClientCodeForVersionMappings(versionMappings))
45
+ return magicSource.toContentAndSourcemap()
46
+ }
47
+
25
48
  const injectors = {
26
49
  html: (urlInfo, { versionMappings }) => {
27
50
  // ideally we would inject an importmap but browser support is too low
@@ -39,24 +62,19 @@ const injectors = {
39
62
  }),
40
63
  )
41
64
  return {
42
- content: stringifyHtmlAst(htmlAst, {
43
- removeOriginalPositionAttributes: true,
44
- }),
65
+ content: stringifyHtmlAst(htmlAst),
45
66
  }
46
67
  },
47
- js_module: (urlInfo, { versionMappings }) => {
48
- const magicSource = createMagicSource(urlInfo.content)
49
- magicSource.prepend(generateClientCodeForVersionMappings(versionMappings))
50
- return magicSource.toContentAndSourcemap()
51
- },
68
+ js_classic: jsInjector,
69
+ js_module: jsInjector,
52
70
  }
53
71
 
54
72
  const generateClientCodeForVersionMappings = (versionMappings) => {
55
73
  return `
56
- var __versionMappings__ = ${JSON.stringify(versionMappings, null, " ")}
57
- var __envGlobal__ = typeof self === 'undefined' ? global : self
74
+ var __versionMappings__ = ${JSON.stringify(versionMappings, null, " ")};
75
+ var __envGlobal__ = typeof self === 'undefined' ? global : self;
58
76
  __envGlobal__.__v__ = function (specifier) {
59
77
  return __versionMappings__[specifier] || specifier
60
- }
78
+ };
61
79
  `
62
80
  }
@@ -0,0 +1,79 @@
1
+ import { createMagicSource } from "@jsenv/utils/sourcemap/magic_source.js"
2
+ import { createVersionGenerator } from "@jsenv/utils/versioning/version_generator.js"
3
+
4
+ import { GRAPH } from "./graph_utils.js"
5
+
6
+ export const injectServiceWorkerUrls = async ({
7
+ finalGraph,
8
+ finalGraphKitchen,
9
+ lineBreakNormalization,
10
+ }) => {
11
+ const serviceWorkerEntryUrlInfos = GRAPH.filter(
12
+ finalGraph,
13
+ (finalUrlInfo) => {
14
+ return (
15
+ finalUrlInfo.subtype === "service_worker" &&
16
+ finalUrlInfo.data.isWebWorkerEntryPoint
17
+ )
18
+ },
19
+ )
20
+ if (serviceWorkerEntryUrlInfos.length === 0) {
21
+ return
22
+ }
23
+ const serviceWorkerUrls = {}
24
+ GRAPH.forEach(finalGraph, (urlInfo) => {
25
+ if (urlInfo.isInline || urlInfo.external) {
26
+ return
27
+ }
28
+ if (!urlInfo.url.startsWith("file:")) {
29
+ return
30
+ }
31
+ if (urlInfo.data.buildUrlIsVersioned) {
32
+ serviceWorkerUrls[urlInfo.data.buildUrlSpecifier] = {
33
+ versioned: true,
34
+ }
35
+ return
36
+ }
37
+ if (!urlInfo.data.version) {
38
+ // when url is not versioned we compute a "version" for that url anyway
39
+ // so that service worker source still changes and navigator
40
+ // detect there is a change
41
+ const versionGenerator = createVersionGenerator()
42
+ versionGenerator.augmentWithContent({
43
+ content: urlInfo.content,
44
+ contentType: urlInfo.contentType,
45
+ lineBreakNormalization,
46
+ })
47
+ const version = versionGenerator.generate()
48
+ urlInfo.data.version = version
49
+ }
50
+ serviceWorkerUrls[urlInfo.data.buildUrlSpecifier] = {
51
+ versioned: false,
52
+ version: urlInfo.data.version,
53
+ }
54
+ })
55
+ await Promise.all(
56
+ serviceWorkerEntryUrlInfos.map(async (serviceWorkerEntryUrlInfo) => {
57
+ const magicSource = createMagicSource(serviceWorkerEntryUrlInfo.content)
58
+ const urlsWithoutSelf = {
59
+ ...serviceWorkerUrls,
60
+ }
61
+ delete urlsWithoutSelf[serviceWorkerEntryUrlInfo.data.buildUrlSpecifier]
62
+ magicSource.prepend(generateClientCode(urlsWithoutSelf))
63
+ const { content, sourcemap } = magicSource.toContentAndSourcemap()
64
+ await finalGraphKitchen.urlInfoTransformer.applyFinalTransformations(
65
+ serviceWorkerEntryUrlInfo,
66
+ {
67
+ content,
68
+ sourcemap,
69
+ },
70
+ )
71
+ }),
72
+ )
73
+ }
74
+
75
+ const generateClientCode = (serviceWorkerUrls) => {
76
+ return `
77
+ self.serviceWorkerUrls = ${JSON.stringify(serviceWorkerUrls, null, " ")};
78
+ `
79
+ }
@@ -0,0 +1,68 @@
1
+ /*
2
+ * Update <link rel="preload"> and friends after build (once we know everything)
3
+ *
4
+ * - Used to remove ressource hint targeting an url that is no longer used:
5
+ * - Happens because of import assertions transpilation (file is inlined into JS)
6
+ */
7
+
8
+ import {
9
+ parseHtmlString,
10
+ visitHtmlAst,
11
+ stringifyHtmlAst,
12
+ getHtmlNodeAttributeByName,
13
+ removeHtmlNode,
14
+ } from "@jsenv/utils/html_ast/html_ast.js"
15
+
16
+ import { GRAPH } from "./graph_utils.js"
17
+
18
+ export const resyncRessourceHints = async ({
19
+ finalGraphKitchen,
20
+ finalGraph,
21
+ buildUrls,
22
+ }) => {
23
+ const ressourceHintActions = []
24
+ GRAPH.forEach(finalGraph, (urlInfo) => {
25
+ if (urlInfo.type !== "html") {
26
+ return
27
+ }
28
+ ressourceHintActions.push(async () => {
29
+ const htmlAst = parseHtmlString(urlInfo.content, {
30
+ storeOriginalPositions: false,
31
+ })
32
+ const visitLinkWithHref = (linkNode, hrefAttribute) => {
33
+ const href = hrefAttribute.value
34
+ if (!href || href.startsWith("data:")) {
35
+ return
36
+ }
37
+ const buildUrl = buildUrls[href]
38
+ const urlInfo = finalGraph.getUrlInfo(buildUrl)
39
+ if (!urlInfo) {
40
+ return
41
+ }
42
+ if (urlInfo.dependents.size === 0) {
43
+ removeHtmlNode(linkNode)
44
+ return
45
+ }
46
+ }
47
+ visitHtmlAst(htmlAst, (node) => {
48
+ if (node.nodeName !== "link") {
49
+ return
50
+ }
51
+ const hrefAttribute = getHtmlNodeAttributeByName(node, "href")
52
+ if (!hrefAttribute) {
53
+ return
54
+ }
55
+ visitLinkWithHref(node, hrefAttribute)
56
+ })
57
+ await finalGraphKitchen.urlInfoTransformer.applyFinalTransformations(
58
+ urlInfo,
59
+ {
60
+ content: stringifyHtmlAst(htmlAst),
61
+ },
62
+ )
63
+ })
64
+ })
65
+ await Promise.all(
66
+ ressourceHintActions.map((ressourceHintAction) => ressourceHintAction()),
67
+ )
68
+ }
@@ -0,0 +1,205 @@
1
+ /*
2
+ * startBuildServer is mean to interact with the build files;
3
+ * files that will be deployed to production server(s).
4
+ * We want to be as close as possible from the production in order to:
5
+ * - run lighthouse
6
+ * - run an automated test tool such as cypress, playwright
7
+ * - see exactly how build file behaves (debug, measure perf, etc)
8
+ * For these reasons "startBuildServer" must be as close as possible from a static file server.
9
+ * It is not meant to provide a nice developper experience: this is the role "startDevServer".
10
+ *
11
+ * Conclusion:
12
+ * "startBuildServer" must be as close as possible from a static file server because
13
+ * we want to be in the user shoes and we should not alter build files.
14
+ */
15
+
16
+ import {
17
+ jsenvAccessControlAllowedHeaders,
18
+ startServer,
19
+ pluginServerTiming,
20
+ pluginRequestWaitingCheck,
21
+ pluginCORS,
22
+ fetchFileSystem,
23
+ } from "@jsenv/server"
24
+ import { assertAndNormalizeDirectoryUrl } from "@jsenv/filesystem"
25
+ import { createLogger } from "@jsenv/logger"
26
+ import { Abort } from "@jsenv/abort"
27
+
28
+ import { initProcessAutorestart } from "@jsenv/utils/file_watcher/process_auto_restart.js"
29
+ import { createTaskLog } from "@jsenv/utils/logs/task_log.js"
30
+ import { watchFiles } from "@jsenv/utils/file_watcher/file_watcher.js"
31
+ import { executeCommand } from "@jsenv/utils/command/command.js"
32
+
33
+ export const startBuildServer = async ({
34
+ signal = new AbortController().signal,
35
+ handleSIGINT = true,
36
+ logLevel,
37
+ buildCommandLogLevel = "warn",
38
+ protocol = "http",
39
+ http2,
40
+ certificate,
41
+ privateKey,
42
+ listenAnyIp,
43
+ ip,
44
+ port = 9779,
45
+
46
+ rootDirectoryUrl,
47
+ buildDirectoryUrl,
48
+ buildCommand,
49
+ mainBuildFileUrl = "/index.html",
50
+ watchedFilePatterns,
51
+ cooldownBetweenFileEvents,
52
+ autorestart,
53
+ }) => {
54
+ rootDirectoryUrl = assertAndNormalizeDirectoryUrl(rootDirectoryUrl)
55
+ buildDirectoryUrl = assertAndNormalizeDirectoryUrl(buildDirectoryUrl)
56
+
57
+ const autorestartProcess = await initProcessAutorestart({
58
+ signal,
59
+ handleSIGINT,
60
+ ...(autorestart
61
+ ? {
62
+ enabled: true,
63
+ logLevel: autorestart.logLevel,
64
+ urlToRestart: autorestart.url,
65
+ urlsToWatch: [
66
+ ...(autorestart.urlsToWatch || []),
67
+ new URL("package.json", rootDirectoryUrl),
68
+ new URL("jsenv.config.mjs", rootDirectoryUrl),
69
+ ],
70
+ }
71
+ : {
72
+ enabled: false,
73
+ }),
74
+ })
75
+ if (autorestartProcess.isPrimary) {
76
+ return {
77
+ origin: `${protocol}://127.0.0.1:${port}`,
78
+ stop: () => {
79
+ autorestartProcess.stop()
80
+ },
81
+ }
82
+ }
83
+ signal = autorestartProcess.signal
84
+ const logger = createLogger({ logLevel })
85
+
86
+ let buildPromise
87
+ let buildAbortController
88
+ const runBuild = async () => {
89
+ buildAbortController = new AbortController()
90
+ const buildOperation = Abort.startOperation()
91
+ buildOperation.addAbortSignal(signal)
92
+ buildOperation.addAbortSignal(buildAbortController.signal)
93
+ const buildTask = createTaskLog(logger, `execute build command`)
94
+ buildPromise = executeCommand(buildCommand, {
95
+ cwd: rootDirectoryUrl,
96
+ logLevel: buildCommandLogLevel,
97
+ signal: buildOperation.signal,
98
+ })
99
+ try {
100
+ await buildPromise
101
+ buildTask.done()
102
+ } catch (e) {
103
+ if (e.code === "ABORT_ERR") {
104
+ buildTask.fail(`execute build command aborted`)
105
+ } else {
106
+ buildTask.fail()
107
+ throw e
108
+ }
109
+ }
110
+ }
111
+
112
+ const startServerTask = createTaskLog(logger, "start build server")
113
+ const server = await startServer({
114
+ signal,
115
+ stopOnExit: false,
116
+ stopOnSIGINT: false,
117
+ stopOnInternalError: false,
118
+ keepProcessAlive: true,
119
+ logLevel,
120
+ startLog: false,
121
+
122
+ protocol,
123
+ http2,
124
+ certificate,
125
+ privateKey,
126
+ listenAnyIp,
127
+ ip,
128
+ port,
129
+ plugins: {
130
+ ...pluginCORS({
131
+ accessControlAllowRequestOrigin: true,
132
+ accessControlAllowRequestMethod: true,
133
+ accessControlAllowRequestHeaders: true,
134
+ accessControlAllowedRequestHeaders: [
135
+ ...jsenvAccessControlAllowedHeaders,
136
+ "x-jsenv-execution-id",
137
+ ],
138
+ accessControlAllowCredentials: true,
139
+ }),
140
+ ...pluginServerTiming(),
141
+ ...pluginRequestWaitingCheck({
142
+ requestWaitingMs: 60 * 1000,
143
+ }),
144
+ },
145
+ sendErrorDetails: true,
146
+ requestToResponse: async (request) => {
147
+ await buildPromise
148
+ const urlIsVersioned = new URL(
149
+ request.ressource,
150
+ request.origin,
151
+ ).searchParams.has("v")
152
+ if (mainBuildFileUrl && request.ressource === "/") {
153
+ request = {
154
+ ...request,
155
+ ressource: mainBuildFileUrl,
156
+ }
157
+ }
158
+ return fetchFileSystem(
159
+ new URL(request.ressource.slice(1), buildDirectoryUrl),
160
+ {
161
+ headers: request.headers,
162
+ cacheControl: urlIsVersioned
163
+ ? `private,max-age=${SECONDS_IN_30_DAYS},immutable`
164
+ : "private,max-age=0,must-revalidate",
165
+ etagEnabled: true,
166
+ compressionEnabled: !request.pathname.endsWith(".mp4"),
167
+ rootDirectoryUrl: buildDirectoryUrl,
168
+ canReadDirectory: true,
169
+ },
170
+ )
171
+ },
172
+ })
173
+ startServerTask.done()
174
+ logger.info(``)
175
+ Object.keys(server.origins).forEach((key) => {
176
+ logger.info(`- ${server.origins[key]}`)
177
+ })
178
+ logger.info(``)
179
+
180
+ const unregisterDirectoryLifecyle = watchFiles({
181
+ rootDirectoryUrl,
182
+ watchedFilePatterns,
183
+ cooldownBetweenFileEvents,
184
+ fileChangeCallback: ({ url, event }) => {
185
+ buildAbortController.abort()
186
+ // setTimeout is to ensure the abortController.abort() above
187
+ // is properly taken into account so that logs about abort comes first
188
+ // then logs about re-running the build happens
189
+ setTimeout(() => {
190
+ logger.info(`${url.slice(rootDirectoryUrl.length)} ${event} -> rebuild`)
191
+ runBuild()
192
+ })
193
+ },
194
+ })
195
+ signal.addEventListener("abort", () => {
196
+ unregisterDirectoryLifecyle()
197
+ })
198
+ runBuild()
199
+ return {
200
+ origin: server.origin,
201
+ stop: () => server.stop(),
202
+ }
203
+ }
204
+
205
+ const SECONDS_IN_30_DAYS = 60 * 60 * 24 * 30
@@ -1,8 +1,8 @@
1
1
  import { readFileSync } from "node:fs"
2
- import { urlToContentType } from "@jsenv/server"
3
2
  import { collectFiles, normalizeStructuredMetaMap } from "@jsenv/filesystem"
4
3
 
5
4
  import { DataUrl } from "@jsenv/utils/urls/data_url.js"
5
+ import { CONTENT_TYPE } from "@jsenv/utils/content_type/content_type.js"
6
6
 
7
7
  export const jsenvPluginExplorer = ({ groups }) => {
8
8
  const htmlClientFileUrl = new URL("./client/explorer.html", import.meta.url)
@@ -43,7 +43,7 @@ export const jsenvPluginExplorer = ({ groups }) => {
43
43
  html = html.replace(
44
44
  "FAVICON_HREF",
45
45
  DataUrl.stringify({
46
- contentType: urlToContentType(faviconClientFileUrl),
46
+ contentType: CONTENT_TYPE.fromUrlExtension(faviconClientFileUrl),
47
47
  base64Flag: true,
48
48
  data: readFileSync(new URL(faviconClientFileUrl)).toString("base64"),
49
49
  }),
@@ -20,7 +20,7 @@ export const jsenvPluginToolbar = ({ logs = false } = {}) => {
20
20
  appliesDuring: {
21
21
  dev: true,
22
22
  },
23
- transform: {
23
+ transformUrlContent: {
24
24
  html: ({ url, content }, { referenceUtils }) => {
25
25
  if (url === toolbarHtmlClientFileUrl) {
26
26
  return null
@@ -28,10 +28,12 @@ export const jsenvPluginToolbar = ({ logs = false } = {}) => {
28
28
  const htmlAst = parseHtmlString(content)
29
29
  const [toolbarInjectorReference] = referenceUtils.inject({
30
30
  type: "js_import_export",
31
+ expectedType: "js_module",
31
32
  specifier: toolbarInjectorClientFileUrl,
32
33
  })
33
34
  const [toolbarClientFileReference] = referenceUtils.inject({
34
35
  type: "iframe_src",
36
+ expectedType: "html",
35
37
  specifier: toolbarHtmlClientFileUrl,
36
38
  })
37
39
  injectScriptAsEarlyAsPossible(