@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,290 +1,291 @@
1
- import { nextService, fetchFileSystem } from "@jsenv/server"
2
- import {
3
- resolveUrl,
4
- resolveDirectoryUrl,
5
- normalizeStructuredMetaMap,
6
- urlToMeta,
7
- } from "@jsenv/filesystem"
8
-
9
- import { serverUrlToCompileInfo } from "@jsenv/core/src/internal/url_conversion.js"
10
- import { setUrlExtension } from "../url_utils.js"
11
- import {
12
- COMPILE_ID_BUILD_GLOBAL,
13
- COMPILE_ID_BUILD_GLOBAL_FILES,
14
- COMPILE_ID_BUILD_COMMONJS,
15
- COMPILE_ID_BUILD_COMMONJS_FILES,
16
- } from "../CONSTANTS.js"
17
- import { compileFile } from "./compileFile.js"
18
- import { compileHtml } from "./jsenvCompilerForHtml.js"
19
- import { compileImportmap } from "./jsenvCompilerForImportmap.js"
20
- import { compileJavascript } from "./jsenvCompilerForJavaScript.js"
21
-
22
- const jsenvCompilers = {
23
- "**/*.js": compileJavascript,
24
- "**/*.jsx": compileJavascript,
25
- "**/*.ts": compileJavascript,
26
- "**/*.tsx": compileJavascript,
27
- "**/*.mjs": compileJavascript,
28
-
29
- "**/*.html": compileHtml,
30
- "**/*.importmap": compileImportmap,
31
- }
32
-
33
- export const createCompiledFileService = ({
34
- compileServerOperation,
35
- logger,
36
-
37
- projectDirectoryUrl,
38
- outDirectoryRelativeUrl,
39
-
40
- runtimeSupport,
41
- transformTopLevelAwait,
42
- moduleOutFormat,
43
- importMetaFormat,
44
- babelPluginMap,
45
- groupMap,
46
- customCompilers,
47
-
48
- jsenvToolbarInjection,
49
-
50
- projectFileRequestedCallback,
51
- compileCacheStrategy,
52
- sourcemapMethod,
53
- sourcemapExcludeSources,
54
- }) => {
55
- Object.keys(customCompilers).forEach((key) => {
56
- const value = customCompilers[key]
57
- if (typeof value !== "function") {
58
- throw new TypeError(
59
- `Compiler must be a function, found ${value} for "${key}"`,
60
- )
61
- }
62
- })
63
- const compileMeta = normalizeStructuredMetaMap(
64
- {
65
- jsenvCompiler: jsenvCompilers,
66
- customCompiler: customCompilers,
67
- },
68
- projectDirectoryUrl,
69
- )
70
-
71
- return (request) => {
72
- const { origin, ressource } = request
73
- // we use "ressourceToPathname" to remove eventual query param from the url
74
- // Without this a pattern like "**/*.js" would not match "file.js?t=1"
75
- // This would result in file not being compiled when they should
76
- const requestUrl = `${origin}${ressourceToPathname(ressource)}`
77
-
78
- const requestCompileInfo = serverUrlToCompileInfo(requestUrl, {
79
- outDirectoryRelativeUrl,
80
- compileServerOrigin: origin,
81
- })
82
-
83
- // not inside compile directory -> nothing to compile
84
- if (!requestCompileInfo.insideCompileDirectory) {
85
- return null
86
- }
87
-
88
- const { compileId, afterCompileId } = requestCompileInfo
89
- // serve files inside /.jsenv/* directly without compilation
90
- // this is just to allow some files to be written inside outDirectory and read directly
91
- // if asked by the client (such __compile_server_meta__.json)
92
- if (!compileId) {
93
- return fetchFileSystem(
94
- new URL(request.ressource.slice(1), projectDirectoryUrl),
95
- {
96
- headers: request.headers,
97
- etagEnabled: true,
98
- },
99
- )
100
- }
101
-
102
- const allowedCompileIds = [
103
- ...Object.keys(groupMap),
104
- COMPILE_ID_BUILD_GLOBAL,
105
- COMPILE_ID_BUILD_GLOBAL_FILES,
106
- COMPILE_ID_BUILD_COMMONJS,
107
- COMPILE_ID_BUILD_COMMONJS_FILES,
108
- ]
109
- if (!allowedCompileIds.includes(compileId)) {
110
- return {
111
- status: 400,
112
- statusText: `compileId must be one of ${allowedCompileIds}, received ${compileId}`,
113
- }
114
- }
115
-
116
- // nothing after compileId, we don't know what to compile (not supposed to happen)
117
- if (afterCompileId === "") {
118
- return null
119
- }
120
-
121
- const originalFileRelativeUrl = afterCompileId
122
- projectFileRequestedCallback(originalFileRelativeUrl, request)
123
-
124
- const originalFileUrl = `${projectDirectoryUrl}${originalFileRelativeUrl}`
125
- const compileDirectoryRelativeUrl = `${outDirectoryRelativeUrl}${compileId}/`
126
- const compileDirectoryUrl = resolveDirectoryUrl(
127
- compileDirectoryRelativeUrl,
128
- projectDirectoryUrl,
129
- )
130
- const compiledFileUrl = resolveUrl(
131
- originalFileRelativeUrl,
132
- compileDirectoryUrl,
133
- )
134
-
135
- const compiler = getCompiler({ originalFileUrl, compileMeta })
136
- // no compiler -> serve original file
137
- // we don't redirect otherwise it complexify ressource tracking
138
- // and url resolution
139
- if (!compiler) {
140
- return nextService({
141
- ...request,
142
- ressource: `/${originalFileRelativeUrl}`,
143
- })
144
- }
145
-
146
- // compile this if needed
147
- const compileResponsePromise = compileFile({
148
- compileServerOperation,
149
- logger,
150
-
151
- projectDirectoryUrl,
152
- originalFileUrl,
153
- compiledFileUrl,
154
-
155
- compileCacheStrategy,
156
- projectFileRequestedCallback,
157
- request,
158
- compile: ({ code, map }) => {
159
- return compiler({
160
- logger,
161
-
162
- code,
163
- map,
164
- url: originalFileUrl,
165
- compiledUrl: compiledFileUrl,
166
- projectDirectoryUrl,
167
- compileServerOrigin: request.origin,
168
- outDirectoryRelativeUrl,
169
- compileId,
170
- request,
171
-
172
- runtimeSupport,
173
- moduleOutFormat,
174
- importMetaFormat,
175
- transformTopLevelAwait,
176
- babelPluginMap: babelPluginMapFromCompileId(compileId, {
177
- babelPluginMap,
178
- groupMap,
179
- }),
180
-
181
- sourcemapMethod,
182
- sourcemapExcludeSources,
183
- jsenvToolbarInjection,
184
- })
185
- },
186
- })
187
- return compileResponsePromise
188
- }
189
- }
190
-
191
- const getCompiler = ({ originalFileUrl, compileMeta }) => {
192
- const { jsenvCompiler, customCompiler } = urlToMeta({
193
- url: originalFileUrl,
194
- structuredMetaMap: compileMeta,
195
- })
196
-
197
- if (!jsenvCompiler && !customCompiler) {
198
- return null
199
- }
200
-
201
- // there is only a jsenvCompiler
202
- if (jsenvCompiler && !customCompiler) {
203
- return jsenvCompiler
204
- }
205
-
206
- // there is a custom compiler and potentially a jsenv compiler
207
- return async (params) => {
208
- // do custom compilation first
209
- const customResult = await customCompiler(params)
210
- // then check if jsenv compiler should apply
211
- // to the new result contentType
212
- const jsenvCompilerAfterCustomCompilation =
213
- getJsenvCompilerAfterCustomCompilation({
214
- url: originalFileUrl,
215
- contentType: customResult.contentType,
216
- compileMeta,
217
- })
218
- if (!jsenvCompilerAfterCustomCompilation) {
219
- return customResult
220
- }
221
- const jsenvResult = await jsenvCompilerAfterCustomCompilation({
222
- ...params,
223
- code: customResult.compiledSource,
224
- map: customResult.sourcemap,
225
- })
226
- return {
227
- ...customResult,
228
- ...jsenvResult,
229
- }
230
- }
231
- }
232
-
233
- const getJsenvCompilerAfterCustomCompilation = ({
234
- url,
235
- contentType,
236
- compileMeta,
237
- }) => {
238
- const extensionToForce = contentTypeExtensions[contentType]
239
- const urlForcingExtension = extensionToForce
240
- ? setUrlExtension(url, extensionToForce)
241
- : url
242
- const { jsenvCompiler } = urlToMeta({
243
- url: urlForcingExtension,
244
- structuredMetaMap: compileMeta,
245
- })
246
- return jsenvCompiler
247
- }
248
-
249
- // should match contentType where there is a jsenv compiler
250
- // back to an extension
251
- const contentTypeExtensions = {
252
- "application/javascript": ".js",
253
- "text/html": ".html",
254
- "application/importmap+json": ".importmap",
255
- // "text/css": ".css",
256
- }
257
-
258
- const babelPluginMapFromCompileId = (
259
- compileId,
260
- { babelPluginMap, groupMap },
261
- ) => {
262
- const babelPluginMapForGroup = {}
263
-
264
- groupMap[compileId].pluginRequiredNameArray.forEach((requiredPluginName) => {
265
- const babelPlugin = babelPluginMap[requiredPluginName]
266
- if (babelPlugin) {
267
- babelPluginMapForGroup[requiredPluginName] = babelPlugin
268
- }
269
- })
270
-
271
- Object.keys(babelPluginMap).forEach((key) => {
272
- if (key.startsWith("syntax-")) {
273
- babelPluginMapForGroup[key] = babelPluginMap[key]
274
- }
275
- if (key === "transform-replace-expressions") {
276
- babelPluginMapForGroup[key] = babelPluginMap[key]
277
- }
278
- })
279
-
280
- return babelPluginMapForGroup
281
- }
282
-
283
- const ressourceToPathname = (ressource) => {
284
- const searchSeparatorIndex = ressource.indexOf("?")
285
- const pathname =
286
- searchSeparatorIndex === -1
287
- ? ressource
288
- : ressource.slice(0, searchSeparatorIndex)
289
- return pathname
290
- }
1
+ import { nextService, fetchFileSystem } from "@jsenv/server"
2
+ import {
3
+ resolveUrl,
4
+ resolveDirectoryUrl,
5
+ normalizeStructuredMetaMap,
6
+ urlToMeta,
7
+ } from "@jsenv/filesystem"
8
+
9
+ import { serverUrlToCompileInfo } from "@jsenv/core/src/internal/url_conversion.js"
10
+ import { setUrlExtension } from "../url_utils.js"
11
+ import {
12
+ COMPILE_ID_BUILD_GLOBAL,
13
+ COMPILE_ID_BUILD_GLOBAL_FILES,
14
+ COMPILE_ID_BUILD_COMMONJS,
15
+ COMPILE_ID_BUILD_COMMONJS_FILES,
16
+ } from "../CONSTANTS.js"
17
+ import { compileFile } from "./compileFile.js"
18
+ import { compileHtml } from "./jsenvCompilerForHtml.js"
19
+ import { compileImportmap } from "./jsenvCompilerForImportmap.js"
20
+ import { compileJavascript } from "./jsenvCompilerForJavaScript.js"
21
+
22
+ const jsenvCompilers = {
23
+ "**/*.js": compileJavascript,
24
+ "**/*.jsx": compileJavascript,
25
+ "**/*.ts": compileJavascript,
26
+ "**/*.tsx": compileJavascript,
27
+ "**/*.mjs": compileJavascript,
28
+
29
+ "**/*.html": compileHtml,
30
+ "**/*.importmap": compileImportmap,
31
+ }
32
+
33
+ export const createCompiledFileService = ({
34
+ compileServerOperation,
35
+ logger,
36
+
37
+ projectDirectoryUrl,
38
+ outDirectoryRelativeUrl,
39
+
40
+ runtimeSupport,
41
+ transformTopLevelAwait,
42
+ moduleOutFormat,
43
+ importMetaFormat,
44
+ babelPluginMap,
45
+ groupMap,
46
+ customCompilers,
47
+
48
+ jsenvToolbarInjection,
49
+
50
+ projectFileRequestedCallback,
51
+ compileCacheStrategy,
52
+ sourcemapMethod,
53
+ sourcemapExcludeSources,
54
+ }) => {
55
+ Object.keys(customCompilers).forEach((key) => {
56
+ const value = customCompilers[key]
57
+ if (typeof value !== "function") {
58
+ throw new TypeError(
59
+ `Compiler must be a function, found ${value} for "${key}"`,
60
+ )
61
+ }
62
+ })
63
+ const compileMeta = normalizeStructuredMetaMap(
64
+ {
65
+ jsenvCompiler: jsenvCompilers,
66
+ customCompiler: customCompilers,
67
+ },
68
+ projectDirectoryUrl,
69
+ )
70
+
71
+ return (request, { pushResponse }) => {
72
+ const { origin, ressource } = request
73
+ // we use "ressourceToPathname" to remove eventual query param from the url
74
+ // Without this a pattern like "**/*.js" would not match "file.js?t=1"
75
+ // This would result in file not being compiled when they should
76
+ const requestUrl = `${origin}${ressourceToPathname(ressource)}`
77
+
78
+ const requestCompileInfo = serverUrlToCompileInfo(requestUrl, {
79
+ outDirectoryRelativeUrl,
80
+ compileServerOrigin: origin,
81
+ })
82
+
83
+ // not inside compile directory -> nothing to compile
84
+ if (!requestCompileInfo.insideCompileDirectory) {
85
+ return null
86
+ }
87
+
88
+ const { compileId, afterCompileId } = requestCompileInfo
89
+ // serve files inside /.jsenv/* directly without compilation
90
+ // this is just to allow some files to be written inside outDirectory and read directly
91
+ // if asked by the client (such __compile_server_meta__.json)
92
+ if (!compileId) {
93
+ return fetchFileSystem(
94
+ new URL(request.ressource.slice(1), projectDirectoryUrl),
95
+ {
96
+ headers: request.headers,
97
+ etagEnabled: true,
98
+ },
99
+ )
100
+ }
101
+
102
+ const allowedCompileIds = [
103
+ ...Object.keys(groupMap),
104
+ COMPILE_ID_BUILD_GLOBAL,
105
+ COMPILE_ID_BUILD_GLOBAL_FILES,
106
+ COMPILE_ID_BUILD_COMMONJS,
107
+ COMPILE_ID_BUILD_COMMONJS_FILES,
108
+ ]
109
+ if (!allowedCompileIds.includes(compileId)) {
110
+ return {
111
+ status: 400,
112
+ statusText: `compileId must be one of ${allowedCompileIds}, received ${compileId}`,
113
+ }
114
+ }
115
+
116
+ // nothing after compileId, we don't know what to compile (not supposed to happen)
117
+ if (afterCompileId === "") {
118
+ return null
119
+ }
120
+
121
+ const originalFileRelativeUrl = afterCompileId
122
+ projectFileRequestedCallback(originalFileRelativeUrl, request)
123
+
124
+ const originalFileUrl = `${projectDirectoryUrl}${originalFileRelativeUrl}`
125
+ const compileDirectoryRelativeUrl = `${outDirectoryRelativeUrl}${compileId}/`
126
+ const compileDirectoryUrl = resolveDirectoryUrl(
127
+ compileDirectoryRelativeUrl,
128
+ projectDirectoryUrl,
129
+ )
130
+ const compiledFileUrl = resolveUrl(
131
+ originalFileRelativeUrl,
132
+ compileDirectoryUrl,
133
+ )
134
+
135
+ const compiler = getCompiler({ originalFileUrl, compileMeta })
136
+ // no compiler -> serve original file
137
+ // we don't redirect otherwise it complexify ressource tracking
138
+ // and url resolution
139
+ if (!compiler) {
140
+ return nextService({
141
+ ...request,
142
+ ressource: `/${originalFileRelativeUrl}`,
143
+ })
144
+ }
145
+
146
+ // compile this if needed
147
+ const compileResponsePromise = compileFile({
148
+ compileServerOperation,
149
+ logger,
150
+
151
+ projectDirectoryUrl,
152
+ originalFileUrl,
153
+ compiledFileUrl,
154
+
155
+ compileCacheStrategy,
156
+ projectFileRequestedCallback,
157
+ request,
158
+ pushResponse,
159
+ compile: ({ code, map }) => {
160
+ return compiler({
161
+ logger,
162
+
163
+ code,
164
+ map,
165
+ url: originalFileUrl,
166
+ compiledUrl: compiledFileUrl,
167
+ projectDirectoryUrl,
168
+ compileServerOrigin: request.origin,
169
+ outDirectoryRelativeUrl,
170
+ compileId,
171
+ request,
172
+
173
+ runtimeSupport,
174
+ moduleOutFormat,
175
+ importMetaFormat,
176
+ transformTopLevelAwait,
177
+ babelPluginMap: babelPluginMapFromCompileId(compileId, {
178
+ babelPluginMap,
179
+ groupMap,
180
+ }),
181
+
182
+ sourcemapMethod,
183
+ sourcemapExcludeSources,
184
+ jsenvToolbarInjection,
185
+ })
186
+ },
187
+ })
188
+ return compileResponsePromise
189
+ }
190
+ }
191
+
192
+ const getCompiler = ({ originalFileUrl, compileMeta }) => {
193
+ const { jsenvCompiler, customCompiler } = urlToMeta({
194
+ url: originalFileUrl,
195
+ structuredMetaMap: compileMeta,
196
+ })
197
+
198
+ if (!jsenvCompiler && !customCompiler) {
199
+ return null
200
+ }
201
+
202
+ // there is only a jsenvCompiler
203
+ if (jsenvCompiler && !customCompiler) {
204
+ return jsenvCompiler
205
+ }
206
+
207
+ // there is a custom compiler and potentially a jsenv compiler
208
+ return async (params) => {
209
+ // do custom compilation first
210
+ const customResult = await customCompiler(params)
211
+ // then check if jsenv compiler should apply
212
+ // to the new result contentType
213
+ const jsenvCompilerAfterCustomCompilation =
214
+ getJsenvCompilerAfterCustomCompilation({
215
+ url: originalFileUrl,
216
+ contentType: customResult.contentType,
217
+ compileMeta,
218
+ })
219
+ if (!jsenvCompilerAfterCustomCompilation) {
220
+ return customResult
221
+ }
222
+ const jsenvResult = await jsenvCompilerAfterCustomCompilation({
223
+ ...params,
224
+ code: customResult.compiledSource,
225
+ map: customResult.sourcemap,
226
+ })
227
+ return {
228
+ ...customResult,
229
+ ...jsenvResult,
230
+ }
231
+ }
232
+ }
233
+
234
+ const getJsenvCompilerAfterCustomCompilation = ({
235
+ url,
236
+ contentType,
237
+ compileMeta,
238
+ }) => {
239
+ const extensionToForce = contentTypeExtensions[contentType]
240
+ const urlForcingExtension = extensionToForce
241
+ ? setUrlExtension(url, extensionToForce)
242
+ : url
243
+ const { jsenvCompiler } = urlToMeta({
244
+ url: urlForcingExtension,
245
+ structuredMetaMap: compileMeta,
246
+ })
247
+ return jsenvCompiler
248
+ }
249
+
250
+ // should match contentType where there is a jsenv compiler
251
+ // back to an extension
252
+ const contentTypeExtensions = {
253
+ "application/javascript": ".js",
254
+ "text/html": ".html",
255
+ "application/importmap+json": ".importmap",
256
+ // "text/css": ".css",
257
+ }
258
+
259
+ const babelPluginMapFromCompileId = (
260
+ compileId,
261
+ { babelPluginMap, groupMap },
262
+ ) => {
263
+ const babelPluginMapForGroup = {}
264
+
265
+ groupMap[compileId].pluginRequiredNameArray.forEach((requiredPluginName) => {
266
+ const babelPlugin = babelPluginMap[requiredPluginName]
267
+ if (babelPlugin) {
268
+ babelPluginMapForGroup[requiredPluginName] = babelPlugin
269
+ }
270
+ })
271
+
272
+ Object.keys(babelPluginMap).forEach((key) => {
273
+ if (key.startsWith("syntax-")) {
274
+ babelPluginMapForGroup[key] = babelPluginMap[key]
275
+ }
276
+ if (key === "transform-replace-expressions") {
277
+ babelPluginMapForGroup[key] = babelPluginMap[key]
278
+ }
279
+ })
280
+
281
+ return babelPluginMapForGroup
282
+ }
283
+
284
+ const ressourceToPathname = (ressource) => {
285
+ const searchSeparatorIndex = ressource.indexOf("?")
286
+ const pathname =
287
+ searchSeparatorIndex === -1
288
+ ? ressource
289
+ : ressource.slice(0, searchSeparatorIndex)
290
+ return pathname
291
+ }