@barocss/server 0.5.0 → 0.6.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/dist/index.cjs CHANGED
@@ -1,6 +1,5 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("@barocss/kit");class l{constructor(s={}){this.context=a.createContext(s)}parseClass(s){return a.parseClassToAst(s,this.context)}generateCss(s){const r=a.generateCssRules(s,this.context),t=new Set(r.flatMap(({rootCssList:e})=>e).filter(Boolean)),c=r.map(({css:e})=>e).filter(Boolean),o=this.colorVarsBlock([...t,...c].join(`
2
- `));return[...o?[o]:[],...t,...c].join(`
3
- `)}colorVarsBlock(s){const r=e=>[...e.matchAll(/var\((--[\w-]*color-[\w-]+)/g)].map(n=>n[1]),t=r(s);if(t.length===0)return"";const c=new Map;for(const e of this.context.themeToCssVars().matchAll(/^\s*(--[\w-]+):\s*(.+);$/gm))c.set(e[1],e[2]);const o=new Map;for(;t.length;){const e=t.pop(),n=c.get(e);n===void 0||o.has(e)||(o.set(e,n),t.push(...r(n)))}return o.size===0?"":`:root,:host {
4
- `+[...o].map(([e,n])=>` ${e}: ${n};`).join(`
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("@barocss/kit"),a=h=>[...h.matchAll(/var\((--[\w-]+)/g)].map(t=>t[1]);class l{constructor(t={},s={}){this.classCache=new Map,this.themeDefs=null,this.cacheSize=Math.max(0,s.cacheSize??1e4),this.setConfig(t)}setConfig(t){this.context=i.createContext(t),this.classCache.clear(),this.themeDefs=null}parseClass(t){return i.parseClassToAst(t,this.context)}generateCss(t){const s=t.split(/\s+/).filter(Boolean).map(r=>this.entryFor(r)),e=this.uniqueRoots(s.flatMap(r=>r.roots)),c=this.sortRules(s.filter(r=>r.css)),o=[...e.flatMap(r=>r.refs),...c.flatMap(r=>r.refs)],n=this.themeVarsBlock(o);return[...n?[n]:[],...e.map(r=>r.css),...c.map(r=>r.css)].join(`
2
+ `)}entryFor(t){const s=this.classCache.get(t);if(s)return this.classCache.delete(t),this.classCache.set(t,s),s;const e={css:"",key:null,refs:[],roots:[]};for(const{css:c,rootCssList:o}of i.generateCssRules(t,this.context)){c&&(e.css=c);for(const n of o){if(!n)continue;const r=/^\s*@property\s+(--[\w-]+)/.exec(n)?.[1]??n;e.roots.push({css:n,dedupeKey:r,refs:a(n)})}}return e.css&&(e.key=i.ruleSortKey(e.css),e.refs=a(e.css)),this.cacheSize>0&&(this.classCache.size>=this.cacheSize&&this.classCache.delete(this.classCache.keys().next().value),this.classCache.set(t,e)),e}generateCssForClasses(t){return t.map(s=>({className:s,css:this.generateCss(s)}))}uniqueRoots(t){const s=new Set;return t.filter(({dedupeKey:e})=>!s.has(e)&&!!s.add(e))}sortRules(t){return t.map((s,e)=>({entry:s,i:e})).sort((s,e)=>i.compareKeys(s.entry.key,e.entry.key)||s.i-e.i).map(({entry:s})=>s)}themeVarsBlock(t){const s=t;if(s.length===0)return"";let e=this.themeDefs;if(!e){e=this.themeDefs=new Map;for(const o of this.context.themeToCssVars().matchAll(/^\s*(--[\w-]+):\s*(.+);$/gm))e.set(o[1],o[2])}const c=new Map;for(;s.length;){const o=s.pop(),n=e.get(o);n===void 0||c.has(o)||(c.set(o,n),s.push(...a(n)))}return c.size===0?"":`:root,:host {
3
+ `+[...c].map(([o,n])=>` ${o}: ${n};`).join(`
5
4
  `)+`
6
- }`}generateCssForClasses(s){return s.map(t=>({className:t,css:this.generateCss(t)}))}}exports.ServerRuntime=l;
5
+ }`}}exports.ServerRuntime=l;
package/dist/index.d.ts CHANGED
@@ -1,31 +1,47 @@
1
1
  import { Config } from '@barocss/kit';
2
- /**
3
- * Server-side runtime for Barocss
4
- *
5
- * This provides server-side utilities for parsing classes and generating CSS
6
- * without browser-specific features like DOM manipulation or MutationObserver.
7
- */
2
+ export interface ServerRuntimeOptions {
3
+ /** Max classes kept in the per-class generation cache (LRU). Default 10000; 0 disables caching. */
4
+ cacheSize?: number;
5
+ }
8
6
  export declare class ServerRuntime {
9
7
  private context;
10
- constructor(config?: Config);
8
+ private readonly cacheSize;
9
+ /** class -> generated entry; Map insertion order doubles as LRU order (#272). */
10
+ private classCache;
11
+ /** Theme var definitions parsed once from themeToCssVars() (#272). */
12
+ private themeDefs;
13
+ constructor(config?: Config, options?: ServerRuntimeOptions);
14
+ /** Replace the config (theme included). Drops every cached result derived from the previous one. */
15
+ setConfig(config: Config): void;
11
16
  /**
12
17
  * Parse a class name and return its AST
13
18
  */
14
19
  parseClass(className: string): import('@barocss/kit').AstNode[];
15
20
  /**
16
- * Generate CSS for a class name
21
+ * Generate CSS for a class name (or whitespace-separated class names) as one complete sheet (#267):
22
+ * one `:root,:host` block defining every theme var the output references, each root/@property block
23
+ * once, then the class rules in Tailwind variant order (base < sm < md < lg ...).
17
24
  */
18
25
  generateCss(className: string): string;
26
+ /** Generate (or reuse) one class's rules. Same per-class output generateCssRules gives for a class list. */
27
+ private entryFor;
19
28
  /**
20
- * #228: theme colours are emitted as var(--color-*). Define only the ones this output references
21
- * (the full theme block is large), including --color-* vars those definitions reference in turn.
22
- */
23
- private colorVarsBlock;
24
- /**
25
- * Parse multiple classes and return their CSS
29
+ * CSS per class, in input order. Each entry is self-contained (its own `:root,:host` vars block,
30
+ * @property blocks and variant-sorted rules), so entries repeat shared blocks and are not ordered
31
+ * against each other. For one complete sheet use `generateCss(classes.join(' '))`.
26
32
  */
27
33
  generateCssForClasses(classes: string[]): {
28
34
  className: string;
29
35
  css: string;
30
36
  }[];
37
+ /** Dedupe root-level blocks by content, and @property blocks by property name. */
38
+ private uniqueRoots;
39
+ /** Stable sort by the #254 variant key shared with @barocss/browser. */
40
+ private sortRules;
41
+ /**
42
+ * #228 generalised (#267): define every var(--x) the output references that the theme defines
43
+ * (radius, text, spacing, shadow, font, colour ...), including vars those definitions reference.
44
+ * themeToCssVars() already omits self-referencing entries (#260).
45
+ */
46
+ private themeVarsBlock;
31
47
  }
package/dist/index.es.js CHANGED
@@ -1,52 +1,86 @@
1
- import { createContext as c, parseClassToAst as l, generateCssRules as i } from "@barocss/kit";
2
- class p {
3
- constructor(e = {}) {
4
- this.context = c(e);
1
+ import { createContext as h, parseClassToAst as l, generateCssRules as f, ruleSortKey as u, compareKeys as p } from "@barocss/kit";
2
+ const a = (i) => [...i.matchAll(/var\((--[\w-]+)/g)].map((t) => t[1]);
3
+ class C {
4
+ constructor(t = {}, s = {}) {
5
+ this.classCache = /* @__PURE__ */ new Map(), this.themeDefs = null, this.cacheSize = Math.max(0, s.cacheSize ?? 1e4), this.setConfig(t);
6
+ }
7
+ /** Replace the config (theme included). Drops every cached result derived from the previous one. */
8
+ setConfig(t) {
9
+ this.context = h(t), this.classCache.clear(), this.themeDefs = null;
5
10
  }
6
11
  /**
7
12
  * Parse a class name and return its AST
8
13
  */
9
- parseClass(e) {
10
- return l(e, this.context);
14
+ parseClass(t) {
15
+ return l(t, this.context);
11
16
  }
12
17
  /**
13
- * Generate CSS for a class name
18
+ * Generate CSS for a class name (or whitespace-separated class names) as one complete sheet (#267):
19
+ * one `:root,:host` block defining every theme var the output references, each root/@property block
20
+ * once, then the class rules in Tailwind variant order (base < sm < md < lg ...).
14
21
  */
15
- generateCss(e) {
16
- const r = i(e, this.context), t = new Set(r.flatMap(({ rootCssList: s }) => s).filter(Boolean)), a = r.map(({ css: s }) => s).filter(Boolean), o = this.colorVarsBlock([...t, ...a].join(`
17
- `));
18
- return [...o ? [o] : [], ...t, ...a].join(`
22
+ generateCss(t) {
23
+ const s = t.split(/\s+/).filter(Boolean).map((r) => this.entryFor(r)), e = this.uniqueRoots(s.flatMap((r) => r.roots)), c = this.sortRules(s.filter((r) => r.css)), n = [...e.flatMap((r) => r.refs), ...c.flatMap((r) => r.refs)], o = this.themeVarsBlock(n);
24
+ return [...o ? [o] : [], ...e.map((r) => r.css), ...c.map((r) => r.css)].join(`
19
25
  `);
20
26
  }
27
+ /** Generate (or reuse) one class's rules. Same per-class output generateCssRules gives for a class list. */
28
+ entryFor(t) {
29
+ const s = this.classCache.get(t);
30
+ if (s)
31
+ return this.classCache.delete(t), this.classCache.set(t, s), s;
32
+ const e = { css: "", key: null, refs: [], roots: [] };
33
+ for (const { css: c, rootCssList: n } of f(t, this.context)) {
34
+ c && (e.css = c);
35
+ for (const o of n) {
36
+ if (!o) continue;
37
+ const r = /^\s*@property\s+(--[\w-]+)/.exec(o)?.[1] ?? o;
38
+ e.roots.push({ css: o, dedupeKey: r, refs: a(o) });
39
+ }
40
+ }
41
+ return e.css && (e.key = u(e.css), e.refs = a(e.css)), this.cacheSize > 0 && (this.classCache.size >= this.cacheSize && this.classCache.delete(this.classCache.keys().next().value), this.classCache.set(t, e)), e;
42
+ }
21
43
  /**
22
- * #228: theme colours are emitted as var(--color-*). Define only the ones this output references
23
- * (the full theme block is large), including --color-* vars those definitions reference in turn.
44
+ * CSS per class, in input order. Each entry is self-contained (its own `:root,:host` vars block,
45
+ * @property blocks and variant-sorted rules), so entries repeat shared blocks and are not ordered
46
+ * against each other. For one complete sheet use `generateCss(classes.join(' '))`.
24
47
  */
25
- colorVarsBlock(e) {
26
- const r = (s) => [...s.matchAll(/var\((--[\w-]*color-[\w-]+)/g)].map((n) => n[1]), t = r(e);
27
- if (t.length === 0) return "";
28
- const a = /* @__PURE__ */ new Map();
29
- for (const s of this.context.themeToCssVars().matchAll(/^\s*(--[\w-]+):\s*(.+);$/gm)) a.set(s[1], s[2]);
30
- const o = /* @__PURE__ */ new Map();
31
- for (; t.length; ) {
32
- const s = t.pop(), n = a.get(s);
33
- n === void 0 || o.has(s) || (o.set(s, n), t.push(...r(n)));
34
- }
35
- return o.size === 0 ? "" : `:root,:host {
36
- ` + [...o].map(([s, n]) => ` ${s}: ${n};`).join(`
37
- `) + `
38
- }`;
48
+ generateCssForClasses(t) {
49
+ return t.map((s) => ({ className: s, css: this.generateCss(s) }));
50
+ }
51
+ /** Dedupe root-level blocks by content, and @property blocks by property name. */
52
+ uniqueRoots(t) {
53
+ const s = /* @__PURE__ */ new Set();
54
+ return t.filter(({ dedupeKey: e }) => !s.has(e) && !!s.add(e));
55
+ }
56
+ /** Stable sort by the #254 variant key shared with @barocss/browser. */
57
+ sortRules(t) {
58
+ return t.map((s, e) => ({ entry: s, i: e })).sort((s, e) => p(s.entry.key, e.entry.key) || s.i - e.i).map(({ entry: s }) => s);
39
59
  }
40
60
  /**
41
- * Parse multiple classes and return their CSS
61
+ * #228 generalised (#267): define every var(--x) the output references that the theme defines
62
+ * (radius, text, spacing, shadow, font, colour ...), including vars those definitions reference.
63
+ * themeToCssVars() already omits self-referencing entries (#260).
42
64
  */
43
- generateCssForClasses(e) {
44
- return e.map((t) => ({
45
- className: t,
46
- css: this.generateCss(t)
47
- }));
65
+ themeVarsBlock(t) {
66
+ const s = t;
67
+ if (s.length === 0) return "";
68
+ let e = this.themeDefs;
69
+ if (!e) {
70
+ e = this.themeDefs = /* @__PURE__ */ new Map();
71
+ for (const n of this.context.themeToCssVars().matchAll(/^\s*(--[\w-]+):\s*(.+);$/gm)) e.set(n[1], n[2]);
72
+ }
73
+ const c = /* @__PURE__ */ new Map();
74
+ for (; s.length; ) {
75
+ const n = s.pop(), o = e.get(n);
76
+ o === void 0 || c.has(n) || (c.set(n, o), s.push(...a(o)));
77
+ }
78
+ return c.size === 0 ? "" : `:root,:host {
79
+ ` + [...c].map(([n, o]) => ` ${n}: ${o};`).join(`
80
+ `) + `
81
+ }`;
48
82
  }
49
83
  }
50
84
  export {
51
- p as ServerRuntime
85
+ C as ServerRuntime
52
86
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barocss/server",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/barocss/barocss.git",
@@ -26,7 +26,7 @@
26
26
  },
27
27
  "dependencies": {
28
28
  "jsdom": "^26.1.0",
29
- "@barocss/kit": "0.5.0"
29
+ "@barocss/kit": "0.6.0"
30
30
  },
31
31
  "devDependencies": {
32
32
  "vite": "^7.1.3",