@jsenv/ast 2.0.0 → 3.0.0
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/css/parse_css_urls.js +33 -0
- package/src/css/postcss_plugin_url_visitor.js +3 -2
- package/src/html/html_node.js +9 -3
- package/src/js/js_static_analysis/import_export.js +16 -16
- package/src/js/js_static_analysis/import_meta_resolve.js +4 -4
- package/src/js/js_static_analysis/new_url.js +12 -12
- package/src/js/js_static_analysis/system.js +12 -12
- package/src/js/js_static_analysis/web_worker.js +4 -4
- package/src/js/js_static_analysis/web_worker_entry_point.js +4 -4
- package/src/main.js +1 -2
package/package.json
CHANGED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { applyPostCss } from "./apply_post_css.js"
|
|
2
|
+
import { postCssPluginUrlVisitor } from "./postcss_plugin_url_visitor.js"
|
|
3
|
+
|
|
4
|
+
export const parseCssUrls = async ({ css, url }) => {
|
|
5
|
+
const cssUrls = []
|
|
6
|
+
await applyPostCss({
|
|
7
|
+
content: css,
|
|
8
|
+
url,
|
|
9
|
+
sourcemaps: false,
|
|
10
|
+
plugins: [
|
|
11
|
+
postCssPluginUrlVisitor({
|
|
12
|
+
urlVisitor: ({
|
|
13
|
+
type,
|
|
14
|
+
specifier,
|
|
15
|
+
specifierStart,
|
|
16
|
+
specifierEnd,
|
|
17
|
+
specifierLine,
|
|
18
|
+
specifierColumn,
|
|
19
|
+
}) => {
|
|
20
|
+
cssUrls.push({
|
|
21
|
+
type: `css_${type}`,
|
|
22
|
+
specifier,
|
|
23
|
+
start: specifierStart,
|
|
24
|
+
end: specifierEnd,
|
|
25
|
+
line: specifierLine,
|
|
26
|
+
column: specifierColumn,
|
|
27
|
+
})
|
|
28
|
+
},
|
|
29
|
+
}),
|
|
30
|
+
],
|
|
31
|
+
})
|
|
32
|
+
return cssUrls
|
|
33
|
+
}
|
|
@@ -12,8 +12,9 @@ hence sourcemap cannot point the original source location
|
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
14
|
import { pathToFileURL } from "node:url"
|
|
15
|
+
import { createRequire } from "node:module"
|
|
15
16
|
|
|
16
|
-
|
|
17
|
+
const require = createRequire(import.meta.url)
|
|
17
18
|
|
|
18
19
|
export const postCssPluginUrlVisitor = ({ urlVisitor = () => null }) => {
|
|
19
20
|
const parseCssValue = require("postcss-value-parser")
|
|
@@ -100,7 +101,7 @@ export const postCssPluginUrlVisitor = ({ urlVisitor = () => null }) => {
|
|
|
100
101
|
)
|
|
101
102
|
const specifierIndex = atRuleRaw.indexOf(atImportNode.params)
|
|
102
103
|
const specifierStart = atRuleStart + specifierIndex
|
|
103
|
-
const specifierEnd = specifierStart +
|
|
104
|
+
const specifierEnd = specifierStart + parsed.nodes[0].sourceEndIndex
|
|
104
105
|
const specifierLine = atImportNode.source.start.line
|
|
105
106
|
const specifierColumn =
|
|
106
107
|
atImportNode.source.start.column + specifierIndex
|
package/src/html/html_node.js
CHANGED
|
@@ -104,11 +104,11 @@ const injectWithWhitespaces = (nodeToInsert, futureParentNode, futureIndex) => {
|
|
|
104
104
|
const previousSiblings = childNodes.slice(0, futureIndex)
|
|
105
105
|
const nextSiblings = childNodes.slice(futureIndex)
|
|
106
106
|
const futureChildNodes = []
|
|
107
|
-
const previousSibling = previousSiblings[
|
|
107
|
+
const previousSibling = previousSiblings[previousSiblings.length - 1]
|
|
108
108
|
if (previousSibling) {
|
|
109
109
|
futureChildNodes.push(...previousSiblings)
|
|
110
110
|
}
|
|
111
|
-
if (!previousSibling || previousSibling
|
|
111
|
+
if (!previousSibling || !isWhitespaceNode(previousSibling)) {
|
|
112
112
|
futureChildNodes.push({
|
|
113
113
|
nodeName: "#text",
|
|
114
114
|
value: "\n ",
|
|
@@ -117,7 +117,7 @@ const injectWithWhitespaces = (nodeToInsert, futureParentNode, futureIndex) => {
|
|
|
117
117
|
}
|
|
118
118
|
futureChildNodes.push(nodeToInsert)
|
|
119
119
|
const nextSibling = nextSiblings[0]
|
|
120
|
-
if (!nextSibling || nextSibling
|
|
120
|
+
if (!nextSibling || !isWhitespaceNode(nextSibling)) {
|
|
121
121
|
futureChildNodes.push({
|
|
122
122
|
nodeName: "#text",
|
|
123
123
|
value: "\n ",
|
|
@@ -130,6 +130,12 @@ const injectWithWhitespaces = (nodeToInsert, futureParentNode, futureIndex) => {
|
|
|
130
130
|
futureParentNode.childNodes = futureChildNodes
|
|
131
131
|
}
|
|
132
132
|
|
|
133
|
+
const isWhitespaceNode = (node) => {
|
|
134
|
+
if (node.nodeName !== "#text") return false
|
|
135
|
+
if (node.value.length === 0) return false
|
|
136
|
+
return /^\s+$/.test(node.value)
|
|
137
|
+
}
|
|
138
|
+
|
|
133
139
|
const findChild = ({ childNodes = [] }, predicate) => childNodes.find(predicate)
|
|
134
140
|
|
|
135
141
|
const stringifyAttributes = (object) => {
|
|
@@ -7,10 +7,10 @@ export const analyzeImportDeclaration = (node, { onUrl }) => {
|
|
|
7
7
|
type: "js_import",
|
|
8
8
|
subtype: "import_static",
|
|
9
9
|
specifier: specifierNode.value,
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
start: specifierNode.start,
|
|
11
|
+
end: specifierNode.end,
|
|
12
|
+
line: specifierNode.loc.start.line,
|
|
13
|
+
column: specifierNode.loc.start.column,
|
|
14
14
|
expectedType: assertionInfo ? assertionInfo.assert.type : "js_module",
|
|
15
15
|
...assertionInfo,
|
|
16
16
|
})
|
|
@@ -26,10 +26,10 @@ export const analyzeImportExpression = (node, { onUrl }) => {
|
|
|
26
26
|
type: "js_import",
|
|
27
27
|
subtype: "import_dynamic",
|
|
28
28
|
specifier: specifierNode.value,
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
29
|
+
start: specifierNode.start,
|
|
30
|
+
end: specifierNode.end,
|
|
31
|
+
line: specifierNode.loc.start.line,
|
|
32
|
+
column: specifierNode.loc.start.column,
|
|
33
33
|
expectedType: assertionInfo ? assertionInfo.assert.type : "js_module",
|
|
34
34
|
...assertionInfo,
|
|
35
35
|
})
|
|
@@ -48,10 +48,10 @@ export const analyzeExportNamedDeclaration = (node, { onUrl }) => {
|
|
|
48
48
|
type: "js_import",
|
|
49
49
|
subtype: "export_named",
|
|
50
50
|
specifier: specifierNode.value,
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
51
|
+
start: specifierNode.start,
|
|
52
|
+
end: specifierNode.end,
|
|
53
|
+
line: specifierNode.loc.start.line,
|
|
54
|
+
column: specifierNode.loc.start.column,
|
|
55
55
|
})
|
|
56
56
|
}
|
|
57
57
|
export const analyzeExportAllDeclaration = (node, { onUrl }) => {
|
|
@@ -60,10 +60,10 @@ export const analyzeExportAllDeclaration = (node, { onUrl }) => {
|
|
|
60
60
|
type: "js_import",
|
|
61
61
|
subtype: "export_all",
|
|
62
62
|
specifier: specifierNode.value,
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
63
|
+
start: specifierNode.start,
|
|
64
|
+
end: specifierNode.end,
|
|
65
|
+
line: specifierNode.loc.start.line,
|
|
66
|
+
column: specifierNode.loc.start.column,
|
|
67
67
|
})
|
|
68
68
|
}
|
|
69
69
|
|
|
@@ -18,10 +18,10 @@ export const analyzeImportMetaResolveCall = (node, { onUrl }) => {
|
|
|
18
18
|
type: "js_import",
|
|
19
19
|
subtype: "import_meta_resolve",
|
|
20
20
|
specifier: firstArg.value,
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
start: firstArg.start,
|
|
22
|
+
end: firstArg.end,
|
|
23
|
+
line: firstArg.loc.start.line,
|
|
24
|
+
column: firstArg.loc.start.column,
|
|
25
25
|
})
|
|
26
26
|
}
|
|
27
27
|
}
|
|
@@ -17,10 +17,10 @@ export const analyzeNewUrlCall = (node, { isJsModule, onUrl }) => {
|
|
|
17
17
|
type: "js_url",
|
|
18
18
|
subtype: "new_url_first_arg",
|
|
19
19
|
specifier: specifierNode.value,
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
start: specifierNode.start,
|
|
21
|
+
end: specifierNode.end,
|
|
22
|
+
line: specifierNode.loc.start.line,
|
|
23
|
+
column: specifierNode.loc.start.column,
|
|
24
24
|
})
|
|
25
25
|
}
|
|
26
26
|
return
|
|
@@ -46,10 +46,10 @@ export const analyzeNewUrlCall = (node, { isJsModule, onUrl }) => {
|
|
|
46
46
|
type: "js_url",
|
|
47
47
|
subtype: "new_url_first_arg",
|
|
48
48
|
specifier: specifierNode.value,
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
49
|
+
start: specifierNode.start,
|
|
50
|
+
end: specifierNode.end,
|
|
51
|
+
line: specifierNode.loc.start.line,
|
|
52
|
+
column: specifierNode.loc.start.column,
|
|
53
53
|
baseUrlType,
|
|
54
54
|
baseUrl:
|
|
55
55
|
baseUrlType === "StringLiteral" ? secondArgNode.value : undefined,
|
|
@@ -61,10 +61,10 @@ export const analyzeNewUrlCall = (node, { isJsModule, onUrl }) => {
|
|
|
61
61
|
type: "js_url",
|
|
62
62
|
subtype: "new_url_second_arg",
|
|
63
63
|
specifier: specifierNode.value,
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
64
|
+
start: specifierNode.start,
|
|
65
|
+
end: specifierNode.end,
|
|
66
|
+
line: specifierNode.loc.start.line,
|
|
67
|
+
column: specifierNode.loc.start.column,
|
|
68
68
|
})
|
|
69
69
|
}
|
|
70
70
|
}
|
|
@@ -34,10 +34,10 @@ const analyzeSystemRegisterDeps = (node, { onUrl }) => {
|
|
|
34
34
|
subtype: "system_register_arg",
|
|
35
35
|
expectedType: "js_classic",
|
|
36
36
|
specifier: specifierNode.value,
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
37
|
+
start: specifierNode.start,
|
|
38
|
+
end: specifierNode.end,
|
|
39
|
+
line: specifierNode.loc.start.line,
|
|
40
|
+
column: specifierNode.loc.start.column,
|
|
41
41
|
})
|
|
42
42
|
}
|
|
43
43
|
})
|
|
@@ -65,10 +65,10 @@ export const analyzeSystemImportCall = (node, { onUrl }) => {
|
|
|
65
65
|
subtype: "system_import_arg",
|
|
66
66
|
expectedType: "js_classic",
|
|
67
67
|
specifier: specifierNode.value,
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
68
|
+
start: specifierNode.start,
|
|
69
|
+
end: specifierNode.end,
|
|
70
|
+
line: specifierNode.loc.start.line,
|
|
71
|
+
column: specifierNode.loc.start.column,
|
|
72
72
|
})
|
|
73
73
|
}
|
|
74
74
|
}
|
|
@@ -97,10 +97,10 @@ export const analyzeSystemResolveCall = (node, { onUrl }) => {
|
|
|
97
97
|
type: "js_url",
|
|
98
98
|
subtype: "system_resolve_arg",
|
|
99
99
|
specifier: specifierNode.value,
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
100
|
+
start: specifierNode.start,
|
|
101
|
+
end: specifierNode.end,
|
|
102
|
+
line: specifierNode.loc.start.line,
|
|
103
|
+
column: specifierNode.loc.start.column,
|
|
104
104
|
})
|
|
105
105
|
}
|
|
106
106
|
}
|
|
@@ -22,10 +22,10 @@ export const analyzeImportScriptCalls = (node, { onUrl }) => {
|
|
|
22
22
|
subtype: "self_import_scripts_arg",
|
|
23
23
|
expectedType: "js_classic",
|
|
24
24
|
specifier: specifierNode.value,
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
start: specifierNode.start,
|
|
26
|
+
end: specifierNode.end,
|
|
27
|
+
line: specifierNode.loc.start.line,
|
|
28
|
+
column: specifierNode.loc.start.column,
|
|
29
29
|
})
|
|
30
30
|
}
|
|
31
31
|
})
|
|
@@ -124,10 +124,10 @@ const analyzeWorkerCallArguments = (
|
|
|
124
124
|
expectedSubtype,
|
|
125
125
|
typePropertyNode,
|
|
126
126
|
specifier: specifierNode.value,
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
127
|
+
start: specifierNode.start,
|
|
128
|
+
end: specifierNode.end,
|
|
129
|
+
line: specifierNode.loc.start.line,
|
|
130
|
+
column: specifierNode.loc.start.column,
|
|
131
131
|
})
|
|
132
132
|
return
|
|
133
133
|
}
|
package/src/main.js
CHANGED
|
@@ -35,8 +35,7 @@ export {
|
|
|
35
35
|
} from "./html/html_node_position.js"
|
|
36
36
|
|
|
37
37
|
// css
|
|
38
|
-
export {
|
|
39
|
-
export { postCssPluginUrlVisitor } from "./css/postcss_plugin_url_visitor.js"
|
|
38
|
+
export { parseCssUrls } from "./css/parse_css_urls.js"
|
|
40
39
|
|
|
41
40
|
// js
|
|
42
41
|
export { applyBabelPlugins } from "./js/apply_babel_plugins.js"
|