@jsenv/core 22.4.0 → 23.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.
Files changed (106) hide show
  1. package/dist/jsenv_browser_system.js +331 -256
  2. package/dist/jsenv_browser_system.js.map +55 -33
  3. package/dist/jsenv_compile_proxy.js +186 -108
  4. package/dist/jsenv_compile_proxy.js.map +48 -42
  5. package/dist/jsenv_exploring_redirector.js +148 -71
  6. package/dist/jsenv_exploring_redirector.js.map +26 -20
  7. package/dist/jsenv_toolbar.js +215 -104
  8. package/dist/jsenv_toolbar.js.map +39 -26
  9. package/helpers/new_stylesheet/new_stylesheet.js +411 -0
  10. package/{LICENSE → license} +0 -0
  11. package/main.js +8 -7
  12. package/package.json +23 -19
  13. package/readme.md +9 -6
  14. package/src/buildProject.js +3 -7
  15. package/src/execute.js +23 -10
  16. package/src/executeTestPlan.js +0 -4
  17. package/src/importUsingChildProcess.js +36 -32
  18. package/src/internal/{babel-plugin-replace-expressions.js → babel_plugin_replace_expressions.js} +0 -0
  19. package/src/internal/{babel-plugin-transform-import-meta.js → babel_plugin_transform_import_meta.js} +0 -0
  20. package/src/internal/browser-launcher/executeHtmlFile.js +6 -8
  21. package/src/internal/browser-launcher/jsenv-browser-system.js +3 -0
  22. package/src/internal/building/asset_url_versioning.js +5 -9
  23. package/src/internal/building/buildServiceWorker.js +6 -13
  24. package/src/internal/building/buildUsingRollup.js +41 -4
  25. package/src/internal/building/build_logs.js +11 -0
  26. package/src/internal/building/build_stats.js +7 -1
  27. package/src/internal/building/createJsenvRollupPlugin.js +402 -297
  28. package/src/internal/building/css/parseCssRessource.js +67 -71
  29. package/src/internal/building/css/parseCssUrls.js +2 -2
  30. package/src/internal/building/css/{postcss-urlhash-plugin.js → postcss_plugin_url_visitor.js} +43 -21
  31. package/src/internal/building/css/replaceCssUrls.js +17 -14
  32. package/src/internal/building/css_module.js +47 -0
  33. package/src/internal/building/html/parseHtmlRessource.js +44 -43
  34. package/src/internal/building/import_references.js +81 -0
  35. package/src/internal/building/importmap/parseImportmapRessource.js +5 -2
  36. package/src/internal/building/js/minifyJs.js +30 -3
  37. package/src/internal/building/js/parseJsRessource.js +70 -77
  38. package/src/internal/building/json/parseJsonRessource.js +3 -2
  39. package/src/internal/building/parseRessource.js +11 -8
  40. package/src/internal/building/parsing.utils.js +4 -15
  41. package/src/internal/building/ressource_builder.js +142 -114
  42. package/src/internal/building/ressource_builder_util.js +31 -18
  43. package/src/internal/building/{fetchSourcemap.js → sourcemap_loader.js} +29 -27
  44. package/src/internal/building/svg/parseSvgRessource.js +7 -3
  45. package/src/internal/building/url-versioning.js +2 -1
  46. package/src/internal/building/url_fetcher.js +79 -0
  47. package/src/internal/building/url_loader.js +267 -0
  48. package/src/internal/building/url_trace.js +1 -1
  49. package/src/internal/building/webmanifest/parseWebmanifestRessource.js +9 -4
  50. package/src/internal/compiling/{js-compilation-service/ensureGlobalThisImportBabelPlugin.js → babel_plugin_global_this_as_jsenv_import.js} +4 -2
  51. package/src/internal/compiling/babel_plugin_import_assertions.js +100 -0
  52. package/src/internal/compiling/babel_plugin_new_stylesheet_as_jsenv_import.js +109 -0
  53. package/src/internal/compiling/babel_plugin_transform_import_specifier.js +86 -0
  54. package/src/internal/compiling/babel_plugins.js +2 -0
  55. package/src/internal/compiling/createCompiledFileService.js +46 -15
  56. package/src/internal/compiling/html_source_file_service.js +2 -2
  57. package/src/internal/compiling/js-compilation-service/{transformBabelHelperToImportBabelPlugin.js → babel_plugin_babel_helpers_as_jsenv_imports.js} +1 -1
  58. package/src/internal/compiling/js-compilation-service/{ensureRegeneratorRuntimeImportBabelPlugin.js → babel_plugin_regenerator_runtime_as_jsenv_import.js} +1 -1
  59. package/src/internal/compiling/js-compilation-service/jsenvTransform.js +7 -16
  60. package/src/internal/compiling/js-compilation-service/transformJs.js +0 -2
  61. package/src/internal/compiling/jsenvCompilerForHtml.js +2 -7
  62. package/src/internal/compiling/rollup_plugin_commonjs_named_exports.js +2 -2
  63. package/src/internal/compiling/startCompileServer.js +11 -4
  64. package/src/internal/escapeTemplateStringSpecialCharacters.js +20 -0
  65. package/src/internal/executing/coverage/{babel-plugin-instrument.js → babel_plugin_instrument.js} +17 -6
  66. package/src/internal/executing/coverage/relativeUrlToEmptyCoverage.js +1 -1
  67. package/src/internal/executing/executeConcurrently.js +5 -3
  68. package/src/internal/executing/executePlan.js +16 -2
  69. package/src/internal/executing/generateFileExecutionSteps.js +2 -1
  70. package/src/internal/executing/launchAndExecute.js +43 -69
  71. package/src/internal/executing/writeLog.js +25 -18
  72. package/src/internal/exploring/exploring.css +2 -1
  73. package/src/internal/exploring/exploring.redirector.js +0 -1
  74. package/src/internal/generateGroupMap/generateGroupMap.js +14 -10
  75. package/src/internal/generateGroupMap/jsenvBabelPluginCompatMap.js +30 -0
  76. package/src/internal/generateGroupMap/jsenvPluginCompatMap.js +0 -6
  77. package/src/internal/generateGroupMap/one_runtime_compat.js +9 -38
  78. package/src/internal/generateGroupMap/runtime_compat.js +9 -24
  79. package/src/internal/generateGroupMap/runtime_compat_composition.js +2 -12
  80. package/src/internal/generateGroupMap/runtime_support.js +53 -0
  81. package/src/internal/jsenvInternalFiles.js +0 -1
  82. package/src/internal/node-launcher/createControllableNodeProcess.js +2 -3
  83. package/src/internal/response_validation.js +143 -0
  84. package/src/internal/runtime/createBrowserRuntime/createBrowserRuntime.js +8 -3
  85. package/src/internal/runtime/createBrowserRuntime/createBrowserSystem.js +165 -25
  86. package/src/internal/runtime/createBrowserRuntime/displayErrorInDocument.js +7 -6
  87. package/src/internal/runtime/createBrowserRuntime/scanBrowserRuntimeFeatures.js +124 -68
  88. package/src/internal/runtime/createNodeRuntime/createNodeRuntime.js +8 -86
  89. package/src/internal/runtime/createNodeRuntime/createNodeSystem.js +53 -9
  90. package/src/internal/runtime/createNodeRuntime/scanNodeRuntimeFeatures.js +115 -0
  91. package/src/internal/runtime/module-registration.js +86 -136
  92. package/src/internal/runtime/resolveGroup.js +2 -3
  93. package/src/internal/toolbar/compilation/toolbar.compilation.js +15 -17
  94. package/src/internal/toolbar/eventsource/toolbar.eventsource.js +35 -8
  95. package/src/internal/toolbar/toolbar.main.js +7 -4
  96. package/src/internal/url_utils.js +33 -0
  97. package/src/internal/url_utils.test.js +39 -0
  98. package/src/jsonToJavaScriptModule.js +12 -0
  99. package/src/launchBrowser.js +50 -34
  100. package/src/launchNode.js +6 -3
  101. package/src/requireUsingChildProcess.js +36 -32
  102. package/src/startExploring.js +25 -11
  103. package/src/internal/building/transformImportMetaUrlReferences.js +0 -71
  104. package/src/internal/runtime/resolveBrowserGroup.js +0 -5
  105. package/src/internal/runtime/resolveNodeGroup.js +0 -5
  106. package/src/internal/validateResponseStatusIsOk.js +0 -91
@@ -1,5 +1,5 @@
1
- import { basename } from "path"
2
1
  import { urlToFilename, urlToRelativeUrl, resolveUrl } from "@jsenv/filesystem"
2
+
3
3
  import {
4
4
  getCssSourceMappingUrl,
5
5
  setCssSourceMappingUrl,
@@ -11,19 +11,27 @@ import { replaceCssUrls } from "./replaceCssUrls.js"
11
11
  export const parseCssRessource = async (
12
12
  cssRessource,
13
13
  { notifyReferenceFound },
14
- { urlToOriginalServerUrl, minify, minifyCssOptions },
14
+ { asProjectUrl, asOriginalUrl, minify, minifyCssOptions },
15
15
  ) => {
16
16
  const cssString = String(cssRessource.bufferBeforeBuild)
17
17
  const cssSourcemapUrl = getCssSourceMappingUrl(cssString)
18
18
  let sourcemapReference
19
19
  if (cssSourcemapUrl) {
20
20
  sourcemapReference = notifyReferenceFound({
21
- ressourceContentTypeExpected: "application/json",
21
+ contentTypeExpected: ["application/json", "application/octet-stream"],
22
22
  ressourceSpecifier: cssSourcemapUrl,
23
23
  // we don't really know the line or column
24
24
  // but let's asusme it the last line and first column
25
25
  referenceLine: cssString.split(/\r?\n/).length - 1,
26
26
  referenceColumn: 0,
27
+ isSourcemap: true,
28
+ })
29
+ } else {
30
+ sourcemapReference = notifyReferenceFound({
31
+ contentType: "application/octet-stream",
32
+ ressourceSpecifier: `${urlToFilename(cssRessource.url)}.map`,
33
+ isPlaceholder: true,
34
+ isSourcemap: true,
27
35
  })
28
36
  }
29
37
 
@@ -48,15 +56,22 @@ export const parseCssRessource = async (
48
56
  urlNodeReferenceMapping.set(urlDeclaration.urlNode, urlReference)
49
57
  })
50
58
 
51
- return async ({
52
- getUrlRelativeToImporter,
53
- precomputeBuildRelativeUrl,
54
- registerAssetEmitter,
55
- }) => {
56
- const cssReplaceResult = await replaceCssUrls(
57
- cssString,
58
- cssRessource.url,
59
- ({ urlNode }) => {
59
+ return async ({ getUrlRelativeToImporter, buildDirectoryUrl }) => {
60
+ const sourcemapRessource = sourcemapReference.ressource
61
+
62
+ let code = cssString
63
+ let map = sourcemapRessource.isPlaceholder
64
+ ? undefined
65
+ : JSON.parse(String(sourcemapRessource.bufferBeforeBuild))
66
+
67
+ const cssCompiledUrl = cssRessource.url
68
+ const cssOriginalUrl = asOriginalUrl(cssCompiledUrl)
69
+
70
+ const replaceCssResult = await replaceCssUrls({
71
+ url: map ? asProjectUrl(cssCompiledUrl) : cssOriginalUrl,
72
+ code: cssString,
73
+ map,
74
+ getUrlReplacementValue: ({ urlNode }) => {
60
75
  const nodeCandidates = Array.from(urlNodeReferenceMapping.keys())
61
76
  const urlNodeFound = nodeCandidates.find((urlNodeCandidate) =>
62
77
  isSameCssDocumentUrlNode(urlNodeCandidate, urlNode),
@@ -65,7 +80,7 @@ export const parseCssRessource = async (
65
80
  return urlNode.value
66
81
  }
67
82
 
68
- // url node nous dit quel réfrence y correspond
83
+ // url node nous dit quel référence y correspond
69
84
  const urlNodeReference = urlNodeReferenceMapping.get(urlNodeFound)
70
85
  const cssUrlRessource = urlNodeReference.ressource
71
86
 
@@ -80,74 +95,55 @@ export const parseCssRessource = async (
80
95
  }
81
96
  return getUrlRelativeToImporter(cssUrlRessource)
82
97
  },
83
- {
84
- cssMinification: minify,
85
- cssMinificationOptions: minifyCssOptions,
86
- // https://postcss.org/api/#sourcemapoptions
87
- sourcemapOptions: sourcemapReference
88
- ? { prev: String(sourcemapReference.ressource.bufferAfterBuild) }
89
- : {},
90
- },
91
- )
92
- const code = cssReplaceResult.css
93
- const map = cssReplaceResult.map.toJSON()
94
- const cssBuildRelativeUrl = precomputeBuildRelativeUrl(code)
98
+ cssMinification: minify,
99
+ cssMinificationOptions: minifyCssOptions,
100
+ })
101
+ code = replaceCssResult.code
102
+ map = replaceCssResult.map
95
103
 
96
- const cssSourcemapFilename = `${basename(cssBuildRelativeUrl)}.map`
104
+ cssRessource.buildEnd(code)
97
105
 
98
- // In theory code should never be modified once the url for caching is computed
99
- // because url for caching depends on file content.
106
+ // In theory code should never be modified once buildEnd() is called
107
+ // because buildRelativeUrl might be versioned based on file content
100
108
  // There is an exception for sourcemap because we want to update sourcemap.file
101
109
  // to the cached filename of the css file.
102
110
  // To achieve that we set/update the sourceMapping url comment in compiled css file.
103
111
  // This is totally fine to do that because sourcemap and css file lives togethers
104
112
  // so this comment changes nothing regarding cache invalidation and is not important
105
- // to decide the filename for this css asset.
106
- const cssSourceAfterTransformation = setCssSourceMappingUrl(
107
- code,
108
- cssSourcemapFilename,
113
+ // to decide buildRelativeUrl for this css file.
114
+ const cssBuildUrl = resolveUrl(
115
+ cssRessource.buildRelativeUrl,
116
+ buildDirectoryUrl,
117
+ )
118
+ const sourcemapPrecomputedBuildUrl = resolveUrl(
119
+ `${urlToFilename(cssBuildUrl)}.map`,
120
+ cssBuildUrl,
109
121
  )
110
122
 
111
- registerAssetEmitter(({ buildDirectoryUrl, emitAsset }) => {
112
- const cssBuildUrl = resolveUrl(
113
- cssRessource.buildRelativeUrl,
114
- buildDirectoryUrl,
115
- )
116
- const mapBuildUrl = resolveUrl(cssSourcemapFilename, cssBuildUrl)
117
- map.file = urlToFilename(cssBuildUrl)
118
- if (map.sources) {
119
- // hum en fait si css est inline, alors la source n'est pas le fichier compilé
120
- // mais bien le fichier html compilé ?
121
- const importerUrl = cssRessource.isInline
122
- ? urlToOriginalServerUrl(cssRessource.url)
123
- : cssRessource.url
124
- map.sources = map.sources.map((source) => {
125
- const sourceUrl = resolveUrl(source, importerUrl)
126
- const sourceUrlRelativeToSourceMap = urlToRelativeUrl(
127
- sourceUrl,
128
- mapBuildUrl,
129
- )
130
- return sourceUrlRelativeToSourceMap
131
- })
132
- }
133
-
134
- const mapSource = JSON.stringify(map, null, " ")
135
- const buildRelativeUrl = urlToRelativeUrl(mapBuildUrl, buildDirectoryUrl)
136
- if (sourcemapReference) {
137
- sourcemapReference.ressource.buildRelativeUrl = buildRelativeUrl
138
- sourcemapReference.ressource.bufferAfterBuild = mapSource
139
- } else {
140
- emitAsset({
141
- fileName: buildRelativeUrl,
142
- source: mapSource,
143
- })
144
- }
145
- })
146
-
147
- return {
148
- buildRelativeUrl: cssBuildRelativeUrl,
149
- bufferAfterBuild: cssSourceAfterTransformation,
123
+ map.file = urlToFilename(cssBuildUrl)
124
+ if (map.sources) {
125
+ map.sources = map.sources.map((source) => {
126
+ const sourceUrl = resolveUrl(source, cssOriginalUrl)
127
+ const sourceUrlRelativeToSourceMap = urlToRelativeUrl(
128
+ sourceUrl,
129
+ sourcemapPrecomputedBuildUrl,
130
+ )
131
+ return sourceUrlRelativeToSourceMap
132
+ })
150
133
  }
134
+ const mapSource = JSON.stringify(map, null, " ")
135
+ sourcemapRessource.buildEnd(mapSource)
136
+
137
+ const sourcemapBuildUrl = resolveUrl(
138
+ sourcemapRessource.buildRelativeUrl,
139
+ buildDirectoryUrl,
140
+ )
141
+ const sourcemapUrlForCss = urlToRelativeUrl(sourcemapBuildUrl, cssBuildUrl)
142
+ const codeWithSourcemapComment = setCssSourceMappingUrl(
143
+ code,
144
+ sourcemapUrlForCss,
145
+ )
146
+ cssRessource.bufferAfterBuild = codeWithSourcemapComment
151
147
  }
152
148
  }
153
149
 
@@ -1,11 +1,11 @@
1
1
  import { applyPostCss } from "./applyPostCss.js"
2
- import { postCssUrlHashPlugin } from "./postcss-urlhash-plugin.js"
2
+ import { postCssPluginUrlVisitor } from "./postcss_plugin_url_visitor.js"
3
3
 
4
4
  export const parseCssUrls = async (css, cssUrl = "file:///file.css") => {
5
5
  const atImports = []
6
6
  const urlDeclarations = []
7
7
 
8
- const postCssPlugins = [postCssUrlHashPlugin]
8
+ const postCssPlugins = [postCssPluginUrlVisitor]
9
9
  const postCssOptions = { collectUrls: true }
10
10
  const result = await applyPostCss(css, cssUrl, postCssPlugins, postCssOptions)
11
11
 
@@ -14,12 +14,12 @@ hence sourcemap cannot point the original source location
14
14
  import { fileSystemPathToUrl, resolveUrl } from "@jsenv/filesystem"
15
15
  import { require } from "@jsenv/core/src/internal/require.js"
16
16
 
17
- export const postCssUrlHashPlugin = () => {
17
+ export const postCssPluginUrlVisitor = () => {
18
18
  const parseCssValue = require("postcss-value-parser")
19
19
  const stringifyCssNodes = parseCssValue.stringify
20
20
 
21
21
  return {
22
- postcssPlugin: "urlhash",
22
+ postcssPlugin: "url_visitor",
23
23
  prepare: (result) => {
24
24
  const {
25
25
  from,
@@ -27,6 +27,7 @@ export const postCssUrlHashPlugin = () => {
27
27
  getUrlReplacementValue = () => undefined,
28
28
  } = result.opts
29
29
  const fromUrl = fileSystemPathToUrl(from)
30
+ const mutations = []
30
31
  return {
31
32
  AtRule: {
32
33
  import: (atImportNode, { AtRule }) => {
@@ -126,10 +127,12 @@ export const postCssUrlHashPlugin = () => {
126
127
  },
127
128
  },
128
129
  Declaration: (declarationNode) => {
129
- walkUrls(declarationNode, {
130
- parseCssValue,
130
+ const parsed = parseCssValue(declarationNode.value)
131
+ const urlMutations = []
132
+
133
+ walkUrls(parsed, {
131
134
  stringifyCssNodes,
132
- visitor: (url, urlNode) => {
135
+ visitor: ({ url, urlNode }) => {
133
136
  // Empty URL
134
137
  if (!urlNode || url.length === 0) {
135
138
  declarationNode.warn(
@@ -155,28 +158,40 @@ export const postCssUrlHashPlugin = () => {
155
158
  urlNode,
156
159
  }
157
160
 
158
- const urlNewValue = getUrlReplacementValue(urlReference)
159
- if (urlNewValue) {
160
- urlNode.value = urlNewValue
161
- }
162
-
163
161
  if (collectUrls) {
164
162
  result.messages.push(urlReference)
165
163
  }
164
+
165
+ const urlNewValue = getUrlReplacementValue(urlReference)
166
+ if (urlNewValue) {
167
+ urlMutations.push(() => {
168
+ urlNode.value = urlNewValue
169
+ })
170
+ }
166
171
  },
167
172
  })
173
+
174
+ if (urlMutations.length) {
175
+ mutations.push(() => {
176
+ urlMutations.forEach((urlMutation) => {
177
+ urlMutation()
178
+ })
179
+ declarationNode.value = parsed.toString()
180
+ })
181
+ }
182
+ },
183
+ OnceExit: () => {
184
+ mutations.forEach((mutation) => {
185
+ mutation()
186
+ })
168
187
  },
169
188
  }
170
189
  },
171
190
  }
172
191
  }
173
- postCssUrlHashPlugin.postcss = true
192
+ postCssPluginUrlVisitor.postcss = true
174
193
 
175
- const walkUrls = (
176
- declarationNode,
177
- { parseCssValue, stringifyCssNodes, visitor },
178
- ) => {
179
- const parsed = parseCssValue(declarationNode.value)
194
+ const walkUrls = (parsed, { stringifyCssNodes, visitor }) => {
180
195
  parsed.walk((node) => {
181
196
  // https://github.com/andyjansson/postcss-functions
182
197
  if (isUrlFunctionNode(node)) {
@@ -186,14 +201,20 @@ const walkUrls = (
186
201
  urlNode && urlNode.type === "string"
187
202
  ? urlNode.value
188
203
  : stringifyCssNodes(nodes)
189
- visitor(url.trim(), urlNode)
204
+ visitor({
205
+ url: url.trim(),
206
+ urlNode,
207
+ })
190
208
  return
191
209
  }
192
210
 
193
211
  if (isImageSetFunctionNode(node)) {
194
212
  Array.from(node.nodes).forEach((childNode) => {
195
213
  if (childNode.type === "string") {
196
- visitor(childNode.value.trim(), childNode)
214
+ visitor({
215
+ url: childNode.value.trim(),
216
+ urlNode: childNode,
217
+ })
197
218
  return
198
219
  }
199
220
 
@@ -204,14 +225,15 @@ const walkUrls = (
204
225
  urlNode && urlNode.type === "string"
205
226
  ? urlNode.value
206
227
  : stringifyCssNodes(nodes)
207
- visitor(url.trim(), urlNode)
228
+ visitor({
229
+ url: url.trim(),
230
+ urlNode,
231
+ })
208
232
  return
209
233
  }
210
234
  })
211
235
  }
212
236
  })
213
-
214
- declarationNode.value = parsed.toString()
215
237
  }
216
238
 
217
239
  const isUrlFunctionNode = (node) => {
@@ -1,19 +1,17 @@
1
1
  import { require } from "@jsenv/core/src/internal/require.js"
2
2
  import { applyPostCss } from "./applyPostCss.js"
3
- import { postCssUrlHashPlugin } from "./postcss-urlhash-plugin.js"
3
+ import { postCssPluginUrlVisitor } from "./postcss_plugin_url_visitor.js"
4
4
 
5
- export const replaceCssUrls = async (
6
- css,
7
- cssUrl,
5
+ export const replaceCssUrls = async ({
6
+ url,
7
+ code,
8
+ map,
8
9
  getUrlReplacementValue,
9
- {
10
- cssMinification = false,
11
- cssMinificationOptions,
12
- sourcemapOptions = {},
13
- } = {},
14
- ) => {
10
+ cssMinification = false,
11
+ cssMinificationOptions,
12
+ } = {}) => {
15
13
  const postcssPlugins = [
16
- postCssUrlHashPlugin,
14
+ postCssPluginUrlVisitor,
17
15
  ...(cssMinification
18
16
  ? [await getCssMinificationPlugin(cssMinificationOptions)]
19
17
  : []),
@@ -22,11 +20,16 @@ export const replaceCssUrls = async (
22
20
  getUrlReplacementValue,
23
21
  map: {
24
22
  inline: false,
25
- ...sourcemapOptions,
23
+ // https://postcss.org/api/#sourcemapoptions
24
+ ...(map ? { prev: JSON.stringify(map) } : {}),
26
25
  },
27
26
  }
28
- const result = await applyPostCss(css, cssUrl, postcssPlugins, postcssOptions)
29
- return result
27
+ const result = await applyPostCss(code, url, postcssPlugins, postcssOptions)
28
+
29
+ return {
30
+ code: result.css,
31
+ map: result.map.toJSON(),
32
+ }
30
33
  }
31
34
 
32
35
  const getCssMinificationPlugin = async (cssMinificationOptions = {}) => {
@@ -0,0 +1,47 @@
1
+ import { resolveUrl, urlToRelativeUrl } from "@jsenv/filesystem"
2
+
3
+ import { replaceCssUrls } from "@jsenv/core/src/internal/building/css/replaceCssUrls.js"
4
+ import { escapeTemplateStringSpecialCharacters } from "@jsenv/core/src/internal/escapeTemplateStringSpecialCharacters.js"
5
+ import {
6
+ getCssSourceMappingUrl,
7
+ setCssSourceMappingUrl,
8
+ } from "@jsenv/core/src/internal/sourceMappingURLUtils.js"
9
+
10
+ export const convertCssTextToJavascriptModule = async ({
11
+ cssUrl,
12
+ jsUrl,
13
+ code,
14
+ map,
15
+ }) => {
16
+ const directoryUrl = resolveUrl("./", cssUrl)
17
+ const jsDirectoryUrl = resolveUrl("./", jsUrl)
18
+ if (directoryUrl !== jsDirectoryUrl) {
19
+ const cssUrlReplaceResult = await replaceCssUrls({
20
+ url: cssUrl,
21
+ code,
22
+ map,
23
+ getUrlReplacementValue: ({ specifier }) => {
24
+ const urlForCss = resolveUrl(specifier, cssUrl)
25
+ const urlForJs = urlToRelativeUrl(urlForCss, jsUrl)
26
+ return urlForJs
27
+ },
28
+ })
29
+ code = cssUrlReplaceResult.code
30
+ map = cssUrlReplaceResult.map
31
+ const sourcemapUrlSpecifier = getCssSourceMappingUrl(code)
32
+ const sourcemapUrlForCss = resolveUrl(sourcemapUrlSpecifier, cssUrl)
33
+ const sourcemapUrlForJs = urlToRelativeUrl(sourcemapUrlForCss, jsUrl)
34
+ code = setCssSourceMappingUrl(code, sourcemapUrlForJs)
35
+ }
36
+
37
+ const cssTextEscaped = escapeTemplateStringSpecialCharacters(code)
38
+
39
+ return {
40
+ code: `
41
+ const stylesheet = new CSSStyleSheet()
42
+
43
+ stylesheet.replaceSync(\`${cssTextEscaped}\`)
44
+
45
+ export default stylesheet`,
46
+ }
47
+ }
@@ -17,7 +17,7 @@ Or be sure to also reference this url somewhere in the html file like
17
17
  */
18
18
 
19
19
  import {
20
- urlToBasename,
20
+ urlToFilename,
21
21
  urlToRelativeUrl,
22
22
  resolveUrl,
23
23
  urlToParentUrl,
@@ -71,20 +71,18 @@ export const parseHtmlRessource = async (
71
71
 
72
72
  const linksMutations = collectNodesMutations(
73
73
  links,
74
- notifiers,
74
+ {
75
+ ...notifiers,
76
+ ressourceHintNeverUsedCallback,
77
+ },
75
78
  htmlRessource,
76
- [
77
- linkStylesheetHrefVisitor,
78
- (link, notifiers) =>
79
- linkHrefVisitor(link, {
80
- ...notifiers,
81
- ressourceHintNeverUsedCallback,
82
- }),
83
- ],
79
+ [linkStylesheetHrefVisitor, linkHrefVisitor],
84
80
  )
85
81
  const scriptsMutations = collectNodesMutations(
86
82
  scripts,
87
- notifiers,
83
+ {
84
+ ...notifiers,
85
+ },
88
86
  htmlRessource,
89
87
  [
90
88
  // regular javascript are not parseable by rollup
@@ -154,9 +152,10 @@ export const parseHtmlRessource = async (
154
152
  })
155
153
 
156
154
  const htmlAfterTransformation = htmlAstToHtmlString(htmlAst)
157
- return minify
155
+ const html = minify
158
156
  ? minifyHtml(htmlAfterTransformation, minifyHtmlOptions)
159
157
  : htmlAfterTransformation
158
+ htmlRessource.buildEnd(html)
160
159
  }
161
160
  }
162
161
 
@@ -179,7 +178,7 @@ const regularScriptSrcVisitor = (
179
178
  }
180
179
 
181
180
  const remoteScriptReference = notifyReferenceFound({
182
- ressourceContentTypeExpected: "application/javascript",
181
+ contentTypeExpected: "application/javascript",
183
182
  ressourceSpecifier: srcAttribute.value,
184
183
  ...referenceLocationFromHtmlNode(script, "src"),
185
184
  })
@@ -238,13 +237,14 @@ const regularScriptTextNodeVisitor = (
238
237
  return null
239
238
  }
240
239
 
240
+ const ressourceSpecifier = getUniqueNameForInlineHtmlNode(
241
+ script,
242
+ scripts,
243
+ `${urlToFilename(htmlRessource.url)}__inline__[id].js`,
244
+ )
241
245
  const jsReference = notifyReferenceFound({
242
- ressourceContentTypeExpected: "application/javascript",
243
- ressourceSpecifier: getUniqueNameForInlineHtmlNode(
244
- script,
245
- scripts,
246
- `${urlToBasename(htmlRessource.url)}.[id].js`,
247
- ),
246
+ contentTypeExpected: "application/javascript",
247
+ ressourceSpecifier,
248
248
  ...referenceLocationFromHtmlNode(script),
249
249
  contentType: "application/javascript",
250
250
  bufferBeforeBuild: Buffer.from(textNode.value),
@@ -270,7 +270,7 @@ const moduleScriptSrcVisitor = (script, { format, notifyReferenceFound }) => {
270
270
  }
271
271
 
272
272
  const remoteScriptReference = notifyReferenceFound({
273
- ressourceContentTypeExpected: "application/javascript",
273
+ contentTypeExpected: "application/javascript",
274
274
  ressourceSpecifier: srcAttribute.value,
275
275
  ...referenceLocationFromHtmlNode(script, "src"),
276
276
  isJsModule: true,
@@ -334,13 +334,14 @@ const moduleScriptTextNodeVisitor = (
334
334
  return null
335
335
  }
336
336
 
337
+ const ressourceSpecifier = getUniqueNameForInlineHtmlNode(
338
+ script,
339
+ scripts,
340
+ `${urlToFilename(htmlRessource.url)}__inline__[id].js`,
341
+ )
337
342
  const jsReference = notifyReferenceFound({
338
- ressourceContentTypeExpected: "application/javascript",
339
- ressourceSpecifier: getUniqueNameForInlineHtmlNode(
340
- script,
341
- scripts,
342
- `${urlToBasename(htmlRessource.url)}.[id].js`,
343
- ),
343
+ contentTypeExpected: "application/javascript",
344
+ ressourceSpecifier,
344
345
  ...referenceLocationFromHtmlNode(script),
345
346
  contentType: "application/javascript",
346
347
  bufferBeforeBuild: textNode.value,
@@ -352,7 +353,10 @@ const moduleScriptTextNodeVisitor = (
352
353
  removeHtmlNodeAttribute(script, typeAttribute)
353
354
  }
354
355
  const { bufferAfterBuild } = jsReference.ressource
355
- textNode.value = bufferAfterBuild
356
+ const jsText = String(bufferAfterBuild)
357
+ // ici on voudrait pouvoir ajouter le commentaire de la sourcemap
358
+ // sauf que cela se produit un poil plus tard je crois?
359
+ textNode.value = jsText
356
360
  }
357
361
  }
358
362
 
@@ -373,7 +377,7 @@ const importmapScriptSrcVisitor = (
373
377
  }
374
378
 
375
379
  const importmapReference = notifyReferenceFound({
376
- ressourceContentTypeExpected: "application/importmap+json",
380
+ contentTypeExpected: "application/importmap+json",
377
381
  ressourceSpecifier: srcAttribute.value,
378
382
  ...referenceLocationFromHtmlNode(script, "src"),
379
383
  // here we want to force the fileName for the importmap
@@ -460,11 +464,11 @@ const importmapScriptTextNodeVisitor = (
460
464
  }
461
465
 
462
466
  const importmapReference = notifyReferenceFound({
463
- ressourceContentTypeExpected: "application/importmap+json",
467
+ contentTypeExpected: "application/importmap+json",
464
468
  ressourceSpecifier: getUniqueNameForInlineHtmlNode(
465
469
  script,
466
470
  scripts,
467
- `${urlToBasename(htmlRessource.url)}.[id].importmap`,
471
+ `${urlToFilename(htmlRessource.url)}__inline__[id].importmap`,
468
472
  ),
469
473
  ...referenceLocationFromHtmlNode(script),
470
474
 
@@ -500,7 +504,7 @@ const linkStylesheetHrefVisitor = (
500
504
  }
501
505
 
502
506
  const cssReference = notifyReferenceFound({
503
- ressourceContentTypeExpected: "text/css",
507
+ contentTypeExpected: "text/css",
504
508
  ressourceSpecifier: hrefAttribute.value,
505
509
  ...referenceLocationFromHtmlNode(link, "href"),
506
510
  })
@@ -555,26 +559,25 @@ const linkHrefVisitor = (
555
559
  "modulepreload",
556
560
  ].includes(rel)
557
561
 
558
- let ressourceContentTypeExpected
562
+ let contentTypeExpected
559
563
  const typeAttribute = getHtmlNodeAttributeByName(link, "type")
560
564
  const type = typeAttribute ? typeAttribute.value : ""
561
565
  let isJsModule = false
562
566
  if (type) {
563
- ressourceContentTypeExpected = type
567
+ contentTypeExpected = type
564
568
  } else if (rel === "manifest") {
565
- ressourceContentTypeExpected = "application/manifest+json"
569
+ contentTypeExpected = "application/manifest+json"
566
570
  } else if (rel === "modulepreload") {
567
- ressourceContentTypeExpected = "application/javascript"
571
+ contentTypeExpected = "application/javascript"
568
572
  isJsModule = true
569
573
  }
570
574
 
571
575
  const linkReference = notifyReferenceFound({
572
576
  isRessourceHint,
573
- ressourceContentTypeExpected,
577
+ contentTypeExpected,
574
578
  ressourceSpecifier: hrefAttribute.value,
575
579
  ...referenceLocationFromHtmlNode(link, "href"),
576
- urlVersioningDisabled:
577
- ressourceContentTypeExpected === "application/manifest+json",
580
+ urlVersioningDisabled: contentTypeExpected === "application/manifest+json",
578
581
  isJsModule,
579
582
  })
580
583
  return ({ getUrlRelativeToImporter }) => {
@@ -636,11 +639,11 @@ const styleTextNodeVisitor = (
636
639
  }
637
640
 
638
641
  const inlineStyleReference = notifyReferenceFound({
639
- ressourceContentTypeExpected: "text/css",
642
+ contentTypeExpected: "text/css",
640
643
  ressourceSpecifier: getUniqueNameForInlineHtmlNode(
641
644
  style,
642
645
  styles,
643
- `${urlToBasename(htmlRessource.url)}.[id].css`,
646
+ `${urlToFilename(htmlRessource.url)}__inline__[id].css`,
644
647
  ),
645
648
  ...referenceLocationFromHtmlNode(style),
646
649
 
@@ -714,9 +717,7 @@ const sourceSrcVisitor = (source, { notifyReferenceFound }) => {
714
717
 
715
718
  const typeAttribute = getHtmlNodeAttributeByName(source, "type")
716
719
  const srcReference = notifyReferenceFound({
717
- ressourceContentTypeExpected: typeAttribute
718
- ? typeAttribute.value
719
- : undefined,
720
+ contentTypeExpected: typeAttribute ? typeAttribute.value : undefined,
720
721
  ressourceSpecifier: srcAttribute.value,
721
722
  ...referenceLocationFromHtmlNode(source, "src"),
722
723
  })