@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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/ast",
3
- "version": "1.4.3",
3
+ "version": "1.4.4",
4
4
  "description": "Parse and transform HTML,CSS and JS",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -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-plugin-owner": jsenvPluginName,
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 insertAfter(node, bodyNode)
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-plugin-owner": jsenvPluginName,
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 insertBefore(scriptNode, importmapParent, nextSibling)
55
+ return insertHtmlNodeBefore(scriptNode, importmapParent, nextSibling)
58
56
  }
59
57
  if (nextSibling.nodeName === "link") {
60
58
  after = nextSibling
61
59
  }
62
60
  }
63
- return insertAfter(scriptNode, importmapParent, after)
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 insertBefore(scriptNode, headNode, child)
69
+ return insertHtmlNodeBefore(scriptNode, headNode, child)
72
70
  }
73
71
  if (child.nodeName === "link") {
74
72
  after = child
75
73
  }
76
74
  }
77
- return insertAfter(scriptNode, headNode, after)
75
+ return insertHtmlNodeAfter(scriptNode, headNode, after)
78
76
  }
79
77
 
80
- const insertBefore = (nodeToInsert, futureParentNode, futureNextSibling) => {
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 insertAfter = (nodeToInsert, futureParentNode, futurePrevSibling) => {
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
- return Object.keys(object)
131
- .map((key) => `${key}=${valueToHtmlAttributeValue(object[key])}`)
132
- .join(" ")
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) => {
@@ -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-plugin-owner": undefined,
72
- "jsenv-plugin-action": undefined,
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 && isDocumentCurrentScriptSrc(secondArgNode)) {
91
- return "document.currentScript.src"
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
  }
package/src/main.js CHANGED
@@ -22,6 +22,7 @@ export {
22
22
  createHtmlNode,
23
23
  injectHtmlNode,
24
24
  injectScriptNodeAsEarlyAsPossible,
25
+ insertHtmlNodeAfter,
25
26
  } from "./html/html_node.js"
26
27
  export {
27
28
  inlineScriptNode,