@jsenv/ast 1.4.10 → 2.0.1

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.10",
3
+ "version": "2.0.1",
4
4
  "description": "Parse and transform HTML,CSS and JS",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -34,7 +34,6 @@
34
34
  },
35
35
  "dependencies": {
36
36
  "@babel/parser": "7.20.7",
37
- "@parcel/css": "1.14.0",
38
37
  "acorn": "8.8.1",
39
38
  "acorn-import-assertions": "1.8.0",
40
39
  "acorn-walk": "8.2.0",
@@ -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[0]
107
+ const previousSibling = previousSiblings[previousSiblings.length - 1]
108
108
  if (previousSibling) {
109
109
  futureChildNodes.push(...previousSiblings)
110
110
  }
111
- if (!previousSibling || previousSibling.nodeName !== "#text") {
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.nodeName !== "#text") {
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) => {
package/src/main.js CHANGED
@@ -36,7 +36,6 @@ export {
36
36
 
37
37
  // css
38
38
  export { applyPostCss } from "./css/apply_post_css.js"
39
- export { transpileWithParcel, minifyWithParcel } from "./css/parcel_css.js"
40
39
  export { postCssPluginUrlVisitor } from "./css/postcss_plugin_url_visitor.js"
41
40
 
42
41
  // js
@@ -1,58 +0,0 @@
1
- import { createRequire } from "node:module"
2
- import { fileURLToPath } from "node:url"
3
-
4
- const require = createRequire(import.meta.url)
5
-
6
- export const transpileWithParcel = (urlInfo, context) => {
7
- const css = require("@parcel/css")
8
- const targets = runtimeCompatToTargets(context.runtimeCompat)
9
- const { code, map } = css.transform({
10
- filename: fileURLToPath(urlInfo.originalUrl),
11
- code: Buffer.from(urlInfo.content),
12
- targets,
13
- minify: false,
14
- })
15
- return { code, map }
16
- }
17
-
18
- export const minifyWithParcel = (urlInfo, context) => {
19
- const css = require("@parcel/css")
20
- const targets = runtimeCompatToTargets(context.runtimeCompat)
21
- const { code, map } = css.transform({
22
- filename: fileURLToPath(urlInfo.originalUrl),
23
- code: Buffer.from(urlInfo.content),
24
- targets,
25
- minify: true,
26
- })
27
- return { code, map }
28
- }
29
-
30
- export const bundleWithParcel = (urlInfo, context) => {
31
- const targets = runtimeCompatToTargets(context.runtimeCompat)
32
- const css = require("@parcel/css")
33
- const { code, map } = css.bundle({
34
- filename: fileURLToPath(urlInfo.originalUrl),
35
- targets,
36
- minify: false,
37
- })
38
- return { code, map }
39
- }
40
-
41
- const runtimeCompatToTargets = (runtimeCompat) => {
42
- const targets = {}
43
- ;["chrome", "firefox", "ie", "opera", "safari"].forEach((runtimeName) => {
44
- const version = runtimeCompat[runtimeName]
45
- if (version) {
46
- targets[runtimeName] = versionToBits(version)
47
- }
48
- })
49
- return targets
50
- }
51
-
52
- const versionToBits = (version) => {
53
- const [major, minor = 0, patch = 0] = version
54
- .split("-")[0]
55
- .split(".")
56
- .map((v) => parseInt(v, 10))
57
- return (major << 16) | (minor << 8) | patch
58
- }