@jsenv/ast 1.1.0 → 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,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/ast",
3
- "version": "1.1.0",
3
+ "version": "1.1.3",
4
4
  "description": "Parse and transform HTML,CSS and JS",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -9,8 +9,7 @@
9
9
  "directory": "packages/ast"
10
10
  },
11
11
  "publishConfig": {
12
- "access": "public",
13
- "registry": "https://registry.npmjs.org"
12
+ "access": "public"
14
13
  },
15
14
  "author": {
16
15
  "name": "dmail",
@@ -34,8 +33,8 @@
34
33
  "test": "node --conditions=development ./scripts/test.mjs"
35
34
  },
36
35
  "dependencies": {
37
- "@babel/parser": "7.18.5",
38
- "@parcel/css": "1.10.1",
36
+ "@babel/parser": "7.18.8",
37
+ "@parcel/css": "1.11.2",
39
38
  "acorn": "8.7.1",
40
39
  "acorn-import-assertions": "1.8.0",
41
40
  "acorn-walk": "8.2.0",
@@ -43,4 +42,4 @@
43
42
  "postcss": "8.4.14",
44
43
  "postcss-value-parser": "4.2.0"
45
44
  }
46
- }
45
+ }
@@ -1,17 +1,80 @@
1
1
  import { getHtmlNodeAttribute } from "./html_node_attributes.js"
2
2
 
3
3
  export const analyzeScriptNode = (scriptNode) => {
4
- const type = getHtmlNodeAttribute(scriptNode, "type")
5
- if (type === undefined || type === "text/javascript") {
6
- return "classic"
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
- if (type === "module") {
9
- return "module"
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 (type === "importmap") {
12
- return "importmap"
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) => {
@@ -27,11 +27,11 @@ export const injectScriptNodeAsEarlyAsPossible = (htmlAst, scriptNode) => {
27
27
  "injected-by": "jsenv",
28
28
  })
29
29
  }
30
- const isModule = analyzeScriptNode(scriptNode) === "module"
31
- if (isModule) {
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