@jsenv/ast 1.3.0 → 1.3.2

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.3.0",
3
+ "version": "1.3.2",
4
4
  "description": "Parse and transform HTML,CSS and JS",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -33,8 +33,8 @@
33
33
  "test": "node --conditions=development ./scripts/test.mjs"
34
34
  },
35
35
  "dependencies": {
36
- "@babel/parser": "7.19.0",
37
- "@parcel/css": "1.13.1",
36
+ "@babel/parser": "7.19.1",
37
+ "@parcel/css": "1.14.0",
38
38
  "acorn": "8.8.0",
39
39
  "acorn-import-assertions": "1.8.0",
40
40
  "acorn-walk": "8.2.0",
@@ -29,23 +29,42 @@ export const injectScriptNodeAsEarlyAsPossible = (
29
29
  const isJsModule = analyzeScriptNode(scriptNode).type === "js_module"
30
30
  if (isJsModule) {
31
31
  const firstImportmapScript = findHtmlNode(htmlAst, (node) => {
32
- if (node.nodeName !== "script") return false
33
- return analyzeScriptNode(node).type === "importmap"
32
+ return (
33
+ node.nodeName === "script" &&
34
+ analyzeScriptNode(node).type === "importmap"
35
+ )
34
36
  })
37
+
35
38
  if (firstImportmapScript) {
36
- return insertAfter(
37
- scriptNode,
38
- firstImportmapScript.parentNode,
39
- firstImportmapScript,
39
+ const importmapParent = firstImportmapScript.parentNode
40
+ const importmapSiblings = importmapParent.childNodes
41
+ const nextSiblings = importmapSiblings.slice(
42
+ importmapSiblings.indexOf(firstImportmapScript) + 1,
40
43
  )
44
+ let after = firstImportmapScript
45
+ for (const nextSibling of nextSiblings) {
46
+ if (nextSibling.nodeName === "script") {
47
+ return insertBefore(scriptNode, importmapParent, nextSibling)
48
+ }
49
+ if (nextSibling.nodeName === "link") {
50
+ after = nextSibling
51
+ }
52
+ }
53
+ return insertAfter(scriptNode, importmapParent, after)
41
54
  }
42
55
  }
43
56
  const headNode = findChild(htmlAst, (node) => node.nodeName === "html")
44
57
  .childNodes[0]
45
- const firstHeadScript = findChild(headNode, (node) => {
46
- return node.nodeName === "script"
47
- })
48
- return insertBefore(scriptNode, headNode, firstHeadScript)
58
+ let after = headNode.childNodes[0]
59
+ for (const child of headNode.childNodes) {
60
+ if (child.nodeName === "script") {
61
+ return insertBefore(scriptNode, headNode, child)
62
+ }
63
+ if (child.nodeName === "link") {
64
+ after = child
65
+ }
66
+ }
67
+ return insertAfter(scriptNode, headNode, after)
49
68
  }
50
69
 
51
70
  const insertBefore = (nodeToInsert, futureParentNode, futureNextSibling) => {