@jsenv/core 23.8.2 → 23.8.7
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.
- package/dist/jsenv_browser_system.js +46 -39
- package/dist/jsenv_browser_system.js.map +14 -14
- package/dist/jsenv_compile_proxy.js.map +6 -6
- package/dist/jsenv_exploring_index.js.map +5 -5
- package/dist/jsenv_exploring_redirector.js.map +12 -12
- package/dist/jsenv_toolbar.js.map +7 -7
- package/dist/jsenv_toolbar_injector.js.map +5 -5
- package/helpers/babel/.eslintrc.cjs +24 -24
- package/helpers/babel/AsyncGenerator/AsyncGenerator.js +81 -81
- package/helpers/babel/AwaitValue/AwaitValue.js +3 -3
- package/helpers/babel/applyDecoratorDescriptor/applyDecoratorDescriptor.js +33 -33
- package/helpers/babel/arrayLikeToArray/arrayLikeToArray.js +7 -7
- package/helpers/babel/arrayWithHoles/arrayWithHoles.js +4 -4
- package/helpers/babel/arrayWithoutHoles/arrayWithoutHoles.js +6 -6
- package/helpers/babel/assertThisInitialized/assertThisInitialized.js +7 -7
- package/helpers/babel/asyncGeneratorDelegate/asyncGeneratorDelegate.js +40 -40
- package/helpers/babel/asyncIterator/asyncIterator.js +12 -12
- package/helpers/babel/asyncToGenerator/asyncToGenerator.js +34 -34
- package/helpers/babel/awaitAsyncGenerator/awaitAsyncGenerator.js +5 -5
- package/helpers/babel/classApplyDescriptorDestructureSet/classApplyDescriptorDestructureSet.js +20 -20
- package/helpers/babel/classApplyDescriptorGet/classApplyDescriptorGet.js +6 -6
- package/helpers/babel/classApplyDescriptorSet/classApplyDescriptorSet.js +13 -13
- package/helpers/babel/classCallCheck/classCallCheck.js +5 -5
- package/helpers/babel/classCheckPrivateStaticAccess/classCheckPrivateStaticAccess.js +5 -5
- package/helpers/babel/classCheckPrivateStaticFieldDescriptor/classCheckPrivateStaticFieldDescriptor.js +6 -6
- package/helpers/babel/classExtractFieldDescriptor/classExtractFieldDescriptor.js +7 -7
- package/helpers/babel/classNameTDZError/classNameTDZError.js +4 -4
- package/helpers/babel/classPrivateFieldDestructureSet/classPrivateFieldDestructureSet.js +7 -7
- package/helpers/babel/classPrivateFieldGet/classPrivateFieldGet.js +7 -7
- package/helpers/babel/classPrivateFieldLooseBase/classPrivateFieldLooseBase.js +6 -6
- package/helpers/babel/classPrivateFieldLooseKey/classPrivateFieldLooseKey.js +5 -5
- package/helpers/babel/classPrivateFieldSet/classPrivateFieldSet.js +8 -8
- package/helpers/babel/classPrivateMethodGet/classPrivateMethodGet.js +6 -6
- package/helpers/babel/classPrivateMethodSet/classPrivateMethodSet.js +3 -3
- package/helpers/babel/classStaticPrivateFieldSpecGet/classStaticPrivateFieldSpecGet.js +9 -9
- package/helpers/babel/classStaticPrivateFieldSpecSet/classStaticPrivateFieldSpecSet.js +15 -15
- package/helpers/babel/classStaticPrivateMethodGet/classStaticPrivateMethodGet.js +6 -6
- package/helpers/babel/classStaticPrivateMethodSet/classStaticPrivateMethodSet.js +3 -3
- package/helpers/babel/construct/construct.js +16 -16
- package/helpers/babel/createClass/createClass.js +15 -15
- package/helpers/babel/createForOfIteratorHelper/createForOfIteratorHelper.js +60 -60
- package/helpers/babel/createForOfIteratorHelperLoose/createForOfIteratorHelperLoose.js +23 -23
- package/helpers/babel/createRawReactElement/createRawReactElement.js +50 -50
- package/helpers/babel/createSuper/createSuper.js +22 -22
- package/helpers/babel/decorate/decorate.js +403 -403
- package/helpers/babel/defaults/defaults.js +11 -11
- package/helpers/babel/defineEnumerableProperties/defineEnumerableProperties.js +23 -23
- package/helpers/babel/defineProperty/defineProperty.js +18 -18
- package/helpers/babel/extends/extends.js +14 -14
- package/helpers/babel/get/get.js +13 -13
- package/helpers/babel/getPrototypeOf/getPrototypeOf.js +4 -4
- package/helpers/babel/inherits/inherits.js +15 -15
- package/helpers/babel/inheritsLoose/inheritsLoose.js +7 -7
- package/helpers/babel/initializerDefineProperty/initializerDefineProperty.js +10 -10
- package/helpers/babel/initializerWarningHelper/initializerWarningHelper.js +6 -6
- package/helpers/babel/instanceof/instanceof.js +6 -6
- package/helpers/babel/interopRequireDefault/interopRequireDefault.js +3 -3
- package/helpers/babel/interopRequireWildcard/interopRequireWildcard.js +37 -37
- package/helpers/babel/isNativeFunction/isNativeFunction.js +4 -4
- package/helpers/babel/isNativeReflectConstruct/isNativeReflectConstruct.js +21 -21
- package/helpers/babel/iterableToArray/iterableToArray.js +7 -7
- package/helpers/babel/iterableToArrayLimit/iterableToArrayLimit.js +36 -36
- package/helpers/babel/iterableToArrayLimitLoose/iterableToArrayLimitLoose.js +10 -10
- package/helpers/babel/jsx/jsx.js +45 -45
- package/helpers/babel/maybeArrayLike/maybeArrayLike.js +10 -10
- package/helpers/babel/newArrowCheck/newArrowCheck.js +5 -5
- package/helpers/babel/nonIterableRest/nonIterableRest.js +5 -5
- package/helpers/babel/nonIterableSpread/nonIterableSpread.js +5 -5
- package/helpers/babel/objectDestructuringEmpty/objectDestructuringEmpty.js +3 -3
- package/helpers/babel/objectSpread/objectSpread.js +23 -23
- package/helpers/babel/objectSpread2/objectSpread2.js +33 -33
- package/helpers/babel/objectWithoutProperties/objectWithoutProperties.js +19 -19
- package/helpers/babel/objectWithoutPropertiesLoose/objectWithoutPropertiesLoose.js +13 -13
- package/helpers/babel/possibleConstructorReturn/possibleConstructorReturn.js +10 -10
- package/helpers/babel/readOnlyError/readOnlyError.js +4 -4
- package/helpers/babel/readme.md +9 -9
- package/helpers/babel/set/set.js +44 -44
- package/helpers/babel/setPrototypeOf/setPrototypeOf.js +6 -6
- package/helpers/babel/skipFirstGeneratorNext/skipFirstGeneratorNext.js +8 -8
- package/helpers/babel/slicedToArray/slicedToArray.js +10 -10
- package/helpers/babel/slicedToArrayLoose/slicedToArrayLoose.js +13 -13
- package/helpers/babel/superPropBase/superPropBase.js +10 -10
- package/helpers/babel/taggedTemplateLiteral/taggedTemplateLiteral.js +10 -10
- package/helpers/babel/taggedTemplateLiteralLoose/taggedTemplateLiteralLoose.js +7 -7
- package/helpers/babel/tdz/tdz.js +4 -4
- package/helpers/babel/temporalRef/temporalRef.js +6 -6
- package/helpers/babel/temporalUndefined/temporalUndefined.js +3 -3
- package/helpers/babel/toArray/toArray.js +10 -10
- package/helpers/babel/toConsumableArray/toConsumableArray.js +10 -10
- package/helpers/babel/toPrimitive/toPrimitive.js +10 -10
- package/helpers/babel/toPropertyKey/toPropertyKey.js +6 -6
- package/helpers/babel/typeof/typeof.js +14 -14
- package/helpers/babel/unsupportedIterableToArray/unsupportedIterableToArray.js +12 -12
- package/helpers/babel/wrapAsyncGenerator/wrapAsyncGenerator.js +8 -8
- package/helpers/babel/wrapNativeSuper/wrapNativeSuper.js +30 -30
- package/helpers/babel/wrapRegExp/wrapRegExp.js +63 -63
- package/helpers/babel/writeOnlyError/writeOnlyError.js +4 -4
- package/helpers/regenerator-runtime/regenerator-runtime.js +748 -748
- package/{LICENSE → license} +21 -21
- package/package.json +2 -2
- package/src/buildProject.js +300 -300
- package/src/execute.js +184 -184
- package/src/internal/browser-launcher/jsenv-browser-system.js +203 -199
- package/src/internal/building/buildUsingRollup.js +2 -10
- package/src/internal/compiling/babel_plugin_import_assertions.js +121 -121
- package/src/internal/compiling/babel_plugin_import_metadata.js +22 -22
- package/src/internal/compiling/babel_plugin_import_visitor.js +84 -84
- package/src/internal/compiling/compile-directory/getOrGenerateCompiledFile.js +268 -268
- package/src/internal/compiling/compile-directory/updateMeta.js +154 -154
- package/src/internal/compiling/compile-directory/validateCache.js +265 -265
- package/src/internal/compiling/compileFile.js +233 -224
- package/src/internal/compiling/compileHtml.js +550 -550
- package/src/internal/compiling/createCompiledFileService.js +291 -291
- package/src/internal/compiling/html_source_file_service.js +403 -404
- package/src/internal/compiling/js-compilation-service/jsenvTransform.js +272 -270
- package/src/internal/compiling/jsenvCompilerForHtml.js +374 -308
- package/src/internal/compiling/jsenvCompilerForJavaScript.js +2 -0
- package/src/internal/compiling/startCompileServer.js +1086 -1048
- package/src/internal/compiling/transformResultToCompilationResult.js +220 -220
- package/src/internal/executing/coverage/babel_plugin_instrument.js +90 -90
- package/src/internal/executing/coverage/reportToCoverage.js +193 -187
- package/src/internal/executing/executePlan.js +183 -183
- package/src/internal/executing/launchAndExecute.js +458 -458
- package/src/internal/generateGroupMap/featuresCompatMap.js +29 -0
- package/src/internal/generateGroupMap/jsenvBabelPluginCompatMap.js +1 -8
- package/src/internal/runtime/createBrowserRuntime/scanBrowserRuntimeFeatures.js +246 -246
- package/src/internal/runtime/createNodeRuntime/scanNodeRuntimeFeatures.js +112 -112
- package/src/internal/runtime/s.js +727 -727
- package/src/internal/toolbar/jsenv-logo.svg +144 -144
- package/src/internal/toolbar/toolbar.main.css +196 -196
- package/src/internal/toolbar/toolbar.main.js +227 -227
- package/src/internal/url_conversion.js +317 -317
- package/src/startExploring.js +309 -309
|
@@ -1,291 +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, { 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
|
-
}
|
|
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
|
+
}
|