@tenphi/tasty 0.0.0-snapshot.fafcbfe → 0.0.0-snapshot.ffe00e9

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.
Files changed (306) hide show
  1. package/README.md +315 -159
  2. package/dist/_virtual/_rolldown/runtime.js +1 -2
  3. package/dist/chunks/cacheKey.d.ts +1 -0
  4. package/dist/chunks/cacheKey.js +17 -10
  5. package/dist/chunks/cacheKey.js.map +1 -1
  6. package/dist/chunks/definitions.d.ts +1 -1
  7. package/dist/chunks/definitions.js +3 -5
  8. package/dist/chunks/definitions.js.map +1 -1
  9. package/dist/chunks/index.d.ts +1 -0
  10. package/dist/chunks/renderChunk.d.ts +1 -0
  11. package/dist/chunks/renderChunk.js +32 -34
  12. package/dist/chunks/renderChunk.js.map +1 -1
  13. package/dist/compute-styles.d.ts +31 -0
  14. package/dist/compute-styles.js +357 -0
  15. package/dist/compute-styles.js.map +1 -0
  16. package/dist/config.d.ts +116 -30
  17. package/dist/config.js +144 -45
  18. package/dist/config.js.map +1 -1
  19. package/dist/core/index.d.ts +12 -11
  20. package/dist/core/index.js +11 -10
  21. package/dist/counter-style/index.js +51 -0
  22. package/dist/counter-style/index.js.map +1 -0
  23. package/dist/debug.d.ts +26 -141
  24. package/dist/debug.js +357 -637
  25. package/dist/debug.js.map +1 -1
  26. package/dist/font-face/index.js +63 -0
  27. package/dist/font-face/index.js.map +1 -0
  28. package/dist/hooks/index.d.ts +7 -0
  29. package/dist/hooks/useCounterStyle.d.ts +50 -0
  30. package/dist/hooks/useCounterStyle.js +45 -0
  31. package/dist/hooks/useCounterStyle.js.map +1 -0
  32. package/dist/hooks/useFontFace.d.ts +43 -0
  33. package/dist/hooks/useFontFace.js +69 -0
  34. package/dist/hooks/useFontFace.js.map +1 -0
  35. package/dist/hooks/useGlobalStyles.d.ts +3 -0
  36. package/dist/hooks/useGlobalStyles.js +23 -2
  37. package/dist/hooks/useGlobalStyles.js.map +1 -1
  38. package/dist/hooks/useKeyframes.js +13 -4
  39. package/dist/hooks/useKeyframes.js.map +1 -1
  40. package/dist/hooks/useProperty.js +31 -14
  41. package/dist/hooks/useProperty.js.map +1 -1
  42. package/dist/hooks/useRawCSS.js +8 -2
  43. package/dist/hooks/useRawCSS.js.map +1 -1
  44. package/dist/hooks/useStyles.d.ts +4 -4
  45. package/dist/hooks/useStyles.js +7 -147
  46. package/dist/hooks/useStyles.js.map +1 -1
  47. package/dist/index.d.ts +23 -12
  48. package/dist/index.js +14 -11
  49. package/dist/injector/index.d.ts +41 -11
  50. package/dist/injector/index.js +48 -11
  51. package/dist/injector/index.js.map +1 -1
  52. package/dist/injector/injector.d.ts +58 -3
  53. package/dist/injector/injector.js +204 -41
  54. package/dist/injector/injector.js.map +1 -1
  55. package/dist/injector/sheet-manager.d.ts +18 -17
  56. package/dist/injector/sheet-manager.js +45 -73
  57. package/dist/injector/sheet-manager.js.map +1 -1
  58. package/dist/injector/types.d.ts +119 -19
  59. package/dist/keyframes/index.js +1 -1
  60. package/dist/parser/classify.js +6 -6
  61. package/dist/parser/classify.js.map +1 -1
  62. package/dist/parser/const.js +30 -3
  63. package/dist/parser/const.js.map +1 -1
  64. package/dist/parser/lru.js +1 -1
  65. package/dist/parser/lru.js.map +1 -1
  66. package/dist/parser/parser.js +2 -3
  67. package/dist/parser/parser.js.map +1 -1
  68. package/dist/parser/tokenizer.js +1 -1
  69. package/dist/parser/tokenizer.js.map +1 -1
  70. package/dist/parser/types.js +1 -1
  71. package/dist/parser/types.js.map +1 -1
  72. package/dist/pipeline/conditions.js +15 -9
  73. package/dist/pipeline/conditions.js.map +1 -1
  74. package/dist/pipeline/exclusive.js +1 -2
  75. package/dist/pipeline/exclusive.js.map +1 -1
  76. package/dist/pipeline/index.d.ts +3 -1
  77. package/dist/pipeline/index.js +139 -72
  78. package/dist/pipeline/index.js.map +1 -1
  79. package/dist/pipeline/materialize.js +405 -210
  80. package/dist/pipeline/materialize.js.map +1 -1
  81. package/dist/pipeline/parseStateKey.d.ts +1 -1
  82. package/dist/pipeline/parseStateKey.js +38 -22
  83. package/dist/pipeline/parseStateKey.js.map +1 -1
  84. package/dist/pipeline/simplify.js +112 -154
  85. package/dist/pipeline/simplify.js.map +1 -1
  86. package/dist/pipeline/warnings.js +18 -0
  87. package/dist/pipeline/warnings.js.map +1 -0
  88. package/dist/plugins/index.d.ts +2 -0
  89. package/dist/plugins/okhsl-plugin.js +3 -277
  90. package/dist/plugins/okhsl-plugin.js.map +1 -1
  91. package/dist/plugins/types.d.ts +9 -2
  92. package/dist/properties/index.js +83 -19
  93. package/dist/properties/index.js.map +1 -1
  94. package/dist/properties/property-type-resolver.d.ts +24 -0
  95. package/dist/properties/property-type-resolver.js +90 -0
  96. package/dist/properties/property-type-resolver.js.map +1 -0
  97. package/dist/ssr/astro.d.ts +29 -0
  98. package/dist/ssr/astro.js +64 -0
  99. package/dist/ssr/astro.js.map +1 -0
  100. package/dist/ssr/async-storage.d.ts +17 -0
  101. package/dist/ssr/async-storage.js +34 -0
  102. package/dist/ssr/async-storage.js.map +1 -0
  103. package/dist/ssr/collect-auto-properties.js +39 -0
  104. package/dist/ssr/collect-auto-properties.js.map +1 -0
  105. package/dist/ssr/collector.d.ts +102 -0
  106. package/dist/ssr/collector.js +219 -0
  107. package/dist/ssr/collector.js.map +1 -0
  108. package/dist/ssr/format-global-rules.js +22 -0
  109. package/dist/ssr/format-global-rules.js.map +1 -0
  110. package/dist/ssr/format-keyframes.js +69 -0
  111. package/dist/ssr/format-keyframes.js.map +1 -0
  112. package/dist/ssr/format-property.js +49 -0
  113. package/dist/ssr/format-property.js.map +1 -0
  114. package/dist/ssr/format-rules.js +73 -0
  115. package/dist/ssr/format-rules.js.map +1 -0
  116. package/dist/ssr/hydrate.d.ts +22 -0
  117. package/dist/ssr/hydrate.js +49 -0
  118. package/dist/ssr/hydrate.js.map +1 -0
  119. package/dist/ssr/index.d.ts +4 -0
  120. package/dist/ssr/index.js +10 -0
  121. package/dist/ssr/index.js.map +1 -0
  122. package/dist/ssr/next.d.ts +43 -0
  123. package/dist/ssr/next.js +74 -0
  124. package/dist/ssr/next.js.map +1 -0
  125. package/dist/ssr/ssr-collector-ref.js +12 -0
  126. package/dist/ssr/ssr-collector-ref.js.map +1 -0
  127. package/dist/states/index.js +13 -259
  128. package/dist/states/index.js.map +1 -1
  129. package/dist/static/index.js +1 -2
  130. package/dist/static/inject.d.ts +5 -0
  131. package/dist/static/inject.js +17 -0
  132. package/dist/static/inject.js.map +1 -0
  133. package/dist/static/tastyStatic.js +1 -2
  134. package/dist/static/tastyStatic.js.map +1 -1
  135. package/dist/static/types.js +1 -1
  136. package/dist/styles/border.d.ts +2 -2
  137. package/dist/styles/border.js +30 -24
  138. package/dist/styles/border.js.map +1 -1
  139. package/dist/styles/color.d.ts +3 -3
  140. package/dist/styles/color.js +11 -8
  141. package/dist/styles/color.js.map +1 -1
  142. package/dist/styles/const.js +17 -0
  143. package/dist/styles/const.js.map +1 -0
  144. package/dist/styles/createStyle.js +29 -27
  145. package/dist/styles/createStyle.js.map +1 -1
  146. package/dist/styles/dimension.js +15 -3
  147. package/dist/styles/dimension.js.map +1 -1
  148. package/dist/styles/directional.js +133 -0
  149. package/dist/styles/directional.js.map +1 -0
  150. package/dist/styles/display.d.ts +3 -10
  151. package/dist/styles/display.js +45 -39
  152. package/dist/styles/display.js.map +1 -1
  153. package/dist/styles/fade.d.ts +2 -2
  154. package/dist/styles/fade.js +9 -5
  155. package/dist/styles/fade.js.map +1 -1
  156. package/dist/styles/fill.d.ts +15 -17
  157. package/dist/styles/fill.js +9 -9
  158. package/dist/styles/fill.js.map +1 -1
  159. package/dist/styles/flow.d.ts +3 -3
  160. package/dist/styles/flow.js +1 -1
  161. package/dist/styles/flow.js.map +1 -1
  162. package/dist/styles/gap.d.ts +1 -1
  163. package/dist/styles/gap.js +4 -3
  164. package/dist/styles/gap.js.map +1 -1
  165. package/dist/styles/height.d.ts +1 -1
  166. package/dist/styles/height.js +1 -2
  167. package/dist/styles/height.js.map +1 -1
  168. package/dist/styles/index.d.ts +0 -1
  169. package/dist/styles/index.js +3 -4
  170. package/dist/styles/index.js.map +1 -1
  171. package/dist/styles/inset.d.ts +1 -29
  172. package/dist/styles/inset.js +19 -135
  173. package/dist/styles/inset.js.map +1 -1
  174. package/dist/styles/list.d.ts +5 -5
  175. package/dist/styles/list.js +4 -2
  176. package/dist/styles/list.js.map +1 -1
  177. package/dist/styles/margin.d.ts +1 -1
  178. package/dist/styles/margin.js +17 -89
  179. package/dist/styles/margin.js.map +1 -1
  180. package/dist/styles/outline.d.ts +1 -1
  181. package/dist/styles/outline.js +6 -16
  182. package/dist/styles/outline.js.map +1 -1
  183. package/dist/styles/padding.d.ts +1 -1
  184. package/dist/styles/padding.js +17 -89
  185. package/dist/styles/padding.js.map +1 -1
  186. package/dist/styles/placement.d.ts +37 -0
  187. package/dist/styles/placement.js +74 -0
  188. package/dist/styles/placement.js.map +1 -0
  189. package/dist/styles/predefined.d.ts +4 -6
  190. package/dist/styles/predefined.js +9 -13
  191. package/dist/styles/predefined.js.map +1 -1
  192. package/dist/styles/preset.d.ts +6 -1
  193. package/dist/styles/preset.js +54 -53
  194. package/dist/styles/preset.js.map +1 -1
  195. package/dist/styles/radius.d.ts +2 -4
  196. package/dist/styles/radius.js +38 -6
  197. package/dist/styles/radius.js.map +1 -1
  198. package/dist/styles/scrollMargin.d.ts +24 -0
  199. package/dist/styles/scrollMargin.js +32 -0
  200. package/dist/styles/scrollMargin.js.map +1 -0
  201. package/dist/styles/scrollbar.d.ts +9 -5
  202. package/dist/styles/scrollbar.js +31 -85
  203. package/dist/styles/scrollbar.js.map +1 -1
  204. package/dist/styles/shadow.d.ts +3 -3
  205. package/dist/styles/shadow.js +4 -3
  206. package/dist/styles/shadow.js.map +1 -1
  207. package/dist/styles/shared.js +17 -0
  208. package/dist/styles/shared.js.map +1 -0
  209. package/dist/styles/transition.d.ts +2 -2
  210. package/dist/styles/transition.js +6 -5
  211. package/dist/styles/transition.js.map +1 -1
  212. package/dist/styles/types.d.ts +94 -28
  213. package/dist/styles/width.d.ts +1 -1
  214. package/dist/styles/width.js +1 -2
  215. package/dist/styles/width.js.map +1 -1
  216. package/dist/tasty.d.ts +46 -894
  217. package/dist/tasty.js +112 -60
  218. package/dist/tasty.js.map +1 -1
  219. package/dist/types.d.ts +3 -3
  220. package/dist/utils/cache-wrapper.js +4 -9
  221. package/dist/utils/cache-wrapper.js.map +1 -1
  222. package/dist/utils/case-converter.js +1 -1
  223. package/dist/utils/color-math.d.ts +46 -0
  224. package/dist/utils/color-math.js +749 -0
  225. package/dist/utils/color-math.js.map +1 -0
  226. package/dist/utils/color-space.d.ts +5 -0
  227. package/dist/utils/color-space.js +228 -0
  228. package/dist/utils/color-space.js.map +1 -0
  229. package/dist/utils/colors.d.ts +1 -1
  230. package/dist/utils/colors.js +3 -2
  231. package/dist/utils/colors.js.map +1 -1
  232. package/dist/utils/css-types.d.ts +1 -1
  233. package/dist/utils/dotize.d.ts +1 -1
  234. package/dist/utils/dotize.js +1 -1
  235. package/dist/utils/dotize.js.map +1 -1
  236. package/dist/utils/filter-base-props.js +1 -1
  237. package/dist/utils/get-display-name.js +1 -1
  238. package/dist/utils/has-keys.js +13 -0
  239. package/dist/utils/has-keys.js.map +1 -0
  240. package/dist/utils/is-dev-env.js +1 -1
  241. package/dist/utils/is-dev-env.js.map +1 -1
  242. package/dist/utils/is-valid-element-type.js +1 -1
  243. package/dist/utils/merge-styles.js +1 -2
  244. package/dist/utils/merge-styles.js.map +1 -1
  245. package/dist/utils/mod-attrs.d.ts +0 -2
  246. package/dist/utils/mod-attrs.js +3 -4
  247. package/dist/utils/mod-attrs.js.map +1 -1
  248. package/dist/utils/process-tokens.d.ts +3 -17
  249. package/dist/utils/process-tokens.js +19 -107
  250. package/dist/utils/process-tokens.js.map +1 -1
  251. package/dist/utils/resolve-recipes.js +1 -2
  252. package/dist/utils/resolve-recipes.js.map +1 -1
  253. package/dist/utils/selector-transform.js +32 -0
  254. package/dist/utils/selector-transform.js.map +1 -0
  255. package/dist/utils/string.js +1 -1
  256. package/dist/utils/string.js.map +1 -1
  257. package/dist/utils/styles.d.ts +5 -89
  258. package/dist/utils/styles.js +35 -400
  259. package/dist/utils/styles.js.map +1 -1
  260. package/dist/utils/typography.d.ts +24 -13
  261. package/dist/utils/typography.js +15 -17
  262. package/dist/utils/typography.js.map +1 -1
  263. package/dist/utils/warnings.js +1 -1
  264. package/dist/zero/babel.d.ts +84 -10
  265. package/dist/zero/babel.js +193 -37
  266. package/dist/zero/babel.js.map +1 -1
  267. package/dist/zero/css-writer.js +1 -2
  268. package/dist/zero/css-writer.js.map +1 -1
  269. package/dist/zero/extractor.js +119 -3
  270. package/dist/zero/extractor.js.map +1 -1
  271. package/dist/zero/index.js +1 -2
  272. package/dist/zero/next.d.ts +56 -30
  273. package/dist/zero/next.js +105 -40
  274. package/dist/zero/next.js.map +1 -1
  275. package/docs/PIPELINE.md +519 -0
  276. package/docs/README.md +31 -0
  277. package/docs/adoption.md +296 -0
  278. package/docs/comparison.md +420 -0
  279. package/docs/configuration.md +326 -0
  280. package/docs/debug.md +318 -0
  281. package/docs/design-system.md +424 -0
  282. package/docs/dsl.md +673 -0
  283. package/docs/getting-started.md +217 -0
  284. package/docs/injector.md +543 -0
  285. package/docs/methodology.md +616 -0
  286. package/docs/runtime.md +572 -0
  287. package/docs/ssr.md +354 -0
  288. package/docs/styles.md +596 -0
  289. package/docs/tasty-static.md +520 -0
  290. package/package.json +106 -31
  291. package/dist/styles/align.d.ts +0 -15
  292. package/dist/styles/align.js +0 -14
  293. package/dist/styles/align.js.map +0 -1
  294. package/dist/styles/justify.d.ts +0 -15
  295. package/dist/styles/justify.js +0 -14
  296. package/dist/styles/justify.js.map +0 -1
  297. package/dist/styles/styledScrollbar.d.ts +0 -47
  298. package/dist/styles/styledScrollbar.js +0 -38
  299. package/dist/styles/styledScrollbar.js.map +0 -1
  300. package/dist/tokens/typography.d.ts +0 -19
  301. package/dist/tokens/typography.js +0 -237
  302. package/dist/tokens/typography.js.map +0 -1
  303. package/dist/utils/hsl-to-rgb.js +0 -38
  304. package/dist/utils/hsl-to-rgb.js.map +0 -1
  305. package/dist/utils/okhsl-to-rgb.js +0 -296
  306. package/dist/utils/okhsl-to-rgb.js.map +0 -1
@@ -1,18 +1,9 @@
1
- import { CacheMetrics, DisposeFunction, GlobalInjectResult, InjectResult, KeyframesCacheEntry, KeyframesInfo, KeyframesResult, KeyframesSteps, PropertyDefinition, RawCSSResult, RootRegistry, RuleInfo, SheetInfo, StyleInjectorConfig, StyleRule } from "./types.js";
1
+ import { CacheMetrics, CounterStyleDescriptors, DisposeFunction, FontFaceDescriptors, FontFaceInput, GCConfig, GCOptions, GlobalInjectResult, InjectResult, KeyframesCacheEntry, KeyframesInfo, KeyframesResult, KeyframesSteps, PropertyDefinition, RawCSSResult, RootRegistry, RuleInfo, SheetInfo, StyleInjectorConfig, StyleRule, StyleUsage } from "./types.js";
2
2
  import { StyleResult } from "../pipeline/index.js";
3
3
  import { SheetManager } from "./sheet-manager.js";
4
4
  import { StyleInjector } from "./injector.js";
5
5
 
6
6
  //#region src/injector/index.d.ts
7
- /**
8
- * Allocate a className for a cacheKey without injecting styles yet
9
- */
10
- declare function allocateClassName(cacheKey: string, options?: {
11
- root?: Document | ShadowRoot;
12
- }): {
13
- className: string;
14
- isNewAllocation: boolean;
15
- };
16
7
  /**
17
8
  * Inject styles and return className with dispose function
18
9
  */
@@ -117,6 +108,24 @@ declare function property(name: string, options?: PropertyOptions): void;
117
108
  declare function isPropertyDefined(name: string, options?: {
118
109
  root?: Document | ShadowRoot;
119
110
  }): boolean;
111
+ /**
112
+ * Inject a CSS @font-face rule.
113
+ *
114
+ * Permanent and global — no dispose or ref-counting.
115
+ * Deduplicates by content hash (family + descriptors).
116
+ */
117
+ declare function fontFace(family: string, descriptors: FontFaceDescriptors, options?: {
118
+ root?: Document | ShadowRoot;
119
+ }): void;
120
+ /**
121
+ * Inject a CSS @counter-style rule.
122
+ *
123
+ * Permanent and global — no dispose or ref-counting.
124
+ * Deduplicates by name (first definition wins).
125
+ */
126
+ declare function counterStyle(name: string, descriptors: CounterStyleDescriptors, options?: {
127
+ root?: Document | ShadowRoot;
128
+ }): void;
120
129
  /**
121
130
  * Get CSS text from all sheets (for SSR)
122
131
  */
@@ -134,6 +143,27 @@ declare function getCssTextForNode(node: ParentNode | Element | DocumentFragment
134
143
  * Force cleanup of unused rules
135
144
  */
136
145
  declare function cleanup(root?: Document | ShadowRoot): void;
146
+ /**
147
+ * Record a render-time usage hit for one or more classNames.
148
+ * Used internally by computeStyles and tasty() to track style popularity for GC.
149
+ */
150
+ declare function touch(className: string, options?: {
151
+ root?: Document | ShadowRoot;
152
+ }): void;
153
+ /**
154
+ * Synchronous garbage collection of unused styles.
155
+ * Scans the DOM for live classNames (never evicts them), then evicts
156
+ * absent styles whose age exceeds their popularity-weighted TTL.
157
+ *
158
+ * @returns Number of styles evicted.
159
+ */
160
+ declare function gc(options?: GCOptions): number;
161
+ /**
162
+ * Event-driven GC with cooldown.
163
+ * Skips if called within the configured cooldown of the last run.
164
+ * Schedules via requestIdleCallback when available.
165
+ */
166
+ declare function maybeGC(options?: GCOptions): void;
137
167
  /**
138
168
  * Check if we're currently running in a test environment
139
169
  */
@@ -153,5 +183,5 @@ declare function destroy(root?: Document | ShadowRoot): void;
153
183
  */
154
184
  declare function createInjector(config?: Partial<StyleInjectorConfig>): StyleInjector;
155
185
  //#endregion
156
- export { PropertyOptions, allocateClassName, cleanup, createInjector, destroy, getCssText, getCssTextForNode, getIsTestEnvironment, getRawCSSText, inject, injectGlobal, injectRawCSS, injector, isPropertyDefined, keyframes, property };
186
+ export { PropertyOptions, cleanup, counterStyle, createInjector, destroy, fontFace, gc, getCssText, getCssTextForNode, getIsTestEnvironment, getRawCSSText, inject, injectGlobal, injectRawCSS, injector, isPropertyDefined, keyframes, maybeGC, property, touch };
157
187
  //# sourceMappingURL=index.d.ts.map
@@ -1,20 +1,14 @@
1
- import { SheetManager } from "./sheet-manager.js";
1
+ import "./sheet-manager.js";
2
2
  import { StyleInjector } from "./injector.js";
3
3
  import { getConfig, getGlobalInjector, isTestEnvironment, markStylesGenerated } from "../config.js";
4
-
5
4
  //#region src/injector/index.ts
6
5
  /**
7
- * Allocate a className for a cacheKey without injecting styles yet
8
- */
9
- function allocateClassName(cacheKey, options) {
10
- return getGlobalInjector().allocateClassName(cacheKey, options);
11
- }
12
- /**
13
6
  * Inject styles and return className with dispose function
14
7
  */
15
8
  function inject(rules, options) {
9
+ const injector = getGlobalInjector();
16
10
  markStylesGenerated();
17
- return getGlobalInjector().inject(rules, options);
11
+ return injector.inject(rules, options);
18
12
  }
19
13
  /**
20
14
  * Inject global rules that should not reserve tasty class names
@@ -93,6 +87,24 @@ function isPropertyDefined(name, options) {
93
87
  return getGlobalInjector().isPropertyDefined(name, options);
94
88
  }
95
89
  /**
90
+ * Inject a CSS @font-face rule.
91
+ *
92
+ * Permanent and global — no dispose or ref-counting.
93
+ * Deduplicates by content hash (family + descriptors).
94
+ */
95
+ function fontFace(family, descriptors, options) {
96
+ return getGlobalInjector().fontFace(family, descriptors, options);
97
+ }
98
+ /**
99
+ * Inject a CSS @counter-style rule.
100
+ *
101
+ * Permanent and global — no dispose or ref-counting.
102
+ * Deduplicates by name (first definition wins).
103
+ */
104
+ function counterStyle(name, descriptors, options) {
105
+ return getGlobalInjector().counterStyle(name, descriptors, options);
106
+ }
107
+ /**
96
108
  * Get CSS text from all sheets (for SSR)
97
109
  */
98
110
  function getCssText(options) {
@@ -121,6 +133,31 @@ function cleanup(root) {
121
133
  return getGlobalInjector().cleanup(root);
122
134
  }
123
135
  /**
136
+ * Record a render-time usage hit for one or more classNames.
137
+ * Used internally by computeStyles and tasty() to track style popularity for GC.
138
+ */
139
+ function touch(className, options) {
140
+ return getGlobalInjector().touch(className, options);
141
+ }
142
+ /**
143
+ * Synchronous garbage collection of unused styles.
144
+ * Scans the DOM for live classNames (never evicts them), then evicts
145
+ * absent styles whose age exceeds their popularity-weighted TTL.
146
+ *
147
+ * @returns Number of styles evicted.
148
+ */
149
+ function gc(options) {
150
+ return getGlobalInjector().gc(options);
151
+ }
152
+ /**
153
+ * Event-driven GC with cooldown.
154
+ * Skips if called within the configured cooldown of the last run.
155
+ * Schedules via requestIdleCallback when available.
156
+ */
157
+ function maybeGC(options) {
158
+ return getGlobalInjector().maybeGC(options);
159
+ }
160
+ /**
124
161
  * Check if we're currently running in a test environment
125
162
  */
126
163
  function getIsTestEnvironment() {
@@ -148,7 +185,7 @@ function createInjector(config = {}) {
148
185
  ...config
149
186
  });
150
187
  }
151
-
152
188
  //#endregion
153
- export { allocateClassName, cleanup, createInjector, destroy, getCssText, getCssTextForNode, getIsTestEnvironment, getRawCSSText, inject, injectGlobal, injectRawCSS, injector, isPropertyDefined, keyframes, property };
189
+ export { cleanup, counterStyle, createInjector, destroy, fontFace, gc, getCssText, getCssTextForNode, getIsTestEnvironment, getRawCSSText, inject, injectGlobal, injectRawCSS, injector, isPropertyDefined, keyframes, maybeGC, property, touch };
190
+
154
191
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../../src/injector/index.ts"],"sourcesContent":["import {\n getConfig,\n getGlobalInjector,\n isTestEnvironment,\n markStylesGenerated,\n} from '../config';\nimport type { StyleResult } from '../pipeline';\n\nimport { StyleInjector } from './injector';\nimport type {\n GlobalInjectResult,\n InjectResult,\n KeyframesResult,\n KeyframesSteps,\n StyleInjectorConfig,\n} from './types';\n\n/**\n * Allocate a className for a cacheKey without injecting styles yet\n */\nexport function allocateClassName(\n cacheKey: string,\n options?: { root?: Document | ShadowRoot },\n): { className: string; isNewAllocation: boolean } {\n return getGlobalInjector().allocateClassName(cacheKey, options);\n}\n\n/**\n * Inject styles and return className with dispose function\n */\nexport function inject(\n rules: StyleResult[],\n options?: { root?: Document | ShadowRoot; cacheKey?: string },\n): InjectResult {\n // Mark that styles have been generated (prevents configuration changes)\n markStylesGenerated();\n\n return getGlobalInjector().inject(rules, options);\n}\n\n/**\n * Inject global rules that should not reserve tasty class names\n */\nexport function injectGlobal(\n rules: StyleResult[],\n options?: { root?: Document | ShadowRoot },\n): GlobalInjectResult {\n return getGlobalInjector().injectGlobal(rules, options);\n}\n\n/**\n * Inject raw CSS text directly without parsing\n * This is a low-overhead method for injecting raw CSS that doesn't need tasty processing.\n * The CSS is inserted into a separate style element to avoid conflicts with tasty's chunking.\n *\n * @example\n * ```tsx\n * // Inject raw CSS\n * const { dispose } = injectRawCSS(`\n * body { margin: 0; padding: 0; }\n * .my-class { color: red; }\n * `);\n *\n * // Later, remove the injected CSS\n * dispose();\n * ```\n */\nexport function injectRawCSS(\n css: string,\n options?: { root?: Document | ShadowRoot },\n): { dispose: () => void } {\n return getGlobalInjector().injectRawCSS(css, options);\n}\n\n/**\n * Get raw CSS text for SSR\n */\nexport function getRawCSSText(options?: {\n root?: Document | ShadowRoot;\n}): string {\n return getGlobalInjector().getRawCSSText(options);\n}\n\n/**\n * Inject keyframes and return object with toString() and dispose()\n */\nexport function keyframes(\n steps: KeyframesSteps,\n nameOrOptions?: string | { root?: Document | ShadowRoot; name?: string },\n): KeyframesResult {\n return getGlobalInjector().keyframes(steps, nameOrOptions);\n}\n\nexport interface PropertyOptions {\n /**\n * CSS syntax string for the property (e.g., '<color>', '<length>', '<angle>')\n * @see https://developer.mozilla.org/en-US/docs/Web/CSS/@property/syntax\n */\n syntax?: string;\n /**\n * Whether the property inherits from parent elements\n * @default true\n */\n inherits?: boolean;\n /**\n * Initial value for the property\n */\n initialValue?: string | number;\n /**\n * Shadow root or document to inject into\n */\n root?: Document | ShadowRoot;\n}\n\n/**\n * Define a CSS @property custom property.\n * This enables advanced features like animating custom properties.\n *\n * Note: @property rules are global and persistent once defined.\n * Re-registering the same property name is a no-op.\n *\n * @param name - The custom property name (must start with --)\n * @param options - Property configuration\n *\n * @example\n * ```ts\n * // Define a color property that can be animated\n * property('--my-color', {\n * syntax: '<color>',\n * initialValue: 'red',\n * });\n *\n * // Define an angle property\n * property('--rotation', {\n * syntax: '<angle>',\n * inherits: false,\n * initialValue: '0deg',\n * });\n * ```\n */\nexport function property(name: string, options?: PropertyOptions): void {\n return getGlobalInjector().property(name, options);\n}\n\n/**\n * Check if a CSS @property has already been defined\n *\n * @param name - The custom property name to check\n * @param options - Options including root\n */\nexport function isPropertyDefined(\n name: string,\n options?: { root?: Document | ShadowRoot },\n): boolean {\n return getGlobalInjector().isPropertyDefined(name, options);\n}\n\n/**\n * Get CSS text from all sheets (for SSR)\n */\nexport function getCssText(options?: { root?: Document | ShadowRoot }): string {\n return getGlobalInjector().getCssText(options);\n}\n\n/**\n * Collect only CSS used by a rendered subtree (like jest-styled-components).\n * Pass the container returned by render(...).\n */\nexport function getCssTextForNode(\n node: ParentNode | Element | DocumentFragment,\n options?: { root?: Document | ShadowRoot },\n): string {\n // Collect tasty-generated class names (t<number>) from the subtree\n const classSet = new Set<string>();\n\n const readClasses = (el: Element) => {\n const cls = el.getAttribute('class');\n if (!cls) return;\n for (const token of cls.split(/\\s+/)) {\n if (/^t\\d+$/.test(token)) classSet.add(token);\n }\n };\n\n // Include node itself if it's an Element\n if ((node as Element).getAttribute) {\n readClasses(node as Element);\n }\n // Walk descendants\n const elements = (node as ParentNode).querySelectorAll\n ? (node as ParentNode).querySelectorAll('[class]')\n : ([] as unknown as NodeListOf<Element>);\n if (elements) elements.forEach(readClasses);\n\n return getGlobalInjector().getCssTextForClasses(classSet, options);\n}\n\n/**\n * Force cleanup of unused rules\n */\nexport function cleanup(root?: Document | ShadowRoot): void {\n return getGlobalInjector().cleanup(root);\n}\n\n/**\n * Check if we're currently running in a test environment\n */\nexport function getIsTestEnvironment(): boolean {\n return isTestEnvironment();\n}\n\n/**\n * Get the global injector instance for debugging\n */\nexport const injector = {\n get instance() {\n return getGlobalInjector();\n },\n};\n\n/**\n * Destroy all resources and clean up\n */\nexport function destroy(root?: Document | ShadowRoot): void {\n return getGlobalInjector().destroy(root);\n}\n\n/**\n * Create a new isolated injector instance\n */\nexport function createInjector(\n config: Partial<StyleInjectorConfig> = {},\n): StyleInjector {\n const defaultConfig = getConfig();\n\n const fullConfig: StyleInjectorConfig = {\n ...defaultConfig,\n // Auto-enable forceTextInjection in test environments\n forceTextInjection: config.forceTextInjection ?? isTestEnvironment(),\n ...config,\n };\n\n return new StyleInjector(fullConfig);\n}\n\n// Re-export types\nexport type {\n StyleInjectorConfig,\n InjectResult,\n DisposeFunction,\n RuleInfo,\n SheetInfo,\n RootRegistry,\n StyleRule,\n KeyframesInfo,\n KeyframesResult,\n KeyframesSteps,\n KeyframesCacheEntry,\n CacheMetrics,\n RawCSSResult,\n PropertyDefinition,\n} from './types';\n\nexport { StyleInjector } from './injector';\nexport { SheetManager } from './sheet-manager';\n"],"mappings":";;;;;;;;AAoBA,SAAgB,kBACd,UACA,SACiD;AACjD,QAAO,mBAAmB,CAAC,kBAAkB,UAAU,QAAQ;;;;;AAMjE,SAAgB,OACd,OACA,SACc;AAEd,sBAAqB;AAErB,QAAO,mBAAmB,CAAC,OAAO,OAAO,QAAQ;;;;;AAMnD,SAAgB,aACd,OACA,SACoB;AACpB,QAAO,mBAAmB,CAAC,aAAa,OAAO,QAAQ;;;;;;;;;;;;;;;;;;;AAoBzD,SAAgB,aACd,KACA,SACyB;AACzB,QAAO,mBAAmB,CAAC,aAAa,KAAK,QAAQ;;;;;AAMvD,SAAgB,cAAc,SAEnB;AACT,QAAO,mBAAmB,CAAC,cAAc,QAAQ;;;;;AAMnD,SAAgB,UACd,OACA,eACiB;AACjB,QAAO,mBAAmB,CAAC,UAAU,OAAO,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkD5D,SAAgB,SAAS,MAAc,SAAiC;AACtE,QAAO,mBAAmB,CAAC,SAAS,MAAM,QAAQ;;;;;;;;AASpD,SAAgB,kBACd,MACA,SACS;AACT,QAAO,mBAAmB,CAAC,kBAAkB,MAAM,QAAQ;;;;;AAM7D,SAAgB,WAAW,SAAoD;AAC7E,QAAO,mBAAmB,CAAC,WAAW,QAAQ;;;;;;AAOhD,SAAgB,kBACd,MACA,SACQ;CAER,MAAM,2BAAW,IAAI,KAAa;CAElC,MAAM,eAAe,OAAgB;EACnC,MAAM,MAAM,GAAG,aAAa,QAAQ;AACpC,MAAI,CAAC,IAAK;AACV,OAAK,MAAM,SAAS,IAAI,MAAM,MAAM,CAClC,KAAI,SAAS,KAAK,MAAM,CAAE,UAAS,IAAI,MAAM;;AAKjD,KAAK,KAAiB,aACpB,aAAY,KAAgB;CAG9B,MAAM,WAAY,KAAoB,mBACjC,KAAoB,iBAAiB,UAAU,GAC/C,EAAE;AACP,KAAI,SAAU,UAAS,QAAQ,YAAY;AAE3C,QAAO,mBAAmB,CAAC,qBAAqB,UAAU,QAAQ;;;;;AAMpE,SAAgB,QAAQ,MAAoC;AAC1D,QAAO,mBAAmB,CAAC,QAAQ,KAAK;;;;;AAM1C,SAAgB,uBAAgC;AAC9C,QAAO,mBAAmB;;;;;AAM5B,MAAa,WAAW,EACtB,IAAI,WAAW;AACb,QAAO,mBAAmB;GAE7B;;;;AAKD,SAAgB,QAAQ,MAAoC;AAC1D,QAAO,mBAAmB,CAAC,QAAQ,KAAK;;;;;AAM1C,SAAgB,eACd,SAAuC,EAAE,EAC1B;AAUf,QAAO,IAAI,cAP6B;EACtC,GAHoB,WAAW;EAK/B,oBAAoB,OAAO,sBAAsB,mBAAmB;EACpE,GAAG;EACJ,CAEmC"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../../src/injector/index.ts"],"sourcesContent":["import {\n getConfig,\n getGlobalInjector,\n isTestEnvironment,\n markStylesGenerated,\n} from '../config';\nimport type { StyleResult } from '../pipeline';\n\nimport { StyleInjector } from './injector';\nimport type {\n CounterStyleDescriptors,\n FontFaceDescriptors,\n GCOptions,\n GlobalInjectResult,\n InjectResult,\n KeyframesResult,\n KeyframesSteps,\n StyleInjectorConfig,\n} from './types';\n\n/**\n * Inject styles and return className with dispose function\n */\nexport function inject(\n rules: StyleResult[],\n options?: { root?: Document | ShadowRoot; cacheKey?: string },\n): InjectResult {\n const injector = getGlobalInjector();\n\n markStylesGenerated();\n\n return injector.inject(rules, options);\n}\n\n/**\n * Inject global rules that should not reserve tasty class names\n */\nexport function injectGlobal(\n rules: StyleResult[],\n options?: { root?: Document | ShadowRoot },\n): GlobalInjectResult {\n return getGlobalInjector().injectGlobal(rules, options);\n}\n\n/**\n * Inject raw CSS text directly without parsing\n * This is a low-overhead method for injecting raw CSS that doesn't need tasty processing.\n * The CSS is inserted into a separate style element to avoid conflicts with tasty's chunking.\n *\n * @example\n * ```tsx\n * // Inject raw CSS\n * const { dispose } = injectRawCSS(`\n * body { margin: 0; padding: 0; }\n * .my-class { color: red; }\n * `);\n *\n * // Later, remove the injected CSS\n * dispose();\n * ```\n */\nexport function injectRawCSS(\n css: string,\n options?: { root?: Document | ShadowRoot },\n): { dispose: () => void } {\n return getGlobalInjector().injectRawCSS(css, options);\n}\n\n/**\n * Get raw CSS text for SSR\n */\nexport function getRawCSSText(options?: {\n root?: Document | ShadowRoot;\n}): string {\n return getGlobalInjector().getRawCSSText(options);\n}\n\n/**\n * Inject keyframes and return object with toString() and dispose()\n */\nexport function keyframes(\n steps: KeyframesSteps,\n nameOrOptions?: string | { root?: Document | ShadowRoot; name?: string },\n): KeyframesResult {\n return getGlobalInjector().keyframes(steps, nameOrOptions);\n}\n\nexport interface PropertyOptions {\n /**\n * CSS syntax string for the property (e.g., '<color>', '<length>', '<angle>')\n * @see https://developer.mozilla.org/en-US/docs/Web/CSS/@property/syntax\n */\n syntax?: string;\n /**\n * Whether the property inherits from parent elements\n * @default true\n */\n inherits?: boolean;\n /**\n * Initial value for the property\n */\n initialValue?: string | number;\n /**\n * Shadow root or document to inject into\n */\n root?: Document | ShadowRoot;\n}\n\n/**\n * Define a CSS @property custom property.\n * This enables advanced features like animating custom properties.\n *\n * Note: @property rules are global and persistent once defined.\n * Re-registering the same property name is a no-op.\n *\n * @param name - The custom property name (must start with --)\n * @param options - Property configuration\n *\n * @example\n * ```ts\n * // Define a color property that can be animated\n * property('--my-color', {\n * syntax: '<color>',\n * initialValue: 'red',\n * });\n *\n * // Define an angle property\n * property('--rotation', {\n * syntax: '<angle>',\n * inherits: false,\n * initialValue: '0deg',\n * });\n * ```\n */\nexport function property(name: string, options?: PropertyOptions): void {\n return getGlobalInjector().property(name, options);\n}\n\n/**\n * Check if a CSS @property has already been defined\n *\n * @param name - The custom property name to check\n * @param options - Options including root\n */\nexport function isPropertyDefined(\n name: string,\n options?: { root?: Document | ShadowRoot },\n): boolean {\n return getGlobalInjector().isPropertyDefined(name, options);\n}\n\n/**\n * Inject a CSS @font-face rule.\n *\n * Permanent and global — no dispose or ref-counting.\n * Deduplicates by content hash (family + descriptors).\n */\nexport function fontFace(\n family: string,\n descriptors: FontFaceDescriptors,\n options?: { root?: Document | ShadowRoot },\n): void {\n return getGlobalInjector().fontFace(family, descriptors, options);\n}\n\n/**\n * Inject a CSS @counter-style rule.\n *\n * Permanent and global — no dispose or ref-counting.\n * Deduplicates by name (first definition wins).\n */\nexport function counterStyle(\n name: string,\n descriptors: CounterStyleDescriptors,\n options?: { root?: Document | ShadowRoot },\n): void {\n return getGlobalInjector().counterStyle(name, descriptors, options);\n}\n\n/**\n * Get CSS text from all sheets (for SSR)\n */\nexport function getCssText(options?: { root?: Document | ShadowRoot }): string {\n return getGlobalInjector().getCssText(options);\n}\n\n/**\n * Collect only CSS used by a rendered subtree (like jest-styled-components).\n * Pass the container returned by render(...).\n */\nexport function getCssTextForNode(\n node: ParentNode | Element | DocumentFragment,\n options?: { root?: Document | ShadowRoot },\n): string {\n // Collect tasty-generated class names (t<number>) from the subtree\n const classSet = new Set<string>();\n\n const readClasses = (el: Element) => {\n const cls = el.getAttribute('class');\n if (!cls) return;\n for (const token of cls.split(/\\s+/)) {\n if (/^t\\d+$/.test(token)) classSet.add(token);\n }\n };\n\n // Include node itself if it's an Element\n if ((node as Element).getAttribute) {\n readClasses(node as Element);\n }\n // Walk descendants\n const elements = (node as ParentNode).querySelectorAll\n ? (node as ParentNode).querySelectorAll('[class]')\n : ([] as unknown as NodeListOf<Element>);\n if (elements) elements.forEach(readClasses);\n\n return getGlobalInjector().getCssTextForClasses(classSet, options);\n}\n\n/**\n * Force cleanup of unused rules\n */\nexport function cleanup(root?: Document | ShadowRoot): void {\n return getGlobalInjector().cleanup(root);\n}\n\n/**\n * Record a render-time usage hit for one or more classNames.\n * Used internally by computeStyles and tasty() to track style popularity for GC.\n */\nexport function touch(\n className: string,\n options?: { root?: Document | ShadowRoot },\n): void {\n return getGlobalInjector().touch(className, options);\n}\n\n/**\n * Synchronous garbage collection of unused styles.\n * Scans the DOM for live classNames (never evicts them), then evicts\n * absent styles whose age exceeds their popularity-weighted TTL.\n *\n * @returns Number of styles evicted.\n */\nexport function gc(options?: GCOptions): number {\n return getGlobalInjector().gc(options);\n}\n\n/**\n * Event-driven GC with cooldown.\n * Skips if called within the configured cooldown of the last run.\n * Schedules via requestIdleCallback when available.\n */\nexport function maybeGC(options?: GCOptions): void {\n return getGlobalInjector().maybeGC(options);\n}\n\n/**\n * Check if we're currently running in a test environment\n */\nexport function getIsTestEnvironment(): boolean {\n return isTestEnvironment();\n}\n\n/**\n * Get the global injector instance for debugging\n */\nexport const injector = {\n get instance() {\n return getGlobalInjector();\n },\n};\n\n/**\n * Destroy all resources and clean up\n */\nexport function destroy(root?: Document | ShadowRoot): void {\n return getGlobalInjector().destroy(root);\n}\n\n/**\n * Create a new isolated injector instance\n */\nexport function createInjector(\n config: Partial<StyleInjectorConfig> = {},\n): StyleInjector {\n const defaultConfig = getConfig();\n\n const fullConfig: StyleInjectorConfig = {\n ...defaultConfig,\n // Auto-enable forceTextInjection in test environments\n forceTextInjection: config.forceTextInjection ?? isTestEnvironment(),\n ...config,\n };\n\n return new StyleInjector(fullConfig);\n}\n\n// Re-export types\nexport type {\n StyleInjectorConfig,\n InjectResult,\n DisposeFunction,\n RuleInfo,\n SheetInfo,\n RootRegistry,\n StyleRule,\n KeyframesInfo,\n KeyframesResult,\n KeyframesSteps,\n KeyframesCacheEntry,\n CacheMetrics,\n RawCSSResult,\n PropertyDefinition,\n FontFaceDescriptors,\n FontFaceInput,\n CounterStyleDescriptors,\n StyleUsage,\n GCConfig,\n GCOptions,\n} from './types';\n\nexport { StyleInjector } from './injector';\nexport { SheetManager } from './sheet-manager';\n"],"mappings":";;;;;;;AAuBA,SAAgB,OACd,OACA,SACc;CACd,MAAM,WAAW,mBAAmB;AAEpC,sBAAqB;AAErB,QAAO,SAAS,OAAO,OAAO,QAAQ;;;;;AAMxC,SAAgB,aACd,OACA,SACoB;AACpB,QAAO,mBAAmB,CAAC,aAAa,OAAO,QAAQ;;;;;;;;;;;;;;;;;;;AAoBzD,SAAgB,aACd,KACA,SACyB;AACzB,QAAO,mBAAmB,CAAC,aAAa,KAAK,QAAQ;;;;;AAMvD,SAAgB,cAAc,SAEnB;AACT,QAAO,mBAAmB,CAAC,cAAc,QAAQ;;;;;AAMnD,SAAgB,UACd,OACA,eACiB;AACjB,QAAO,mBAAmB,CAAC,UAAU,OAAO,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkD5D,SAAgB,SAAS,MAAc,SAAiC;AACtE,QAAO,mBAAmB,CAAC,SAAS,MAAM,QAAQ;;;;;;;;AASpD,SAAgB,kBACd,MACA,SACS;AACT,QAAO,mBAAmB,CAAC,kBAAkB,MAAM,QAAQ;;;;;;;;AAS7D,SAAgB,SACd,QACA,aACA,SACM;AACN,QAAO,mBAAmB,CAAC,SAAS,QAAQ,aAAa,QAAQ;;;;;;;;AASnE,SAAgB,aACd,MACA,aACA,SACM;AACN,QAAO,mBAAmB,CAAC,aAAa,MAAM,aAAa,QAAQ;;;;;AAMrE,SAAgB,WAAW,SAAoD;AAC7E,QAAO,mBAAmB,CAAC,WAAW,QAAQ;;;;;;AAOhD,SAAgB,kBACd,MACA,SACQ;CAER,MAAM,2BAAW,IAAI,KAAa;CAElC,MAAM,eAAe,OAAgB;EACnC,MAAM,MAAM,GAAG,aAAa,QAAQ;AACpC,MAAI,CAAC,IAAK;AACV,OAAK,MAAM,SAAS,IAAI,MAAM,MAAM,CAClC,KAAI,SAAS,KAAK,MAAM,CAAE,UAAS,IAAI,MAAM;;AAKjD,KAAK,KAAiB,aACpB,aAAY,KAAgB;CAG9B,MAAM,WAAY,KAAoB,mBACjC,KAAoB,iBAAiB,UAAU,GAC/C,EAAE;AACP,KAAI,SAAU,UAAS,QAAQ,YAAY;AAE3C,QAAO,mBAAmB,CAAC,qBAAqB,UAAU,QAAQ;;;;;AAMpE,SAAgB,QAAQ,MAAoC;AAC1D,QAAO,mBAAmB,CAAC,QAAQ,KAAK;;;;;;AAO1C,SAAgB,MACd,WACA,SACM;AACN,QAAO,mBAAmB,CAAC,MAAM,WAAW,QAAQ;;;;;;;;;AAUtD,SAAgB,GAAG,SAA6B;AAC9C,QAAO,mBAAmB,CAAC,GAAG,QAAQ;;;;;;;AAQxC,SAAgB,QAAQ,SAA2B;AACjD,QAAO,mBAAmB,CAAC,QAAQ,QAAQ;;;;;AAM7C,SAAgB,uBAAgC;AAC9C,QAAO,mBAAmB;;;;;AAM5B,MAAa,WAAW,EACtB,IAAI,WAAW;AACb,QAAO,mBAAmB;GAE7B;;;;AAKD,SAAgB,QAAQ,MAAoC;AAC1D,QAAO,mBAAmB,CAAC,QAAQ,KAAK;;;;;AAM1C,SAAgB,eACd,SAAuC,EAAE,EAC1B;AAUf,QAAO,IAAI,cAP6B;EACtC,GAHoB,WAAW;EAK/B,oBAAoB,OAAO,sBAAsB,mBAAmB;EACpE,GAAG;EACJ,CAEmC"}
@@ -1,4 +1,4 @@
1
- import { CacheMetrics, GlobalInjectResult, InjectResult, KeyframesResult, KeyframesSteps, PropertyDefinition, RawCSSResult, StyleInjectorConfig } from "./types.js";
1
+ import { CacheMetrics, CounterStyleDescriptors, FontFaceDescriptors, GCOptions, GlobalInjectResult, InjectResult, KeyframesResult, KeyframesSteps, PropertyDefinition, RawCSSResult, StyleInjectorConfig } from "./types.js";
2
2
  import { StyleResult } from "../pipeline/index.js";
3
3
  import { SheetManager } from "./sheet-manager.js";
4
4
 
@@ -6,8 +6,9 @@ import { SheetManager } from "./sheet-manager.js";
6
6
  declare class StyleInjector {
7
7
  private sheetManager;
8
8
  private config;
9
- private cleanupScheduled;
10
9
  private globalRuleCounter;
10
+ private lastGCTime;
11
+ private backgroundSweepInterval;
11
12
  /** @internal — exposed for debug utilities only */
12
13
  get _sheetManager(): SheetManager;
13
14
  constructor(config?: StyleInjectorConfig);
@@ -51,7 +52,16 @@ declare class StyleInjector {
51
52
  root?: Document | ShadowRoot;
52
53
  }): string;
53
54
  /**
54
- * Dispose of a className
55
+ * Increment refCount for an already-injected cacheKey and return a dispose.
56
+ * Used by useStyles on cache hits (hydration or runtime reuse) where
57
+ * the pipeline was skipped but refCount tracking is still needed.
58
+ * Returns null if the cacheKey is not found.
59
+ */
60
+ trackRef(cacheKey: string, options?: {
61
+ root?: Document | ShadowRoot;
62
+ }): InjectResult | null;
63
+ /**
64
+ * Dispose of a className (decrements refCount only).
55
65
  */
56
66
  private dispose;
57
67
  /**
@@ -112,6 +122,24 @@ declare class StyleInjector {
112
122
  isPropertyDefined(name: string, options?: {
113
123
  root?: Document | ShadowRoot;
114
124
  }): boolean;
125
+ /**
126
+ * Inject a CSS @font-face rule.
127
+ *
128
+ * Permanent and global — no dispose or ref-counting.
129
+ * Deduplicates by content hash (family + descriptors).
130
+ */
131
+ fontFace(family: string, descriptors: FontFaceDescriptors, options?: {
132
+ root?: Document | ShadowRoot;
133
+ }): void;
134
+ /**
135
+ * Inject a CSS @counter-style rule.
136
+ *
137
+ * Permanent and global — no dispose or ref-counting.
138
+ * Deduplicates by name (first definition wins).
139
+ */
140
+ counterStyle(name: string, descriptors: CounterStyleDescriptors, options?: {
141
+ root?: Document | ShadowRoot;
142
+ }): void;
115
143
  /**
116
144
  * Inject keyframes and return object with toString() and dispose()
117
145
  *
@@ -129,6 +157,33 @@ declare class StyleInjector {
129
157
  * Dispose keyframes
130
158
  */
131
159
  private disposeKeyframes;
160
+ private static readonly TOUCH_THROTTLE_MS;
161
+ private static readonly TASTY_CLASS_RE;
162
+ /**
163
+ * Record a render-time usage hit for one or more classNames.
164
+ * Handles space-separated multi-chunk classNames.
165
+ * No-op on the server.
166
+ */
167
+ touch(className: string, options?: {
168
+ root?: Document | ShadowRoot;
169
+ }): void;
170
+ /**
171
+ * Synchronous garbage collection.
172
+ *
173
+ * 1. Scans the DOM for live tasty classNames (safety guard).
174
+ * 2. Scores each non-live className via popularity-weighted TTL.
175
+ * 3. Marks evictable styles with refCount = 0 and deletes them.
176
+ * 4. Optionally enforces a hard `cacheCapacity` cap.
177
+ *
178
+ * @returns Number of styles evicted.
179
+ */
180
+ gc(options?: GCOptions): number;
181
+ /**
182
+ * Event-driven GC with cooldown.
183
+ * Skips if called within `cooldown` ms of the last run.
184
+ * Schedules the actual GC via `requestIdleCallback` when available.
185
+ */
186
+ maybeGC(options?: GCOptions): void;
132
187
  /**
133
188
  * Destroy all resources for a root
134
189
  */
@@ -1,8 +1,9 @@
1
+ import { getEffectiveDefinition, normalizePropertyDefinition } from "../properties/index.js";
1
2
  import { isDevEnv } from "../utils/is-dev-env.js";
2
3
  import { parseStyle } from "../utils/styles.js";
3
- import { colorInitialValueToRgb, getEffectiveDefinition, normalizePropertyDefinition } from "../properties/index.js";
4
4
  import { SheetManager } from "./sheet-manager.js";
5
-
5
+ import { fontFaceContentHash, formatFontFaceDeclarations } from "../font-face/index.js";
6
+ import { formatCounterStyleDeclarations } from "../counter-style/index.js";
6
7
  //#region src/injector/injector.ts
7
8
  /**
8
9
  * Generate sequential class name with format t{number}
@@ -10,11 +11,12 @@ import { SheetManager } from "./sheet-manager.js";
10
11
  function generateClassName(counter) {
11
12
  return `t${counter}`;
12
13
  }
13
- var StyleInjector = class {
14
+ var StyleInjector = class StyleInjector {
14
15
  sheetManager;
15
16
  config;
16
- cleanupScheduled = false;
17
17
  globalRuleCounter = 0;
18
+ lastGCTime = 0;
19
+ backgroundSweepInterval = null;
18
20
  /** @internal — exposed for debug utilities only */
19
21
  get _sheetManager() {
20
22
  return this.sheetManager;
@@ -22,6 +24,16 @@ var StyleInjector = class {
22
24
  constructor(config = {}) {
23
25
  this.config = config;
24
26
  this.sheetManager = new SheetManager(config);
27
+ if (config.gc?.auto && typeof document !== "undefined") {
28
+ const interval = config.gc.autoInterval ?? 3e5;
29
+ this.backgroundSweepInterval = setInterval(() => {
30
+ const sweep = () => {
31
+ for (const root of this.sheetManager.getActiveRoots()) this.gc({ root });
32
+ };
33
+ if (typeof requestIdleCallback !== "undefined") requestIdleCallback(() => sweep());
34
+ else sweep();
35
+ }, interval);
36
+ }
25
37
  }
26
38
  /**
27
39
  * Allocate a className for a cacheKey without injecting styles yet.
@@ -92,21 +104,19 @@ var StyleInjector = class {
92
104
  rootPrefix: void 0
93
105
  };
94
106
  });
95
- const colorPropRegex = /^\s*(--[a-z0-9-]+-color)\s*:/i;
96
- for (const rule of rulesToInsert) {
97
- if (!rule.declarations) continue;
98
- const parts = rule.declarations.split(/;+\s*/);
99
- for (const part of parts) {
100
- if (!part) continue;
101
- const match = colorPropRegex.exec(part);
102
- if (match) {
103
- const propName = match[1];
104
- if (!this.isPropertyDefined(propName, { root })) this.property(propName, {
105
- syntax: "<color>",
106
- initialValue: "transparent",
107
+ if (this.config.autoPropertyTypes !== false) {
108
+ const resolver = registry.propertyTypeResolver;
109
+ const defined = registry.injectedProperties;
110
+ for (const rule of rulesToInsert) {
111
+ if (!rule.declarations) continue;
112
+ resolver.scanDeclarations(rule.declarations, (name) => defined.has(name), (name, syntax, initialValue) => {
113
+ this.property(name, {
114
+ syntax,
115
+ inherits: true,
116
+ initialValue,
107
117
  root
108
118
  });
109
- }
119
+ });
110
120
  }
111
121
  }
112
122
  const ruleInfo = this.sheetManager.insertRule(registry, rulesToInsert, className, root);
@@ -141,6 +151,21 @@ var StyleInjector = class {
141
151
  const root = options?.root || document;
142
152
  const registry = this.sheetManager.getRegistry(root);
143
153
  if (!rules || rules.length === 0) return { dispose: () => {} };
154
+ if (this.config.autoPropertyTypes !== false) {
155
+ const resolver = registry.propertyTypeResolver;
156
+ const defined = registry.injectedProperties;
157
+ for (const rule of rules) {
158
+ if (!rule.declarations) continue;
159
+ resolver.scanDeclarations(rule.declarations, (name) => defined.has(name), (name, syntax, initialValue) => {
160
+ this.property(name, {
161
+ syntax,
162
+ inherits: true,
163
+ initialValue,
164
+ root
165
+ });
166
+ });
167
+ }
168
+ }
144
169
  const key = `global:${this.globalRuleCounter++}`;
145
170
  const info = this.sheetManager.insertGlobalRule(registry, rules, key, root);
146
171
  if (registry.metrics) registry.metrics.totalInsertions++;
@@ -165,17 +190,33 @@ var StyleInjector = class {
165
190
  return this.sheetManager.getRawCSSText(root);
166
191
  }
167
192
  /**
168
- * Dispose of a className
193
+ * Increment refCount for an already-injected cacheKey and return a dispose.
194
+ * Used by useStyles on cache hits (hydration or runtime reuse) where
195
+ * the pipeline was skipped but refCount tracking is still needed.
196
+ * Returns null if the cacheKey is not found.
197
+ */
198
+ trackRef(cacheKey, options) {
199
+ const root = options?.root || document;
200
+ const registry = this.sheetManager.getRegistry(root);
201
+ if (!registry.cacheKeyToClassName.has(cacheKey)) return null;
202
+ const className = registry.cacheKeyToClassName.get(cacheKey);
203
+ const currentRefCount = registry.refCounts.get(className) || 0;
204
+ registry.refCounts.set(className, currentRefCount + 1);
205
+ if (registry.metrics) registry.metrics.hits++;
206
+ return {
207
+ className,
208
+ dispose: () => this.dispose(className, registry)
209
+ };
210
+ }
211
+ /**
212
+ * Dispose of a className (decrements refCount only).
169
213
  */
170
214
  dispose(className, registry) {
171
215
  const currentRefCount = registry.refCounts.get(className);
172
216
  if (currentRefCount == null || currentRefCount <= 0) return;
173
217
  const newRefCount = currentRefCount - 1;
174
218
  registry.refCounts.set(className, newRefCount);
175
- if (newRefCount === 0) {
176
- if (registry.metrics) registry.metrics.totalUnused++;
177
- this.sheetManager.checkCleanupNeeded(registry);
178
- }
219
+ if (newRefCount === 0 && registry.metrics) registry.metrics.totalUnused++;
179
220
  }
180
221
  /**
181
222
  * Force bulk cleanup of unused styles
@@ -262,13 +303,7 @@ var StyleInjector = class {
262
303
  const cssName = effectiveResult.cssName;
263
304
  const definition = effectiveResult.definition;
264
305
  const normalizedDef = normalizePropertyDefinition(definition);
265
- const existingDef = registry.injectedProperties.get(cssName);
266
- if (existingDef !== void 0) {
267
- if (existingDef !== normalizedDef) {
268
- if (isDevEnv()) console.warn(`[Tasty] @property ${cssName} was already defined with a different declaration. The new declaration will be ignored. Original: ${existingDef}, New: ${normalizedDef}`);
269
- }
270
- return;
271
- }
306
+ if (registry.injectedProperties.get(cssName) !== void 0) return;
272
307
  const parts = [];
273
308
  if (definition.syntax != null) {
274
309
  let syntax = String(definition.syntax).trim();
@@ -290,15 +325,6 @@ var StyleInjector = class {
290
325
  };
291
326
  if (!this.sheetManager.insertGlobalRule(registry, [rule], `property:${name}`, root)) return;
292
327
  registry.injectedProperties.set(cssName, normalizedDef);
293
- if (effectiveResult.isColor) {
294
- const rgbCssName = `${cssName}-rgb`;
295
- if (!this.isPropertyDefined(rgbCssName, { root })) this.property(rgbCssName, {
296
- syntax: "<number>+",
297
- inherits: definition.inherits,
298
- initialValue: colorInitialValueToRgb(definition.initialValue),
299
- root
300
- });
301
- }
302
328
  }
303
329
  /**
304
330
  * Check whether a given @property name was already injected by this injector.
@@ -316,6 +342,39 @@ var StyleInjector = class {
316
342
  return registry.injectedProperties.has(effectiveResult.cssName);
317
343
  }
318
344
  /**
345
+ * Inject a CSS @font-face rule.
346
+ *
347
+ * Permanent and global — no dispose or ref-counting.
348
+ * Deduplicates by content hash (family + descriptors).
349
+ */
350
+ fontFace(family, descriptors, options) {
351
+ const root = options?.root || document;
352
+ const registry = this.sheetManager.getRegistry(root);
353
+ const hash = fontFaceContentHash(family, descriptors);
354
+ if (registry.injectedFontFaces.has(hash)) return;
355
+ const rule = {
356
+ selector: "@font-face",
357
+ declarations: formatFontFaceDeclarations(family, descriptors)
358
+ };
359
+ if (this.sheetManager.insertGlobalRule(registry, [rule], `fontface:${hash}`, root)) registry.injectedFontFaces.add(hash);
360
+ }
361
+ /**
362
+ * Inject a CSS @counter-style rule.
363
+ *
364
+ * Permanent and global — no dispose or ref-counting.
365
+ * Deduplicates by name (first definition wins).
366
+ */
367
+ counterStyle(name, descriptors, options) {
368
+ const root = options?.root || document;
369
+ const registry = this.sheetManager.getRegistry(root);
370
+ if (registry.injectedCounterStyles.has(name)) return;
371
+ const rule = {
372
+ selector: `@counter-style ${name}`,
373
+ declarations: formatCounterStyleDeclarations(descriptors)
374
+ };
375
+ if (this.sheetManager.insertGlobalRule(registry, [rule], `counterstyle:${name}`, root)) registry.injectedCounterStyles.add(name);
376
+ }
377
+ /**
319
378
  * Inject keyframes and return object with toString() and dispose()
320
379
  *
321
380
  * Keyframes are cached by content (steps). If the same content is injected
@@ -351,11 +410,20 @@ var StyleInjector = class {
351
410
  registry.keyframesNameToContent.set(providedName, contentHash);
352
411
  }
353
412
  } else actualName = `k${registry.keyframesCounter++}`;
354
- const info = this.sheetManager.insertKeyframes(registry, steps, actualName, root);
355
- if (!info) return {
413
+ const result = this.sheetManager.insertKeyframes(registry, steps, actualName, root);
414
+ if (!result) return {
356
415
  toString: () => "",
357
416
  dispose: () => {}
358
417
  };
418
+ const { info, declarations } = result;
419
+ if (this.config.autoPropertyTypes !== false && declarations) registry.propertyTypeResolver.scanDeclarations(declarations, (name) => registry.injectedProperties.has(name), (name, syntax, initialValue) => {
420
+ this.property(name, {
421
+ syntax,
422
+ inherits: true,
423
+ initialValue,
424
+ root
425
+ });
426
+ });
359
427
  registry.keyframesCache.set(contentHash, {
360
428
  name: actualName,
361
429
  refCount: 1,
@@ -390,15 +458,110 @@ var StyleInjector = class {
390
458
  }
391
459
  }
392
460
  }
461
+ static TOUCH_THROTTLE_MS = 5e3;
462
+ static TASTY_CLASS_RE = /^t\d+$/;
463
+ /**
464
+ * Record a render-time usage hit for one or more classNames.
465
+ * Handles space-separated multi-chunk classNames.
466
+ * No-op on the server.
467
+ */
468
+ touch(className, options) {
469
+ if (typeof document === "undefined") return;
470
+ if (!this.config.gc) return;
471
+ const root = options?.root || document;
472
+ const registry = this.sheetManager.getRegistry(root);
473
+ const now = Date.now();
474
+ const parts = className.indexOf(" ") === -1 ? [className] : className.split(" ");
475
+ for (const cls of parts) {
476
+ if (!StyleInjector.TASTY_CLASS_RE.test(cls)) continue;
477
+ if (!registry.rules.has(cls)) continue;
478
+ const entry = registry.usageMap.get(cls);
479
+ if (entry) {
480
+ entry.hitCount++;
481
+ if (now - entry.lastUsedAt > StyleInjector.TOUCH_THROTTLE_MS) entry.lastUsedAt = now;
482
+ } else registry.usageMap.set(cls, {
483
+ hitCount: 1,
484
+ lastUsedAt: now
485
+ });
486
+ }
487
+ }
488
+ /**
489
+ * Synchronous garbage collection.
490
+ *
491
+ * 1. Scans the DOM for live tasty classNames (safety guard).
492
+ * 2. Scores each non-live className via popularity-weighted TTL.
493
+ * 3. Marks evictable styles with refCount = 0 and deletes them.
494
+ * 4. Optionally enforces a hard `cacheCapacity` cap.
495
+ *
496
+ * @returns Number of styles evicted.
497
+ */
498
+ gc(options) {
499
+ if (typeof document === "undefined") return 0;
500
+ const root = options?.root || document;
501
+ const registry = this.sheetManager.getRegistry(root);
502
+ const baseMaxAge = options?.baseMaxAge ?? this.config.gc?.baseMaxAge ?? 6e4;
503
+ const cacheCapacity = options?.cacheCapacity ?? this.config.gc?.cacheCapacity;
504
+ const now = Date.now();
505
+ const liveClasses = /* @__PURE__ */ new Set();
506
+ for (const el of root.querySelectorAll("[class]")) for (const token of el.classList) if (StyleInjector.TASTY_CLASS_RE.test(token)) liveClasses.add(token);
507
+ let swept = 0;
508
+ for (const [className, usage] of registry.usageMap) {
509
+ if (liveClasses.has(className)) continue;
510
+ if ((registry.refCounts.get(className) ?? 0) > 0) continue;
511
+ if (now - usage.lastUsedAt > baseMaxAge * Math.log2(usage.hitCount + 1)) {
512
+ registry.usageMap.delete(className);
513
+ swept++;
514
+ }
515
+ }
516
+ if (cacheCapacity && registry.usageMap.size > cacheCapacity) {
517
+ const scored = [];
518
+ for (const [className, usage] of registry.usageMap) {
519
+ if (liveClasses.has(className)) continue;
520
+ if ((registry.refCounts.get(className) ?? 0) > 0) continue;
521
+ const age = now - usage.lastUsedAt;
522
+ scored.push({
523
+ className,
524
+ score: usage.hitCount * Math.exp(-age / baseMaxAge)
525
+ });
526
+ }
527
+ scored.sort((a, b) => a.score - b.score);
528
+ const toEvict = registry.usageMap.size - cacheCapacity;
529
+ for (let i = 0; i < Math.min(toEvict, scored.length); i++) {
530
+ const { className } = scored[i];
531
+ registry.usageMap.delete(className);
532
+ swept++;
533
+ }
534
+ }
535
+ if (swept > 0) this.sheetManager.forceCleanup(registry);
536
+ return swept;
537
+ }
538
+ /**
539
+ * Event-driven GC with cooldown.
540
+ * Skips if called within `cooldown` ms of the last run.
541
+ * Schedules the actual GC via `requestIdleCallback` when available.
542
+ */
543
+ maybeGC(options) {
544
+ if (typeof document === "undefined") return;
545
+ const cooldown = this.config.gc?.cooldown ?? 3e4;
546
+ const now = Date.now();
547
+ if (now - this.lastGCTime < cooldown) return;
548
+ this.lastGCTime = now;
549
+ if (typeof requestIdleCallback !== "undefined") requestIdleCallback(() => this.gc(options));
550
+ else this.gc(options);
551
+ }
393
552
  /**
394
553
  * Destroy all resources for a root
395
554
  */
396
555
  destroy(root) {
556
+ if (this.backgroundSweepInterval) {
557
+ clearInterval(this.backgroundSweepInterval);
558
+ this.backgroundSweepInterval = null;
559
+ }
397
560
  const targetRoot = root || document;
398
561
  this.sheetManager.cleanup(targetRoot);
399
562
  }
400
563
  };
401
-
402
564
  //#endregion
403
565
  export { StyleInjector };
566
+
404
567
  //# sourceMappingURL=injector.js.map