@jsenv/core 27.0.0-alpha.74 → 27.0.0-alpha.77
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/js/html_supervisor_setup.js +4 -1
- package/dist/main.js +153 -156
- package/package.json +5 -6
- package/src/build/build.js +6 -11
- package/src/build/start_build_server.js +2 -3
- package/src/dev/start_dev_server.js +6 -10
- package/src/execute/execute.js +1 -4
- package/src/execute/runtimes/browsers/from_playwright.js +2 -2
- package/src/execute/runtimes/node/child_exec_options.js +1 -1
- package/src/execute/runtimes/node/node_process.js +2 -2
- package/src/main.js +1 -1
- package/src/omega/errors.js +1 -1
- package/src/omega/kitchen.js +21 -13
- package/src/omega/server/file_service.js +7 -8
- package/src/omega/url_graph.js +1 -1
- package/src/plugins/bundling/js_module/bundle_js_module.js +2 -2
- package/src/plugins/file_urls/jsenv_plugin_file_urls.js +3 -7
- package/src/plugins/html_supervisor/client/html_supervisor_setup.js +4 -1
- package/src/plugins/inject_globals/inject_globals.js +57 -0
- package/src/plugins/inject_globals/jsenv_plugin_inject_globals.js +17 -69
- package/src/plugins/inline/jsenv_plugin_data_urls.js +2 -2
- package/src/plugins/inline/jsenv_plugin_inline.js +1 -1
- package/src/plugins/node_esm_resolution/jsenv_plugin_node_esm_resolution.js +2 -0
- package/src/plugins/plugin_controller.js +2 -2
- package/src/plugins/plugins.js +0 -3
- package/src/plugins/transpilation/as_js_classic/jsenv_plugin_as_js_classic.js +3 -3
- package/src/plugins/transpilation/import_assertions/jsenv_plugin_import_assertions.js +12 -12
- package/src/test/execute_plan.js +7 -4
- package/src/test/execute_test_plan.js +1 -3
- package/src/test/execution_steps.js +1 -1
|
@@ -23,13 +23,12 @@ import {
|
|
|
23
23
|
fetchFileSystem,
|
|
24
24
|
composeServices,
|
|
25
25
|
} from "@jsenv/server"
|
|
26
|
+
|
|
26
27
|
import {
|
|
27
28
|
assertAndNormalizeDirectoryUrl,
|
|
28
29
|
registerDirectoryLifecycle,
|
|
29
30
|
} from "@jsenv/filesystem"
|
|
30
|
-
import { createLogger, loggerToLevels } from "@jsenv/
|
|
31
|
-
|
|
32
|
-
import { createTaskLog } from "@jsenv/log"
|
|
31
|
+
import { createLogger, loggerToLevels, createTaskLog } from "@jsenv/log"
|
|
33
32
|
import { getCallerPosition } from "@jsenv/urls"
|
|
34
33
|
import { initReloadableProcess } from "@jsenv/utils/process_reload/process_reload.js"
|
|
35
34
|
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { parentPort } from "node:worker_threads"
|
|
2
|
+
|
|
2
3
|
import {
|
|
3
4
|
assertAndNormalizeDirectoryUrl,
|
|
4
5
|
registerDirectoryLifecycle,
|
|
5
6
|
} from "@jsenv/filesystem"
|
|
6
|
-
import { createLogger, loggerToLevels } from "@jsenv/
|
|
7
|
-
|
|
8
|
-
import { createTaskLog } from "@jsenv/log"
|
|
7
|
+
import { createLogger, loggerToLevels, createTaskLog } from "@jsenv/log"
|
|
9
8
|
import { getCallerPosition } from "@jsenv/urls"
|
|
10
9
|
import { initReloadableProcess } from "@jsenv/utils/process_reload/process_reload.js"
|
|
11
10
|
import { getCorePlugins } from "@jsenv/core/src/plugins/plugins.js"
|
|
@@ -44,10 +43,9 @@ export const startDevServer = async ({
|
|
|
44
43
|
!parentPort &&
|
|
45
44
|
!process.env.VSCODE_INSPECTOR_OPTIONS,
|
|
46
45
|
clientFiles = {
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"./**/node_modules/": false,
|
|
46
|
+
"./src/": true,
|
|
47
|
+
"./src/**/.*/": false, // any folder starting with a dot is ignored (includes .git,.jsenv for instance)
|
|
48
|
+
"./src/**/node_modules/": false,
|
|
51
49
|
},
|
|
52
50
|
cooldownBetweenFileEvents,
|
|
53
51
|
clientAutoreload = true,
|
|
@@ -65,8 +63,7 @@ export const startDevServer = async ({
|
|
|
65
63
|
},
|
|
66
64
|
plugins = [],
|
|
67
65
|
urlAnalysis = {},
|
|
68
|
-
htmlSupervisor =
|
|
69
|
-
injectedGlobals,
|
|
66
|
+
htmlSupervisor = false,
|
|
70
67
|
nodeEsmResolution,
|
|
71
68
|
fileSystemMagicResolution,
|
|
72
69
|
transpilation,
|
|
@@ -188,7 +185,6 @@ export const startDevServer = async ({
|
|
|
188
185
|
|
|
189
186
|
urlAnalysis,
|
|
190
187
|
htmlSupervisor,
|
|
191
|
-
injectedGlobals,
|
|
192
188
|
nodeEsmResolution,
|
|
193
189
|
fileSystemMagicResolution,
|
|
194
190
|
transpilation,
|
package/src/execute/execute.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { Abort, raceProcessTeardownEvents } from "@jsenv/abort"
|
|
2
2
|
|
|
3
3
|
import { assertAndNormalizeDirectoryUrl } from "@jsenv/filesystem"
|
|
4
|
-
import { createLogger } from "@jsenv/
|
|
5
|
-
|
|
4
|
+
import { createLogger } from "@jsenv/log"
|
|
6
5
|
import { getCorePlugins } from "@jsenv/core/src/plugins/plugins.js"
|
|
7
6
|
import { createUrlGraph } from "@jsenv/core/src/omega/url_graph.js"
|
|
8
7
|
import { createKitchen } from "@jsenv/core/src/omega/kitchen.js"
|
|
@@ -32,7 +31,6 @@ export const execute = async ({
|
|
|
32
31
|
plugins = [],
|
|
33
32
|
nodeEsmResolution,
|
|
34
33
|
fileSystemMagicResolution,
|
|
35
|
-
injectedGlobals,
|
|
36
34
|
transpilation,
|
|
37
35
|
htmlSupervisor = true,
|
|
38
36
|
writeGeneratedFiles = false,
|
|
@@ -87,7 +85,6 @@ export const execute = async ({
|
|
|
87
85
|
runtimeCompat,
|
|
88
86
|
|
|
89
87
|
htmlSupervisor,
|
|
90
|
-
injectedGlobals,
|
|
91
88
|
nodeEsmResolution,
|
|
92
89
|
fileSystemMagicResolution,
|
|
93
90
|
transpilation,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Script } from "node:vm"
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
import { createDetailedMessage } from "@jsenv/log"
|
|
3
4
|
import {
|
|
4
5
|
Abort,
|
|
5
6
|
createCallbackListNotifiedOnce,
|
|
@@ -7,7 +8,6 @@ import {
|
|
|
7
8
|
raceCallbacks,
|
|
8
9
|
} from "@jsenv/abort"
|
|
9
10
|
import { moveUrl } from "@jsenv/urls"
|
|
10
|
-
|
|
11
11
|
import { memoize } from "@jsenv/utils/memoize/memoize.js"
|
|
12
12
|
import { filterV8Coverage } from "@jsenv/utils/coverage/v8_coverage_from_directory.js"
|
|
13
13
|
import { composeTwoFileByFileIstanbulCoverages } from "@jsenv/utils/coverage/istanbul_coverage_composition.js"
|
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
import { fork } from "node:child_process"
|
|
2
|
-
import { urlToFileSystemPath } from "@jsenv/urls"
|
|
3
|
-
import { createDetailedMessage } from "@jsenv/logger"
|
|
4
2
|
import {
|
|
5
3
|
Abort,
|
|
6
4
|
raceCallbacks,
|
|
@@ -8,6 +6,8 @@ import {
|
|
|
8
6
|
} from "@jsenv/abort"
|
|
9
7
|
import { uneval } from "@jsenv/uneval"
|
|
10
8
|
|
|
9
|
+
import { urlToFileSystemPath } from "@jsenv/urls"
|
|
10
|
+
import { createDetailedMessage } from "@jsenv/log"
|
|
11
11
|
import { memoize } from "@jsenv/utils/memoize/memoize.js"
|
|
12
12
|
import { createChildExecOptions } from "./child_exec_options.js"
|
|
13
13
|
import { ExecOptions } from "./exec_options.js"
|
package/src/main.js
CHANGED
|
@@ -24,4 +24,4 @@ export { startBuildServer } from "./build/start_build_server.js"
|
|
|
24
24
|
|
|
25
25
|
// advanced
|
|
26
26
|
export { execute } from "./execute/execute.js"
|
|
27
|
-
export {
|
|
27
|
+
export { jsenvPluginInjectGlobals } from "./plugins/inject_globals/jsenv_plugin_inject_globals.js"
|
package/src/omega/errors.js
CHANGED
package/src/omega/kitchen.js
CHANGED
|
@@ -7,10 +7,8 @@ import {
|
|
|
7
7
|
setUrlFilename,
|
|
8
8
|
} from "@jsenv/urls"
|
|
9
9
|
import { writeFileSync, ensureWindowsDriveLetter } from "@jsenv/filesystem"
|
|
10
|
-
import { createDetailedMessage } from "@jsenv/
|
|
11
|
-
|
|
10
|
+
import { createDetailedMessage } from "@jsenv/log"
|
|
12
11
|
import { CONTENT_TYPE } from "@jsenv/utils/content_type/content_type.js"
|
|
13
|
-
|
|
14
12
|
import { createPluginController } from "../plugins/plugin_controller.js"
|
|
15
13
|
import { urlSpecifierEncoding } from "./url_specifier_encoding.js"
|
|
16
14
|
import { createUrlInfoTransformer } from "./url_graph/url_info_transformations.js"
|
|
@@ -272,24 +270,36 @@ export const createKitchen = ({
|
|
|
272
270
|
)
|
|
273
271
|
return
|
|
274
272
|
}
|
|
275
|
-
|
|
273
|
+
let {
|
|
274
|
+
content,
|
|
275
|
+
contentType,
|
|
276
276
|
data,
|
|
277
277
|
type,
|
|
278
278
|
subtype,
|
|
279
|
-
contentType = "application/octet-stream",
|
|
280
279
|
originalUrl,
|
|
281
280
|
originalContent,
|
|
282
|
-
content,
|
|
283
281
|
sourcemap,
|
|
284
282
|
filename,
|
|
283
|
+
|
|
284
|
+
status = 200,
|
|
285
|
+
headers = {},
|
|
286
|
+
body,
|
|
285
287
|
} = fetchUrlContentReturnValue
|
|
288
|
+
if (status !== 200) {
|
|
289
|
+
throw new Error(`unexpected status, ${status}`)
|
|
290
|
+
}
|
|
291
|
+
if (content === undefined) {
|
|
292
|
+
content = body
|
|
293
|
+
}
|
|
294
|
+
if (contentType === undefined) {
|
|
295
|
+
contentType = headers["content-type"] || "application/octet-stream"
|
|
296
|
+
}
|
|
297
|
+
urlInfo.contentType = contentType
|
|
298
|
+
urlInfo.headers = headers
|
|
286
299
|
urlInfo.type =
|
|
287
300
|
type ||
|
|
288
301
|
reference.expectedType ||
|
|
289
|
-
inferUrlInfoType({
|
|
290
|
-
url: urlInfo.url,
|
|
291
|
-
contentType,
|
|
292
|
-
})
|
|
302
|
+
inferUrlInfoType({ url: urlInfo.url, contentType })
|
|
293
303
|
urlInfo.subtype =
|
|
294
304
|
subtype ||
|
|
295
305
|
reference.expectedSubtype ||
|
|
@@ -298,7 +308,6 @@ export const createKitchen = ({
|
|
|
298
308
|
type: urlInfo.type,
|
|
299
309
|
subtype: urlInfo.subtype,
|
|
300
310
|
})
|
|
301
|
-
urlInfo.contentType = contentType
|
|
302
311
|
// during build urls info are reused and load returns originalUrl/originalContent
|
|
303
312
|
urlInfo.originalUrl = originalUrl || urlInfo.originalUrl
|
|
304
313
|
urlInfo.originalContent =
|
|
@@ -345,8 +354,7 @@ export const createKitchen = ({
|
|
|
345
354
|
const { cookDuringCook = cook } = dishContext
|
|
346
355
|
context.cook = (urlInfo, nestedDishContext) => {
|
|
347
356
|
return cookDuringCook(urlInfo, {
|
|
348
|
-
|
|
349
|
-
clientRuntimeCompat: dishContext.clientRuntimeCompat,
|
|
357
|
+
...dishContext,
|
|
350
358
|
...nestedDishContext,
|
|
351
359
|
})
|
|
352
360
|
}
|
|
@@ -66,7 +66,7 @@ export const createFileService = ({
|
|
|
66
66
|
status: 304,
|
|
67
67
|
headers: {
|
|
68
68
|
"cache-control": `private,max-age=0,must-revalidate`,
|
|
69
|
-
...urlInfo.
|
|
69
|
+
...urlInfo.headers,
|
|
70
70
|
},
|
|
71
71
|
}
|
|
72
72
|
}
|
|
@@ -84,7 +84,6 @@ export const createFileService = ({
|
|
|
84
84
|
urlInfo.type = null
|
|
85
85
|
urlInfo.subtype = null
|
|
86
86
|
urlInfo.timing = {}
|
|
87
|
-
urlInfo.responseHeaders = {}
|
|
88
87
|
}
|
|
89
88
|
const { runtimeName, runtimeVersion } = parseUserAgentHeader(
|
|
90
89
|
request.headers["user-agent"],
|
|
@@ -100,7 +99,7 @@ export const createFileService = ({
|
|
|
100
99
|
? `${rootDirectoryUrl}.jsenv/${runtimeName}@${runtimeVersion}/`
|
|
101
100
|
: `${rootDirectoryUrl}.jsenv/${scenario}/${runtimeName}@${runtimeVersion}/`,
|
|
102
101
|
})
|
|
103
|
-
let { response
|
|
102
|
+
let { response } = urlInfo
|
|
104
103
|
if (response) {
|
|
105
104
|
return response
|
|
106
105
|
}
|
|
@@ -108,13 +107,13 @@ export const createFileService = ({
|
|
|
108
107
|
url: reference.url,
|
|
109
108
|
status: 200,
|
|
110
109
|
headers: {
|
|
111
|
-
"content-
|
|
112
|
-
"content-length": Buffer.byteLength(content),
|
|
110
|
+
"content-length": Buffer.byteLength(urlInfo.content),
|
|
113
111
|
"cache-control": `private,max-age=0,must-revalidate`,
|
|
114
|
-
"eTag": contentEtag,
|
|
115
|
-
...urlInfo.
|
|
112
|
+
"eTag": urlInfo.contentEtag,
|
|
113
|
+
...urlInfo.headers,
|
|
114
|
+
"content-type": urlInfo.contentType,
|
|
116
115
|
},
|
|
117
|
-
body: content,
|
|
116
|
+
body: urlInfo.content,
|
|
118
117
|
timing: urlInfo.timing,
|
|
119
118
|
}
|
|
120
119
|
kitchen.pluginController.callHooks(
|
package/src/omega/url_graph.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { pathToFileURL } from "node:url"
|
|
2
|
+
|
|
2
3
|
import { URL_META } from "@jsenv/url-meta"
|
|
3
4
|
import { isFileSystemPath } from "@jsenv/urls"
|
|
4
|
-
import { createDetailedMessage } from "@jsenv/
|
|
5
|
-
|
|
5
|
+
import { createDetailedMessage } from "@jsenv/log"
|
|
6
6
|
import { babelHelperNameFromUrl } from "@jsenv/babel-plugins"
|
|
7
7
|
import { applyRollupPlugins } from "@jsenv/utils/js_ast/apply_rollup_plugins.js"
|
|
8
8
|
import { sourcemapConverter } from "@jsenv/utils/sourcemap/sourcemap_converter.js"
|
|
@@ -178,15 +178,11 @@ export const jsenvPluginFileUrls = ({
|
|
|
178
178
|
}
|
|
179
179
|
const fileBuffer = readFileSync(urlObject)
|
|
180
180
|
const contentType = CONTENT_TYPE.fromUrlExtension(urlInfo.url)
|
|
181
|
-
if (CONTENT_TYPE.isTextual(contentType)) {
|
|
182
|
-
return {
|
|
183
|
-
contentType,
|
|
184
|
-
content: String(fileBuffer),
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
181
|
return {
|
|
182
|
+
content: CONTENT_TYPE.isTextual(contentType)
|
|
183
|
+
? String(fileBuffer)
|
|
184
|
+
: fileBuffer,
|
|
188
185
|
contentType,
|
|
189
|
-
content: fileBuffer,
|
|
190
186
|
}
|
|
191
187
|
},
|
|
192
188
|
},
|
|
@@ -32,7 +32,10 @@ window.__html_supervisor__ = {
|
|
|
32
32
|
lastWindowError = e.error
|
|
33
33
|
}
|
|
34
34
|
const cleanup = () => {
|
|
35
|
-
|
|
35
|
+
// the execution of the script itself can remove script from the page
|
|
36
|
+
if (script.parentNode) {
|
|
37
|
+
script.parentNode.removeChild(script)
|
|
38
|
+
}
|
|
36
39
|
window.removeEventListener("error", windowErrorCallback)
|
|
37
40
|
}
|
|
38
41
|
window.addEventListener("error", windowErrorCallback)
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import {
|
|
2
|
+
parseHtmlString,
|
|
3
|
+
injectScriptAsEarlyAsPossible,
|
|
4
|
+
createHtmlNode,
|
|
5
|
+
stringifyHtmlAst,
|
|
6
|
+
} from "@jsenv/utils/html_ast/html_ast.js"
|
|
7
|
+
import { createMagicSource } from "@jsenv/utils/sourcemap/magic_source.js"
|
|
8
|
+
|
|
9
|
+
export const injectGlobals = (urlInfo, globals) => {
|
|
10
|
+
if (urlInfo.type === "html") {
|
|
11
|
+
return globalInjectorOnHtml(urlInfo, globals)
|
|
12
|
+
}
|
|
13
|
+
if (urlInfo.type === "js_classic" || urlInfo.type === "js_module") {
|
|
14
|
+
return globalsInjectorOnJs(urlInfo, globals)
|
|
15
|
+
}
|
|
16
|
+
throw new Error(`cannot inject globals into "${urlInfo.type}"`)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const globalInjectorOnHtml = async (urlInfo, globals) => {
|
|
20
|
+
// ideally we would inject an importmap but browser support is too low
|
|
21
|
+
// (even worse for worker/service worker)
|
|
22
|
+
// so for now we inject code into entry points
|
|
23
|
+
const htmlAst = parseHtmlString(urlInfo.content, {
|
|
24
|
+
storeOriginalPositions: false,
|
|
25
|
+
})
|
|
26
|
+
const clientCode = generateClientCodeForGlobals({
|
|
27
|
+
globals,
|
|
28
|
+
isWebWorker: false,
|
|
29
|
+
})
|
|
30
|
+
injectScriptAsEarlyAsPossible(
|
|
31
|
+
htmlAst,
|
|
32
|
+
createHtmlNode({
|
|
33
|
+
"tagName": "script",
|
|
34
|
+
"textContent": clientCode,
|
|
35
|
+
"injected-by": "jsenv:inject_globals",
|
|
36
|
+
}),
|
|
37
|
+
)
|
|
38
|
+
return stringifyHtmlAst(htmlAst)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const globalsInjectorOnJs = async (urlInfo, globals) => {
|
|
42
|
+
const clientCode = generateClientCodeForGlobals({
|
|
43
|
+
globals,
|
|
44
|
+
isWebWorker:
|
|
45
|
+
urlInfo.subtype === "worker" ||
|
|
46
|
+
urlInfo.subtype === "service_worker" ||
|
|
47
|
+
urlInfo.subtype === "shared_worker",
|
|
48
|
+
})
|
|
49
|
+
const magicSource = createMagicSource(urlInfo.content)
|
|
50
|
+
magicSource.prepend(clientCode)
|
|
51
|
+
return magicSource.toContentAndSourcemap()
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const generateClientCodeForGlobals = ({ isWebWorker = false, globals }) => {
|
|
55
|
+
const globalName = isWebWorker ? "self" : "window"
|
|
56
|
+
return `Object.assign(${globalName}, ${JSON.stringify(globals, null, " ")});`
|
|
57
|
+
}
|
|
@@ -1,76 +1,24 @@
|
|
|
1
|
-
import {
|
|
2
|
-
parseHtmlString,
|
|
3
|
-
injectScriptAsEarlyAsPossible,
|
|
4
|
-
createHtmlNode,
|
|
5
|
-
stringifyHtmlAst,
|
|
6
|
-
} from "@jsenv/utils/html_ast/html_ast.js"
|
|
7
|
-
import { createMagicSource } from "@jsenv/utils/sourcemap/magic_source.js"
|
|
8
|
-
import { isWebWorkerUrlInfo } from "@jsenv/core/src/omega/web_workers.js"
|
|
1
|
+
import { injectGlobals } from "./inject_globals.js"
|
|
9
2
|
|
|
10
|
-
export const jsenvPluginInjectGlobals = (
|
|
11
|
-
if (Object.keys(globals).length === 0) {
|
|
12
|
-
return []
|
|
13
|
-
}
|
|
3
|
+
export const jsenvPluginInjectGlobals = (urlAssociations) => {
|
|
14
4
|
return {
|
|
15
5
|
name: "jsenv:inject_globals",
|
|
16
6
|
appliesDuring: "*",
|
|
17
|
-
transformUrlContent: {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
7
|
+
transformUrlContent: async (urlInfo) => {
|
|
8
|
+
const url = Object.keys(urlAssociations).find((url) => {
|
|
9
|
+
return url === urlInfo.url
|
|
10
|
+
})
|
|
11
|
+
if (!url) {
|
|
12
|
+
return null
|
|
13
|
+
}
|
|
14
|
+
let globals = urlAssociations[url]
|
|
15
|
+
if (typeof globals === "function") {
|
|
16
|
+
globals = await globals()
|
|
17
|
+
}
|
|
18
|
+
if (Object.keys(globals).length === 0) {
|
|
19
|
+
return null
|
|
20
|
+
}
|
|
21
|
+
return injectGlobals(urlInfo, globals)
|
|
21
22
|
},
|
|
22
23
|
}
|
|
23
24
|
}
|
|
24
|
-
|
|
25
|
-
export const injectGlobals = (urlInfo, globals) => {
|
|
26
|
-
if (urlInfo.type === "html") {
|
|
27
|
-
return globalInjectorOnHtmlEntryPoint(urlInfo, globals)
|
|
28
|
-
}
|
|
29
|
-
if (urlInfo.type === "js_classic" || urlInfo.type === "js_module") {
|
|
30
|
-
return globalsInjectorOnJsEntryPoints(urlInfo, globals)
|
|
31
|
-
}
|
|
32
|
-
throw new Error(`cannot inject globals into "${urlInfo.type}"`)
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const globalInjectorOnHtmlEntryPoint = async (urlInfo, globals) => {
|
|
36
|
-
if (!urlInfo.data.isEntryPoint) {
|
|
37
|
-
return null
|
|
38
|
-
}
|
|
39
|
-
// ideally we would inject an importmap but browser support is too low
|
|
40
|
-
// (even worse for worker/service worker)
|
|
41
|
-
// so for now we inject code into entry points
|
|
42
|
-
const htmlAst = parseHtmlString(urlInfo.content, {
|
|
43
|
-
storeOriginalPositions: false,
|
|
44
|
-
})
|
|
45
|
-
const clientCode = generateClientCodeForGlobals({
|
|
46
|
-
globals,
|
|
47
|
-
isWebWorker: false,
|
|
48
|
-
})
|
|
49
|
-
injectScriptAsEarlyAsPossible(
|
|
50
|
-
htmlAst,
|
|
51
|
-
createHtmlNode({
|
|
52
|
-
"tagName": "script",
|
|
53
|
-
"textContent": clientCode,
|
|
54
|
-
"injected-by": "jsenv:inject_globals",
|
|
55
|
-
}),
|
|
56
|
-
)
|
|
57
|
-
return stringifyHtmlAst(htmlAst)
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const globalsInjectorOnJsEntryPoints = async (urlInfo, globals) => {
|
|
61
|
-
if (!urlInfo.data.isEntryPoint && !urlInfo.data.isWebWorkerEntryPoint) {
|
|
62
|
-
return null
|
|
63
|
-
}
|
|
64
|
-
const clientCode = generateClientCodeForGlobals({
|
|
65
|
-
globals,
|
|
66
|
-
isWebWorker: isWebWorkerUrlInfo(urlInfo),
|
|
67
|
-
})
|
|
68
|
-
const magicSource = createMagicSource(urlInfo.content)
|
|
69
|
-
magicSource.prepend(clientCode)
|
|
70
|
-
return magicSource.toContentAndSourcemap()
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const generateClientCodeForGlobals = ({ isWebWorker = false, globals }) => {
|
|
74
|
-
const globalName = isWebWorker ? "self" : "window"
|
|
75
|
-
return `Object.assign(${globalName}, ${JSON.stringify(globals, null, " ")});`
|
|
76
|
-
}
|
|
@@ -22,8 +22,8 @@ export const jsenvPluginDataUrls = () => {
|
|
|
22
22
|
} = DATA_URL.parse(urlInfo.url)
|
|
23
23
|
urlInfo.data.base64Flag = base64Flag
|
|
24
24
|
return {
|
|
25
|
-
contentType,
|
|
26
25
|
content: contentFromUrlData({ contentType, base64Flag, urlData }),
|
|
26
|
+
contentType,
|
|
27
27
|
}
|
|
28
28
|
},
|
|
29
29
|
formatUrl: (reference, context) => {
|
|
@@ -40,7 +40,7 @@ export const jsenvPluginDataUrls = () => {
|
|
|
40
40
|
return reference.generatedUrl
|
|
41
41
|
}
|
|
42
42
|
const specifier = DATA_URL.stringify({
|
|
43
|
-
contentType: urlInfo.
|
|
43
|
+
contentType: urlInfo.headers["content-type"],
|
|
44
44
|
base64Flag: urlInfo.data.base64Flag,
|
|
45
45
|
data: urlInfo.data.base64Flag
|
|
46
46
|
? dataToBase64(urlInfo.content)
|
|
@@ -26,10 +26,10 @@ const jsenvPluginInlineUrls = () => {
|
|
|
26
26
|
return null
|
|
27
27
|
}
|
|
28
28
|
return {
|
|
29
|
-
contentType: urlInfo.contentType,
|
|
30
29
|
// we want to fetch the original content otherwise we might re-cook
|
|
31
30
|
// content already cooked
|
|
32
31
|
content: urlInfo.originalContent,
|
|
32
|
+
contentType: urlInfo.contentType,
|
|
33
33
|
}
|
|
34
34
|
},
|
|
35
35
|
}
|
|
@@ -266,11 +266,11 @@ const returnValueAssertions = [
|
|
|
266
266
|
return { content: valueReturned }
|
|
267
267
|
}
|
|
268
268
|
if (typeof valueReturned === "object") {
|
|
269
|
-
const { shouldHandle, content } = valueReturned
|
|
269
|
+
const { shouldHandle, content, body } = valueReturned
|
|
270
270
|
if (shouldHandle === false) {
|
|
271
271
|
return undefined
|
|
272
272
|
}
|
|
273
|
-
if (typeof content !== "string" && !Buffer.isBuffer(content)) {
|
|
273
|
+
if (typeof content !== "string" && !Buffer.isBuffer(content) && !body) {
|
|
274
274
|
throw new Error(
|
|
275
275
|
`Unexpected "content" returned by plugin: it must be a string or a buffer; got ${content}`,
|
|
276
276
|
)
|
package/src/plugins/plugins.js
CHANGED
|
@@ -10,7 +10,6 @@ import { jsenvPluginInline } from "./inline/jsenv_plugin_inline.js"
|
|
|
10
10
|
import { jsenvPluginHtmlSupervisor } from "./html_supervisor/jsenv_plugin_html_supervisor.js"
|
|
11
11
|
import { jsenvPluginCommonJsGlobals } from "./commonjs_globals/jsenv_plugin_commonjs_globals.js"
|
|
12
12
|
import { jsenvPluginImportMetaScenarios } from "./import_meta_scenarios/jsenv_plugin_import_meta_scenarios.js"
|
|
13
|
-
import { jsenvPluginInjectGlobals } from "./inject_globals/jsenv_plugin_inject_globals.js"
|
|
14
13
|
import { jsenvPluginTranspilation } from "./transpilation/jsenv_plugin_transpilation.js"
|
|
15
14
|
import { jsenvPluginNodeRuntime } from "./node_runtime/jsenv_plugin_node_runtime.js"
|
|
16
15
|
// build only
|
|
@@ -32,7 +31,6 @@ export const getCorePlugins = ({
|
|
|
32
31
|
nodeEsmResolution,
|
|
33
32
|
fileSystemMagicResolution,
|
|
34
33
|
directoryReferenceAllowed,
|
|
35
|
-
injectedGlobals,
|
|
36
34
|
transpilation = true,
|
|
37
35
|
minification = false,
|
|
38
36
|
bundling = false,
|
|
@@ -67,7 +65,6 @@ export const getCorePlugins = ({
|
|
|
67
65
|
}),
|
|
68
66
|
jsenvPluginUrlResolution(),
|
|
69
67
|
jsenvPluginUrlVersion(),
|
|
70
|
-
jsenvPluginInjectGlobals(injectedGlobals),
|
|
71
68
|
jsenvPluginCommonJsGlobals(),
|
|
72
69
|
jsenvPluginImportMetaScenarios(),
|
|
73
70
|
|
|
@@ -123,11 +123,11 @@ const jsenvPluginAsJsClassicConversion = ({
|
|
|
123
123
|
})
|
|
124
124
|
urlInfo.data.jsClassicFormat = jsClassicFormat
|
|
125
125
|
return {
|
|
126
|
+
content,
|
|
127
|
+
contentType: "text/javascript",
|
|
128
|
+
type: "js_classic",
|
|
126
129
|
originalUrl: originalUrlInfo.originalUrl,
|
|
127
130
|
originalContent: originalUrlInfo.originalContent,
|
|
128
|
-
type: "js_classic",
|
|
129
|
-
contentType: "text/javascript",
|
|
130
|
-
content,
|
|
131
131
|
sourcemap,
|
|
132
132
|
}
|
|
133
133
|
},
|
|
@@ -98,14 +98,14 @@ const jsenvPluginAsModules = () => {
|
|
|
98
98
|
}
|
|
99
99
|
const jsonText = JSON.stringify(originalUrlInfo.content.trim())
|
|
100
100
|
return {
|
|
101
|
-
originalUrl: originalUrlInfo.originalUrl,
|
|
102
|
-
originalContent: originalUrlInfo.originalContent,
|
|
103
|
-
type: "js_module",
|
|
104
|
-
contentType: "text/javascript",
|
|
105
101
|
// here we could `export default ${jsonText}`:
|
|
106
102
|
// but js engine are optimized to recognize JSON.parse
|
|
107
103
|
// and use a faster parsing strategy
|
|
108
104
|
content: `export default JSON.parse(${jsonText})`,
|
|
105
|
+
contentType: "text/javascript",
|
|
106
|
+
type: "js_module",
|
|
107
|
+
originalUrl: originalUrlInfo.originalUrl,
|
|
108
|
+
originalContent: originalUrlInfo.originalContent,
|
|
109
109
|
}
|
|
110
110
|
},
|
|
111
111
|
}
|
|
@@ -130,10 +130,6 @@ const jsenvPluginAsModules = () => {
|
|
|
130
130
|
canUseTemplateString: true,
|
|
131
131
|
})
|
|
132
132
|
return {
|
|
133
|
-
originalUrl: originalUrlInfo.originalUrl,
|
|
134
|
-
originalContent: originalUrlInfo.originalContent,
|
|
135
|
-
type: "js_module",
|
|
136
|
-
contentType: "text/javascript",
|
|
137
133
|
content: `import { InlineContent } from ${JSON.stringify(
|
|
138
134
|
inlineContentClientFileUrl,
|
|
139
135
|
)}
|
|
@@ -142,6 +138,10 @@ const jsenvPluginAsModules = () => {
|
|
|
142
138
|
const stylesheet = new CSSStyleSheet()
|
|
143
139
|
stylesheet.replaceSync(inlineContent.text)
|
|
144
140
|
export default stylesheet`,
|
|
141
|
+
contentType: "text/javascript",
|
|
142
|
+
type: "js_module",
|
|
143
|
+
originalUrl: originalUrlInfo.originalUrl,
|
|
144
|
+
originalContent: originalUrlInfo.originalContent,
|
|
145
145
|
}
|
|
146
146
|
},
|
|
147
147
|
}
|
|
@@ -166,16 +166,16 @@ const jsenvPluginAsModules = () => {
|
|
|
166
166
|
canUseTemplateString: true,
|
|
167
167
|
})
|
|
168
168
|
return {
|
|
169
|
-
originalUrl: originalUrlInfo.originalUrl,
|
|
170
|
-
originalContent: originalUrlInfo.originalContent,
|
|
171
|
-
type: "js_module",
|
|
172
|
-
contentType: "text/javascript",
|
|
173
169
|
content: `import { InlineContent } from ${JSON.stringify(
|
|
174
170
|
inlineContentClientFileUrl,
|
|
175
171
|
)}
|
|
176
172
|
|
|
177
173
|
const inlineContent = new InlineContent(${textPlain}, { type: "text/plain" })
|
|
178
174
|
export default inlineContent.text`,
|
|
175
|
+
contentType: "text/javascript",
|
|
176
|
+
type: "js_module",
|
|
177
|
+
originalUrl: originalUrlInfo.originalUrl,
|
|
178
|
+
originalContent: originalUrlInfo.originalContent,
|
|
179
179
|
}
|
|
180
180
|
},
|
|
181
181
|
}
|
package/src/test/execute_plan.js
CHANGED
|
@@ -3,10 +3,15 @@ import { memoryUsage } from "node:process"
|
|
|
3
3
|
import wrapAnsi from "wrap-ansi"
|
|
4
4
|
import stripAnsi from "strip-ansi"
|
|
5
5
|
import cuid from "cuid"
|
|
6
|
+
|
|
6
7
|
import { URL_META } from "@jsenv/url-meta"
|
|
7
8
|
import { urlToFileSystemPath } from "@jsenv/urls"
|
|
8
|
-
import {
|
|
9
|
-
|
|
9
|
+
import {
|
|
10
|
+
createDetailedMessage,
|
|
11
|
+
loggerToLevels,
|
|
12
|
+
createLog,
|
|
13
|
+
startSpinner,
|
|
14
|
+
} from "@jsenv/log"
|
|
10
15
|
import { Abort, raceProcessTeardownEvents } from "@jsenv/abort"
|
|
11
16
|
import {
|
|
12
17
|
writeDirectory,
|
|
@@ -57,7 +62,6 @@ export const executePlan = async (
|
|
|
57
62
|
scenario,
|
|
58
63
|
sourcemaps,
|
|
59
64
|
plugins,
|
|
60
|
-
injectedGlobals,
|
|
61
65
|
nodeEsmResolution,
|
|
62
66
|
fileSystemMagicResolution,
|
|
63
67
|
transpilation,
|
|
@@ -144,7 +148,6 @@ export const executePlan = async (
|
|
|
144
148
|
htmlSupervisor: true,
|
|
145
149
|
nodeEsmResolution,
|
|
146
150
|
fileSystemMagicResolution,
|
|
147
|
-
injectedGlobals,
|
|
148
151
|
transpilation: {
|
|
149
152
|
...transpilation,
|
|
150
153
|
getCustomBabelPlugins: ({ clientRuntimeCompat }) => {
|