@kbach/react 0.5.32 → 0.5.34

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.
@@ -1,11 +1,13 @@
1
1
  import {
2
2
  buildConfig,
3
- generateClassCSS
4
- } from "./chunk-SOBLKZGE.mjs";
3
+ generateClassCSS,
4
+ setCSSGenMode
5
+ } from "./chunk-T3SOZ6AO.mjs";
5
6
 
6
7
  // src/vite-plugin.ts
7
8
  import { readFileSync, writeFileSync, existsSync, readdirSync, statSync } from "fs";
8
9
  import { join } from "path";
10
+ setCSSGenMode(true);
9
11
  var KBACH_START = "/* kbach:start */";
10
12
  var KBACH_END = "/* kbach:end */";
11
13
  var GROUPS = [
@@ -43,9 +45,8 @@ function formatKbachCSS(tokenCSS, theme) {
43
45
  const allRaw = [...tokenCSS.values()].filter(Boolean).join("\n");
44
46
  const colorVars = buildColorVarMap(theme, allRaw);
45
47
  function applyVars(css) {
46
- for (const [val, varName] of colorVars) {
48
+ for (const [val, varName] of colorVars)
47
49
  css = css.split(val).join(`var(${varName})`);
48
- }
49
50
  return css;
50
51
  }
51
52
  const groups = new Map(GROUP_ORDER.map((g) => [g, []]));
@@ -58,17 +59,13 @@ function formatKbachCSS(tokenCSS, theme) {
58
59
  const out = ["/* Generated by Kbach \u2014 do not edit */"];
59
60
  if (colorVars.size > 0) {
60
61
  out.push("\n:root {");
61
- for (const [val, varName] of colorVars) {
62
- out.push(` ${varName}: ${val};`);
63
- }
62
+ for (const [val, varName] of colorVars) out.push(` ${varName}: ${val};`);
64
63
  out.push("}");
65
64
  }
66
65
  const globalCSS = buildThemeGlobal(theme);
67
- if (globalCSS) {
68
- out.push(`
66
+ if (globalCSS) out.push(`
69
67
  /* Global */
70
68
  ${globalCSS}`);
71
- }
72
69
  for (const label of GROUP_ORDER) {
73
70
  const items = groups.get(label) ?? [];
74
71
  if (!items.length) continue;
@@ -118,7 +115,6 @@ function toNumericScreens(screens) {
118
115
  }
119
116
  return out;
120
117
  }
121
- var normPath = (p) => p.split("\\").join("/");
122
118
  function scanDir(dir, onFile) {
123
119
  try {
124
120
  for (const entry of readdirSync(dir)) {
@@ -127,7 +123,7 @@ function scanDir(dir, onFile) {
127
123
  try {
128
124
  const st = statSync(full);
129
125
  if (st.isDirectory()) scanDir(full, onFile);
130
- else if (/\.(tsx?|jsx?)$/.test(entry)) onFile(readFileSync(full, "utf-8"), normPath(full));
126
+ else if (/\.(tsx?|jsx?)$/.test(entry)) onFile(readFileSync(full, "utf-8"));
131
127
  } catch {
132
128
  }
133
129
  }
@@ -147,37 +143,20 @@ function extractClassStrings(code) {
147
143
  function kbach(userConfig) {
148
144
  const cfg = buildConfig(userConfig ?? {});
149
145
  const screens = toNumericScreens(cfg.theme.screens ?? {});
150
- const fileTokens = /* @__PURE__ */ new Map();
151
- const tokenCSS = /* @__PURE__ */ new Map();
152
146
  let root = process.cwd();
153
147
  let mainCSSFile = null;
154
- function extractTokensFromCode(code) {
155
- const tokens = /* @__PURE__ */ new Set();
156
- for (const cls of extractClassStrings(code))
157
- for (const token of cls.split(/\s+/).filter(Boolean))
158
- tokens.add(token);
159
- return tokens;
160
- }
161
- function rebuildTokenCSS() {
162
- const live = /* @__PURE__ */ new Set();
163
- for (const tokens of fileTokens.values())
164
- for (const t of tokens) live.add(t);
165
- let changed = false;
166
- for (const token of tokenCSS.keys()) {
167
- if (!live.has(token)) {
168
- tokenCSS.delete(token);
169
- changed = true;
170
- }
148
+ function generateCSS() {
149
+ const tokenCSS = /* @__PURE__ */ new Map();
150
+ for (const dir of ["src", "app", "pages", "components", "views", "layouts"]) {
151
+ scanDir(join(root, dir), (code) => {
152
+ for (const cls of extractClassStrings(code))
153
+ for (const token of cls.split(/\s+/).filter(Boolean))
154
+ if (!tokenCSS.has(token)) {
155
+ const css = generateClassCSS(token, cfg.theme, cfg.darkMode, screens);
156
+ tokenCSS.set(token, css);
157
+ }
158
+ });
171
159
  }
172
- for (const token of live) {
173
- if (!tokenCSS.has(token)) {
174
- tokenCSS.set(token, generateClassCSS(token, cfg.theme, cfg.darkMode, screens));
175
- changed = true;
176
- }
177
- }
178
- return changed;
179
- }
180
- function getFormattedCSS() {
181
160
  return formatKbachCSS(tokenCSS, cfg.theme);
182
161
  }
183
162
  return {
@@ -187,19 +166,12 @@ function kbach(userConfig) {
187
166
  root = resolved.root;
188
167
  },
189
168
  buildStart() {
190
- for (const dir of ["src", "app", "pages", "components", "views", "layouts"])
191
- scanDir(join(root, dir), (code, filePath) => fileTokens.set(filePath, extractTokensFromCode(code)));
192
- rebuildTokenCSS();
193
169
  mainCSSFile = findMainCSSFile(root);
194
- if (mainCSSFile) writeKbachToFile(mainCSSFile, getFormattedCSS());
170
+ if (mainCSSFile) writeKbachToFile(mainCSSFile, generateCSS());
195
171
  },
196
172
  handleHotUpdate({ file }) {
197
173
  if (file.includes("node_modules") || !/\.(tsx?|jsx?)$/.test(file)) return;
198
- try {
199
- fileTokens.set(normPath(file), extractTokensFromCode(readFileSync(file, "utf-8")));
200
- if (rebuildTokenCSS() && mainCSSFile) writeKbachToFile(mainCSSFile, getFormattedCSS());
201
- } catch {
202
- }
174
+ if (mainCSSFile) writeKbachToFile(mainCSSFile, generateCSS());
203
175
  }
204
176
  };
205
177
  }