@jsenv/ast 1.1.2 → 1.1.3
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
CHANGED
|
@@ -1,17 +1,80 @@
|
|
|
1
1
|
import { getHtmlNodeAttribute } from "./html_node_attributes.js"
|
|
2
2
|
|
|
3
3
|
export const analyzeScriptNode = (scriptNode) => {
|
|
4
|
-
const
|
|
5
|
-
if (
|
|
6
|
-
return
|
|
4
|
+
const typeAttribute = getHtmlNodeAttribute(scriptNode, "type")
|
|
5
|
+
if (typeAttribute === undefined || typeAttribute === "text/javascript") {
|
|
6
|
+
return {
|
|
7
|
+
type: "js_classic",
|
|
8
|
+
contentType: "text/javascript",
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
if (typeAttribute === "module") {
|
|
12
|
+
return {
|
|
13
|
+
type: "js_module",
|
|
14
|
+
contentType: "text/javascript",
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
if (typeAttribute === "importmap") {
|
|
18
|
+
return {
|
|
19
|
+
type: "importmap",
|
|
20
|
+
contentType: "application/importmap+json",
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
// jsx
|
|
24
|
+
if (typeAttribute === "text/jsx") {
|
|
25
|
+
return {
|
|
26
|
+
type: "js_classic",
|
|
27
|
+
contentType: "text/javascript",
|
|
28
|
+
extension: ".jsx",
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
if (typeAttribute === "module/jsx") {
|
|
32
|
+
return {
|
|
33
|
+
type: "js_module",
|
|
34
|
+
contentType: "text/javascript",
|
|
35
|
+
extension: ".jsx",
|
|
36
|
+
}
|
|
7
37
|
}
|
|
8
|
-
|
|
9
|
-
|
|
38
|
+
// typescript
|
|
39
|
+
if (typeAttribute === "text/ts") {
|
|
40
|
+
return {
|
|
41
|
+
type: "js_classic",
|
|
42
|
+
contentType: "text/javascript",
|
|
43
|
+
extension: ".ts",
|
|
44
|
+
}
|
|
10
45
|
}
|
|
11
|
-
if (
|
|
12
|
-
return
|
|
46
|
+
if (typeAttribute === "module/ts") {
|
|
47
|
+
return {
|
|
48
|
+
type: "js_module",
|
|
49
|
+
contentType: "text/javascript",
|
|
50
|
+
extension: ".ts",
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
// typescript and jsx
|
|
54
|
+
if (typeAttribute === "text/tsx") {
|
|
55
|
+
return {
|
|
56
|
+
type: "js_classic",
|
|
57
|
+
contentType: "text/javascript",
|
|
58
|
+
extension: ".tsx",
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (typeAttribute === "module/tsx") {
|
|
62
|
+
return {
|
|
63
|
+
type: "js_module",
|
|
64
|
+
contentType: "text/javascript",
|
|
65
|
+
extension: ".tsx",
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
// from MDN about [type] attribute:
|
|
69
|
+
// "Any other value: The embedded content is treated as a data block
|
|
70
|
+
// which won't be processed by the browser. Developers must use a valid MIME type
|
|
71
|
+
// that is not a JavaScript MIME type to denote data blocks.
|
|
72
|
+
// The src attribute will be ignored."
|
|
73
|
+
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-type
|
|
74
|
+
return {
|
|
75
|
+
type: "text",
|
|
76
|
+
contentType: "text/plain",
|
|
13
77
|
}
|
|
14
|
-
return type
|
|
15
78
|
}
|
|
16
79
|
|
|
17
80
|
export const analyzeLinkNode = (linkNode) => {
|
package/src/html/html_node.js
CHANGED
|
@@ -27,11 +27,11 @@ export const injectScriptNodeAsEarlyAsPossible = (htmlAst, scriptNode) => {
|
|
|
27
27
|
"injected-by": "jsenv",
|
|
28
28
|
})
|
|
29
29
|
}
|
|
30
|
-
const
|
|
31
|
-
if (
|
|
30
|
+
const isJsModule = analyzeScriptNode(scriptNode).type === "js_module"
|
|
31
|
+
if (isJsModule) {
|
|
32
32
|
const firstImportmapScript = findHtmlNode(htmlAst, (node) => {
|
|
33
33
|
if (node.nodeName !== "script") return false
|
|
34
|
-
return analyzeScriptNode(node) === "importmap"
|
|
34
|
+
return analyzeScriptNode(node).type === "importmap"
|
|
35
35
|
})
|
|
36
36
|
if (firstImportmapScript) {
|
|
37
37
|
return insertAfter(
|
|
@@ -31,6 +31,13 @@ export const analyzeNewUrlCall = (node, { isJsModule, onUrl }) => {
|
|
|
31
31
|
const baseUrlType = analyzeUrlNodeType(secondArgNode, { isJsModule })
|
|
32
32
|
if (baseUrlType) {
|
|
33
33
|
// we can understand the second argument
|
|
34
|
+
if (
|
|
35
|
+
baseUrlType === "StringLiteral" &&
|
|
36
|
+
secondArgNode.value === "file:///"
|
|
37
|
+
) {
|
|
38
|
+
// ignore new URL(specifier, "file:///")
|
|
39
|
+
return
|
|
40
|
+
}
|
|
34
41
|
const urlType = analyzeUrlNodeType(firstArgNode, { isJsModule })
|
|
35
42
|
if (urlType === "StringLiteral") {
|
|
36
43
|
// we can understand the first argument
|