@jsenv/core 23.6.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 (135) hide show
  1. package/{license → LICENSE} +21 -21
  2. package/dist/jsenv_browser_system.js +35 -35
  3. package/dist/jsenv_browser_system.js.map +14 -14
  4. package/dist/jsenv_compile_proxy.js +47 -128
  5. package/dist/jsenv_compile_proxy.js.map +25 -62
  6. package/dist/jsenv_exploring_index.js.map +5 -5
  7. package/dist/jsenv_exploring_redirector.js +47 -54
  8. package/dist/jsenv_exploring_redirector.js.map +17 -19
  9. package/dist/jsenv_toolbar.js +47 -130
  10. package/dist/jsenv_toolbar.js.map +13 -48
  11. package/dist/jsenv_toolbar_injector.js.map +5 -5
  12. package/helpers/babel/.eslintrc.cjs +24 -24
  13. package/helpers/babel/AsyncGenerator/AsyncGenerator.js +81 -81
  14. package/helpers/babel/AwaitValue/AwaitValue.js +3 -3
  15. package/helpers/babel/applyDecoratorDescriptor/applyDecoratorDescriptor.js +33 -33
  16. package/helpers/babel/arrayLikeToArray/arrayLikeToArray.js +7 -7
  17. package/helpers/babel/arrayWithHoles/arrayWithHoles.js +4 -4
  18. package/helpers/babel/arrayWithoutHoles/arrayWithoutHoles.js +6 -6
  19. package/helpers/babel/assertThisInitialized/assertThisInitialized.js +7 -7
  20. package/helpers/babel/asyncGeneratorDelegate/asyncGeneratorDelegate.js +40 -40
  21. package/helpers/babel/asyncIterator/asyncIterator.js +12 -12
  22. package/helpers/babel/asyncToGenerator/asyncToGenerator.js +34 -34
  23. package/helpers/babel/awaitAsyncGenerator/awaitAsyncGenerator.js +5 -5
  24. package/helpers/babel/classApplyDescriptorDestructureSet/classApplyDescriptorDestructureSet.js +20 -20
  25. package/helpers/babel/classApplyDescriptorGet/classApplyDescriptorGet.js +6 -6
  26. package/helpers/babel/classApplyDescriptorSet/classApplyDescriptorSet.js +13 -13
  27. package/helpers/babel/classCallCheck/classCallCheck.js +5 -5
  28. package/helpers/babel/classCheckPrivateStaticAccess/classCheckPrivateStaticAccess.js +5 -5
  29. package/helpers/babel/classCheckPrivateStaticFieldDescriptor/classCheckPrivateStaticFieldDescriptor.js +6 -6
  30. package/helpers/babel/classExtractFieldDescriptor/classExtractFieldDescriptor.js +7 -7
  31. package/helpers/babel/classNameTDZError/classNameTDZError.js +4 -4
  32. package/helpers/babel/classPrivateFieldDestructureSet/classPrivateFieldDestructureSet.js +7 -7
  33. package/helpers/babel/classPrivateFieldGet/classPrivateFieldGet.js +7 -7
  34. package/helpers/babel/classPrivateFieldLooseBase/classPrivateFieldLooseBase.js +6 -6
  35. package/helpers/babel/classPrivateFieldLooseKey/classPrivateFieldLooseKey.js +5 -5
  36. package/helpers/babel/classPrivateFieldSet/classPrivateFieldSet.js +8 -8
  37. package/helpers/babel/classPrivateMethodGet/classPrivateMethodGet.js +6 -6
  38. package/helpers/babel/classPrivateMethodSet/classPrivateMethodSet.js +3 -3
  39. package/helpers/babel/classStaticPrivateFieldSpecGet/classStaticPrivateFieldSpecGet.js +9 -9
  40. package/helpers/babel/classStaticPrivateFieldSpecSet/classStaticPrivateFieldSpecSet.js +15 -15
  41. package/helpers/babel/classStaticPrivateMethodGet/classStaticPrivateMethodGet.js +6 -6
  42. package/helpers/babel/classStaticPrivateMethodSet/classStaticPrivateMethodSet.js +3 -3
  43. package/helpers/babel/construct/construct.js +16 -16
  44. package/helpers/babel/createClass/createClass.js +15 -15
  45. package/helpers/babel/createForOfIteratorHelper/createForOfIteratorHelper.js +60 -60
  46. package/helpers/babel/createForOfIteratorHelperLoose/createForOfIteratorHelperLoose.js +23 -23
  47. package/helpers/babel/createRawReactElement/createRawReactElement.js +50 -50
  48. package/helpers/babel/createSuper/createSuper.js +22 -22
  49. package/helpers/babel/decorate/decorate.js +403 -403
  50. package/helpers/babel/defaults/defaults.js +11 -11
  51. package/helpers/babel/defineEnumerableProperties/defineEnumerableProperties.js +23 -23
  52. package/helpers/babel/defineProperty/defineProperty.js +18 -18
  53. package/helpers/babel/extends/extends.js +14 -14
  54. package/helpers/babel/get/get.js +13 -13
  55. package/helpers/babel/getPrototypeOf/getPrototypeOf.js +4 -4
  56. package/helpers/babel/inherits/inherits.js +15 -15
  57. package/helpers/babel/inheritsLoose/inheritsLoose.js +7 -7
  58. package/helpers/babel/initializerDefineProperty/initializerDefineProperty.js +10 -10
  59. package/helpers/babel/initializerWarningHelper/initializerWarningHelper.js +6 -6
  60. package/helpers/babel/instanceof/instanceof.js +6 -6
  61. package/helpers/babel/interopRequireDefault/interopRequireDefault.js +3 -3
  62. package/helpers/babel/interopRequireWildcard/interopRequireWildcard.js +37 -37
  63. package/helpers/babel/isNativeFunction/isNativeFunction.js +4 -4
  64. package/helpers/babel/isNativeReflectConstruct/isNativeReflectConstruct.js +21 -21
  65. package/helpers/babel/iterableToArray/iterableToArray.js +7 -7
  66. package/helpers/babel/iterableToArrayLimit/iterableToArrayLimit.js +36 -36
  67. package/helpers/babel/iterableToArrayLimitLoose/iterableToArrayLimitLoose.js +10 -10
  68. package/helpers/babel/jsx/jsx.js +45 -45
  69. package/helpers/babel/maybeArrayLike/maybeArrayLike.js +10 -10
  70. package/helpers/babel/newArrowCheck/newArrowCheck.js +5 -5
  71. package/helpers/babel/nonIterableRest/nonIterableRest.js +5 -5
  72. package/helpers/babel/nonIterableSpread/nonIterableSpread.js +5 -5
  73. package/helpers/babel/objectDestructuringEmpty/objectDestructuringEmpty.js +3 -3
  74. package/helpers/babel/objectSpread/objectSpread.js +23 -23
  75. package/helpers/babel/objectSpread2/objectSpread2.js +33 -33
  76. package/helpers/babel/objectWithoutProperties/objectWithoutProperties.js +19 -19
  77. package/helpers/babel/objectWithoutPropertiesLoose/objectWithoutPropertiesLoose.js +13 -13
  78. package/helpers/babel/possibleConstructorReturn/possibleConstructorReturn.js +10 -10
  79. package/helpers/babel/readOnlyError/readOnlyError.js +4 -4
  80. package/helpers/babel/readme.md +9 -9
  81. package/helpers/babel/set/set.js +44 -44
  82. package/helpers/babel/setPrototypeOf/setPrototypeOf.js +6 -6
  83. package/helpers/babel/skipFirstGeneratorNext/skipFirstGeneratorNext.js +8 -8
  84. package/helpers/babel/slicedToArray/slicedToArray.js +10 -10
  85. package/helpers/babel/slicedToArrayLoose/slicedToArrayLoose.js +13 -13
  86. package/helpers/babel/superPropBase/superPropBase.js +10 -10
  87. package/helpers/babel/taggedTemplateLiteral/taggedTemplateLiteral.js +10 -10
  88. package/helpers/babel/taggedTemplateLiteralLoose/taggedTemplateLiteralLoose.js +7 -7
  89. package/helpers/babel/tdz/tdz.js +4 -4
  90. package/helpers/babel/temporalRef/temporalRef.js +6 -6
  91. package/helpers/babel/temporalUndefined/temporalUndefined.js +3 -3
  92. package/helpers/babel/toArray/toArray.js +10 -10
  93. package/helpers/babel/toConsumableArray/toConsumableArray.js +10 -10
  94. package/helpers/babel/toPrimitive/toPrimitive.js +10 -10
  95. package/helpers/babel/toPropertyKey/toPropertyKey.js +6 -6
  96. package/helpers/babel/typeof/typeof.js +14 -14
  97. package/helpers/babel/unsupportedIterableToArray/unsupportedIterableToArray.js +12 -12
  98. package/helpers/babel/wrapAsyncGenerator/wrapAsyncGenerator.js +8 -8
  99. package/helpers/babel/wrapNativeSuper/wrapNativeSuper.js +30 -30
  100. package/helpers/babel/wrapRegExp/wrapRegExp.js +63 -63
  101. package/helpers/babel/writeOnlyError/writeOnlyError.js +4 -4
  102. package/helpers/regenerator-runtime/regenerator-runtime.js +748 -748
  103. package/package.json +5 -3
  104. package/src/buildProject.js +300 -300
  105. package/src/execute.js +184 -184
  106. package/src/internal/browser-launcher/jsenv-browser-system.js +199 -199
  107. package/src/internal/compiling/babel_plugin_import_assertions.js +121 -100
  108. package/src/internal/compiling/babel_plugin_import_metadata.js +22 -0
  109. package/src/internal/compiling/babel_plugin_import_visitor.js +84 -0
  110. package/src/internal/compiling/compile-directory/getOrGenerateCompiledFile.js +268 -265
  111. package/src/internal/compiling/compile-directory/updateMeta.js +154 -150
  112. package/src/internal/compiling/compile-directory/validateCache.js +265 -265
  113. package/src/internal/compiling/compileFile.js +215 -200
  114. package/src/internal/compiling/compileHtml.js +550 -494
  115. package/src/internal/compiling/createCompiledFileService.js +291 -290
  116. package/src/internal/compiling/html_source_file_service.js +403 -379
  117. package/src/internal/compiling/js-compilation-service/jsenvTransform.js +270 -269
  118. package/src/internal/compiling/jsenvCompilerForHtml.js +300 -293
  119. package/src/internal/compiling/startCompileServer.js +1048 -1131
  120. package/src/internal/compiling/transformResultToCompilationResult.js +220 -217
  121. package/src/internal/executing/coverage/babel_plugin_instrument.js +90 -90
  122. package/src/internal/executing/coverage/reportToCoverage.js +187 -187
  123. package/src/internal/executing/executePlan.js +183 -183
  124. package/src/internal/executing/launchAndExecute.js +458 -450
  125. package/src/internal/runtime/createBrowserRuntime/scanBrowserRuntimeFeatures.js +246 -250
  126. package/src/internal/runtime/createNodeRuntime/scanNodeRuntimeFeatures.js +112 -115
  127. package/src/internal/runtime/s.js +727 -727
  128. package/src/internal/toolbar/jsenv-logo.svg +144 -144
  129. package/src/internal/toolbar/toolbar.main.css +196 -188
  130. package/src/internal/toolbar/toolbar.main.js +227 -228
  131. package/src/internal/url_conversion.js +317 -317
  132. package/src/startExploring.js +309 -309
  133. package/src/internal/compiling/babel_plugin_transform_import_specifier.js +0 -86
  134. package/src/internal/toolbar/animation/animation.css +0 -5
  135. package/src/internal/toolbar/variant/variant.css +0 -3
@@ -1,317 +1,317 @@
1
- import {
2
- resolveDirectoryUrl,
3
- urlToRelativeUrl,
4
- urlIsInsideOf,
5
- resolveUrl,
6
- } from "@jsenv/filesystem"
7
-
8
- // use a fake and predictable compile server origin
9
- // because rollup will check the dependencies url
10
- // when computing the file hash
11
- // https://github.com/rollup/rollup/blob/d6131378f9481a442aeaa6d4e608faf3303366dc/src/Chunk.ts#L483
12
- // this way file hash remains the same when file content does not change
13
- const STATIC_COMPILE_SERVER_AUTHORITY = "//jsenv.com"
14
-
15
- export const createUrlConverter = ({
16
- projectDirectoryUrl,
17
- compileServerOrigin,
18
- compileDirectoryRelativeUrl,
19
- urlMappings,
20
- }) => {
21
- const compileServerOriginForRollup = String(
22
- new URL(STATIC_COMPILE_SERVER_AUTHORITY, compileServerOrigin),
23
- ).slice(0, -1)
24
- urlMappings = normalizeUrlMappings(urlMappings, projectDirectoryUrl)
25
- const mappingsRevertMap = {}
26
-
27
- const asRollupUrl = (url) => {
28
- if (url.startsWith(`${compileServerOrigin}/`)) {
29
- return `${compileServerOriginForRollup}/${url.slice(
30
- `${compileServerOrigin}/`.length,
31
- )}`
32
- }
33
- return url
34
- }
35
-
36
- const asProjectUrl = (url) => {
37
- return projectUrlFromUrl(asServerUrl(url) || url, {
38
- projectDirectoryUrl,
39
- compileServerOrigin,
40
- })
41
- }
42
-
43
- const asServerUrl = (url) => {
44
- if (url.startsWith(`${compileServerOriginForRollup}/`)) {
45
- return `${compileServerOrigin}/${url.slice(
46
- `${compileServerOriginForRollup}/`.length,
47
- )}`
48
- }
49
- return serverUrlFromUrl(url, { projectDirectoryUrl, compileServerOrigin })
50
- }
51
-
52
- const asCompiledUrl = (url) => {
53
- const projectCompiledUrl = compiledProjectUrlFromUrl(
54
- asProjectUrl(url) || url,
55
- {
56
- projectDirectoryUrl,
57
- compileServerOrigin,
58
- compileDirectoryRelativeUrl,
59
- },
60
- )
61
- return projectCompiledUrl
62
- }
63
-
64
- const asCompiledServerUrl = (url) => {
65
- const projectCompiledUrl = asCompiledUrl(url)
66
- return asServerUrl(projectCompiledUrl)
67
- }
68
-
69
- const asOriginalUrl = (url) => {
70
- const projectOriginalUrl = originalProjectUrlFromUrl(
71
- asProjectUrl(url) || url,
72
- {
73
- projectDirectoryUrl,
74
- compileServerOrigin,
75
- compileDirectoryRelativeUrl,
76
- },
77
- )
78
- const withoutMapping = mappingsRevertMap[projectOriginalUrl]
79
- return withoutMapping || projectOriginalUrl
80
- }
81
-
82
- const asOriginalServerUrl = (url) => {
83
- const projectOriginalUrl = asOriginalUrl(url)
84
- return projectOriginalUrl ? asServerUrl(projectOriginalUrl) : null
85
- }
86
-
87
- // Url can
88
- // - come from rollup "load" hook and be compiled
89
- // - come from rollup "load" hook and be the original url
90
- // - come from asset builder and be compiled
91
- // - come from asset builder and be the original url
92
- // We first want to ensure we are talking about the same url
93
- // so we get the originalProjectUrl
94
- // then we should return a mapped url, keeping in mind that
95
- // if the url was compiled it should remain compiled
96
- const applyUrlMappings = (url) => {
97
- const originalProjectUrl = asOriginalUrl(url)
98
- const urlMapping = urlMappings[originalProjectUrl]
99
- if (!urlMapping) {
100
- return url
101
- }
102
-
103
- mappingsRevertMap[urlMapping] = originalProjectUrl
104
- const isInsideProject =
105
- url === projectDirectoryUrl || urlIsInsideOf(url, projectDirectoryUrl)
106
- const projectUrl = isInsideProject ? url : asProjectUrl(url)
107
- const compileDirectoryUrl = resolveUrl(
108
- compileDirectoryRelativeUrl,
109
- projectDirectoryUrl,
110
- )
111
- const isCompiled =
112
- projectUrl === compileDirectoryUrl ||
113
- urlIsInsideOf(projectUrl, compileDirectoryUrl)
114
- if (isCompiled && isInsideProject) {
115
- return asCompiledUrl(urlMapping)
116
- }
117
- if (isCompiled) {
118
- return asCompiledServerUrl(urlMapping)
119
- }
120
- if (isInsideProject) {
121
- return asProjectUrl(urlMapping)
122
- }
123
- return asServerUrl(urlMapping)
124
- }
125
-
126
- return {
127
- asRollupUrl,
128
- asProjectUrl,
129
- asServerUrl,
130
-
131
- asCompiledUrl,
132
- asCompiledServerUrl,
133
- asOriginalUrl,
134
- asOriginalServerUrl,
135
- applyUrlMappings,
136
- }
137
- }
138
-
139
- const normalizeUrlMappings = (urlMappings, baseUrl) => {
140
- const urlMappingsNormalized = {}
141
- Object.keys(urlMappings).forEach((key) => {
142
- const value = urlMappings[key]
143
- const keyNormalized = resolveUrl(key, baseUrl)
144
- const valueNormalized = resolveUrl(value, baseUrl)
145
- urlMappingsNormalized[keyNormalized] = valueNormalized
146
- })
147
- return urlMappingsNormalized
148
- }
149
-
150
- export const serverUrlToCompileInfo = (
151
- url,
152
- { compileServerOrigin, outDirectoryRelativeUrl },
153
- ) => {
154
- const outDirectoryServerUrl = resolveDirectoryUrl(
155
- outDirectoryRelativeUrl,
156
- compileServerOrigin,
157
- )
158
- // not inside compile directory -> nothing to compile
159
- if (!url.startsWith(outDirectoryServerUrl)) {
160
- return { insideCompileDirectory: false }
161
- }
162
-
163
- const afterOutDirectory = url.slice(outDirectoryServerUrl.length)
164
-
165
- // serve files inside /.jsenv/out/* directly without compilation
166
- // this is just to allow some files to be written inside outDirectory and read directly
167
- // if asked by the client (such as env.json, groupMap.json, meta.json)
168
- if (!afterOutDirectory.includes("/") || afterOutDirectory[0] === "/") {
169
- return { insideCompileDirectory: true }
170
- }
171
-
172
- const parts = afterOutDirectory.split("/")
173
- const compileId = parts[0]
174
- // no compileId, we don't know what to compile (not supposed so happen)
175
- if (compileId === "") {
176
- return { insideCompileDirectory: true, compileId: null }
177
- }
178
-
179
- const afterCompileId = parts.slice(1).join("/")
180
- // note: afterCompileId can be '' (but not supposed to happen)
181
- return { insideCompileDirectory: true, compileId, afterCompileId }
182
- }
183
-
184
- // tries to convert an url into an url that is inside projectDirectoryUrl
185
- const projectUrlFromUrl = (
186
- url,
187
- { projectDirectoryUrl, compileServerOrigin },
188
- ) => {
189
- if (url.startsWith(projectDirectoryUrl)) {
190
- return url
191
- }
192
-
193
- const serverUrl = serverUrlFromUrl(url, {
194
- projectDirectoryUrl,
195
- compileServerOrigin,
196
- })
197
- if (serverUrl) {
198
- return `${projectDirectoryUrl}${serverUrl.slice(
199
- `${compileServerOrigin}/`.length,
200
- )}`
201
- }
202
-
203
- return null
204
- }
205
-
206
- // tries to convert an url into an url that is inside compileServerOrigin
207
- const serverUrlFromUrl = (
208
- url,
209
- { projectDirectoryUrl, compileServerOrigin },
210
- ) => {
211
- if (url.startsWith(`${compileServerOrigin}/`)) {
212
- return url
213
- }
214
-
215
- if (url.startsWith(projectDirectoryUrl)) {
216
- return `${compileServerOrigin}/${url.slice(projectDirectoryUrl.length)}`
217
- }
218
-
219
- return null
220
- }
221
-
222
- // tries to convert an url into an url inside compileServerOrigin
223
- // AND return the compiled url is the url is the original version
224
- const compiledProjectUrlFromUrl = (
225
- url,
226
- { projectDirectoryUrl, compileServerOrigin, compileDirectoryRelativeUrl },
227
- ) => {
228
- const projectUrl = projectUrlFromUrl(url, {
229
- projectDirectoryUrl,
230
- compileServerOrigin,
231
- })
232
- if (!projectUrl) {
233
- return null
234
- }
235
-
236
- const compileDirectoryUrl = resolveUrl(
237
- compileDirectoryRelativeUrl,
238
- projectDirectoryUrl,
239
- )
240
- if (
241
- projectUrl === compileDirectoryUrl ||
242
- urlIsInsideOf(projectUrl, compileDirectoryUrl)
243
- ) {
244
- return projectUrl
245
- }
246
-
247
- const projectRelativeUrl = urlToRelativeUrl(projectUrl, projectDirectoryUrl)
248
- if (projectRelativeUrl) {
249
- return resolveUrl(projectRelativeUrl, compileDirectoryUrl)
250
- }
251
-
252
- return null
253
- }
254
-
255
- // tries to convert an url into an url that is inside projectDirectoryUrl
256
- // AND return the original url if the url is the compiled version
257
- const originalProjectUrlFromUrl = (
258
- url,
259
- {
260
- projectDirectoryUrl,
261
- compileServerOrigin,
262
- outDirectoryRelativeUrl,
263
- compileDirectoryRelativeUrl,
264
- },
265
- ) => {
266
- const projectUrl = projectUrlFromUrl(url, {
267
- projectDirectoryUrl,
268
- compileServerOrigin,
269
- })
270
- if (!projectUrl) {
271
- return null
272
- }
273
-
274
- if (!compileDirectoryRelativeUrl) {
275
- const serverUrl = serverUrlFromUrl(projectUrl, {
276
- projectDirectoryUrl,
277
- compileServerOrigin,
278
- })
279
- compileDirectoryRelativeUrl = extractCompileDirectoryRelativeUrl({
280
- serverUrl,
281
- compileServerOrigin,
282
- outDirectoryRelativeUrl,
283
- })
284
- if (!compileDirectoryRelativeUrl) {
285
- return projectUrl
286
- }
287
- }
288
-
289
- const compileDirectoryUrl = resolveUrl(
290
- compileDirectoryRelativeUrl,
291
- projectDirectoryUrl,
292
- )
293
- if (projectUrl === compileDirectoryUrl) {
294
- return projectDirectoryUrl
295
- }
296
- if (!urlIsInsideOf(projectUrl, compileDirectoryUrl)) {
297
- return projectUrl
298
- }
299
-
300
- const relativeUrl = urlToRelativeUrl(projectUrl, compileDirectoryUrl)
301
- return resolveUrl(relativeUrl, projectDirectoryUrl)
302
- }
303
-
304
- const extractCompileDirectoryRelativeUrl = ({
305
- serverUrl,
306
- compileServerOrigin,
307
- outDirectoryRelativeUrl,
308
- }) => {
309
- const compileInfo = serverUrlToCompileInfo(serverUrl, {
310
- compileServerOrigin,
311
- outDirectoryRelativeUrl,
312
- })
313
- if (compileInfo.compiledId) {
314
- return `${outDirectoryRelativeUrl}${compileInfo.compileId}/`
315
- }
316
- return null
317
- }
1
+ import {
2
+ resolveDirectoryUrl,
3
+ urlToRelativeUrl,
4
+ urlIsInsideOf,
5
+ resolveUrl,
6
+ } from "@jsenv/filesystem"
7
+
8
+ // use a fake and predictable compile server origin
9
+ // because rollup will check the dependencies url
10
+ // when computing the file hash
11
+ // https://github.com/rollup/rollup/blob/d6131378f9481a442aeaa6d4e608faf3303366dc/src/Chunk.ts#L483
12
+ // this way file hash remains the same when file content does not change
13
+ const STATIC_COMPILE_SERVER_AUTHORITY = "//jsenv.com"
14
+
15
+ export const createUrlConverter = ({
16
+ projectDirectoryUrl,
17
+ compileServerOrigin,
18
+ compileDirectoryRelativeUrl,
19
+ urlMappings,
20
+ }) => {
21
+ const compileServerOriginForRollup = String(
22
+ new URL(STATIC_COMPILE_SERVER_AUTHORITY, compileServerOrigin),
23
+ ).slice(0, -1)
24
+ urlMappings = normalizeUrlMappings(urlMappings, projectDirectoryUrl)
25
+ const mappingsRevertMap = {}
26
+
27
+ const asRollupUrl = (url) => {
28
+ if (url.startsWith(`${compileServerOrigin}/`)) {
29
+ return `${compileServerOriginForRollup}/${url.slice(
30
+ `${compileServerOrigin}/`.length,
31
+ )}`
32
+ }
33
+ return url
34
+ }
35
+
36
+ const asProjectUrl = (url) => {
37
+ return projectUrlFromUrl(asServerUrl(url) || url, {
38
+ projectDirectoryUrl,
39
+ compileServerOrigin,
40
+ })
41
+ }
42
+
43
+ const asServerUrl = (url) => {
44
+ if (url.startsWith(`${compileServerOriginForRollup}/`)) {
45
+ return `${compileServerOrigin}/${url.slice(
46
+ `${compileServerOriginForRollup}/`.length,
47
+ )}`
48
+ }
49
+ return serverUrlFromUrl(url, { projectDirectoryUrl, compileServerOrigin })
50
+ }
51
+
52
+ const asCompiledUrl = (url) => {
53
+ const projectCompiledUrl = compiledProjectUrlFromUrl(
54
+ asProjectUrl(url) || url,
55
+ {
56
+ projectDirectoryUrl,
57
+ compileServerOrigin,
58
+ compileDirectoryRelativeUrl,
59
+ },
60
+ )
61
+ return projectCompiledUrl
62
+ }
63
+
64
+ const asCompiledServerUrl = (url) => {
65
+ const projectCompiledUrl = asCompiledUrl(url)
66
+ return asServerUrl(projectCompiledUrl)
67
+ }
68
+
69
+ const asOriginalUrl = (url) => {
70
+ const projectOriginalUrl = originalProjectUrlFromUrl(
71
+ asProjectUrl(url) || url,
72
+ {
73
+ projectDirectoryUrl,
74
+ compileServerOrigin,
75
+ compileDirectoryRelativeUrl,
76
+ },
77
+ )
78
+ const withoutMapping = mappingsRevertMap[projectOriginalUrl]
79
+ return withoutMapping || projectOriginalUrl
80
+ }
81
+
82
+ const asOriginalServerUrl = (url) => {
83
+ const projectOriginalUrl = asOriginalUrl(url)
84
+ return projectOriginalUrl ? asServerUrl(projectOriginalUrl) : null
85
+ }
86
+
87
+ // Url can
88
+ // - come from rollup "load" hook and be compiled
89
+ // - come from rollup "load" hook and be the original url
90
+ // - come from asset builder and be compiled
91
+ // - come from asset builder and be the original url
92
+ // We first want to ensure we are talking about the same url
93
+ // so we get the originalProjectUrl
94
+ // then we should return a mapped url, keeping in mind that
95
+ // if the url was compiled it should remain compiled
96
+ const applyUrlMappings = (url) => {
97
+ const originalProjectUrl = asOriginalUrl(url)
98
+ const urlMapping = urlMappings[originalProjectUrl]
99
+ if (!urlMapping) {
100
+ return url
101
+ }
102
+
103
+ mappingsRevertMap[urlMapping] = originalProjectUrl
104
+ const isInsideProject =
105
+ url === projectDirectoryUrl || urlIsInsideOf(url, projectDirectoryUrl)
106
+ const projectUrl = isInsideProject ? url : asProjectUrl(url)
107
+ const compileDirectoryUrl = resolveUrl(
108
+ compileDirectoryRelativeUrl,
109
+ projectDirectoryUrl,
110
+ )
111
+ const isCompiled =
112
+ projectUrl === compileDirectoryUrl ||
113
+ urlIsInsideOf(projectUrl, compileDirectoryUrl)
114
+ if (isCompiled && isInsideProject) {
115
+ return asCompiledUrl(urlMapping)
116
+ }
117
+ if (isCompiled) {
118
+ return asCompiledServerUrl(urlMapping)
119
+ }
120
+ if (isInsideProject) {
121
+ return asProjectUrl(urlMapping)
122
+ }
123
+ return asServerUrl(urlMapping)
124
+ }
125
+
126
+ return {
127
+ asRollupUrl,
128
+ asProjectUrl,
129
+ asServerUrl,
130
+
131
+ asCompiledUrl,
132
+ asCompiledServerUrl,
133
+ asOriginalUrl,
134
+ asOriginalServerUrl,
135
+ applyUrlMappings,
136
+ }
137
+ }
138
+
139
+ const normalizeUrlMappings = (urlMappings, baseUrl) => {
140
+ const urlMappingsNormalized = {}
141
+ Object.keys(urlMappings).forEach((key) => {
142
+ const value = urlMappings[key]
143
+ const keyNormalized = resolveUrl(key, baseUrl)
144
+ const valueNormalized = resolveUrl(value, baseUrl)
145
+ urlMappingsNormalized[keyNormalized] = valueNormalized
146
+ })
147
+ return urlMappingsNormalized
148
+ }
149
+
150
+ export const serverUrlToCompileInfo = (
151
+ url,
152
+ { compileServerOrigin, outDirectoryRelativeUrl },
153
+ ) => {
154
+ const outDirectoryServerUrl = resolveDirectoryUrl(
155
+ outDirectoryRelativeUrl,
156
+ compileServerOrigin,
157
+ )
158
+ // not inside compile directory -> nothing to compile
159
+ if (!url.startsWith(outDirectoryServerUrl)) {
160
+ return { insideCompileDirectory: false }
161
+ }
162
+
163
+ const afterOutDirectory = url.slice(outDirectoryServerUrl.length)
164
+
165
+ // serve files inside /.jsenv/* directly without compilation
166
+ // this is just to allow some files to be written inside outDirectory and read directly
167
+ // if asked by the client (such as __compile_server_meta__.json)
168
+ if (!afterOutDirectory.includes("/") || afterOutDirectory[0] === "/") {
169
+ return { insideCompileDirectory: true }
170
+ }
171
+
172
+ const parts = afterOutDirectory.split("/")
173
+ const compileId = parts[0]
174
+ // no compileId, we don't know what to compile (not supposed so happen)
175
+ if (compileId === "") {
176
+ return { insideCompileDirectory: true, compileId: null }
177
+ }
178
+
179
+ const afterCompileId = parts.slice(1).join("/")
180
+ // note: afterCompileId can be '' (but not supposed to happen)
181
+ return { insideCompileDirectory: true, compileId, afterCompileId }
182
+ }
183
+
184
+ // tries to convert an url into an url that is inside projectDirectoryUrl
185
+ const projectUrlFromUrl = (
186
+ url,
187
+ { projectDirectoryUrl, compileServerOrigin },
188
+ ) => {
189
+ if (url.startsWith(projectDirectoryUrl)) {
190
+ return url
191
+ }
192
+
193
+ const serverUrl = serverUrlFromUrl(url, {
194
+ projectDirectoryUrl,
195
+ compileServerOrigin,
196
+ })
197
+ if (serverUrl) {
198
+ return `${projectDirectoryUrl}${serverUrl.slice(
199
+ `${compileServerOrigin}/`.length,
200
+ )}`
201
+ }
202
+
203
+ return null
204
+ }
205
+
206
+ // tries to convert an url into an url that is inside compileServerOrigin
207
+ const serverUrlFromUrl = (
208
+ url,
209
+ { projectDirectoryUrl, compileServerOrigin },
210
+ ) => {
211
+ if (url.startsWith(`${compileServerOrigin}/`)) {
212
+ return url
213
+ }
214
+
215
+ if (url.startsWith(projectDirectoryUrl)) {
216
+ return `${compileServerOrigin}/${url.slice(projectDirectoryUrl.length)}`
217
+ }
218
+
219
+ return null
220
+ }
221
+
222
+ // tries to convert an url into an url inside compileServerOrigin
223
+ // AND return the compiled url is the url is the original version
224
+ const compiledProjectUrlFromUrl = (
225
+ url,
226
+ { projectDirectoryUrl, compileServerOrigin, compileDirectoryRelativeUrl },
227
+ ) => {
228
+ const projectUrl = projectUrlFromUrl(url, {
229
+ projectDirectoryUrl,
230
+ compileServerOrigin,
231
+ })
232
+ if (!projectUrl) {
233
+ return null
234
+ }
235
+
236
+ const compileDirectoryUrl = resolveUrl(
237
+ compileDirectoryRelativeUrl,
238
+ projectDirectoryUrl,
239
+ )
240
+ if (
241
+ projectUrl === compileDirectoryUrl ||
242
+ urlIsInsideOf(projectUrl, compileDirectoryUrl)
243
+ ) {
244
+ return projectUrl
245
+ }
246
+
247
+ const projectRelativeUrl = urlToRelativeUrl(projectUrl, projectDirectoryUrl)
248
+ if (projectRelativeUrl) {
249
+ return resolveUrl(projectRelativeUrl, compileDirectoryUrl)
250
+ }
251
+
252
+ return null
253
+ }
254
+
255
+ // tries to convert an url into an url that is inside projectDirectoryUrl
256
+ // AND return the original url if the url is the compiled version
257
+ const originalProjectUrlFromUrl = (
258
+ url,
259
+ {
260
+ projectDirectoryUrl,
261
+ compileServerOrigin,
262
+ outDirectoryRelativeUrl,
263
+ compileDirectoryRelativeUrl,
264
+ },
265
+ ) => {
266
+ const projectUrl = projectUrlFromUrl(url, {
267
+ projectDirectoryUrl,
268
+ compileServerOrigin,
269
+ })
270
+ if (!projectUrl) {
271
+ return null
272
+ }
273
+
274
+ if (!compileDirectoryRelativeUrl) {
275
+ const serverUrl = serverUrlFromUrl(projectUrl, {
276
+ projectDirectoryUrl,
277
+ compileServerOrigin,
278
+ })
279
+ compileDirectoryRelativeUrl = extractCompileDirectoryRelativeUrl({
280
+ serverUrl,
281
+ compileServerOrigin,
282
+ outDirectoryRelativeUrl,
283
+ })
284
+ if (!compileDirectoryRelativeUrl) {
285
+ return projectUrl
286
+ }
287
+ }
288
+
289
+ const compileDirectoryUrl = resolveUrl(
290
+ compileDirectoryRelativeUrl,
291
+ projectDirectoryUrl,
292
+ )
293
+ if (projectUrl === compileDirectoryUrl) {
294
+ return projectDirectoryUrl
295
+ }
296
+ if (!urlIsInsideOf(projectUrl, compileDirectoryUrl)) {
297
+ return projectUrl
298
+ }
299
+
300
+ const relativeUrl = urlToRelativeUrl(projectUrl, compileDirectoryUrl)
301
+ return resolveUrl(relativeUrl, projectDirectoryUrl)
302
+ }
303
+
304
+ const extractCompileDirectoryRelativeUrl = ({
305
+ serverUrl,
306
+ compileServerOrigin,
307
+ outDirectoryRelativeUrl,
308
+ }) => {
309
+ const compileInfo = serverUrlToCompileInfo(serverUrl, {
310
+ compileServerOrigin,
311
+ outDirectoryRelativeUrl,
312
+ })
313
+ if (compileInfo.compiledId) {
314
+ return `${outDirectoryRelativeUrl}${compileInfo.compileId}/`
315
+ }
316
+ return null
317
+ }