@jsenv/ast 1.4.3 → 1.4.4
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 +1 -1
- package/src/html/html_node.js +25 -14
- package/src/html/html_parse.js +3 -2
- package/src/js/js_static_analysis/new_url.js +12 -2
- package/src/main.js +1 -0
package/package.json
CHANGED
package/src/html/html_node.js
CHANGED
|
@@ -19,12 +19,11 @@ export const createHtmlNode = ({ tagName, textContent = "", ...rest }) => {
|
|
|
19
19
|
|
|
20
20
|
export const injectHtmlNode = (htmlAst, node, jsenvPluginName = "jsenv") => {
|
|
21
21
|
setHtmlNodeAttributes(node, {
|
|
22
|
-
"jsenv-
|
|
23
|
-
"jsenv-plugin-action": "injected",
|
|
22
|
+
"jsenv-injected-by": jsenvPluginName,
|
|
24
23
|
})
|
|
25
24
|
const htmlHtmlNode = findChild(htmlAst, (node) => node.nodeName === "html")
|
|
26
25
|
const bodyNode = findChild(htmlHtmlNode, (node) => node.nodeName === "body")
|
|
27
|
-
return
|
|
26
|
+
return insertHtmlNodeAfter(node, bodyNode)
|
|
28
27
|
}
|
|
29
28
|
|
|
30
29
|
export const injectScriptNodeAsEarlyAsPossible = (
|
|
@@ -33,8 +32,7 @@ export const injectScriptNodeAsEarlyAsPossible = (
|
|
|
33
32
|
jsenvPluginName = "jsenv",
|
|
34
33
|
) => {
|
|
35
34
|
setHtmlNodeAttributes(scriptNode, {
|
|
36
|
-
"jsenv-
|
|
37
|
-
"jsenv-plugin-action": "injected",
|
|
35
|
+
"jsenv-injected-by": jsenvPluginName,
|
|
38
36
|
})
|
|
39
37
|
const isJsModule = analyzeScriptNode(scriptNode).type === "js_module"
|
|
40
38
|
if (isJsModule) {
|
|
@@ -54,13 +52,13 @@ export const injectScriptNodeAsEarlyAsPossible = (
|
|
|
54
52
|
let after = firstImportmapScript
|
|
55
53
|
for (const nextSibling of nextSiblings) {
|
|
56
54
|
if (nextSibling.nodeName === "script") {
|
|
57
|
-
return
|
|
55
|
+
return insertHtmlNodeBefore(scriptNode, importmapParent, nextSibling)
|
|
58
56
|
}
|
|
59
57
|
if (nextSibling.nodeName === "link") {
|
|
60
58
|
after = nextSibling
|
|
61
59
|
}
|
|
62
60
|
}
|
|
63
|
-
return
|
|
61
|
+
return insertHtmlNodeAfter(scriptNode, importmapParent, after)
|
|
64
62
|
}
|
|
65
63
|
}
|
|
66
64
|
const headNode = findChild(htmlAst, (node) => node.nodeName === "html")
|
|
@@ -68,16 +66,20 @@ export const injectScriptNodeAsEarlyAsPossible = (
|
|
|
68
66
|
let after = headNode.childNodes[0]
|
|
69
67
|
for (const child of headNode.childNodes) {
|
|
70
68
|
if (child.nodeName === "script") {
|
|
71
|
-
return
|
|
69
|
+
return insertHtmlNodeBefore(scriptNode, headNode, child)
|
|
72
70
|
}
|
|
73
71
|
if (child.nodeName === "link") {
|
|
74
72
|
after = child
|
|
75
73
|
}
|
|
76
74
|
}
|
|
77
|
-
return
|
|
75
|
+
return insertHtmlNodeAfter(scriptNode, headNode, after)
|
|
78
76
|
}
|
|
79
77
|
|
|
80
|
-
const
|
|
78
|
+
export const insertHtmlNodeBefore = (
|
|
79
|
+
nodeToInsert,
|
|
80
|
+
futureParentNode,
|
|
81
|
+
futureNextSibling,
|
|
82
|
+
) => {
|
|
81
83
|
const { childNodes = [] } = futureParentNode
|
|
82
84
|
const futureIndex = futureNextSibling
|
|
83
85
|
? childNodes.indexOf(futureNextSibling)
|
|
@@ -85,7 +87,11 @@ const insertBefore = (nodeToInsert, futureParentNode, futureNextSibling) => {
|
|
|
85
87
|
injectWithWhitespaces(nodeToInsert, futureParentNode, futureIndex)
|
|
86
88
|
}
|
|
87
89
|
|
|
88
|
-
const
|
|
90
|
+
export const insertHtmlNodeAfter = (
|
|
91
|
+
nodeToInsert,
|
|
92
|
+
futureParentNode,
|
|
93
|
+
futurePrevSibling,
|
|
94
|
+
) => {
|
|
89
95
|
const { childNodes = [] } = futureParentNode
|
|
90
96
|
const futureIndex = futurePrevSibling
|
|
91
97
|
? childNodes.indexOf(futurePrevSibling) + 1
|
|
@@ -127,9 +133,14 @@ const injectWithWhitespaces = (nodeToInsert, futureParentNode, futureIndex) => {
|
|
|
127
133
|
const findChild = ({ childNodes = [] }, predicate) => childNodes.find(predicate)
|
|
128
134
|
|
|
129
135
|
const stringifyAttributes = (object) => {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
136
|
+
let string = ""
|
|
137
|
+
Object.keys(object).forEach((key) => {
|
|
138
|
+
const value = object[key]
|
|
139
|
+
if (value === undefined) return
|
|
140
|
+
if (string !== "") string += " "
|
|
141
|
+
string += `${key}=${valueToHtmlAttributeValue(value)}`
|
|
142
|
+
})
|
|
143
|
+
return string
|
|
133
144
|
}
|
|
134
145
|
|
|
135
146
|
const valueToHtmlAttributeValue = (value) => {
|
package/src/html/html_parse.js
CHANGED
|
@@ -68,8 +68,9 @@ export const stringifyHtmlAst = (
|
|
|
68
68
|
"original-href-position": undefined,
|
|
69
69
|
"inlined-from-src": undefined,
|
|
70
70
|
"inlined-from-href": undefined,
|
|
71
|
-
"jsenv-
|
|
72
|
-
"jsenv-
|
|
71
|
+
"jsenv-cooked-by": undefined,
|
|
72
|
+
"jsenv-inlined-by": undefined,
|
|
73
|
+
"jsenv-injected-by": undefined,
|
|
73
74
|
"jsenv-debug": undefined,
|
|
74
75
|
})
|
|
75
76
|
},
|
|
@@ -87,8 +87,18 @@ const analyzeUrlNodeType = (secondArgNode, { isJsModule }) => {
|
|
|
87
87
|
if (!isJsModule && isContextMetaUrlFromSystemJs(secondArgNode)) {
|
|
88
88
|
return "context.meta.url"
|
|
89
89
|
}
|
|
90
|
-
if (!isJsModule
|
|
91
|
-
|
|
90
|
+
if (!isJsModule) {
|
|
91
|
+
if (isDocumentCurrentScriptSrc(secondArgNode)) {
|
|
92
|
+
return "document.currentScript.src"
|
|
93
|
+
}
|
|
94
|
+
// new URL('specifier', document.currentScript.src)
|
|
95
|
+
// becomes
|
|
96
|
+
// var _currentUrl = document.currentScript.src
|
|
97
|
+
// new URL('specifier', currentUrl)
|
|
98
|
+
// (search for scope.generateUidIdentifier("currentUrl")
|
|
99
|
+
if (secondArgNode.type === "Identifier") {
|
|
100
|
+
return "document.currentScript.src"
|
|
101
|
+
}
|
|
92
102
|
}
|
|
93
103
|
return null
|
|
94
104
|
}
|