@jsenv/core 27.0.0-alpha.24 → 27.0.0-alpha.27
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/package.json +5 -4
- package/src/build/build.js +22 -10
- package/src/build/resync_ressource_hints.js +7 -22
- package/src/omega/kitchen.js +1 -1
- package/src/omega/url_graph/url_graph_report.js +10 -10
- package/src/omega/url_graph.js +10 -1
- package/src/plugins/transpilation/as_js_classic/jsenv_plugin_as_js_classic.js +19 -25
- package/src/plugins/transpilation/as_js_classic/jsenv_plugin_script_type_module_as_classic.js +152 -52
- package/src/plugins/transpilation/as_js_classic/jsenv_plugin_workers_type_module_as_classic.js +8 -2
- package/src/plugins/transpilation/fetch_original_url_info.js +30 -0
- package/src/plugins/transpilation/import_assertions/jsenv_plugin_import_assertions.js +62 -80
- package/src/plugins/url_references/js/js_urls.js +7 -14
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jsenv/core",
|
|
3
|
-
"version": "27.0.0-alpha.
|
|
3
|
+
"version": "27.0.0-alpha.27",
|
|
4
4
|
"description": "Tool to develop, test and build js projects",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
"node": ">=16.13.0"
|
|
12
12
|
},
|
|
13
13
|
"publishConfig": {
|
|
14
|
-
"access": "public"
|
|
14
|
+
"access": "public",
|
|
15
|
+
"registry": "https://registry.npmjs.org"
|
|
15
16
|
},
|
|
16
17
|
"type": "module",
|
|
17
18
|
"imports": {},
|
|
@@ -66,7 +67,7 @@
|
|
|
66
67
|
"@jsenv/node-esm-resolution": "0.0.4",
|
|
67
68
|
"@jsenv/server": "12.6.1",
|
|
68
69
|
"@jsenv/uneval": "1.6.0",
|
|
69
|
-
"@jsenv/utils": "1.4.
|
|
70
|
+
"@jsenv/utils": "1.4.2",
|
|
70
71
|
"construct-style-sheets-polyfill": "3.1.0",
|
|
71
72
|
"cssnano": "5.1.7",
|
|
72
73
|
"cssnano-preset-default": "5.2.7",
|
|
@@ -110,4 +111,4 @@
|
|
|
110
111
|
"redux": "4.1.2",
|
|
111
112
|
"rollup": "2.70.1"
|
|
112
113
|
}
|
|
113
|
-
}
|
|
114
|
+
}
|
package/src/build/build.js
CHANGED
|
@@ -349,10 +349,30 @@ ${Object.keys(rawGraph.urlInfos).join("\n")}`,
|
|
|
349
349
|
// - injecting "?as_js_classic" for the first time
|
|
350
350
|
// - injecting "?as_js_classic" because the parentUrl has it
|
|
351
351
|
if (reference.original) {
|
|
352
|
+
const referenceOriginalUrl = reference.original.url
|
|
353
|
+
let originalBuildUrl
|
|
354
|
+
if (urlIsInsideOf(referenceOriginalUrl, buildDirectoryUrl)) {
|
|
355
|
+
originalBuildUrl = referenceOriginalUrl
|
|
356
|
+
} else {
|
|
357
|
+
originalBuildUrl = Object.keys(rawUrls).find(
|
|
358
|
+
(key) => rawUrls[key] === referenceOriginalUrl,
|
|
359
|
+
)
|
|
360
|
+
}
|
|
361
|
+
let rawUrl
|
|
362
|
+
if (urlIsInsideOf(reference.url, buildDirectoryUrl)) {
|
|
363
|
+
// rawUrl = rawUrls[reference.url] || reference.url
|
|
364
|
+
const originalBuildUrl =
|
|
365
|
+
buildUrlRedirections[referenceOriginalUrl]
|
|
366
|
+
rawUrl = originalBuildUrl
|
|
367
|
+
? rawUrls[originalBuildUrl]
|
|
368
|
+
: reference.url
|
|
369
|
+
} else {
|
|
370
|
+
rawUrl = reference.url
|
|
371
|
+
}
|
|
352
372
|
// the url info do not exists yet (it will be created after this "normalize" hook)
|
|
353
373
|
// And the content will be generated when url is cooked by url graph loader.
|
|
354
374
|
// Here we just want to reserve an url for that file
|
|
355
|
-
const buildUrl = buildUrlsGenerator.generate(
|
|
375
|
+
const buildUrl = buildUrlsGenerator.generate(rawUrl, {
|
|
356
376
|
urlInfo: {
|
|
357
377
|
data: {
|
|
358
378
|
...reference.data,
|
|
@@ -364,15 +384,8 @@ ${Object.keys(rawGraph.urlInfos).join("\n")}`,
|
|
|
364
384
|
filename: reference.filename,
|
|
365
385
|
},
|
|
366
386
|
})
|
|
367
|
-
const originalUrl = reference.original.url
|
|
368
|
-
const originalBuildUrl = urlIsInsideOf(
|
|
369
|
-
reference.url,
|
|
370
|
-
buildDirectoryUrl,
|
|
371
|
-
)
|
|
372
|
-
? originalUrl
|
|
373
|
-
: Object.keys(rawUrls).find((key) => rawUrls[key] === originalUrl)
|
|
374
387
|
buildUrlRedirections[originalBuildUrl] = buildUrl
|
|
375
|
-
rawUrls[buildUrl] =
|
|
388
|
+
rawUrls[buildUrl] = rawUrl
|
|
376
389
|
return buildUrl
|
|
377
390
|
}
|
|
378
391
|
if (reference.isInline) {
|
|
@@ -811,7 +824,6 @@ ${Object.keys(finalGraph.urlInfos).join("\n")}`,
|
|
|
811
824
|
finalGraph,
|
|
812
825
|
rawUrls,
|
|
813
826
|
buildUrls,
|
|
814
|
-
buildUrlRedirections,
|
|
815
827
|
})
|
|
816
828
|
const cleanupActions = []
|
|
817
829
|
GRAPH.forEach(finalGraph, (urlInfo) => {
|
|
@@ -1,21 +1,24 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Update <link rel="preload"> and friends after build (once we know everything)
|
|
3
|
+
*
|
|
4
|
+
* - Used to remove ressource hint targeting an url that is no longer used:
|
|
5
|
+
* - Happens because of import assertions transpilation (file is inlined into JS)
|
|
6
|
+
*/
|
|
7
|
+
|
|
1
8
|
import {
|
|
2
9
|
parseHtmlString,
|
|
3
10
|
visitHtmlAst,
|
|
4
11
|
stringifyHtmlAst,
|
|
5
12
|
getHtmlNodeAttributeByName,
|
|
6
|
-
assignHtmlNodeAttributes,
|
|
7
13
|
removeHtmlNode,
|
|
8
14
|
} from "@jsenv/utils/html_ast/html_ast.js"
|
|
9
15
|
|
|
10
16
|
import { GRAPH } from "./graph_utils.js"
|
|
11
17
|
|
|
12
|
-
// update ressource hint that where targeting a file that has changed during build
|
|
13
|
-
// (happens for import assertions and file modified by "?as_js_classic")
|
|
14
18
|
export const resyncRessourceHints = async ({
|
|
15
19
|
finalGraphKitchen,
|
|
16
20
|
finalGraph,
|
|
17
21
|
buildUrls,
|
|
18
|
-
buildUrlRedirections,
|
|
19
22
|
}) => {
|
|
20
23
|
const ressourceHintActions = []
|
|
21
24
|
GRAPH.forEach(finalGraph, (urlInfo) => {
|
|
@@ -40,24 +43,6 @@ export const resyncRessourceHints = async ({
|
|
|
40
43
|
removeHtmlNode(linkNode)
|
|
41
44
|
return
|
|
42
45
|
}
|
|
43
|
-
const buildUrlRedirected = buildUrlRedirections[buildUrl]
|
|
44
|
-
if (buildUrlRedirected) {
|
|
45
|
-
const urlInfoRedirected = finalGraph.getUrlInfo(buildUrlRedirected)
|
|
46
|
-
hrefAttribute.value = urlInfoRedirected.data.buildUrlSpecifier
|
|
47
|
-
|
|
48
|
-
if (
|
|
49
|
-
urlInfo.type === "js_module" &&
|
|
50
|
-
urlInfoRedirected.type === "js_classic"
|
|
51
|
-
) {
|
|
52
|
-
const relAttribute = getHtmlNodeAttributeByName(linkNode, "rel")
|
|
53
|
-
if (relAttribute && relAttribute.value === "modulepreload") {
|
|
54
|
-
assignHtmlNodeAttributes(linkNode, {
|
|
55
|
-
rel: "preload",
|
|
56
|
-
as: "script",
|
|
57
|
-
})
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
46
|
}
|
|
62
47
|
visitHtmlAst(htmlAst, (node) => {
|
|
63
48
|
if (node.nodeName !== "link") {
|
package/src/omega/kitchen.js
CHANGED
|
@@ -451,7 +451,7 @@ export const createKitchen = ({
|
|
|
451
451
|
currentUrlInfo !== newUrlInfo &&
|
|
452
452
|
currentUrlInfo.dependents.size === 0
|
|
453
453
|
) {
|
|
454
|
-
|
|
454
|
+
context.urlGraph.deleteUrlInfo(currentReference.url)
|
|
455
455
|
}
|
|
456
456
|
return [nextReference, newUrlInfo]
|
|
457
457
|
},
|
|
@@ -22,7 +22,7 @@ const createUrlGraphReport = (urlGraph) => {
|
|
|
22
22
|
html: 0,
|
|
23
23
|
css: 0,
|
|
24
24
|
js: 0,
|
|
25
|
-
|
|
25
|
+
other: 0,
|
|
26
26
|
sourcemaps: 0,
|
|
27
27
|
total: 0,
|
|
28
28
|
}
|
|
@@ -31,7 +31,7 @@ const createUrlGraphReport = (urlGraph) => {
|
|
|
31
31
|
css: 0,
|
|
32
32
|
js: 0,
|
|
33
33
|
sourcemaps: 0,
|
|
34
|
-
|
|
34
|
+
other: 0,
|
|
35
35
|
total: 0,
|
|
36
36
|
}
|
|
37
37
|
Object.keys(urlInfos).forEach((url) => {
|
|
@@ -81,8 +81,8 @@ const createUrlGraphReport = (urlGraph) => {
|
|
|
81
81
|
sizeGroups.js += urlContentSize
|
|
82
82
|
return
|
|
83
83
|
}
|
|
84
|
-
countGroups.
|
|
85
|
-
sizeGroups.
|
|
84
|
+
countGroups.other++
|
|
85
|
+
sizeGroups.other += urlContentSize
|
|
86
86
|
return
|
|
87
87
|
})
|
|
88
88
|
return {
|
|
@@ -90,7 +90,7 @@ const createUrlGraphReport = (urlGraph) => {
|
|
|
90
90
|
css: { count: countGroups.css, size: sizeGroups.css },
|
|
91
91
|
js: { count: countGroups.js, size: sizeGroups.js },
|
|
92
92
|
sourcemaps: { count: countGroups.sourcemaps, size: sizeGroups.sourcemaps },
|
|
93
|
-
|
|
93
|
+
other: { count: countGroups.other, size: sizeGroups.other },
|
|
94
94
|
total: { count: countGroups.total, size: sizeGroups.total },
|
|
95
95
|
}
|
|
96
96
|
}
|
|
@@ -108,10 +108,10 @@ const determineCategory = (urlInfo) => {
|
|
|
108
108
|
if (urlInfo.type === "js_module" || urlInfo.type === "js_classic") {
|
|
109
109
|
return "js"
|
|
110
110
|
}
|
|
111
|
-
return "
|
|
111
|
+
return "other"
|
|
112
112
|
}
|
|
113
113
|
|
|
114
|
-
const createRepartitionMessage = ({ html, css, js,
|
|
114
|
+
const createRepartitionMessage = ({ html, css, js, other }) => {
|
|
115
115
|
const parts = []
|
|
116
116
|
if (html.count) {
|
|
117
117
|
parts.push(
|
|
@@ -141,10 +141,10 @@ const createRepartitionMessage = ({ html, css, js, assets }) => {
|
|
|
141
141
|
// } (${byteAsFileSize(sourcemaps.size)})`,
|
|
142
142
|
// )
|
|
143
143
|
// }
|
|
144
|
-
if (
|
|
144
|
+
if (other.count) {
|
|
145
145
|
parts.push(
|
|
146
|
-
`${ANSI.color(`
|
|
147
|
-
|
|
146
|
+
`${ANSI.color(`other:`, ANSI.GREY)} ${other.count} (${byteAsFileSize(
|
|
147
|
+
other.size,
|
|
148
148
|
)})`,
|
|
149
149
|
)
|
|
150
150
|
}
|
package/src/omega/url_graph.js
CHANGED
|
@@ -4,7 +4,16 @@ import { urlToRelativeUrl } from "@jsenv/filesystem"
|
|
|
4
4
|
export const createUrlGraph = () => {
|
|
5
5
|
const urlInfos = {}
|
|
6
6
|
const getUrlInfo = (url) => urlInfos[url]
|
|
7
|
-
const deleteUrlInfo = (url) =>
|
|
7
|
+
const deleteUrlInfo = (url) => {
|
|
8
|
+
const urlInfo = urlInfos[url]
|
|
9
|
+
if (urlInfo) {
|
|
10
|
+
delete urlInfos[url]
|
|
11
|
+
if (urlInfo.sourcemapReference) {
|
|
12
|
+
deleteUrlInfo(urlInfo.sourcemapReference.url)
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
8
17
|
const reuseOrCreateUrlInfo = (url) => {
|
|
9
18
|
const existingUrlInfo = urlInfos[url]
|
|
10
19
|
if (existingUrlInfo) return existingUrlInfo
|
|
@@ -15,11 +15,12 @@ import { createRequire } from "node:module"
|
|
|
15
15
|
import { readFileSync, urlToFilename } from "@jsenv/filesystem"
|
|
16
16
|
|
|
17
17
|
import { requireBabelPlugin } from "@jsenv/babel-plugins"
|
|
18
|
-
|
|
19
18
|
import { applyBabelPlugins } from "@jsenv/utils/js_ast/apply_babel_plugins.js"
|
|
20
19
|
import { injectQueryParams } from "@jsenv/utils/urls/url_utils.js"
|
|
21
20
|
import { createMagicSource } from "@jsenv/utils/sourcemap/magic_source.js"
|
|
22
21
|
import { composeTwoSourcemaps } from "@jsenv/utils/sourcemap/sourcemap_composition_v3.js"
|
|
22
|
+
|
|
23
|
+
import { fetchOriginalUrlInfo } from "../fetch_original_url_info.js"
|
|
23
24
|
import { babelPluginTransformImportMetaUrl } from "./helpers/babel_plugin_transform_import_meta_url.js"
|
|
24
25
|
import { jsenvPluginScriptTypeModuleAsClassic } from "./jsenv_plugin_script_type_module_as_classic.js"
|
|
25
26
|
import { jsenvPluginWorkersTypeModuleAsClassic } from "./jsenv_plugin_workers_type_module_as_classic.js"
|
|
@@ -48,13 +49,16 @@ const asJsClassic = ({ systemJsInjection, systemJsClientFileUrl }) => {
|
|
|
48
49
|
appliesDuring: "*",
|
|
49
50
|
// forward ?as_js_classic to referenced urls
|
|
50
51
|
normalizeUrl: (reference, context) => {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
52
|
+
// We want to propagate transformation of js module to js classic
|
|
53
|
+
// but only for import specifier (static/dynamic import + re-export)
|
|
54
|
+
// All other references won't get the ?as_js_classic
|
|
55
|
+
// otherwise we could try to transform inline ressources, specifiers inside new URL(). ...
|
|
56
|
+
if (
|
|
57
|
+
reference.type !== "js_import_export" &&
|
|
58
|
+
reference.subtype !== "system_register_arg" &&
|
|
59
|
+
reference.subtype !== "system_import_arg"
|
|
60
|
+
) {
|
|
61
|
+
return null
|
|
58
62
|
}
|
|
59
63
|
const parentUrlInfo = context.urlGraph.getUrlInfo(reference.parentUrl)
|
|
60
64
|
if (
|
|
@@ -70,28 +74,18 @@ const asJsClassic = ({ systemJsInjection, systemJsClientFileUrl }) => {
|
|
|
70
74
|
return urlTransformed
|
|
71
75
|
},
|
|
72
76
|
fetchUrlContent: async (urlInfo, context) => {
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
78
|
-
searchParams.delete("as_js_classic")
|
|
79
|
-
const originalUrl = urlObject.href
|
|
80
|
-
const originalReference = {
|
|
81
|
-
...(context.reference.original || context.reference),
|
|
77
|
+
const originalUrlInfo = await fetchOriginalUrlInfo({
|
|
78
|
+
urlInfo,
|
|
79
|
+
context,
|
|
80
|
+
searchParam: "as_js_classic",
|
|
82
81
|
// override the expectedType to "js_module"
|
|
83
82
|
// because when there is ?as_js_classic it means the underlying ressource
|
|
84
83
|
// is a js_module
|
|
85
84
|
expectedType: "js_module",
|
|
86
|
-
}
|
|
87
|
-
originalReference.url = originalUrl
|
|
88
|
-
const originalUrlInfo = context.urlGraph.reuseOrCreateUrlInfo(
|
|
89
|
-
originalReference.url,
|
|
90
|
-
)
|
|
91
|
-
await context.fetchUrlContent({
|
|
92
|
-
reference: originalReference,
|
|
93
|
-
urlInfo: originalUrlInfo,
|
|
94
85
|
})
|
|
86
|
+
if (!originalUrlInfo) {
|
|
87
|
+
return null
|
|
88
|
+
}
|
|
95
89
|
const isJsEntryPoint =
|
|
96
90
|
// in general html files are entry points
|
|
97
91
|
// but during build js can be sepcified as an entry point
|
package/src/plugins/transpilation/as_js_classic/jsenv_plugin_script_type_module_as_classic.js
CHANGED
|
@@ -2,7 +2,8 @@ import {
|
|
|
2
2
|
getHtmlNodeAttributeByName,
|
|
3
3
|
getHtmlNodeTextNode,
|
|
4
4
|
parseHtmlString,
|
|
5
|
-
|
|
5
|
+
removeHtmlNodeAttributeByName,
|
|
6
|
+
assignHtmlNodeAttributes,
|
|
6
7
|
stringifyHtmlAst,
|
|
7
8
|
visitHtmlAst,
|
|
8
9
|
htmlNodePosition,
|
|
@@ -30,51 +31,163 @@ export const jsenvPluginScriptTypeModuleAsClassic = ({
|
|
|
30
31
|
return null
|
|
31
32
|
}
|
|
32
33
|
const htmlAst = parseHtmlString(urlInfo.content)
|
|
33
|
-
const
|
|
34
|
-
const
|
|
35
|
-
const
|
|
34
|
+
const preloadAsScriptNodes = []
|
|
35
|
+
const modulePreloadNodes = []
|
|
36
|
+
const moduleScriptNodes = []
|
|
37
|
+
const classicScriptNodes = []
|
|
38
|
+
const visitLinkNodes = (node) => {
|
|
39
|
+
if (node.nodeName !== "link") {
|
|
40
|
+
return
|
|
41
|
+
}
|
|
42
|
+
const relAttribute = getHtmlNodeAttributeByName(node, "rel")
|
|
43
|
+
const rel = relAttribute ? relAttribute.value : undefined
|
|
44
|
+
if (rel === "modulepreload") {
|
|
45
|
+
modulePreloadNodes.push(node)
|
|
46
|
+
return
|
|
47
|
+
}
|
|
48
|
+
if (rel === "preload") {
|
|
49
|
+
const asAttribute = getHtmlNodeAttributeByName(node, "as")
|
|
50
|
+
const as = asAttribute ? asAttribute.value : undefined
|
|
51
|
+
if (as === "script") {
|
|
52
|
+
preloadAsScriptNodes.push(node)
|
|
53
|
+
}
|
|
54
|
+
return
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const visitScriptNodes = (node) => {
|
|
36
58
|
if (node.nodeName !== "script") {
|
|
37
59
|
return
|
|
38
60
|
}
|
|
39
61
|
const typeAttribute = getHtmlNodeAttributeByName(node, "type")
|
|
40
|
-
|
|
62
|
+
const type = typeAttribute ? typeAttribute.value : undefined
|
|
63
|
+
if (type === "module") {
|
|
64
|
+
moduleScriptNodes.push(node)
|
|
65
|
+
return
|
|
66
|
+
}
|
|
67
|
+
if (type === undefined || type === "text/javascript") {
|
|
68
|
+
classicScriptNodes.push(node)
|
|
41
69
|
return
|
|
42
70
|
}
|
|
43
|
-
|
|
71
|
+
}
|
|
72
|
+
visitHtmlAst(htmlAst, (node) => {
|
|
73
|
+
visitLinkNodes(node)
|
|
74
|
+
visitScriptNodes(node)
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
const classicScriptUrls = []
|
|
78
|
+
const moduleScriptUrls = []
|
|
79
|
+
classicScriptNodes.forEach((classicScriptNode) => {
|
|
80
|
+
const srcAttribute = getHtmlNodeAttributeByName(
|
|
81
|
+
classicScriptNode,
|
|
82
|
+
"src",
|
|
83
|
+
)
|
|
84
|
+
if (srcAttribute) {
|
|
85
|
+
const url = new URL(srcAttribute.value, urlInfo.url).href
|
|
86
|
+
classicScriptUrls.push(url)
|
|
87
|
+
}
|
|
88
|
+
})
|
|
89
|
+
moduleScriptNodes.forEach((moduleScriptNode) => {
|
|
90
|
+
const srcAttribute = getHtmlNodeAttributeByName(
|
|
91
|
+
moduleScriptNode,
|
|
92
|
+
"src",
|
|
93
|
+
)
|
|
94
|
+
if (srcAttribute) {
|
|
95
|
+
const url = new URL(srcAttribute.value, urlInfo.url).href
|
|
96
|
+
moduleScriptUrls.push(url)
|
|
97
|
+
}
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
const jsModuleUrls = []
|
|
101
|
+
const getReferenceAsJsClassic = async (reference) => {
|
|
102
|
+
const [newReference, newUrlInfo] = context.referenceUtils.update(
|
|
103
|
+
reference,
|
|
104
|
+
{
|
|
105
|
+
expectedType: "js_classic",
|
|
106
|
+
specifier: injectQueryParamsIntoSpecifier(reference.specifier, {
|
|
107
|
+
as_js_classic: "",
|
|
108
|
+
}),
|
|
109
|
+
filename: generateJsClassicFilename(reference.url),
|
|
110
|
+
},
|
|
111
|
+
)
|
|
112
|
+
const jsModuleUrl = newUrlInfo.url
|
|
113
|
+
if (!jsModuleUrls.includes(jsModuleUrl)) {
|
|
114
|
+
jsModuleUrls.push(newUrlInfo.url)
|
|
115
|
+
// during dev it means js modules will be cooked before server sends the HTML
|
|
116
|
+
// it's ok because:
|
|
117
|
+
// - during dev script_type_module are supported (dev use a recent browser)
|
|
118
|
+
// - even if browser is not supported it still works it's jus a bit slower
|
|
119
|
+
// because it needs to decide if systemjs will be injected or not
|
|
120
|
+
await context.cook({
|
|
121
|
+
reference: newReference,
|
|
122
|
+
urlInfo: newUrlInfo,
|
|
123
|
+
})
|
|
124
|
+
}
|
|
125
|
+
return [newReference, newUrlInfo]
|
|
126
|
+
}
|
|
127
|
+
const actions = []
|
|
128
|
+
preloadAsScriptNodes.forEach((preloadAsScriptNode) => {
|
|
129
|
+
const hrefAttribute = getHtmlNodeAttributeByName(
|
|
130
|
+
preloadAsScriptNode,
|
|
131
|
+
"href",
|
|
132
|
+
)
|
|
133
|
+
const href = hrefAttribute.value
|
|
134
|
+
const url = new URL(href, urlInfo.url).href
|
|
135
|
+
const expectedScriptType = moduleScriptUrls.includes(url)
|
|
136
|
+
? "module"
|
|
137
|
+
: "classic"
|
|
138
|
+
// keep in mind:
|
|
139
|
+
// when the url is not referenced by a <script type="module">
|
|
140
|
+
// we assume we want to preload "classic" but it might not be the case
|
|
141
|
+
// but it's unlikely to happen and people should use "modulepreload" in that case anyway
|
|
142
|
+
if (expectedScriptType === "module") {
|
|
143
|
+
actions.push(async () => {
|
|
144
|
+
const [newReference] = await getReferenceAsJsClassic(
|
|
145
|
+
context.referenceUtils.findByGeneratedSpecifier(href),
|
|
146
|
+
)
|
|
147
|
+
assignHtmlNodeAttributes(preloadAsScriptNode, {
|
|
148
|
+
href: newReference.generatedSpecifier,
|
|
149
|
+
})
|
|
150
|
+
removeHtmlNodeAttributeByName(preloadAsScriptNode, "crossorigin")
|
|
151
|
+
})
|
|
152
|
+
}
|
|
153
|
+
})
|
|
154
|
+
modulePreloadNodes.forEach((modulePreloadNode) => {
|
|
155
|
+
const hrefAttribute = getHtmlNodeAttributeByName(
|
|
156
|
+
modulePreloadNode,
|
|
157
|
+
"href",
|
|
158
|
+
)
|
|
159
|
+
const href = hrefAttribute.value
|
|
160
|
+
actions.push(async () => {
|
|
161
|
+
const [newReference] = await getReferenceAsJsClassic(
|
|
162
|
+
context.referenceUtils.findByGeneratedSpecifier(href),
|
|
163
|
+
)
|
|
164
|
+
assignHtmlNodeAttributes(modulePreloadNode, {
|
|
165
|
+
rel: "preload",
|
|
166
|
+
as: "script",
|
|
167
|
+
href: newReference.generatedSpecifier,
|
|
168
|
+
})
|
|
169
|
+
})
|
|
170
|
+
})
|
|
171
|
+
moduleScriptNodes.forEach((moduleScriptNode) => {
|
|
172
|
+
const srcAttribute = getHtmlNodeAttributeByName(
|
|
173
|
+
moduleScriptNode,
|
|
174
|
+
"src",
|
|
175
|
+
)
|
|
44
176
|
if (srcAttribute) {
|
|
45
177
|
actions.push(async () => {
|
|
46
178
|
const specifier = srcAttribute.value
|
|
47
|
-
const
|
|
48
|
-
context.referenceUtils.findByGeneratedSpecifier(specifier)
|
|
49
|
-
const [newReference, newUrlInfo] = context.referenceUtils.update(
|
|
50
|
-
reference,
|
|
51
|
-
{
|
|
52
|
-
expectedType: "js_classic",
|
|
53
|
-
specifier: injectQueryParamsIntoSpecifier(specifier, {
|
|
54
|
-
as_js_classic: "",
|
|
55
|
-
}),
|
|
56
|
-
filename: generateJsClassicFilename(reference.url),
|
|
57
|
-
},
|
|
179
|
+
const [newReference] = await getReferenceAsJsClassic(
|
|
180
|
+
context.referenceUtils.findByGeneratedSpecifier(specifier),
|
|
58
181
|
)
|
|
59
|
-
|
|
182
|
+
removeHtmlNodeAttributeByName(moduleScriptNode, "type")
|
|
60
183
|
srcAttribute.value = newReference.generatedSpecifier
|
|
61
|
-
// during dev it means js modules will be cooked before server sends the HTML
|
|
62
|
-
// it's ok because:
|
|
63
|
-
// - during dev script_type_module are supported (dev use a recent browser)
|
|
64
|
-
// - even if browser is not supported it still works it's jus a bit slower
|
|
65
|
-
// because it needs to decide if systemjs will be injected or not
|
|
66
|
-
await context.cook({
|
|
67
|
-
reference: newReference,
|
|
68
|
-
urlInfo: newUrlInfo,
|
|
69
|
-
})
|
|
70
|
-
jsModuleUrlInfos.push(newUrlInfo)
|
|
71
184
|
})
|
|
72
185
|
return
|
|
73
186
|
}
|
|
74
|
-
const textNode = getHtmlNodeTextNode(
|
|
187
|
+
const textNode = getHtmlNodeTextNode(moduleScriptNode)
|
|
75
188
|
actions.push(async () => {
|
|
76
189
|
const { line, column, lineEnd, columnEnd, isOriginal } =
|
|
77
|
-
htmlNodePosition.readNodePosition(
|
|
190
|
+
htmlNodePosition.readNodePosition(moduleScriptNode, {
|
|
78
191
|
preferOriginal: true,
|
|
79
192
|
})
|
|
80
193
|
let inlineScriptUrl = generateInlineContentUrl({
|
|
@@ -86,7 +199,7 @@ export const jsenvPluginScriptTypeModuleAsClassic = ({
|
|
|
86
199
|
columnEnd,
|
|
87
200
|
})
|
|
88
201
|
const [inlineReference] = context.referenceUtils.foundInline({
|
|
89
|
-
node,
|
|
202
|
+
node: moduleScriptNode,
|
|
90
203
|
type: "script_src",
|
|
91
204
|
expectedType: "js_module",
|
|
92
205
|
// we remove 1 to the line because imagine the following html:
|
|
@@ -99,39 +212,26 @@ export const jsenvPluginScriptTypeModuleAsClassic = ({
|
|
|
99
212
|
contentType: "application/javascript",
|
|
100
213
|
content: textNode.value,
|
|
101
214
|
})
|
|
102
|
-
const [
|
|
215
|
+
const [, newUrlInfo] = await getReferenceAsJsClassic(
|
|
103
216
|
inlineReference,
|
|
104
|
-
{
|
|
105
|
-
expectedType: "js_classic",
|
|
106
|
-
specifier: injectQueryParamsIntoSpecifier(inlineScriptUrl, {
|
|
107
|
-
as_js_classic: "",
|
|
108
|
-
}),
|
|
109
|
-
filename: generateJsClassicFilename(inlineReference.url),
|
|
110
|
-
},
|
|
111
217
|
)
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
urlInfo: newUrlInfo,
|
|
115
|
-
})
|
|
116
|
-
removeHtmlNodeAttribute(node, typeAttribute)
|
|
117
|
-
setHtmlNodeGeneratedText(node, {
|
|
218
|
+
removeHtmlNodeAttributeByName(moduleScriptNode, "type")
|
|
219
|
+
setHtmlNodeGeneratedText(moduleScriptNode, {
|
|
118
220
|
generatedText: newUrlInfo.content,
|
|
119
221
|
generatedBy: "jsenv:script_type_module_as_classic",
|
|
120
222
|
})
|
|
121
|
-
jsModuleUrlInfos.push(newUrlInfo)
|
|
122
223
|
})
|
|
123
|
-
}
|
|
124
|
-
visitHtmlAst(htmlAst, (node) => {
|
|
125
|
-
visitScriptTypeModule(node)
|
|
126
224
|
})
|
|
225
|
+
|
|
127
226
|
if (actions.length === 0) {
|
|
128
227
|
return null
|
|
129
228
|
}
|
|
130
229
|
await Promise.all(actions.map((action) => action()))
|
|
131
230
|
if (systemJsInjection) {
|
|
132
|
-
const needsSystemJs =
|
|
133
|
-
(
|
|
134
|
-
|
|
231
|
+
const needsSystemJs = jsModuleUrls.some(
|
|
232
|
+
(jsModuleUrl) =>
|
|
233
|
+
context.urlGraph.getUrlInfo(jsModuleUrl).data.jsClassicFormat ===
|
|
234
|
+
"system",
|
|
135
235
|
)
|
|
136
236
|
if (needsSystemJs) {
|
|
137
237
|
const [systemJsReference] = context.referenceUtils.inject({
|
package/src/plugins/transpilation/as_js_classic/jsenv_plugin_workers_type_module_as_classic.js
CHANGED
|
@@ -25,6 +25,7 @@ export const jsenvPluginWorkersTypeModuleAsClassic = ({
|
|
|
25
25
|
babelPluginMetadataWorkerMentions,
|
|
26
26
|
{
|
|
27
27
|
workersToTranspile,
|
|
28
|
+
isJsModule: urlInfo.type === "js_module",
|
|
28
29
|
},
|
|
29
30
|
],
|
|
30
31
|
],
|
|
@@ -101,7 +102,10 @@ const getWorkersToTranspile = (urlInfo, context) => {
|
|
|
101
102
|
return { worker, serviceWorker, sharedWorker }
|
|
102
103
|
}
|
|
103
104
|
|
|
104
|
-
const babelPluginMetadataWorkerMentions = (
|
|
105
|
+
const babelPluginMetadataWorkerMentions = (
|
|
106
|
+
_,
|
|
107
|
+
{ workersToTranspile, isJsModule },
|
|
108
|
+
) => {
|
|
105
109
|
return {
|
|
106
110
|
name: "metadata-worker-mentions",
|
|
107
111
|
visitor: {
|
|
@@ -110,7 +114,9 @@ const babelPluginMetadataWorkerMentions = (_, { workersToTranspile }) => {
|
|
|
110
114
|
const visitors = {
|
|
111
115
|
NewExpression: (path) => {
|
|
112
116
|
if (workersToTranspile.worker || workersToTranspile.sharedWorker) {
|
|
113
|
-
const mentions = analyzeNewWorkerOrNewSharedWorker(path
|
|
117
|
+
const mentions = analyzeNewWorkerOrNewSharedWorker(path, {
|
|
118
|
+
isJsModule,
|
|
119
|
+
})
|
|
114
120
|
if (mentions) {
|
|
115
121
|
workerMentions.push(...mentions)
|
|
116
122
|
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export const fetchOriginalUrlInfo = async ({
|
|
2
|
+
urlInfo,
|
|
3
|
+
context,
|
|
4
|
+
searchParam,
|
|
5
|
+
expectedType,
|
|
6
|
+
}) => {
|
|
7
|
+
const urlObject = new URL(urlInfo.url)
|
|
8
|
+
const { searchParams } = urlObject
|
|
9
|
+
if (!searchParams.has(searchParam)) {
|
|
10
|
+
return null
|
|
11
|
+
}
|
|
12
|
+
searchParams.delete(searchParam)
|
|
13
|
+
const originalUrl = urlObject.href
|
|
14
|
+
const originalReference = {
|
|
15
|
+
...(context.reference.original || context.reference),
|
|
16
|
+
expectedType,
|
|
17
|
+
}
|
|
18
|
+
originalReference.url = originalUrl
|
|
19
|
+
const originalUrlInfo = context.urlGraph.reuseOrCreateUrlInfo(
|
|
20
|
+
originalReference.url,
|
|
21
|
+
)
|
|
22
|
+
await context.fetchUrlContent({
|
|
23
|
+
reference: originalReference,
|
|
24
|
+
urlInfo: originalUrlInfo,
|
|
25
|
+
})
|
|
26
|
+
if (originalUrlInfo.dependents.size === 0) {
|
|
27
|
+
context.urlGraph.deleteUrlInfo(originalUrlInfo.url)
|
|
28
|
+
}
|
|
29
|
+
return originalUrlInfo
|
|
30
|
+
}
|
|
@@ -5,6 +5,7 @@ import { createMagicSource } from "@jsenv/utils/sourcemap/magic_source.js"
|
|
|
5
5
|
import { injectQueryParamsIntoSpecifier } from "@jsenv/utils/urls/url_utils.js"
|
|
6
6
|
import { JS_QUOTES } from "@jsenv/utils/string/js_quotes.js"
|
|
7
7
|
|
|
8
|
+
import { fetchOriginalUrlInfo } from "../fetch_original_url_info.js"
|
|
8
9
|
import { babelPluginMetadataImportAssertions } from "./helpers/babel_plugin_metadata_import_assertions.js"
|
|
9
10
|
|
|
10
11
|
export const jsenvPluginImportAssertions = () => {
|
|
@@ -95,125 +96,106 @@ const jsenvPluginAsModules = () => {
|
|
|
95
96
|
const asJsonModule = {
|
|
96
97
|
name: `jsenv:as_json_module`,
|
|
97
98
|
appliesDuring: "*",
|
|
98
|
-
fetchUrlContent: (urlInfo, context) => {
|
|
99
|
-
|
|
99
|
+
fetchUrlContent: async (urlInfo, context) => {
|
|
100
|
+
const originalUrlInfo = await fetchOriginalUrlInfo({
|
|
100
101
|
urlInfo,
|
|
101
102
|
context,
|
|
102
103
|
searchParam: "as_json_module",
|
|
103
|
-
|
|
104
|
-
// here we could `export default ${jsonText}`:
|
|
105
|
-
// but js engine are optimized to recognize JSON.parse
|
|
106
|
-
// and use a faster parsing strategy
|
|
107
|
-
return `export default JSON.parse(${JSON.stringify(
|
|
108
|
-
urlInfo.content.trim(),
|
|
109
|
-
)})`
|
|
110
|
-
},
|
|
104
|
+
expectedType: "json",
|
|
111
105
|
})
|
|
106
|
+
if (!originalUrlInfo) {
|
|
107
|
+
return null
|
|
108
|
+
}
|
|
109
|
+
const jsonText = JSON.stringify(originalUrlInfo.content.trim())
|
|
110
|
+
return {
|
|
111
|
+
type: "js_module",
|
|
112
|
+
contentType: "text/javascript",
|
|
113
|
+
// here we could `export default ${jsonText}`:
|
|
114
|
+
// but js engine are optimized to recognize JSON.parse
|
|
115
|
+
// and use a faster parsing strategy
|
|
116
|
+
content: `export default JSON.parse(${jsonText})`,
|
|
117
|
+
}
|
|
112
118
|
},
|
|
113
119
|
}
|
|
114
120
|
|
|
115
121
|
const asCssModule = {
|
|
116
122
|
name: `jsenv:as_css_module`,
|
|
117
123
|
appliesDuring: "*",
|
|
118
|
-
fetchUrlContent: (urlInfo, context) => {
|
|
119
|
-
|
|
124
|
+
fetchUrlContent: async (urlInfo, context) => {
|
|
125
|
+
const originalUrlInfo = await fetchOriginalUrlInfo({
|
|
120
126
|
urlInfo,
|
|
121
127
|
context,
|
|
122
128
|
searchParam: "as_css_module",
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
const inlineContent = new InlineContent(${cssText}, { type: "text/css" })
|
|
135
|
-
const stylesheet = new CSSStyleSheet()
|
|
136
|
-
stylesheet.replaceSync(inlineContent.text)
|
|
137
|
-
export default stylesheet`
|
|
138
|
-
},
|
|
129
|
+
expectedType: "css",
|
|
130
|
+
})
|
|
131
|
+
if (!originalUrlInfo) {
|
|
132
|
+
return null
|
|
133
|
+
}
|
|
134
|
+
const cssText = JS_QUOTES.escapeSpecialChars(originalUrlInfo.content, {
|
|
135
|
+
// If template string is choosen and runtime do not support template literals
|
|
136
|
+
// it's ok because "jsenv:new_inline_content" plugin executes after this one
|
|
137
|
+
// and convert template strings into raw strings
|
|
138
|
+
canUseTemplateString: true,
|
|
139
139
|
})
|
|
140
|
+
return {
|
|
141
|
+
type: "js_module",
|
|
142
|
+
contentType: "text/javascript",
|
|
143
|
+
content: `import { InlineContent } from ${JSON.stringify(
|
|
144
|
+
inlineContentClientFileUrl,
|
|
145
|
+
)}
|
|
146
|
+
|
|
147
|
+
const inlineContent = new InlineContent(${cssText}, { type: "text/css" })
|
|
148
|
+
const stylesheet = new CSSStyleSheet()
|
|
149
|
+
stylesheet.replaceSync(inlineContent.text)
|
|
150
|
+
export default stylesheet`,
|
|
151
|
+
}
|
|
140
152
|
},
|
|
141
153
|
}
|
|
142
154
|
|
|
143
155
|
const asTextModule = {
|
|
144
156
|
name: `jsenv:as_text_module`,
|
|
145
157
|
appliesDuring: "*",
|
|
146
|
-
fetchUrlContent: (urlInfo, context) => {
|
|
147
|
-
|
|
158
|
+
fetchUrlContent: async (urlInfo, context) => {
|
|
159
|
+
const originalUrlInfo = await fetchOriginalUrlInfo({
|
|
148
160
|
urlInfo,
|
|
149
161
|
context,
|
|
150
162
|
searchParam: "as_text_module",
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
const inlineContent = new InlineContent(${textPlain}, { type: "text/plain" })
|
|
163
|
-
export default inlineContent.text`
|
|
164
|
-
},
|
|
163
|
+
expectedType: "text",
|
|
164
|
+
})
|
|
165
|
+
if (!originalUrlInfo) {
|
|
166
|
+
return null
|
|
167
|
+
}
|
|
168
|
+
const textPlain = JS_QUOTES.escapeSpecialChars(urlInfo.content, {
|
|
169
|
+
// If template string is choosen and runtime do not support template literals
|
|
170
|
+
// it's ok because "jsenv:new_inline_content" plugin executes after this one
|
|
171
|
+
// and convert template strings into raw strings
|
|
172
|
+
canUseTemplateString: true,
|
|
165
173
|
})
|
|
174
|
+
return {
|
|
175
|
+
type: "js_module",
|
|
176
|
+
contentType: "text/javascript",
|
|
177
|
+
content: `import { InlineContent } from ${JSON.stringify(
|
|
178
|
+
inlineContentClientFileUrl,
|
|
179
|
+
)}
|
|
180
|
+
|
|
181
|
+
const inlineContent = new InlineContent(${textPlain}, { type: "text/plain" })
|
|
182
|
+
export default inlineContent.text`,
|
|
183
|
+
}
|
|
166
184
|
},
|
|
167
185
|
}
|
|
168
186
|
|
|
169
187
|
return [asJsonModule, asCssModule, asTextModule]
|
|
170
188
|
}
|
|
171
189
|
|
|
172
|
-
const fetchOriginalUrl = async ({
|
|
173
|
-
urlInfo,
|
|
174
|
-
context,
|
|
175
|
-
searchParam,
|
|
176
|
-
expectedType,
|
|
177
|
-
convertToJsModule,
|
|
178
|
-
}) => {
|
|
179
|
-
const urlObject = new URL(urlInfo.url)
|
|
180
|
-
const { searchParams } = urlObject
|
|
181
|
-
if (!searchParams.has(searchParam)) {
|
|
182
|
-
return null
|
|
183
|
-
}
|
|
184
|
-
searchParams.delete(searchParam)
|
|
185
|
-
const originalUrl = urlObject.href
|
|
186
|
-
const originalReference = {
|
|
187
|
-
...(context.reference.original || context.reference),
|
|
188
|
-
expectedType,
|
|
189
|
-
}
|
|
190
|
-
originalReference.url = originalUrl
|
|
191
|
-
const originalUrlInfo = context.urlGraph.reuseOrCreateUrlInfo(
|
|
192
|
-
originalReference.url,
|
|
193
|
-
)
|
|
194
|
-
await context.fetchUrlContent({
|
|
195
|
-
reference: originalReference,
|
|
196
|
-
urlInfo: originalUrlInfo,
|
|
197
|
-
})
|
|
198
|
-
return {
|
|
199
|
-
type: "js_module",
|
|
200
|
-
contentType: "text/javascript",
|
|
201
|
-
content: convertToJsModule(originalUrlInfo, context),
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
|
|
205
190
|
const importAsInfos = {
|
|
206
191
|
json: {
|
|
207
192
|
searchParam: "as_json_module",
|
|
208
|
-
expectedType: "json",
|
|
209
193
|
},
|
|
210
194
|
css: {
|
|
211
195
|
searchParam: "as_css_module",
|
|
212
|
-
expectedType: "css",
|
|
213
196
|
},
|
|
214
197
|
text: {
|
|
215
198
|
searchParam: "as_text_module",
|
|
216
|
-
expectedType: "text",
|
|
217
199
|
},
|
|
218
200
|
}
|
|
219
201
|
|
|
@@ -18,10 +18,7 @@ export const parseAndTransformJsUrls = async (urlInfo, context) => {
|
|
|
18
18
|
const isWebWorker = isWebWorkerUrlInfo(urlInfo)
|
|
19
19
|
const { metadata } = await applyBabelPlugins({
|
|
20
20
|
babelPlugins: [
|
|
21
|
-
[
|
|
22
|
-
babelPluginMetadataJsUrlMentions,
|
|
23
|
-
{ isJsModule, isWebWorker, searchSystemJs: !isJsModule },
|
|
24
|
-
],
|
|
21
|
+
[babelPluginMetadataJsUrlMentions, { isJsModule, isWebWorker }],
|
|
25
22
|
],
|
|
26
23
|
urlInfo,
|
|
27
24
|
})
|
|
@@ -67,10 +64,7 @@ export const parseAndTransformJsUrls = async (urlInfo, context) => {
|
|
|
67
64
|
* https://github.com/mjackson/babel-plugin-import-visitor
|
|
68
65
|
*
|
|
69
66
|
*/
|
|
70
|
-
const babelPluginMetadataJsUrlMentions = (
|
|
71
|
-
_,
|
|
72
|
-
{ isJsModule, isWebWorker, searchSystemJs },
|
|
73
|
-
) => {
|
|
67
|
+
const babelPluginMetadataJsUrlMentions = (_, { isJsModule, isWebWorker }) => {
|
|
74
68
|
return {
|
|
75
69
|
name: "metadata-js-mentions",
|
|
76
70
|
visitor: {
|
|
@@ -112,17 +106,16 @@ const babelPluginMetadataJsUrlMentions = (
|
|
|
112
106
|
},
|
|
113
107
|
NewExpression: (path) => {
|
|
114
108
|
callStaticAnalyzers(path, [
|
|
115
|
-
analyzeNewWorkerOrNewSharedWorker,
|
|
116
|
-
analyzeNewUrlCall,
|
|
109
|
+
(path) => analyzeNewWorkerOrNewSharedWorker(path, { isJsModule }),
|
|
110
|
+
(path) => analyzeNewUrlCall(path, { isJsModule }),
|
|
117
111
|
])
|
|
118
112
|
},
|
|
119
113
|
}
|
|
120
114
|
const callExpressionStaticAnalysers = [
|
|
121
|
-
...(isJsModule
|
|
115
|
+
...(isJsModule
|
|
116
|
+
? [analyzeImportCall]
|
|
117
|
+
: [analyzeSystemRegisterCall, analyzeSystemImportCall]),
|
|
122
118
|
...(isWebWorker ? [analyzeImportScriptCalls] : []),
|
|
123
|
-
...(searchSystemJs
|
|
124
|
-
? [analyzeSystemRegisterCall, analyzeSystemImportCall]
|
|
125
|
-
: []),
|
|
126
119
|
analyzeServiceWorkerRegisterCall,
|
|
127
120
|
]
|
|
128
121
|
visitors.CallExpression = (path) => {
|