@kbach/react 1.0.0-beta.5 → 1.0.0-beta.7

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-P4CQSYIL.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
+ };
@@ -27,17 +27,64 @@ interface KbachPluginOptions {
27
27
  * purpose as Tailwind's `safelist`.
28
28
  */
29
29
  safelist?: string[];
30
+ /**
31
+ * Path to the `kbach:start`/`kbach:end` marker file, relative to Vite's
32
+ * resolved root. Defaults to `"src/kbach.css"` — override this for any
33
+ * project whose source root isn't `src/`: React Router v7's framework
34
+ * mode (`app/kbach.css`), a Pages-Router-style Next.js app using this
35
+ * plugin directly (`styles/kbach.css`), or any other convention. Getting
36
+ * this wrong fails loudly at build start (`ENOENT` writing the file's
37
+ * parent directory) rather than silently generating nothing, but it's
38
+ * still worth setting explicitly if your project doesn't use `src/`.
39
+ */
40
+ cssFile?: string;
30
41
  }
42
+ declare const DEFAULT_SCAN_DIRS: string[];
31
43
  declare const CSS_SYNC_DEBOUNCE_MS = 50;
44
+ /**
45
+ * Auto-discovers `kbach.config.js` at the project root — `undefined` if it
46
+ * doesn't exist (the common case: most projects have no customization at
47
+ * all, so this is a silent, expected no-op, not a warning).
48
+ *
49
+ * Uses `require()` (a real project's `kbach.config.js` is plain CommonJS,
50
+ * `module.exports = {...}`, matching every example in this package's own
51
+ * README), built via `createRequire(root)` rather than
52
+ * `createRequire(import.meta.url)` — this file is written as ESM source,
53
+ * but tsup's CJS build output has no real `import.meta.url` (esbuild
54
+ * leaves it `undefined` there), which would make `createRequire` throw for
55
+ * anyone consuming this package's `require('@kbach/react/vite')` entry.
56
+ * `root` works as `createRequire`'s base in either build: it's always an
57
+ * absolute, already-`existsSync`-validated-by-caller directory, and
58
+ * `require()` given `path` (itself already absolute) doesn't consult the
59
+ * base for resolution anyway — the base only matters for a RELATIVE
60
+ * specifier, which this never passes it.
61
+ */
62
+ declare function loadConfigFile(root: string): KbachConfig | undefined;
63
+ /**
64
+ * Resolves the theme to use, applying the same "explicit theme/config
65
+ * always wins over kbach.config.js auto-discovery" precedence both
66
+ * vite-plugin and postcss-plugin need identically.
67
+ */
68
+ declare function resolveEffectiveTheme(root: string, options: {
69
+ theme?: ThemeConfig;
70
+ config?: KbachConfig;
71
+ }): ThemeConfig;
32
72
  /**
33
73
  * Build-time static CSS generation for Vite — scans source files for
34
74
  * Kbach class strings, resolves them through the same Rust engine the
35
75
  * runtime uses (via the Node-target WASM build, see wasmNode.ts), and
36
- * writes a real kbach.css. Simplified vs old-kbach's version: expects
37
- * `src/kbach.css` to exist at a fixed conventional path (create it with
38
- * `/* kbach:start *\/` / `/* kbach:end *\/` markers) rather than scanning
39
- * the whole project for a file named kbach.css.
76
+ * writes a real kbach.css. Simplified vs old-kbach's version: expects a
77
+ * marker file to already exist at a fixed conventional path — `src/
78
+ * kbach.css` by default, override via `cssFile` for any other source root
79
+ * (create it with `/* kbach:start *\/` / `/* kbach:end *\/` markers)
80
+ * rather than scanning the whole project for a file named kbach.css.
81
+ *
82
+ * The actual scanning/resolution/generation logic lives in
83
+ * `staticCss/engine.ts`, shared with `postcss-plugin/index.ts` (Next.js/
84
+ * webpack/Turbopack) — this file is now just the Vite-specific wiring:
85
+ * plugin lifecycle hooks, the dev-server watcher, and the debounced
86
+ * file-write.
40
87
  */
41
88
  declare function kbach(options?: KbachPluginOptions): Plugin;
42
89
 
43
- export { CSS_SYNC_DEBOUNCE_MS, type KbachPluginOptions, kbach };
90
+ export { CSS_SYNC_DEBOUNCE_MS, DEFAULT_SCAN_DIRS, type KbachPluginOptions, kbach, loadConfigFile, resolveEffectiveTheme };
@@ -27,17 +27,64 @@ interface KbachPluginOptions {
27
27
  * purpose as Tailwind's `safelist`.
28
28
  */
29
29
  safelist?: string[];
30
+ /**
31
+ * Path to the `kbach:start`/`kbach:end` marker file, relative to Vite's
32
+ * resolved root. Defaults to `"src/kbach.css"` — override this for any
33
+ * project whose source root isn't `src/`: React Router v7's framework
34
+ * mode (`app/kbach.css`), a Pages-Router-style Next.js app using this
35
+ * plugin directly (`styles/kbach.css`), or any other convention. Getting
36
+ * this wrong fails loudly at build start (`ENOENT` writing the file's
37
+ * parent directory) rather than silently generating nothing, but it's
38
+ * still worth setting explicitly if your project doesn't use `src/`.
39
+ */
40
+ cssFile?: string;
30
41
  }
42
+ declare const DEFAULT_SCAN_DIRS: string[];
31
43
  declare const CSS_SYNC_DEBOUNCE_MS = 50;
44
+ /**
45
+ * Auto-discovers `kbach.config.js` at the project root — `undefined` if it
46
+ * doesn't exist (the common case: most projects have no customization at
47
+ * all, so this is a silent, expected no-op, not a warning).
48
+ *
49
+ * Uses `require()` (a real project's `kbach.config.js` is plain CommonJS,
50
+ * `module.exports = {...}`, matching every example in this package's own
51
+ * README), built via `createRequire(root)` rather than
52
+ * `createRequire(import.meta.url)` — this file is written as ESM source,
53
+ * but tsup's CJS build output has no real `import.meta.url` (esbuild
54
+ * leaves it `undefined` there), which would make `createRequire` throw for
55
+ * anyone consuming this package's `require('@kbach/react/vite')` entry.
56
+ * `root` works as `createRequire`'s base in either build: it's always an
57
+ * absolute, already-`existsSync`-validated-by-caller directory, and
58
+ * `require()` given `path` (itself already absolute) doesn't consult the
59
+ * base for resolution anyway — the base only matters for a RELATIVE
60
+ * specifier, which this never passes it.
61
+ */
62
+ declare function loadConfigFile(root: string): KbachConfig | undefined;
63
+ /**
64
+ * Resolves the theme to use, applying the same "explicit theme/config
65
+ * always wins over kbach.config.js auto-discovery" precedence both
66
+ * vite-plugin and postcss-plugin need identically.
67
+ */
68
+ declare function resolveEffectiveTheme(root: string, options: {
69
+ theme?: ThemeConfig;
70
+ config?: KbachConfig;
71
+ }): ThemeConfig;
32
72
  /**
33
73
  * Build-time static CSS generation for Vite — scans source files for
34
74
  * Kbach class strings, resolves them through the same Rust engine the
35
75
  * runtime uses (via the Node-target WASM build, see wasmNode.ts), and
36
- * writes a real kbach.css. Simplified vs old-kbach's version: expects
37
- * `src/kbach.css` to exist at a fixed conventional path (create it with
38
- * `/* kbach:start *\/` / `/* kbach:end *\/` markers) rather than scanning
39
- * the whole project for a file named kbach.css.
76
+ * writes a real kbach.css. Simplified vs old-kbach's version: expects a
77
+ * marker file to already exist at a fixed conventional path — `src/
78
+ * kbach.css` by default, override via `cssFile` for any other source root
79
+ * (create it with `/* kbach:start *\/` / `/* kbach:end *\/` markers)
80
+ * rather than scanning the whole project for a file named kbach.css.
81
+ *
82
+ * The actual scanning/resolution/generation logic lives in
83
+ * `staticCss/engine.ts`, shared with `postcss-plugin/index.ts` (Next.js/
84
+ * webpack/Turbopack) — this file is now just the Vite-specific wiring:
85
+ * plugin lifecycle hooks, the dev-server watcher, and the debounced
86
+ * file-write.
40
87
  */
41
88
  declare function kbach(options?: KbachPluginOptions): Plugin;
42
89
 
43
- export { CSS_SYNC_DEBOUNCE_MS, type KbachPluginOptions, kbach };
90
+ export { CSS_SYNC_DEBOUNCE_MS, DEFAULT_SCAN_DIRS, type KbachPluginOptions, kbach, loadConfigFile, resolveEffectiveTheme };