@barocss/browser 0.5.0 → 0.7.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.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { Config } from '@barocss/kit';
2
2
  import { GenerateCssRulesResult } from '@barocss/kit';
3
3
  import { IncrementalParser } from '@barocss/kit';
4
+ import { RuleKey } from '@barocss/kit';
4
5
 
5
6
  export declare function baroBoot({ loadingClassName, ...options }?: BaroBootOptions): void;
6
7
 
@@ -17,7 +18,17 @@ export declare class BrowserRuntime {
17
18
  private options;
18
19
  private isDestroyed;
19
20
  private existing;
21
+ /** #274: @keyframes names the page's own sheets define (filled with `existing`). */
22
+ private existingKeyframes;
20
23
  private existingSheetCount;
24
+ /** #269: classes requested explicitly through addClass(); never reclaimed. */
25
+ private pinned;
26
+ private gc;
27
+ private reclaimedCount;
28
+ /** #268: class rules adopted from `<style data-barocss-ssr>`, in sheet order, and the classes they lead. */
29
+ private ssrRules;
30
+ private ssrClasses;
31
+ private observedOnce;
21
32
  private incrementalParser;
22
33
  private changeDetector;
23
34
  private stylePartitionManager;
@@ -28,6 +39,15 @@ export declare class BrowserRuntime {
28
39
  */
29
40
  private debugLog;
30
41
  private init;
42
+ /**
43
+ * #268: adopt the class rules of server-rendered `<style data-barocss-ssr>` sheets in <head>, at startup
44
+ * (constructor and the first observe()). Each rule moves
45
+ * (same task, so no paint in between) into the partition its class would get if generated here, at
46
+ * its #254 sorted position, so a later client `sm:` rule lands before a server `lg:` rule. Its classes
47
+ * are never regenerated and never reclaimed. `:root`, `@property` and `@keyframes` stay in the sheet.
48
+ */
49
+ private adoptSsrSheets;
50
+ private insertSsrRules;
31
51
  private injectPreflightCSS;
32
52
  private ensureCssVars;
33
53
  private getInsertionPoint;
@@ -49,6 +69,13 @@ export declare class BrowserRuntime {
49
69
  applyParseResults(results: Array<GenerateCssRulesResult>, _opts?: {
50
70
  isBrowser?: boolean;
51
71
  }): void;
72
+ /** #269: a class that must never be reclaimed. */
73
+ private isPermanent;
74
+ /**
75
+ * #269: delete the generated rules of classes no live element uses. Root/@property/@keyframes rules stay
76
+ * (they are shared and harmless); a rule text another cached class still emits is kept.
77
+ */
78
+ private reclaim;
52
79
  /** Class names defined by the page's own stylesheets (BaroCSS's sheets and cross-origin sheets excluded). */
53
80
  getExistingClasses(): Set<string>;
54
81
  /**
@@ -70,6 +97,12 @@ export declare class BrowserRuntime {
70
97
  runtime: {
71
98
  cachedClasses: number;
72
99
  rootCacheSize: number;
100
+ ruleCount: number;
101
+ reclaimedClasses: number;
102
+ gc: {
103
+ trackedClasses: number;
104
+ candidates: number;
105
+ } | null;
73
106
  };
74
107
  ast: {
75
108
  size: number;
@@ -106,6 +139,12 @@ export declare class BrowserRuntime {
106
139
  runtime: {
107
140
  cachedClasses: number;
108
141
  rootCacheSize: number;
142
+ ruleCount: number;
143
+ reclaimedClasses: number;
144
+ gc: {
145
+ trackedClasses: number;
146
+ candidates: number;
147
+ } | null;
109
148
  };
110
149
  ast: {
111
150
  size: number;
@@ -142,6 +181,21 @@ export declare interface BrowserRuntimeOptions {
142
181
  * with the same name is treated as covered, and rules added later to an already-indexed sheet aren't seen.
143
182
  */
144
183
  skipExisting?: boolean;
184
+ /**
185
+ * #269: reclaim the rules of classes that no element inside the observed root carries any more.
186
+ * On by default; it only acts on classes seen through `observe()`. A class is deleted only after
187
+ * its refcount has stayed 0 for `gcGraceMs` and a live-DOM re-check finds no element with it.
188
+ * Never reclaimed: classes passed to `addClass()`, classes any pre-existing (non-BaroCSS) sheet
189
+ * defines (build output, server sheet), root/@property/preflight rules. `false` disables it.
190
+ */
191
+ gc?: boolean;
192
+ /** #269: how long a class must stay unused before its rules are deleted (default 3000 ms). */
193
+ gcGraceMs?: number;
194
+ /**
195
+ * #269: soft cap on cached classes. When exceeded, unused (refcount 0) classes are evicted
196
+ * oldest-first without waiting for the grace period; classes in use are never evicted. Default: no cap.
197
+ */
198
+ maxRules?: number;
145
199
  }
146
200
 
147
201
  /**
@@ -174,6 +228,9 @@ export declare class ChangeDetector {
174
228
  * @param BrowserRuntime - Optional BrowserRuntime instance for CSS injection
175
229
  */
176
230
  constructor(incrementalParser: IncrementalParser, BrowserRuntime?: BrowserRuntime, getCategory?: (cls: string) => string | undefined);
231
+ /** #269: refcount/GC tracker, when the runtime has GC enabled. */
232
+ private gc;
233
+ setGc(gc: ClassGc | null): void;
177
234
  setParser(parser: IncrementalParser): void;
178
235
  /**
179
236
  * Starts observing DOM changes for new CSS classes
@@ -221,6 +278,61 @@ export declare class ChangeDetector {
221
278
  disconnect(): void;
222
279
  }
223
280
 
281
+ declare class ClassGc {
282
+ private host;
283
+ private graceMs;
284
+ private maxRules;
285
+ private now;
286
+ private counts;
287
+ private counted;
288
+ /** class -> time its count reached 0 (insertion order = oldest first). */
289
+ private candidates;
290
+ private timer;
291
+ private root;
292
+ constructor(host: ClassGcHost, graceMs: number, maxRules: number, now?: () => number);
293
+ /** Start counting for a new root: count every element currently inside it. */
294
+ setRoot(root: Element): void;
295
+ count(cls: string): number;
296
+ /** Re-count `el` and (optionally) all its descendants from their current state. */
297
+ reconcileTree(node: Node): void;
298
+ reconcile(el: Element): void;
299
+ /** Call after a mutation batch has been counted and its classes inserted. */
300
+ afterBatch(): void;
301
+ private schedule;
302
+ /** Reclaim candidates whose grace period elapsed (plus LRU overflow). Public for tests. */
303
+ sweep(): void;
304
+ private inDom;
305
+ cancel(): void;
306
+ stats(): {
307
+ trackedClasses: number;
308
+ candidates: number;
309
+ };
310
+ }
311
+
312
+ /**
313
+ * #269: per-class refcount over the observed root, plus a delayed sweep that
314
+ * reclaims classes no live element carries any more.
315
+ *
316
+ * Counting is reconciliation, not delta arithmetic: for every element a
317
+ * mutation batch touches (attribute target, added subtree, removed subtree) we
318
+ * compare the classes we last counted for it with what it carries *now*
319
+ * (nothing if it is no longer inside the root). That makes the count
320
+ * independent of record order, so remove-then-re-add in one batch, moves
321
+ * between parents and edits made while detached all settle to the true state.
322
+ * An element we miss can only leak a count (rule kept), never drop one.
323
+ *
324
+ * Before a class is reclaimed the sweep re-checks the live DOM
325
+ * (`getElementsByClassName`), so even a miscount cannot unstyle a live element.
326
+ */
327
+ declare interface ClassGcHost {
328
+ /** Delete the rules of these classes. */
329
+ reclaim(classes: string[]): void;
330
+ /** Whether a class must never be reclaimed (pinned, found in a pre-existing sheet, ...). */
331
+ isPermanent(cls: string): boolean;
332
+ /** Number of generated rules/classes currently cached (for the LRU cap). */
333
+ cachedCount(): number;
334
+ }
335
+
224
336
  /** Collect literal className tokens from a json-render Spec's flat elements map. */
225
337
  export declare function collectJsonRenderClassNames(spec: unknown): string[];
226
338
 
@@ -242,30 +354,14 @@ export declare function normalizeClassNameList(className: any): string[];
242
354
  /** Submit literal classes synchronously before UI mount; the caller validates class support. */
243
355
  export declare function preloadJsonRenderClasses(spec: unknown, runtime: Pick<BrowserRuntime, 'addClass'>): void;
244
356
 
245
- /**
246
- * Tailwind-compatible cascade order for runtime-inserted rules (#254).
247
- *
248
- * The runtime discovers classes in DOM order, so without sorting `lg:px-8`
249
- * seen before `sm:px-6` would land earlier and lose at >= 1024px. Each rule
250
- * gets a sort key derived from its leading `@media` / `@container` preludes:
251
- *
252
- * 0 base, state media (hover), motion/contrast, unknown
253
- * 1 max-* breakpoints (larger width first)
254
- * 2 min-* breakpoints (smaller width first)
255
- * 3 @max-* container queries (larger width first)
256
- * 4 @min-* container queries (smaller width first)
257
- * 5 orientation, dark (prefers-color-scheme), print, forced-colors
258
- *
259
- * Nested at-rules (e.g. `sm:dark:`) contribute one key pair per level, so
260
- * `sm:` < `sm:dark:` < `md:`. Equal keys keep discovery order.
261
- */
262
- declare type RuleKey = number[];
263
-
264
357
  export declare const shadcnTheme: {
265
358
  colors: Record<string, string>;
266
359
  borderRadius: Record<string, string>;
267
360
  };
268
361
 
362
+ /** #268: marks a server-rendered sheet (`@barocss/server` `ssrStyleTag()`); the runtime adopts its class rules. */
363
+ export declare const SSR_STYLE_SELECTOR = "style[data-barocss-ssr]";
364
+
269
365
  export declare interface StylePartition {
270
366
  id: string;
271
367
  styles: string[];
@@ -316,6 +412,15 @@ export declare class StylePartitionManager {
316
412
  success: number;
317
413
  failed: number;
318
414
  };
415
+ /**
416
+ * Remove one generated rule (#269 GC). Keeps `styles`, the #254 `keys` and the
417
+ * sheet's cssRules parallel: one deleteRule at the rule's index, or a text
418
+ * rebuild when the sheet isn't solely ours / has no CSSOM. Returns whether
419
+ * the rule was found.
420
+ */
421
+ removeRule(rule: string, category?: string): boolean;
422
+ /** Number of generated (non-root, non-preflight) rules currently held. */
423
+ get ruleCount(): number;
319
424
  /**
320
425
  * 특정 규칙이 어느 파티션에 있는지 찾기
321
426
  */