@kbach/react 1.0.0-beta.4 → 1.0.0-beta.6

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.
@@ -0,0 +1,79 @@
1
+ import {
2
+ DEFAULT_SCAN_DIRS,
3
+ createKbachStaticCssEngine,
4
+ resolveEffectiveTheme
5
+ } from "./chunk-GPLCUNFX.mjs";
6
+ import "./chunk-NKR3PSWO.mjs";
7
+ import "./chunk-N5HCQIBB.mjs";
8
+
9
+ // src/postcss-plugin/index.ts
10
+ import postcss from "postcss";
11
+ import { resolve as resolvePath } from "path";
12
+ var KBACH_START_TEXT = "kbach:start";
13
+ var KBACH_END_TEXT = "kbach:end";
14
+ function isKbachMarker(node, text) {
15
+ return !!node && node.type === "comment" && node.text === text;
16
+ }
17
+ function spliceGeneratedCss(root, generatedCss) {
18
+ const nodes = root.nodes;
19
+ let startIdx = -1;
20
+ let endIdx = -1;
21
+ for (let i = 0; i < nodes.length; i++) {
22
+ if (startIdx === -1 && isKbachMarker(nodes[i], KBACH_START_TEXT)) {
23
+ startIdx = i;
24
+ continue;
25
+ }
26
+ if (startIdx !== -1 && isKbachMarker(nodes[i], KBACH_END_TEXT)) {
27
+ endIdx = i;
28
+ break;
29
+ }
30
+ }
31
+ if (startIdx === -1 || endIdx === -1) {
32
+ console.warn(
33
+ `[kbach] postcss plugin: no "/* kbach:start */" / "/* kbach:end */" marker pair found \u2014 add both to the file this plugin processes (see @kbach/react/vite's own convention). Nothing was generated.`
34
+ );
35
+ return;
36
+ }
37
+ for (const stale of nodes.slice(startIdx + 1, endIdx)) stale.remove();
38
+ const parsed = postcss.parse(generatedCss);
39
+ const newNodes = [...parsed.nodes];
40
+ let after = nodes[startIdx];
41
+ for (const node of newNodes) {
42
+ root.insertAfter(after, node);
43
+ after = node;
44
+ }
45
+ }
46
+ function kbachPostcss(options = {}) {
47
+ return {
48
+ postcssPlugin: "kbach",
49
+ Once(root, { result }) {
50
+ const projectRoot = options.root ?? process.cwd();
51
+ const theme = resolveEffectiveTheme(projectRoot, options);
52
+ const includeDirs = options.include ?? DEFAULT_SCAN_DIRS;
53
+ const safelist = options.safelist ?? [];
54
+ const engine = createKbachStaticCssEngine({
55
+ root: projectRoot,
56
+ theme,
57
+ themeJson: JSON.stringify(theme),
58
+ includeDirs,
59
+ safelist
60
+ });
61
+ engine.scanProjectCssSelectorsOnce();
62
+ engine.initialScan();
63
+ engine.processSafelist();
64
+ for (const dir of includeDirs) {
65
+ result.messages.push({
66
+ type: "dir-dependency",
67
+ plugin: "kbach",
68
+ dir: resolvePath(projectRoot, dir),
69
+ glob: "**/*"
70
+ });
71
+ }
72
+ spliceGeneratedCss(root, engine.generateCSS());
73
+ }
74
+ };
75
+ }
76
+ kbachPostcss.postcss = true;
77
+ export {
78
+ kbachPostcss as default
79
+ };
@@ -28,6 +28,36 @@ interface KbachPluginOptions {
28
28
  */
29
29
  safelist?: string[];
30
30
  }
31
+ declare const DEFAULT_SCAN_DIRS: string[];
32
+ declare const CSS_SYNC_DEBOUNCE_MS = 50;
33
+ /**
34
+ * Auto-discovers `kbach.config.js` at the project root — `undefined` if it
35
+ * doesn't exist (the common case: most projects have no customization at
36
+ * all, so this is a silent, expected no-op, not a warning).
37
+ *
38
+ * Uses `require()` (a real project's `kbach.config.js` is plain CommonJS,
39
+ * `module.exports = {...}`, matching every example in this package's own
40
+ * README), built via `createRequire(root)` rather than
41
+ * `createRequire(import.meta.url)` — this file is written as ESM source,
42
+ * but tsup's CJS build output has no real `import.meta.url` (esbuild
43
+ * leaves it `undefined` there), which would make `createRequire` throw for
44
+ * anyone consuming this package's `require('@kbach/react/vite')` entry.
45
+ * `root` works as `createRequire`'s base in either build: it's always an
46
+ * absolute, already-`existsSync`-validated-by-caller directory, and
47
+ * `require()` given `path` (itself already absolute) doesn't consult the
48
+ * base for resolution anyway — the base only matters for a RELATIVE
49
+ * specifier, which this never passes it.
50
+ */
51
+ declare function loadConfigFile(root: string): KbachConfig | undefined;
52
+ /**
53
+ * Resolves the theme to use, applying the same "explicit theme/config
54
+ * always wins over kbach.config.js auto-discovery" precedence both
55
+ * vite-plugin and postcss-plugin need identically.
56
+ */
57
+ declare function resolveEffectiveTheme(root: string, options: {
58
+ theme?: ThemeConfig;
59
+ config?: KbachConfig;
60
+ }): ThemeConfig;
31
61
  /**
32
62
  * Build-time static CSS generation for Vite — scans source files for
33
63
  * Kbach class strings, resolves them through the same Rust engine the
@@ -36,7 +66,13 @@ interface KbachPluginOptions {
36
66
  * `src/kbach.css` to exist at a fixed conventional path (create it with
37
67
  * `/* kbach:start *\/` / `/* kbach:end *\/` markers) rather than scanning
38
68
  * the whole project for a file named kbach.css.
69
+ *
70
+ * The actual scanning/resolution/generation logic lives in
71
+ * `staticCss/engine.ts`, shared with `postcss-plugin/index.ts` (Next.js/
72
+ * webpack/Turbopack) — this file is now just the Vite-specific wiring:
73
+ * plugin lifecycle hooks, the dev-server watcher, and the debounced
74
+ * file-write.
39
75
  */
40
76
  declare function kbach(options?: KbachPluginOptions): Plugin;
41
77
 
42
- export { type KbachPluginOptions, kbach };
78
+ export { CSS_SYNC_DEBOUNCE_MS, DEFAULT_SCAN_DIRS, type KbachPluginOptions, kbach, loadConfigFile, resolveEffectiveTheme };
@@ -28,6 +28,36 @@ interface KbachPluginOptions {
28
28
  */
29
29
  safelist?: string[];
30
30
  }
31
+ declare const DEFAULT_SCAN_DIRS: string[];
32
+ declare const CSS_SYNC_DEBOUNCE_MS = 50;
33
+ /**
34
+ * Auto-discovers `kbach.config.js` at the project root — `undefined` if it
35
+ * doesn't exist (the common case: most projects have no customization at
36
+ * all, so this is a silent, expected no-op, not a warning).
37
+ *
38
+ * Uses `require()` (a real project's `kbach.config.js` is plain CommonJS,
39
+ * `module.exports = {...}`, matching every example in this package's own
40
+ * README), built via `createRequire(root)` rather than
41
+ * `createRequire(import.meta.url)` — this file is written as ESM source,
42
+ * but tsup's CJS build output has no real `import.meta.url` (esbuild
43
+ * leaves it `undefined` there), which would make `createRequire` throw for
44
+ * anyone consuming this package's `require('@kbach/react/vite')` entry.
45
+ * `root` works as `createRequire`'s base in either build: it's always an
46
+ * absolute, already-`existsSync`-validated-by-caller directory, and
47
+ * `require()` given `path` (itself already absolute) doesn't consult the
48
+ * base for resolution anyway — the base only matters for a RELATIVE
49
+ * specifier, which this never passes it.
50
+ */
51
+ declare function loadConfigFile(root: string): KbachConfig | undefined;
52
+ /**
53
+ * Resolves the theme to use, applying the same "explicit theme/config
54
+ * always wins over kbach.config.js auto-discovery" precedence both
55
+ * vite-plugin and postcss-plugin need identically.
56
+ */
57
+ declare function resolveEffectiveTheme(root: string, options: {
58
+ theme?: ThemeConfig;
59
+ config?: KbachConfig;
60
+ }): ThemeConfig;
31
61
  /**
32
62
  * Build-time static CSS generation for Vite — scans source files for
33
63
  * Kbach class strings, resolves them through the same Rust engine the
@@ -36,7 +66,13 @@ interface KbachPluginOptions {
36
66
  * `src/kbach.css` to exist at a fixed conventional path (create it with
37
67
  * `/* kbach:start *\/` / `/* kbach:end *\/` markers) rather than scanning
38
68
  * the whole project for a file named kbach.css.
69
+ *
70
+ * The actual scanning/resolution/generation logic lives in
71
+ * `staticCss/engine.ts`, shared with `postcss-plugin/index.ts` (Next.js/
72
+ * webpack/Turbopack) — this file is now just the Vite-specific wiring:
73
+ * plugin lifecycle hooks, the dev-server watcher, and the debounced
74
+ * file-write.
39
75
  */
40
76
  declare function kbach(options?: KbachPluginOptions): Plugin;
41
77
 
42
- export { type KbachPluginOptions, kbach };
78
+ export { CSS_SYNC_DEBOUNCE_MS, DEFAULT_SCAN_DIRS, type KbachPluginOptions, kbach, loadConfigFile, resolveEffectiveTheme };