@jsenv/core 23.7.1 → 23.8.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 (34) hide show
  1. package/dist/jsenv_browser_system.js.map +1 -1
  2. package/dist/jsenv_compile_proxy.js.map +1 -1
  3. package/dist/jsenv_exploring_redirector.js.map +1 -1
  4. package/dist/jsenv_toolbar.js +0 -2
  5. package/dist/jsenv_toolbar.js.map +3 -3
  6. package/package.json +2 -2
  7. package/src/buildProject.js +300 -300
  8. package/src/execute.js +184 -184
  9. package/src/internal/browser-launcher/jsenv-browser-system.js +199 -199
  10. package/src/internal/compiling/babel_plugin_import_assertions.js +121 -100
  11. package/src/internal/compiling/babel_plugin_import_metadata.js +22 -0
  12. package/src/internal/compiling/babel_plugin_import_visitor.js +84 -0
  13. package/src/internal/compiling/compile-directory/getOrGenerateCompiledFile.js +268 -265
  14. package/src/internal/compiling/compile-directory/updateMeta.js +154 -150
  15. package/src/internal/compiling/compile-directory/validateCache.js +265 -265
  16. package/src/internal/compiling/compileFile.js +215 -194
  17. package/src/internal/compiling/compileHtml.js +550 -494
  18. package/src/internal/compiling/createCompiledFileService.js +291 -290
  19. package/src/internal/compiling/html_source_file_service.js +403 -379
  20. package/src/internal/compiling/js-compilation-service/jsenvTransform.js +270 -269
  21. package/src/internal/compiling/jsenvCompilerForHtml.js +300 -293
  22. package/src/internal/compiling/startCompileServer.js +1048 -1052
  23. package/src/internal/compiling/transformResultToCompilationResult.js +220 -217
  24. package/src/internal/executing/executePlan.js +183 -183
  25. package/src/internal/executing/launchAndExecute.js +458 -458
  26. package/src/internal/runtime/createBrowserRuntime/scanBrowserRuntimeFeatures.js +246 -246
  27. package/src/internal/runtime/createNodeRuntime/scanNodeRuntimeFeatures.js +112 -112
  28. package/src/internal/toolbar/toolbar.main.css +196 -188
  29. package/src/internal/toolbar/toolbar.main.js +227 -228
  30. package/src/internal/url_conversion.js +317 -317
  31. package/src/startExploring.js +309 -309
  32. package/src/internal/compiling/babel_plugin_transform_import_specifier.js +0 -86
  33. package/src/internal/toolbar/animation/animation.css +0 -5
  34. package/src/internal/toolbar/variant/variant.css +0 -3
@@ -1,246 +1,246 @@
1
- import { fetchJson } from "../../browser-utils/fetchJson.js"
2
- import { computeCompileIdFromGroupId } from "../computeCompileIdFromGroupId.js"
3
- import { detectBrowser } from "../detectBrowser/detectBrowser.js"
4
- import { resolveGroup } from "../resolveGroup.js"
5
-
6
- export const scanBrowserRuntimeFeatures = async ({
7
- coverageHandledFromOutside = false,
8
- failFastOnFeatureDetection = false,
9
- } = {}) => {
10
- const {
11
- outDirectoryRelativeUrl,
12
- inlineImportMapIntoHTML,
13
- customCompilerPatterns,
14
- compileServerGroupMap,
15
- } = await fetchJson("/.jsenv/__compile_server_meta__.json")
16
-
17
- const browser = detectBrowser()
18
- const compileId = computeCompileIdFromGroupId({
19
- groupId: resolveGroup(browser, compileServerGroupMap),
20
- groupMap: compileServerGroupMap,
21
- })
22
- const groupInfo = compileServerGroupMap[compileId]
23
-
24
- const featuresReport = {
25
- importmap: undefined,
26
- dynamicImport: undefined,
27
- topLevelAwait: undefined,
28
- jsonImportAssertions: undefined,
29
- cssImportAssertions: undefined,
30
- newStylesheet: undefined,
31
- }
32
- await detectSupportedFeatures({
33
- featuresReport,
34
- failFastOnFeatureDetection,
35
- inlineImportMapIntoHTML,
36
- })
37
- const pluginRequiredNameArray = await pluginRequiredNamesFromGroupInfo(
38
- groupInfo,
39
- {
40
- featuresReport,
41
- coverageHandledFromOutside,
42
- },
43
- )
44
-
45
- const canAvoidCompilation =
46
- customCompilerPatterns.length === 0 &&
47
- pluginRequiredNameArray.length === 0 &&
48
- featuresReport.importmap &&
49
- featuresReport.dynamicImport &&
50
- featuresReport.topLevelAwait
51
-
52
- return {
53
- canAvoidCompilation,
54
- featuresReport,
55
- customCompilerPatterns,
56
- pluginRequiredNameArray,
57
- inlineImportMapIntoHTML,
58
- outDirectoryRelativeUrl,
59
- compileId,
60
- browser,
61
- }
62
- }
63
-
64
- const detectSupportedFeatures = async ({
65
- featuresReport,
66
- failFastOnFeatureDetection,
67
- inlineImportMapIntoHTML,
68
- }) => {
69
- // start testing importmap support first and not in paralell
70
- // so that there is not module script loaded beore importmap is injected
71
- // it would log an error in chrome console and return undefined
72
- const importmap = await supportsImportmap({
73
- // chrome supports inline but not remote importmap
74
- // https://github.com/WICG/import-maps/issues/235
75
-
76
- // at this stage we won't know if the html file will use
77
- // an importmap or not and if that importmap is inline or specified with an src
78
- // so we should test if browser support local and remote importmap.
79
- // But there exploring server can inline importmap by transforming html
80
- // and in that case we can test only the local importmap support
81
- // so we test importmap support and the remote one
82
- remote: !inlineImportMapIntoHTML,
83
- })
84
- featuresReport.importmap = importmap
85
- if (!importmap && failFastOnFeatureDetection) {
86
- return
87
- }
88
-
89
- const dynamicImport = await supportsDynamicImport()
90
- featuresReport.dynamicImport = dynamicImport
91
- if (!dynamicImport && failFastOnFeatureDetection) {
92
- return
93
- }
94
-
95
- const topLevelAwait = await supportsTopLevelAwait()
96
- featuresReport.topLevelAwait = topLevelAwait
97
- if (!topLevelAwait && failFastOnFeatureDetection) {
98
- return
99
- }
100
- }
101
-
102
- const pluginRequiredNamesFromGroupInfo = async (
103
- groupInfo,
104
- { featuresReport, coverageHandledFromOutside },
105
- ) => {
106
- const { pluginRequiredNameArray } = groupInfo
107
- const requiredPluginNames = pluginRequiredNameArray.slice()
108
- const markPluginAsSupported = (name) => {
109
- const index = requiredPluginNames.indexOf(name)
110
- if (index > -1) {
111
- requiredPluginNames.splice(index, 1)
112
- }
113
- }
114
-
115
- // When instrumentation CAN be handed by playwright
116
- // https://playwright.dev/docs/api/class-chromiumcoverage#chromiumcoveragestartjscoverageoptions
117
- // coverageHandledFromOutside is true and "transform-instrument" becomes non mandatory
118
- if (coverageHandledFromOutside) {
119
- markPluginAsSupported("transform-instrument")
120
- }
121
-
122
- if (pluginRequiredNameArray.includes("transform-import-assertions")) {
123
- const jsonImportAssertions = await supportsJsonImportAssertions()
124
- featuresReport.jsonImportAssertions = jsonImportAssertions
125
-
126
- const cssImportAssertions = await supportsCssImportAssertions()
127
- featuresReport.cssImportAssertions = cssImportAssertions
128
-
129
- if (jsonImportAssertions && cssImportAssertions) {
130
- markPluginAsSupported("transform-import-assertions")
131
- }
132
- }
133
-
134
- if (pluginRequiredNameArray.includes("new-stylesheet-as-jsenv-import")) {
135
- const newStylesheet = supportsNewStylesheet()
136
- featuresReport.newStylesheet = newStylesheet
137
- markPluginAsSupported("new-stylesheet-as-jsenv-import")
138
- }
139
-
140
- return requiredPluginNames
141
- }
142
-
143
- const supportsNewStylesheet = () => {
144
- try {
145
- // eslint-disable-next-line no-new
146
- new CSSStyleSheet()
147
- return true
148
- } catch (e) {
149
- return false
150
- }
151
- }
152
-
153
- const supportsImportmap = async ({ remote = true } = {}) => {
154
- const specifier = asBase64Url(`export default false`)
155
-
156
- const importMap = {
157
- imports: {
158
- [specifier]: asBase64Url(`export default true`),
159
- },
160
- }
161
-
162
- const importmapScript = document.createElement("script")
163
- const importmapString = JSON.stringify(importMap, null, " ")
164
- importmapScript.type = "importmap"
165
- if (remote) {
166
- importmapScript.src = `data:application/json;base64,${window.btoa(
167
- importmapString,
168
- )}`
169
- } else {
170
- importmapScript.textContent = importmapString
171
- }
172
-
173
- document.body.appendChild(importmapScript)
174
-
175
- const scriptModule = document.createElement("script")
176
- scriptModule.type = "module"
177
- scriptModule.src = asBase64Url(
178
- `import supported from "${specifier}"; window.__importmap_supported = supported`,
179
- )
180
-
181
- return new Promise((resolve, reject) => {
182
- scriptModule.onload = () => {
183
- const supported = window.__importmap_supported
184
- delete window.__importmap_supported
185
- document.body.removeChild(scriptModule)
186
- document.body.removeChild(importmapScript)
187
- resolve(supported)
188
- }
189
- scriptModule.onerror = () => {
190
- document.body.removeChild(scriptModule)
191
- document.body.removeChild(importmapScript)
192
- reject()
193
- }
194
- document.body.appendChild(scriptModule)
195
- })
196
- }
197
-
198
- const supportsDynamicImport = async () => {
199
- const moduleSource = asBase64Url(`export default 42`)
200
- try {
201
- const namespace = await import(moduleSource)
202
- return namespace.default === 42
203
- } catch (e) {
204
- return false
205
- }
206
- }
207
-
208
- const supportsTopLevelAwait = async () => {
209
- const moduleSource = asBase64Url(`export default await Promise.resolve(42)`)
210
- try {
211
- const namespace = await import(moduleSource)
212
- return namespace.default === 42
213
- } catch (e) {
214
- return false
215
- }
216
- }
217
-
218
- const supportsJsonImportAssertions = async () => {
219
- const jsonBase64Url = asBase64Url("42", "application/json")
220
- const moduleSource = asBase64Url(
221
- `export { default } from "${jsonBase64Url}" assert { type: "json" }`,
222
- )
223
- try {
224
- const namespace = await import(moduleSource)
225
- return namespace.default === 42
226
- } catch (e) {
227
- return false
228
- }
229
- }
230
-
231
- const supportsCssImportAssertions = async () => {
232
- const cssBase64Url = asBase64Url("p { color: red; }", "text/css")
233
- const moduleSource = asBase64Url(
234
- `export { default } from "${cssBase64Url}" assert { type: "css" }`,
235
- )
236
- try {
237
- const namespace = await import(moduleSource)
238
- return namespace.default instanceof CSSStyleSheet
239
- } catch (e) {
240
- return false
241
- }
242
- }
243
-
244
- const asBase64Url = (text, mimeType = "application/javascript") => {
245
- return `data:${mimeType};base64,${window.btoa(text)}`
246
- }
1
+ import { fetchJson } from "../../browser-utils/fetchJson.js"
2
+ import { computeCompileIdFromGroupId } from "../computeCompileIdFromGroupId.js"
3
+ import { detectBrowser } from "../detectBrowser/detectBrowser.js"
4
+ import { resolveGroup } from "../resolveGroup.js"
5
+
6
+ export const scanBrowserRuntimeFeatures = async ({
7
+ coverageHandledFromOutside = false,
8
+ failFastOnFeatureDetection = false,
9
+ } = {}) => {
10
+ const {
11
+ outDirectoryRelativeUrl,
12
+ inlineImportMapIntoHTML,
13
+ customCompilerPatterns,
14
+ compileServerGroupMap,
15
+ } = await fetchJson("/.jsenv/__compile_server_meta__.json")
16
+
17
+ const browser = detectBrowser()
18
+ const compileId = computeCompileIdFromGroupId({
19
+ groupId: resolveGroup(browser, compileServerGroupMap),
20
+ groupMap: compileServerGroupMap,
21
+ })
22
+ const groupInfo = compileServerGroupMap[compileId]
23
+
24
+ const featuresReport = {
25
+ importmap: undefined,
26
+ dynamicImport: undefined,
27
+ topLevelAwait: undefined,
28
+ jsonImportAssertions: undefined,
29
+ cssImportAssertions: undefined,
30
+ newStylesheet: undefined,
31
+ }
32
+ await detectSupportedFeatures({
33
+ featuresReport,
34
+ failFastOnFeatureDetection,
35
+ inlineImportMapIntoHTML,
36
+ })
37
+ const pluginRequiredNameArray = await pluginRequiredNamesFromGroupInfo(
38
+ groupInfo,
39
+ {
40
+ featuresReport,
41
+ coverageHandledFromOutside,
42
+ },
43
+ )
44
+
45
+ const canAvoidCompilation =
46
+ customCompilerPatterns.length === 0 &&
47
+ pluginRequiredNameArray.length === 0 &&
48
+ featuresReport.importmap &&
49
+ featuresReport.dynamicImport &&
50
+ featuresReport.topLevelAwait
51
+
52
+ return {
53
+ canAvoidCompilation,
54
+ featuresReport,
55
+ customCompilerPatterns,
56
+ pluginRequiredNameArray,
57
+ inlineImportMapIntoHTML,
58
+ outDirectoryRelativeUrl,
59
+ compileId,
60
+ browser,
61
+ }
62
+ }
63
+
64
+ const detectSupportedFeatures = async ({
65
+ featuresReport,
66
+ failFastOnFeatureDetection,
67
+ inlineImportMapIntoHTML,
68
+ }) => {
69
+ // start testing importmap support first and not in paralell
70
+ // so that there is not module script loaded beore importmap is injected
71
+ // it would log an error in chrome console and return undefined
72
+ const importmap = await supportsImportmap({
73
+ // chrome supports inline but not remote importmap
74
+ // https://github.com/WICG/import-maps/issues/235
75
+
76
+ // at this stage we won't know if the html file will use
77
+ // an importmap or not and if that importmap is inline or specified with an src
78
+ // so we should test if browser support local and remote importmap.
79
+ // But there exploring server can inline importmap by transforming html
80
+ // and in that case we can test only the local importmap support
81
+ // so we test importmap support and the remote one
82
+ remote: !inlineImportMapIntoHTML,
83
+ })
84
+ featuresReport.importmap = importmap
85
+ if (!importmap && failFastOnFeatureDetection) {
86
+ return
87
+ }
88
+
89
+ const dynamicImport = await supportsDynamicImport()
90
+ featuresReport.dynamicImport = dynamicImport
91
+ if (!dynamicImport && failFastOnFeatureDetection) {
92
+ return
93
+ }
94
+
95
+ const topLevelAwait = await supportsTopLevelAwait()
96
+ featuresReport.topLevelAwait = topLevelAwait
97
+ if (!topLevelAwait && failFastOnFeatureDetection) {
98
+ return
99
+ }
100
+ }
101
+
102
+ const pluginRequiredNamesFromGroupInfo = async (
103
+ groupInfo,
104
+ { featuresReport, coverageHandledFromOutside },
105
+ ) => {
106
+ const { pluginRequiredNameArray } = groupInfo
107
+ const requiredPluginNames = pluginRequiredNameArray.slice()
108
+ const markPluginAsSupported = (name) => {
109
+ const index = requiredPluginNames.indexOf(name)
110
+ if (index > -1) {
111
+ requiredPluginNames.splice(index, 1)
112
+ }
113
+ }
114
+
115
+ // When instrumentation CAN be handed by playwright
116
+ // https://playwright.dev/docs/api/class-chromiumcoverage#chromiumcoveragestartjscoverageoptions
117
+ // coverageHandledFromOutside is true and "transform-instrument" becomes non mandatory
118
+ if (coverageHandledFromOutside) {
119
+ markPluginAsSupported("transform-instrument")
120
+ }
121
+
122
+ if (pluginRequiredNameArray.includes("transform-import-assertions")) {
123
+ const jsonImportAssertions = await supportsJsonImportAssertions()
124
+ featuresReport.jsonImportAssertions = jsonImportAssertions
125
+
126
+ const cssImportAssertions = await supportsCssImportAssertions()
127
+ featuresReport.cssImportAssertions = cssImportAssertions
128
+
129
+ if (jsonImportAssertions && cssImportAssertions) {
130
+ markPluginAsSupported("transform-import-assertions")
131
+ }
132
+ }
133
+
134
+ if (pluginRequiredNameArray.includes("new-stylesheet-as-jsenv-import")) {
135
+ const newStylesheet = supportsNewStylesheet()
136
+ featuresReport.newStylesheet = newStylesheet
137
+ markPluginAsSupported("new-stylesheet-as-jsenv-import")
138
+ }
139
+
140
+ return requiredPluginNames
141
+ }
142
+
143
+ const supportsNewStylesheet = () => {
144
+ try {
145
+ // eslint-disable-next-line no-new
146
+ new CSSStyleSheet()
147
+ return true
148
+ } catch (e) {
149
+ return false
150
+ }
151
+ }
152
+
153
+ const supportsImportmap = async ({ remote = true } = {}) => {
154
+ const specifier = asBase64Url(`export default false`)
155
+
156
+ const importMap = {
157
+ imports: {
158
+ [specifier]: asBase64Url(`export default true`),
159
+ },
160
+ }
161
+
162
+ const importmapScript = document.createElement("script")
163
+ const importmapString = JSON.stringify(importMap, null, " ")
164
+ importmapScript.type = "importmap"
165
+ if (remote) {
166
+ importmapScript.src = `data:application/json;base64,${window.btoa(
167
+ importmapString,
168
+ )}`
169
+ } else {
170
+ importmapScript.textContent = importmapString
171
+ }
172
+
173
+ document.body.appendChild(importmapScript)
174
+
175
+ const scriptModule = document.createElement("script")
176
+ scriptModule.type = "module"
177
+ scriptModule.src = asBase64Url(
178
+ `import supported from "${specifier}"; window.__importmap_supported = supported`,
179
+ )
180
+
181
+ return new Promise((resolve, reject) => {
182
+ scriptModule.onload = () => {
183
+ const supported = window.__importmap_supported
184
+ delete window.__importmap_supported
185
+ document.body.removeChild(scriptModule)
186
+ document.body.removeChild(importmapScript)
187
+ resolve(supported)
188
+ }
189
+ scriptModule.onerror = () => {
190
+ document.body.removeChild(scriptModule)
191
+ document.body.removeChild(importmapScript)
192
+ reject()
193
+ }
194
+ document.body.appendChild(scriptModule)
195
+ })
196
+ }
197
+
198
+ const supportsDynamicImport = async () => {
199
+ const moduleSource = asBase64Url(`export default 42`)
200
+ try {
201
+ const namespace = await import(moduleSource)
202
+ return namespace.default === 42
203
+ } catch (e) {
204
+ return false
205
+ }
206
+ }
207
+
208
+ const supportsTopLevelAwait = async () => {
209
+ const moduleSource = asBase64Url(`export default await Promise.resolve(42)`)
210
+ try {
211
+ const namespace = await import(moduleSource)
212
+ return namespace.default === 42
213
+ } catch (e) {
214
+ return false
215
+ }
216
+ }
217
+
218
+ const supportsJsonImportAssertions = async () => {
219
+ const jsonBase64Url = asBase64Url("42", "application/json")
220
+ const moduleSource = asBase64Url(
221
+ `export { default } from "${jsonBase64Url}" assert { type: "json" }`,
222
+ )
223
+ try {
224
+ const namespace = await import(moduleSource)
225
+ return namespace.default === 42
226
+ } catch (e) {
227
+ return false
228
+ }
229
+ }
230
+
231
+ const supportsCssImportAssertions = async () => {
232
+ const cssBase64Url = asBase64Url("p { color: red; }", "text/css")
233
+ const moduleSource = asBase64Url(
234
+ `export { default } from "${cssBase64Url}" assert { type: "css" }`,
235
+ )
236
+ try {
237
+ const namespace = await import(moduleSource)
238
+ return namespace.default instanceof CSSStyleSheet
239
+ } catch (e) {
240
+ return false
241
+ }
242
+ }
243
+
244
+ const asBase64Url = (text, mimeType = "application/javascript") => {
245
+ return `data:${mimeType};base64,${window.btoa(text)}`
246
+ }