@barocss/server 0.4.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 +5 -2
- package/dist/index.d.ts +30 -9
- package/dist/index.es.js +71 -16
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,5 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
2
|
-
`)}
|
|
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(`
|
|
4
|
+
`)+`
|
|
5
|
+
}`}}exports.ServerRuntime=l;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,26 +1,47 @@
|
|
|
1
1
|
import { Config } from '@barocss/kit';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
|
|
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
|
-
*
|
|
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(' '))`.
|
|
21
32
|
*/
|
|
22
33
|
generateCssForClasses(classes: string[]): {
|
|
23
34
|
className: string;
|
|
24
35
|
css: string;
|
|
25
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;
|
|
26
47
|
}
|
package/dist/index.es.js
CHANGED
|
@@ -1,31 +1,86 @@
|
|
|
1
|
-
import { createContext as
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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(
|
|
10
|
-
return
|
|
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(
|
|
16
|
-
|
|
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(`
|
|
17
25
|
`);
|
|
18
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
|
+
}
|
|
43
|
+
/**
|
|
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(' '))`.
|
|
47
|
+
*/
|
|
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);
|
|
59
|
+
}
|
|
19
60
|
/**
|
|
20
|
-
*
|
|
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).
|
|
21
64
|
*/
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
+
}`;
|
|
27
82
|
}
|
|
28
83
|
}
|
|
29
84
|
export {
|
|
30
|
-
|
|
85
|
+
C as ServerRuntime
|
|
31
86
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@barocss/server",
|
|
3
|
-
"version": "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.
|
|
29
|
+
"@barocss/kit": "0.6.0"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"vite": "^7.1.3",
|