forgecss 0.10.0 → 0.12.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/lib/usages.js CHANGED
@@ -1,58 +1,27 @@
1
- import swc from "@swc/core";
2
- import { readFile, writeFile } from "fs/promises";
3
- import { fromHtml } from "hast-util-from-html";
4
- import { visit } from "unist-util-visit";
5
-
6
- const FUNC_NAME = 'fx';
7
1
  let USAGES = {};
8
2
 
9
- const { parse } = swc;
10
-
11
- export async function findUsages(filePath, fileContent = null) {
3
+ export async function findUsages(filePath, content, JSXParser) {
12
4
  try {
13
5
  if (USAGES[filePath]) {
14
6
  // already processed
15
7
  return;
16
8
  }
17
9
  USAGES[filePath] = [];
18
- const content = fileContent ? fileContent : await readFile(filePath, "utf-8");
19
- const extension = filePath.split('.').pop().toLowerCase();
10
+ const extension = filePath.split(".").pop().toLowerCase();
20
11
 
21
12
  // HTML
22
13
  if (extension === "html") {
23
- const ast = fromHtml(content);
24
- visit(ast, "element", (node) => {
25
- if (node.properties.className) {
26
- USAGES[filePath].push(node.properties.className.join(" "));
27
- }
14
+ extractClassNamesFromHTML(content).forEach((cls) => {
15
+ USAGES[filePath].push(cls);
28
16
  });
29
17
  return;
30
18
  }
31
-
32
- // JSX/TSX
33
- const ast = await parse(content, {
34
- syntax: "typescript",
35
- tsx: true,
36
- decorators: false
37
- });
38
- // writeFile(process.cwd() + '/ast.json', JSON.stringify(ast, null, 2), 'utf-8').catch(() => {});
39
- traverseASTNode(ast, {
40
- JSXExpressionContainer(node) {
41
- if (node?.expression?.callee?.value === FUNC_NAME && node?.expression?.arguments) {
42
- if (node?.expression?.arguments[0]) {
43
- const arg = node.expression.arguments[0];
44
- let value = arg?.expression.value;
45
- if (arg.expression.type === "TemplateLiteral") {
46
- let quasis = arg.expression.quasis.map((elem) => elem?.cooked || "");
47
- value = quasis.join("");
48
- }
49
- USAGES[filePath].push(value);
50
- }
51
- }
52
- }
53
- });
19
+ if (JSXParser && (extension === "jsx" || extension === "tsx")) {
20
+ await JSXParser(content, USAGES, filePath);
21
+ return;
22
+ }
54
23
  } catch (err) {
55
- console.error(`forgecss: error processing file ${filePath.replace(process.cwd(), '')}`, err);
24
+ console.error(`forgecss: error processing file ${filePath.replace(process.cwd(), "")}`, err);
56
25
  }
57
26
  }
58
27
  export function invalidateUsageCache(filePath) {
@@ -67,39 +36,15 @@ export function invalidateUsageCache(filePath) {
67
36
  export function getUsages() {
68
37
  return USAGES;
69
38
  }
70
- function traverseASTNode(node, visitors, stack = []) {
71
- if (!node || typeof node.type !== "string") {
72
- return;
73
- }
74
39
 
75
- const visitor = visitors[node.type];
76
- if (visitor) {
77
- visitor(node, stack);
78
- }
79
-
80
- for (const key in node) {
81
- if (!node.hasOwnProperty(key)) continue;
82
-
83
- const child = node[key];
40
+ function extractClassNamesFromHTML(html) {
41
+ const result = [];
42
+ const classAttrRE = /\bclass\s*=\s*(["'])(.*?)\1/gis;
84
43
 
85
- if (Array.isArray(child)) {
86
- child.forEach((c) => {
87
- if (c) {
88
- if (typeof c.type === "string") {
89
- traverseASTNode(c, visitors, [node].concat(stack));
90
- } else if (c?.expression && typeof c.expression.type === "string") {
91
- traverseASTNode(c.expression, visitors, [node].concat(stack));
92
- } else if (c?.callee && typeof c.callee.type === "string") {
93
- traverseASTNode(c.callee, visitors, [node].concat(stack));
94
- } else if (c?.left && typeof c.left.type === "string") {
95
- traverseASTNode(c.left, visitors, [node].concat(stack));
96
- } else if (c?.right && typeof c.right.type === "string") {
97
- traverseASTNode(c.right, visitors, [node].concat(stack));
98
- }
99
- }
100
- });
101
- } else if (child && typeof child.type === "string") {
102
- traverseASTNode(child, visitors, [node].concat(stack));
103
- }
44
+ let match;
45
+ while ((match = classAttrRE.exec(html))) {
46
+ result.push(match[2].trim());
104
47
  }
48
+
49
+ return result;
105
50
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "forgecss",
3
- "version": "0.10.0",
3
+ "version": "0.12.0",
4
4
  "type": "module",
5
5
  "description": "ForgeCSS turns strings into fully generated responsive CSS using a custom DSL.",
6
6
  "author": "Krasimir Tsonev",
@@ -27,15 +27,13 @@
27
27
  "@swc/core": "1.12.1",
28
28
  "chokidar": "^4.0.3",
29
29
  "commander": "^14.0.2",
30
- "hast-util-from-html": "^2.0.3",
31
30
  "postcss": "^8.5.6",
32
- "postcss-safe-parser": "^7.0.1",
33
- "unist-util-visit": "^5.0.0"
31
+ "postcss-safe-parser": "^7.0.1"
34
32
  },
35
33
  "devDependencies": {
36
34
  "esbuild": "^0.27.1"
37
35
  },
38
36
  "bin": {
39
- "forgecss": "./cli.js"
37
+ "forgecss": "./index.cli.js"
40
38
  }
41
39
  }
package/scripts/build.js CHANGED
@@ -1,17 +1,14 @@
1
1
  import path from "path";
2
2
  import esbuild from "esbuild";
3
3
  import { fileURLToPath } from "url";
4
- import { readFileSync } from "fs";
5
4
 
6
5
  const __filename = fileURLToPath(import.meta.url);
7
6
  const __dirname = path.dirname(__filename);
8
-
9
- const pkg = JSON.parse(readFileSync(path.join(__dirname, "..", "package.json"), "utf8"));
10
7
  const minify = true;
11
8
 
12
9
  (async function () {
13
10
  await esbuild.build({
14
- entryPoints: [path.join(__dirname, "..", "standalone", "client.js")],
11
+ entryPoints: [path.join(__dirname, "..", "index.fx.js")],
15
12
  bundle: true,
16
13
  minify,
17
14
  outfile: path.join(__dirname, "..", "dist", "client.min.js"),
@@ -20,10 +17,10 @@ const minify = true;
20
17
  plugins: []
21
18
  });
22
19
  await esbuild.build({
23
- entryPoints: [path.join(__dirname, "..", "standalone", "forgecss.js")],
20
+ entryPoints: [path.join(__dirname, "..", "index.browser.js")],
24
21
  bundle: true,
25
22
  minify,
26
- outfile: path.join(__dirname, "..", "dist", "forgecss.min.js"),
23
+ outfile: path.join(__dirname, "..", "dist", "standalone.min.js"),
27
24
  platform: "browser",
28
25
  sourcemap: false,
29
26
  plugins: []