vue-hook-optimizer 0.0.55 → 0.0.57

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.mjs CHANGED
@@ -30,7 +30,9 @@ function analyze(content) {
30
30
  if (path.node.key.type === "Identifier" && path.node.key.name === "ref") {
31
31
  if (path.node.value.type === "StringLiteral") {
32
32
  const name = path.node.value.value;
33
- name && nodes.add(name);
33
+ if (name) {
34
+ nodes.add(name);
35
+ }
34
36
  }
35
37
  }
36
38
  },
@@ -39,7 +41,9 @@ function analyze(content) {
39
41
  if (path.node.callee.type === "Identifier" && path.node.callee.name === "_resolveComponent") {
40
42
  if (path.node.arguments[0].type === "StringLiteral") {
41
43
  const name = path.node.arguments[0].value;
42
- name && nodes.add(name);
44
+ if (name) {
45
+ nodes.add(name);
46
+ }
43
47
  }
44
48
  }
45
49
  }
@@ -139,7 +143,7 @@ function getComment(node) {
139
143
  return;
140
144
  }
141
145
  if (_comment.value.trim().startsWith("*")) {
142
- comment += `${_comment.value.trim().replace(/^[\s]*\*+[\s]*\**/gm, "").trim()}
146
+ comment += `${_comment.value.trim().replace(/^\s*\*+\s*\**/gm, "").trim()}
143
147
  `;
144
148
  }
145
149
  });
@@ -148,7 +152,7 @@ function getComment(node) {
148
152
  return;
149
153
  }
150
154
  if (_comment.value.trim().startsWith("*")) {
151
- comment += `${_comment.value.trim().replace(/^[\s]*\*+[\s]*\**/gm, "").trim()}
155
+ comment += `${_comment.value.trim().replace(/^\s*\*+\s*\**/gm, "").trim()}
152
156
  `;
153
157
  } else {
154
158
  comment += `${_comment.value.trim()}
@@ -1841,6 +1845,67 @@ function analyze4(content, type = "vue", lineOffset = 0, addInfo = true) {
1841
1845
  };
1842
1846
  }
1843
1847
 
1848
+ // src/analyze/style.ts
1849
+ function lexBinding(content, start) {
1850
+ let state = 0 /* inParens */;
1851
+ let parenDepth = 0;
1852
+ for (let i = start; i < content.length; i++) {
1853
+ const char = content.charAt(i);
1854
+ switch (state) {
1855
+ case 0 /* inParens */:
1856
+ if (char === "'") {
1857
+ state = 1 /* inSingleQuoteString */;
1858
+ } else if (char === '"') {
1859
+ state = 2 /* inDoubleQuoteString */;
1860
+ } else if (char === "(") {
1861
+ parenDepth++;
1862
+ } else if (char === ")") {
1863
+ if (parenDepth > 0) {
1864
+ parenDepth--;
1865
+ } else {
1866
+ return i;
1867
+ }
1868
+ }
1869
+ break;
1870
+ case 1 /* inSingleQuoteString */:
1871
+ if (char === "'") {
1872
+ state = 0 /* inParens */;
1873
+ }
1874
+ break;
1875
+ case 2 /* inDoubleQuoteString */:
1876
+ if (char === '"') {
1877
+ state = 0 /* inParens */;
1878
+ }
1879
+ break;
1880
+ }
1881
+ }
1882
+ return null;
1883
+ }
1884
+ function normalizeExpression(exp) {
1885
+ exp = exp.trim();
1886
+ if (exp[0] === "'" && exp[exp.length - 1] === "'" || exp[0] === '"' && exp[exp.length - 1] === '"') {
1887
+ return exp.slice(1, -1);
1888
+ }
1889
+ return exp;
1890
+ }
1891
+ var vBindRE = /v-bind\s*\(/g;
1892
+ function analyze5(styles) {
1893
+ const nodes = /* @__PURE__ */ new Set();
1894
+ styles.forEach((style) => {
1895
+ let match;
1896
+ const content = style.content.replace(/\/\*([\s\S]*?)\*\/|\/\/.*/g, "");
1897
+ while (match = vBindRE.exec(content)) {
1898
+ const start = match.index + match[0].length;
1899
+ const end = lexBinding(content, start);
1900
+ if (end !== null) {
1901
+ const variable = normalizeExpression(content.slice(start, end));
1902
+ nodes.add(variable);
1903
+ }
1904
+ }
1905
+ });
1906
+ return nodes;
1907
+ }
1908
+
1844
1909
  // src/suggest/split.ts
1845
1910
  function dfs(graph, node, targets, visited, component) {
1846
1911
  component.add(node);
@@ -1915,7 +1980,7 @@ function findLinearPaths(graph) {
1915
1980
  const linearPaths = [];
1916
1981
  const visitedNodes = /* @__PURE__ */ new Set();
1917
1982
  for (const [node, edges] of graph.entries()) {
1918
- if (edges.size === 1 && !visitedNodes.has(node) && node.type === "fun" /* fun */) {
1983
+ if (edges.size === 1 && !visitedNodes.has(node)) {
1919
1984
  const path = [node];
1920
1985
  let nextNode = Array.from(edges)[0];
1921
1986
  visitedNodes.add(node);
@@ -2015,7 +2080,8 @@ var SuggestionType = /* @__PURE__ */ ((SuggestionType2) => {
2015
2080
  SuggestionType2["error"] = "error";
2016
2081
  return SuggestionType2;
2017
2082
  })(SuggestionType || {});
2018
- function gen(graph, usedNodes) {
2083
+ function gen(graph, nodesUsedInTemplate, nodesUsedInStyle = /* @__PURE__ */ new Set()) {
2084
+ const usedNodes = /* @__PURE__ */ new Set([...nodesUsedInTemplate, ...nodesUsedInStyle]);
2019
2085
  const suggestions = [];
2020
2086
  const splitedGraph = splitGraph(graph.edges);
2021
2087
  splitedGraph.forEach((g) => {
@@ -2079,7 +2145,8 @@ function gen(graph, usedNodes) {
2079
2145
  }
2080
2146
 
2081
2147
  // src/vis.ts
2082
- function getVisData(graph, usedNodes) {
2148
+ function getVisData(graph, nodesUsedInTemplate, nodesUsedInStyle = /* @__PURE__ */ new Set()) {
2149
+ const usedNodes = /* @__PURE__ */ new Set([...nodesUsedInTemplate, ...nodesUsedInStyle]);
2083
2150
  const nodes = [];
2084
2151
  const edges = [];
2085
2152
  graph.nodes.forEach((node) => {
@@ -2090,7 +2157,12 @@ function getVisData(graph, usedNodes) {
2090
2157
  group: usedNodes.has(node.label) || node.info?.used?.size ? "used" : "normal",
2091
2158
  title: `${node.info?.used?.size ? `used by ${Array.from(node.info?.used || [])?.map((i) => `\`${i}\``).join(",")}
2092
2159
 
2093
- ` : ""}${usedNodes.has(node.label) ? "used in template\n\n" : ""}${node.info?.comment || ""}`.trim() || void 0,
2160
+ ` : ""}${usedNodes.has(node.label) ? `used in ${[
2161
+ nodesUsedInStyle.has(node.label) ? "style" : "",
2162
+ nodesUsedInTemplate.has(node.label) ? "template" : ""
2163
+ ].filter(Boolean).join(" and ")}
2164
+
2165
+ ` : ""}${node.info?.comment || ""}`.trim() || void 0,
2094
2166
  info: node.info
2095
2167
  });
2096
2168
  });
@@ -2121,6 +2193,7 @@ export {
2121
2193
  SuggestionType,
2122
2194
  analyze3 as analyzeOptions,
2123
2195
  analyze2 as analyzeSetupScript,
2196
+ analyze5 as analyzeStyle,
2124
2197
  analyze as analyzeTemplate,
2125
2198
  analyze4 as analyzeTsx,
2126
2199
  gen,