@elastic/distillate 0.0.0 → 0.1.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.
Files changed (285) hide show
  1. package/LICENSE.txt +93 -0
  2. package/NOTICE.txt +30 -0
  3. package/README.md +142 -0
  4. package/THIRD_PARTY_LICENSES.md +768 -0
  5. package/dist/cjs/collector.js +420 -0
  6. package/dist/cjs/collector.js.map +1 -0
  7. package/dist/cjs/css_scan.js +186 -0
  8. package/dist/cjs/css_scan.js.map +1 -0
  9. package/dist/cjs/dom_sink.js +40 -0
  10. package/dist/cjs/dom_sink.js.map +1 -0
  11. package/dist/cjs/emotion.js +189 -0
  12. package/dist/cjs/emotion.js.map +1 -0
  13. package/dist/cjs/engine.js +79 -0
  14. package/dist/cjs/engine.js.map +1 -0
  15. package/dist/cjs/environment.js +9 -0
  16. package/dist/cjs/environment.js.map +1 -0
  17. package/dist/cjs/hash.js +86 -0
  18. package/dist/cjs/hash.js.map +1 -0
  19. package/dist/cjs/idents.js +23 -0
  20. package/dist/cjs/idents.js.map +1 -0
  21. package/dist/cjs/index.js +42 -0
  22. package/dist/cjs/index.js.map +1 -0
  23. package/dist/cjs/instance.js +34 -0
  24. package/dist/cjs/instance.js.map +1 -0
  25. package/dist/cjs/local_vars.js +100 -0
  26. package/dist/cjs/local_vars.js.map +1 -0
  27. package/dist/cjs/minify.js +77 -0
  28. package/dist/cjs/minify.js.map +1 -0
  29. package/dist/cjs/names.js +77 -0
  30. package/dist/cjs/names.js.map +1 -0
  31. package/dist/cjs/nesting.js +406 -0
  32. package/dist/cjs/nesting.js.map +1 -0
  33. package/dist/cjs/package.json +3 -0
  34. package/dist/cjs/runtime.js +257 -0
  35. package/dist/cjs/runtime.js.map +1 -0
  36. package/dist/cjs/styles/authoring.js +169 -0
  37. package/dist/cjs/styles/authoring.js.map +1 -0
  38. package/dist/cjs/styles/declarations.js +139 -0
  39. package/dist/cjs/styles/declarations.js.map +1 -0
  40. package/dist/cjs/styles/index.js +32 -0
  41. package/dist/cjs/styles/index.js.map +1 -0
  42. package/dist/cjs/styles/module.js +233 -0
  43. package/dist/cjs/styles/module.js.map +1 -0
  44. package/dist/cjs/styles/registry.js +187 -0
  45. package/dist/cjs/styles/registry.js.map +1 -0
  46. package/dist/cjs/styles/types.js +9 -0
  47. package/dist/cjs/styles/types.js.map +1 -0
  48. package/dist/cjs/testing.js +37 -0
  49. package/dist/cjs/testing.js.map +1 -0
  50. package/dist/cjs/theme.js +249 -0
  51. package/dist/cjs/theme.js.map +1 -0
  52. package/dist/cjs/tokens/contextual_var.js +39 -0
  53. package/dist/cjs/tokens/contextual_var.js.map +1 -0
  54. package/dist/cjs/tokens/index.js +24 -0
  55. package/dist/cjs/tokens/index.js.map +1 -0
  56. package/dist/cjs/tokens/kind.js +14 -0
  57. package/dist/cjs/tokens/kind.js.map +1 -0
  58. package/dist/cjs/tokens/light_dark.js +42 -0
  59. package/dist/cjs/tokens/light_dark.js.map +1 -0
  60. package/dist/cjs/tokens/scale_token.js +28 -0
  61. package/dist/cjs/tokens/scale_token.js.map +1 -0
  62. package/dist/cjs/tokens/theme_token.js +28 -0
  63. package/dist/cjs/tokens/theme_token.js.map +1 -0
  64. package/dist/cjs/var_invariant.js +115 -0
  65. package/dist/cjs/var_invariant.js.map +1 -0
  66. package/dist/collector.d.ts +84 -0
  67. package/dist/collector.d.ts.map +1 -0
  68. package/dist/collector.js +416 -0
  69. package/dist/collector.js.map +1 -0
  70. package/dist/css_scan.d.ts +57 -0
  71. package/dist/css_scan.d.ts.map +1 -0
  72. package/dist/css_scan.js +180 -0
  73. package/dist/css_scan.js.map +1 -0
  74. package/dist/dom_sink.d.ts +27 -0
  75. package/dist/dom_sink.d.ts.map +1 -0
  76. package/dist/dom_sink.js +36 -0
  77. package/dist/dom_sink.js.map +1 -0
  78. package/dist/emotion.d.ts +47 -0
  79. package/dist/emotion.d.ts.map +1 -0
  80. package/dist/emotion.js +184 -0
  81. package/dist/emotion.js.map +1 -0
  82. package/dist/engine.d.ts +41 -0
  83. package/dist/engine.d.ts.map +1 -0
  84. package/dist/engine.js +75 -0
  85. package/dist/engine.js.map +1 -0
  86. package/dist/environment.d.ts +43 -0
  87. package/dist/environment.d.ts.map +1 -0
  88. package/dist/environment.js +8 -0
  89. package/dist/environment.js.map +1 -0
  90. package/dist/hash.d.ts +2 -0
  91. package/dist/hash.d.ts.map +1 -0
  92. package/dist/hash.js +82 -0
  93. package/dist/hash.js.map +1 -0
  94. package/dist/idents.d.ts +9 -0
  95. package/dist/idents.d.ts.map +1 -0
  96. package/dist/idents.js +19 -0
  97. package/dist/idents.js.map +1 -0
  98. package/dist/index.d.ts +9 -0
  99. package/dist/index.d.ts.map +1 -0
  100. package/dist/index.js +14 -0
  101. package/dist/index.js.map +1 -0
  102. package/dist/instance.d.ts +16 -0
  103. package/dist/instance.d.ts.map +1 -0
  104. package/dist/instance.js +30 -0
  105. package/dist/instance.js.map +1 -0
  106. package/dist/local_vars.d.ts +75 -0
  107. package/dist/local_vars.d.ts.map +1 -0
  108. package/dist/local_vars.js +92 -0
  109. package/dist/local_vars.js.map +1 -0
  110. package/dist/minify.d.ts +11 -0
  111. package/dist/minify.d.ts.map +1 -0
  112. package/dist/minify.js +73 -0
  113. package/dist/minify.js.map +1 -0
  114. package/dist/names.d.ts +39 -0
  115. package/dist/names.d.ts.map +1 -0
  116. package/dist/names.js +70 -0
  117. package/dist/names.js.map +1 -0
  118. package/dist/nesting.d.ts +59 -0
  119. package/dist/nesting.d.ts.map +1 -0
  120. package/dist/nesting.js +400 -0
  121. package/dist/nesting.js.map +1 -0
  122. package/dist/runtime.d.ts +36 -0
  123. package/dist/runtime.d.ts.map +1 -0
  124. package/dist/runtime.js +252 -0
  125. package/dist/runtime.js.map +1 -0
  126. package/dist/styles/authoring.d.ts +59 -0
  127. package/dist/styles/authoring.d.ts.map +1 -0
  128. package/dist/styles/authoring.js +155 -0
  129. package/dist/styles/authoring.js.map +1 -0
  130. package/dist/styles/declarations.d.ts +10 -0
  131. package/dist/styles/declarations.d.ts.map +1 -0
  132. package/dist/styles/declarations.js +132 -0
  133. package/dist/styles/declarations.js.map +1 -0
  134. package/dist/styles/index.d.ts +7 -0
  135. package/dist/styles/index.d.ts.map +1 -0
  136. package/dist/styles/index.js +12 -0
  137. package/dist/styles/index.js.map +1 -0
  138. package/dist/styles/module.d.ts +5 -0
  139. package/dist/styles/module.d.ts.map +1 -0
  140. package/dist/styles/module.js +229 -0
  141. package/dist/styles/module.js.map +1 -0
  142. package/dist/styles/registry.d.ts +48 -0
  143. package/dist/styles/registry.d.ts.map +1 -0
  144. package/dist/styles/registry.js +182 -0
  145. package/dist/styles/registry.js.map +1 -0
  146. package/dist/styles/types.d.ts +211 -0
  147. package/dist/styles/types.d.ts.map +1 -0
  148. package/dist/styles/types.js +8 -0
  149. package/dist/styles/types.js.map +1 -0
  150. package/dist/testing.d.ts +10 -0
  151. package/dist/testing.d.ts.map +1 -0
  152. package/dist/testing.js +31 -0
  153. package/dist/testing.js.map +1 -0
  154. package/dist/theme.d.ts +85 -0
  155. package/dist/theme.d.ts.map +1 -0
  156. package/dist/theme.js +240 -0
  157. package/dist/theme.js.map +1 -0
  158. package/dist/tokens/contextual_var.d.ts +35 -0
  159. package/dist/tokens/contextual_var.d.ts.map +1 -0
  160. package/dist/tokens/contextual_var.js +33 -0
  161. package/dist/tokens/contextual_var.js.map +1 -0
  162. package/dist/tokens/index.d.ts +5 -0
  163. package/dist/tokens/index.d.ts.map +1 -0
  164. package/dist/tokens/index.js +11 -0
  165. package/dist/tokens/index.js.map +1 -0
  166. package/dist/tokens/kind.d.ts +2 -0
  167. package/dist/tokens/kind.d.ts.map +1 -0
  168. package/dist/tokens/kind.js +10 -0
  169. package/dist/tokens/kind.js.map +1 -0
  170. package/dist/tokens/light_dark.d.ts +20 -0
  171. package/dist/tokens/light_dark.d.ts.map +1 -0
  172. package/dist/tokens/light_dark.js +37 -0
  173. package/dist/tokens/light_dark.js.map +1 -0
  174. package/dist/tokens/scale_token.d.ts +20 -0
  175. package/dist/tokens/scale_token.d.ts.map +1 -0
  176. package/dist/tokens/scale_token.js +23 -0
  177. package/dist/tokens/scale_token.js.map +1 -0
  178. package/dist/tokens/theme_token.d.ts +20 -0
  179. package/dist/tokens/theme_token.d.ts.map +1 -0
  180. package/dist/tokens/theme_token.js +23 -0
  181. package/dist/tokens/theme_token.js.map +1 -0
  182. package/dist/var_invariant.d.ts +25 -0
  183. package/dist/var_invariant.d.ts.map +1 -0
  184. package/dist/var_invariant.js +108 -0
  185. package/dist/var_invariant.js.map +1 -0
  186. package/docs/concepts/authoring.md +115 -0
  187. package/docs/concepts/collection.md +66 -0
  188. package/docs/concepts/distillery.md +47 -0
  189. package/docs/concepts/index.md +18 -0
  190. package/docs/concepts/naming-and-output.md +123 -0
  191. package/docs/concepts/ordering.md +61 -0
  192. package/docs/concepts/single-copy.md +103 -0
  193. package/docs/concepts/tokens.md +99 -0
  194. package/docs/docset.yml +53 -0
  195. package/docs/examples/01-stylesheet.ts +20 -0
  196. package/docs/examples/02-artifact.ts +26 -0
  197. package/docs/examples/03-variants.ts +40 -0
  198. package/docs/examples/04-emotion.ts +33 -0
  199. package/docs/examples/05-var-invariant.ts +31 -0
  200. package/docs/examples/06-payload.ts +85 -0
  201. package/docs/examples/fixture.ts +22 -0
  202. package/docs/getting-started/index.md +14 -0
  203. package/docs/getting-started/installation.md +30 -0
  204. package/docs/getting-started/quick-start.md +61 -0
  205. package/docs/getting-started/your-first-stylesheet.md +57 -0
  206. package/docs/guides/compact-artifacts.md +87 -0
  207. package/docs/guides/emotion-migration.md +60 -0
  208. package/docs/guides/index.md +17 -0
  209. package/docs/guides/name-collisions.md +37 -0
  210. package/docs/guides/non-css-surfaces.md +73 -0
  211. package/docs/guides/react-renderer.md +158 -0
  212. package/docs/guides/theming.md +107 -0
  213. package/docs/guides/var-invariant.md +24 -0
  214. package/docs/guides/vs-emotion.md +61 -0
  215. package/docs/index.md +73 -0
  216. package/docs/logo.svg +7 -0
  217. package/docs/playground/index.html +12 -0
  218. package/docs/playground/package.json +23 -0
  219. package/docs/playground/src/app/app.tsx +109 -0
  220. package/docs/playground/src/app/css_panel.tsx +119 -0
  221. package/docs/playground/src/app/editor_panel.tsx +87 -0
  222. package/docs/playground/src/app/index.ts +8 -0
  223. package/docs/playground/src/app/monaco_css_editor.tsx +84 -0
  224. package/docs/playground/src/app/monaco_source_editor.tsx +132 -0
  225. package/docs/playground/src/app/preview_panel.tsx +99 -0
  226. package/docs/playground/src/app/tokens_panel.tsx +117 -0
  227. package/docs/playground/src/app/types.ts +10 -0
  228. package/docs/playground/src/examples/snippets.test.ts +28 -0
  229. package/docs/playground/src/examples/snippets.ts +418 -0
  230. package/docs/playground/src/lib/compile.test.ts +157 -0
  231. package/docs/playground/src/lib/compile.ts +242 -0
  232. package/docs/playground/src/lib/demo_environment.test.ts +123 -0
  233. package/docs/playground/src/lib/demo_environment.ts +140 -0
  234. package/docs/playground/src/lib/editor_lib.test.ts +39 -0
  235. package/docs/playground/src/lib/editor_lib.ts +142 -0
  236. package/docs/playground/src/lib/format_css.test.ts +41 -0
  237. package/docs/playground/src/lib/format_css.ts +71 -0
  238. package/docs/playground/src/main.tsx +25 -0
  239. package/docs/playground/src/setup/chrome.test.ts +47 -0
  240. package/docs/playground/src/setup/chrome.ts +365 -0
  241. package/docs/playground/src/setup/monaco_env.ts +64 -0
  242. package/docs/playground/tsconfig.json +22 -0
  243. package/docs/playground/vite.config.ts +37 -0
  244. package/docs/playground/vitest.config.ts +35 -0
  245. package/docs/playground.md +28 -0
  246. package/docs/reference/api/emotion.md +372 -0
  247. package/docs/reference/api/index-1.md +2180 -0
  248. package/docs/reference/api/index.md +9 -0
  249. package/docs/reference/api/testing.md +92 -0
  250. package/docs/reference/api.md +12 -0
  251. package/docs/reference/entry-points.md +125 -0
  252. package/docs/reference/environment.md +60 -0
  253. package/docs/reference/index.md +11 -0
  254. package/docs/reference/okf-map.md +92 -0
  255. package/package.json +127 -4
  256. package/src/collector.ts +526 -0
  257. package/src/css_scan.ts +263 -0
  258. package/src/dom_sink.ts +65 -0
  259. package/src/emotion.ts +311 -0
  260. package/src/engine.ts +191 -0
  261. package/src/environment.ts +56 -0
  262. package/src/hash.ts +111 -0
  263. package/src/idents.ts +21 -0
  264. package/src/index.ts +93 -0
  265. package/src/instance.ts +49 -0
  266. package/src/local_vars.ts +220 -0
  267. package/src/minify.ts +87 -0
  268. package/src/names.ts +129 -0
  269. package/src/nesting.ts +596 -0
  270. package/src/runtime.ts +428 -0
  271. package/src/styles/authoring.ts +244 -0
  272. package/src/styles/declarations.ts +172 -0
  273. package/src/styles/index.ts +58 -0
  274. package/src/styles/module.ts +396 -0
  275. package/src/styles/registry.ts +261 -0
  276. package/src/styles/types.ts +283 -0
  277. package/src/testing.ts +48 -0
  278. package/src/theme.ts +454 -0
  279. package/src/tokens/contextual_var.ts +72 -0
  280. package/src/tokens/index.ts +17 -0
  281. package/src/tokens/kind.ts +13 -0
  282. package/src/tokens/light_dark.ts +58 -0
  283. package/src/tokens/scale_token.ts +41 -0
  284. package/src/tokens/theme_token.ts +42 -0
  285. package/src/var_invariant.ts +147 -0
@@ -0,0 +1,526 @@
1
+ /*
2
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3
+ * or more contributor license agreements. Licensed under the Elastic License
4
+ * 2.0; you may not use this file except in compliance with the Elastic License
5
+ * 2.0.
6
+ */
7
+
8
+ import { isLocalVarOverrideMarker } from './local_vars';
9
+ import {
10
+ createStyleNameResolver,
11
+ type StyleNameMode,
12
+ type StyleNameResolver,
13
+ type StyleTarget,
14
+ } from './names';
15
+ import type {
16
+ ContextualVarPath,
17
+ CssVarPath,
18
+ Declarations,
19
+ DefaultGroupDeps,
20
+ StyleEntry,
21
+ StyleHandle,
22
+ StyleMedia,
23
+ StyleRegistry,
24
+ StyleRule,
25
+ StylesModule,
26
+ } from './styles';
27
+ import { isEmptyDeclarations } from './styles/declarations';
28
+
29
+ const warnedHandles = new WeakSet<StyleHandle>();
30
+
31
+ const dependentEntriesCache = new WeakMap<
32
+ StylesModule,
33
+ Map<string, StyleEntry[]>
34
+ >();
35
+
36
+ const dependentEntries = (
37
+ module: StylesModule,
38
+ handleKey: string
39
+ ): readonly StyleEntry[] => {
40
+ let cached = dependentEntriesCache.get(module);
41
+ if (!cached) {
42
+ cached = new Map<string, StyleEntry[]>();
43
+ for (const entry of module.entries) {
44
+ if (entry.kind === 'rule') {
45
+ if (!entry.auto) {
46
+ continue;
47
+ }
48
+ for (const dep of entry.dependsOn) {
49
+ appendDependent(cached, dep, entry);
50
+ }
51
+ continue;
52
+ }
53
+ if (entry.kind === 'media') {
54
+ const deps = new Set<string>();
55
+ for (const nested of entry.rules) {
56
+ if (!nested.auto) {
57
+ continue;
58
+ }
59
+ nested.dependsOn.forEach((key) => deps.add(key));
60
+ }
61
+ for (const dep of deps) {
62
+ appendDependent(cached, dep, entry);
63
+ }
64
+ }
65
+ }
66
+ dependentEntriesCache.set(module, cached);
67
+ }
68
+ return cached.get(handleKey) ?? [];
69
+ };
70
+
71
+ const appendDependent = (
72
+ cache: Map<string, StyleEntry[]>,
73
+ key: string,
74
+ entry: StyleEntry
75
+ ): void => {
76
+ const list = cache.get(key);
77
+ if (list) {
78
+ list.push(entry);
79
+ return;
80
+ }
81
+ cache.set(key, [entry]);
82
+ };
83
+
84
+ /** Target, naming mode, registry, and prefix for one {@link StylesCollector}. */
85
+ export interface StylesCollectorOptions {
86
+ /** Target context (e.g. `artifact` or `stylesheet`). */
87
+ target: StyleTarget;
88
+ /** Desired name mode (`compact` or `readable`). */
89
+ names: StyleNameMode;
90
+ /** Registry modules were authored against; used for auto-collection and compact-name snapshots. */
91
+ registry: StyleRegistry;
92
+ /** Brand prefix forwarded to the name resolver. */
93
+ prefix: string;
94
+ /** When `true`, emit no-op-handle warnings through `warn`. Inherited from `createDistillery({ dev })`. */
95
+ dev?: boolean;
96
+ /** Warning sink for no-op handles. Used only when `dev` is `true`. Defaults to `console.warn`. */
97
+ warn?: (message: string) => void;
98
+ }
99
+
100
+ /** Reachable entries for one render or one stylesheet. */
101
+ export class StylesCollector {
102
+ /** The target collection mode. */
103
+ readonly target: StyleTarget;
104
+ /** The naming mode (compact vs readable). */
105
+ readonly names: StyleNameMode;
106
+ /** Associated registry. */
107
+ readonly registry: StyleRegistry;
108
+ /** Emitted class name prefix. */
109
+ readonly prefix: string;
110
+ private readonly dev: boolean;
111
+ private readonly warn: (message: string) => void;
112
+ private readonly entries = new Map<string, StyleEntry>();
113
+ private readonly handles = new Map<string, StyleHandle>();
114
+ private readonly themeDeps = new Set<string>();
115
+ private readonly varDeps = new Set<ContextualVarPath>();
116
+
117
+ constructor({
118
+ target,
119
+ names,
120
+ registry,
121
+ prefix,
122
+ dev = false,
123
+ warn = (message) => console.warn(message),
124
+ }: StylesCollectorOptions) {
125
+ this.target = target;
126
+ this.names = names;
127
+ this.registry = registry;
128
+ this.prefix = prefix;
129
+ this.dev = dev;
130
+ this.warn = warn;
131
+ }
132
+
133
+ /**
134
+ * Collects a module's non-variant entries, or one explicit entry.
135
+ *
136
+ * Variant entries stay off this path; collect them via `useHandles`.
137
+ *
138
+ * @param value Module (all non-variant entries), a single entry, or an array of entries.
139
+ */
140
+ use(value: StylesModule | StyleEntry | readonly StyleEntry[]): void {
141
+ if (isReadonlyArray(value)) {
142
+ value.forEach((entry) => this.use(entry));
143
+ return;
144
+ }
145
+
146
+ if (isStylesModule(value)) {
147
+ // Aggregate deps from non-variant entries only. The module-level
148
+ // `themeDeps`/`varDeps` snapshot would also include variant entries'
149
+ // deps, which would push their var paths into the resolver's compact
150
+ // registry and steal short names from actually-rendered handles.
151
+ for (const entry of value.entries) {
152
+ if (entry.variant) {
153
+ continue;
154
+ }
155
+ if (entry.kind === 'handle' && !this.retainHandle(entry)) {
156
+ continue;
157
+ }
158
+ this.entries.set(entryKey(entry), entry);
159
+ if (entry.kind === 'handle' || entry.kind === 'rule') {
160
+ this.addDeclarationDeps(entry.declarations);
161
+ } else {
162
+ for (const inner of entry.rules) {
163
+ this.addDeclarationDeps(inner.declarations);
164
+ }
165
+ }
166
+ }
167
+ for (const handle of value.handleList) {
168
+ if (handle.variant) {
169
+ continue;
170
+ }
171
+ if (!this.retainHandle(handle)) {
172
+ continue;
173
+ }
174
+ this.handles.set(handle.key, handle);
175
+ }
176
+ return;
177
+ }
178
+
179
+ this.addEntry(value);
180
+ }
181
+
182
+ /**
183
+ * Collects every entry on `module`, including variants. Used by the stylesheet target.
184
+ *
185
+ * @param module Module whose entries should all be collected.
186
+ */
187
+ useAllEntries(module: StylesModule): void {
188
+ for (const entry of module.entries) {
189
+ if (entry.kind === 'handle' && !this.retainHandle(entry)) {
190
+ continue;
191
+ }
192
+ this.entries.set(entryKey(entry), entry);
193
+ }
194
+ for (const handle of module.handleList) {
195
+ if (!this.retainHandle(handle)) {
196
+ continue;
197
+ }
198
+ this.handles.set(handle.key, handle);
199
+ }
200
+ module.themeDeps.forEach((path) => this.themeDeps.add(path));
201
+ module.varDeps.forEach((path) => this.varDeps.add(path));
202
+ }
203
+
204
+ /**
205
+ * Collects handles and auto-includes rules whose selector deps are all present.
206
+ *
207
+ * Media blocks include only inner rules whose deps are met.
208
+ *
209
+ * @param handles Handles resolved during this render pass (e.g. from `resolveClassName`).
210
+ * @returns Handles that were retained. Empty untargeted handles are omitted in compact mode.
211
+ */
212
+ useHandles(handles: readonly StyleHandle[]): readonly StyleHandle[] {
213
+ const retained: StyleHandle[] = [];
214
+ for (const handle of handles) {
215
+ if (!this.retainHandle(handle)) {
216
+ continue;
217
+ }
218
+ retained.push(handle);
219
+ if (this.handles.has(handle.key)) {
220
+ continue;
221
+ }
222
+ this.entries.set(entryKey(handle), handle);
223
+ this.handles.set(handle.key, handle);
224
+ this.addDeclarationDeps(handle.declarations);
225
+ }
226
+ for (const handle of retained) {
227
+ const module = this.registry.module(handle.moduleName);
228
+ if (!module) {
229
+ continue;
230
+ }
231
+ for (const dependent of dependentEntries(module, handle.key)) {
232
+ this.tryActivateDependent(dependent);
233
+ }
234
+ }
235
+ return retained;
236
+ }
237
+
238
+ /** Collects `auto` rules on `module` whose every selector dependency is already collected. */
239
+ useRulesWhenDepsMet(module: StylesModule): void {
240
+ for (const entry of module.entries) {
241
+ if (entry.kind !== 'rule') {
242
+ continue;
243
+ }
244
+ if (this.entries.has(entryKey(entry))) {
245
+ continue;
246
+ }
247
+ if (this.allDepsMet(entry.dependsOn)) {
248
+ this.addEntry(entry);
249
+ }
250
+ }
251
+ }
252
+
253
+ private tryActivateDependent(entry: StyleEntry): void {
254
+ if (entry.kind === 'rule') {
255
+ if (!entry.auto) {
256
+ return;
257
+ }
258
+ if (this.entries.has(entryKey(entry))) {
259
+ return;
260
+ }
261
+ if (!this.allDepsMet(entry.dependsOn)) {
262
+ return;
263
+ }
264
+ this.addEntry(entry);
265
+ return;
266
+ }
267
+ if (entry.kind === 'media') {
268
+ const liveRules = entry.rules.filter(
269
+ (rule) => rule.auto && this.allDepsMet(rule.dependsOn)
270
+ );
271
+ if (liveRules.length === 0) {
272
+ return;
273
+ }
274
+ const filtered: StyleMedia =
275
+ liveRules.length === entry.rules.length
276
+ ? entry
277
+ : { ...entry, rules: liveRules };
278
+ this.entries.set(entryKey(filtered), filtered);
279
+ for (const rule of liveRules) {
280
+ this.addDeclarationDeps(rule.declarations);
281
+ }
282
+ }
283
+ }
284
+
285
+ private allDepsMet(dependsOn: ReadonlySet<string>): boolean {
286
+ for (const dep of dependsOn) {
287
+ if (!this.handles.has(dep)) {
288
+ return false;
289
+ }
290
+ }
291
+ return true;
292
+ }
293
+
294
+ /** Marks a theme token path as reachable so it appears in the emitted theme block, even if no collected declaration reads it. */
295
+ useThemeVar(path: string): void {
296
+ this.themeDeps.add(path);
297
+ }
298
+
299
+ /** Creates a {@link StyleNameResolver} scoped to this collector's current dep set. */
300
+ createResolver(): StyleNameResolver {
301
+ const finalized = this.finalizeDeps();
302
+ const snapshot = this.registry.freeze({
303
+ classKeys:
304
+ this.names === 'compact'
305
+ ? [...this.handles.keys()]
306
+ : this.registry.classKeys,
307
+ cssVarKeys: cssVarKeysFrom(finalized),
308
+ });
309
+ return createStyleNameResolver({
310
+ names: this.names,
311
+ prefix: this.prefix,
312
+ classKeys: snapshot.classKeys,
313
+ cssVarKeys: snapshot.cssVarKeys,
314
+ });
315
+ }
316
+
317
+ /** Collected style entries sorted for emission (base rules before all `@media`/`@container` blocks). */
318
+ get collectedEntries(): readonly StyleEntry[] {
319
+ return [...this.entries.values()].sort(compareEntriesForEmission);
320
+ }
321
+
322
+ /** Theme token paths that survive reachability; emitted in the theme scope block. */
323
+ get collectedThemeDeps(): ReadonlySet<string> {
324
+ return this.finalizeDeps().theme;
325
+ }
326
+
327
+ /** Contextual var paths that survive reachability; used by the runtime for compact-name substitution. */
328
+ get collectedVarDeps(): ReadonlySet<ContextualVarPath> {
329
+ return this.finalizeDeps().vars;
330
+ }
331
+
332
+ // Reachability-aware default-emission set used by the runtime. For a
333
+ // given handle and `${group}` site, returns the keys whose declarations
334
+ // should ship: union of group-keys appearing in `handle.declarations.deps.refs`
335
+ // and in `deps.refs` of every collected rule/media-inner-rule whose
336
+ // selector targets `handle`.
337
+ /**
338
+ * Returns the variable keys within `groupPath` that are reachable from `handle` or from any collected rule targeting it.
339
+ *
340
+ * @param handle Handle whose declaration refs and dependent rules are consulted.
341
+ * @param groupPath Group prefix path (e.g. `vars/chip/look`).
342
+ */
343
+ reachableDefaults(
344
+ handle: StyleHandle,
345
+ groupPath: `vars/${string}`
346
+ ): ReadonlySet<string> {
347
+ const groupPrefix = `${groupPath}/`;
348
+ const reachable = new Set<string>();
349
+ const harvest = (refs: ReadonlySet<ContextualVarPath>): void => {
350
+ for (const path of refs) {
351
+ if (path.startsWith(groupPrefix)) {
352
+ reachable.add(path.slice(groupPrefix.length));
353
+ }
354
+ }
355
+ };
356
+
357
+ harvest(handle.declarations.deps.refs);
358
+
359
+ for (const entry of this.entries.values()) {
360
+ if (entry.kind === 'rule') {
361
+ if (entry.dependsOn.has(handle.key)) {
362
+ harvest(entry.declarations.deps.refs);
363
+ }
364
+ continue;
365
+ }
366
+ if (entry.kind === 'media') {
367
+ for (const innerRule of entry.rules) {
368
+ if (innerRule.dependsOn.has(handle.key)) {
369
+ harvest(innerRule.declarations.deps.refs);
370
+ }
371
+ }
372
+ }
373
+ }
374
+
375
+ return reachable;
376
+ }
377
+
378
+ // Computes the final theme/var dep sets, folding in default-marker
379
+ // contributions only for keys that survive reachability. Called once per
380
+ // `createResolver()` and per `collectedThemeDeps`/`collectedVarDeps`
381
+ // access; collection mutations between calls re-derive the result.
382
+ private finalizeDeps(): {
383
+ theme: ReadonlySet<string>;
384
+ vars: ReadonlySet<ContextualVarPath>;
385
+ } {
386
+ const theme = new Set<string>(this.themeDeps);
387
+ const vars = new Set<ContextualVarPath>(this.varDeps);
388
+
389
+ for (const handle of this.handles.values()) {
390
+ for (const group of handle.declarations.deps.defaults) {
391
+ const reachable = this.reachableDefaults(handle, group.groupPath);
392
+ for (const item of group.keys) {
393
+ if (!reachable.has(item.key)) {
394
+ continue;
395
+ }
396
+ vars.add(item.path);
397
+ item.valueDeps.theme.forEach((path) => theme.add(path));
398
+ item.valueDeps.vars.forEach((path) => vars.add(path));
399
+ }
400
+ }
401
+ }
402
+
403
+ // Default markers inside rule / media-inner-rule declarations don't have
404
+ // a host handle to anchor reachability against. The runtime emits every
405
+ // listed key when rendering such a declaration, so we mirror that here
406
+ // and include all of them in the registry.
407
+ for (const entry of this.entries.values()) {
408
+ if (entry.kind === 'rule') {
409
+ includeAllDefaults(entry.declarations.deps.defaults, theme, vars);
410
+ continue;
411
+ }
412
+ if (entry.kind === 'media') {
413
+ for (const inner of entry.rules) {
414
+ includeAllDefaults(inner.declarations.deps.defaults, theme, vars);
415
+ }
416
+ }
417
+ }
418
+
419
+ return { theme, vars };
420
+ }
421
+
422
+ private retainHandle(handle: StyleHandle): boolean {
423
+ if (!isEmptyDeclarations(handle.declarations)) {
424
+ return true;
425
+ }
426
+ if (this.registry.targetsHandle(handle.key)) {
427
+ return true;
428
+ }
429
+ if (this.dev && !warnedHandles.has(handle)) {
430
+ warnedHandles.add(handle);
431
+ this.warn(
432
+ `Style module "${handle.moduleName}" handle "${handle.localName}" has no declarations and no rule targets it; delete the empty template.`
433
+ );
434
+ }
435
+ return this.names !== 'compact';
436
+ }
437
+
438
+ private addEntry(entry: StyleEntry): void {
439
+ if (entry.kind === 'handle' && !this.retainHandle(entry)) {
440
+ return;
441
+ }
442
+ this.entries.set(entryKey(entry), entry);
443
+ if (entry.kind === 'handle') {
444
+ this.handles.set(entry.key, entry);
445
+ this.addDeclarationDeps(entry.declarations);
446
+ return;
447
+ }
448
+ if (entry.kind === 'rule') {
449
+ this.addDeclarationDeps(entry.declarations);
450
+ return;
451
+ }
452
+ for (const rule of entry.rules) {
453
+ this.addDeclarationDeps(rule.declarations);
454
+ }
455
+ }
456
+
457
+ private addDeclarationDeps(declarations: Declarations): void {
458
+ declarations.deps.theme.forEach((path) => this.themeDeps.add(path));
459
+ declarations.deps.vars.forEach((path) => this.varDeps.add(path));
460
+ }
461
+ }
462
+
463
+ const includeAllDefaults = (
464
+ defaults: ReadonlyArray<DefaultGroupDeps>,
465
+ theme: Set<string>,
466
+ vars: Set<ContextualVarPath>
467
+ ): void => {
468
+ for (const group of defaults) {
469
+ for (const item of group.keys) {
470
+ vars.add(item.path);
471
+ item.valueDeps.theme.forEach((path) => theme.add(path));
472
+ item.valueDeps.vars.forEach((path) => vars.add(path));
473
+ }
474
+ }
475
+ };
476
+
477
+ const cssVarKeysFrom = (finalized: {
478
+ theme: ReadonlySet<string>;
479
+ vars: ReadonlySet<ContextualVarPath>;
480
+ }): readonly CssVarPath[] => {
481
+ const keys = new Set<CssVarPath>();
482
+ finalized.theme.forEach((path) => keys.add(path));
483
+ finalized.vars.forEach((path) => keys.add(path));
484
+ return [...keys].sort();
485
+ };
486
+
487
+ const entryKey = (entry: StyleEntry): string => {
488
+ if (entry.kind === 'media') {
489
+ return `media/${entry.key}`;
490
+ }
491
+ return entry.key;
492
+ };
493
+
494
+ const mediaSortRank = (entry: StyleEntry): number =>
495
+ entry.kind === 'media' ? 1 : 0;
496
+
497
+ const localVarOverrideRank = (entry: StyleEntry): number => {
498
+ if (entry.kind === 'media') {
499
+ return 0;
500
+ }
501
+ return entry.declarations.css.some(isLocalVarOverrideMarker) ? 1 : 0;
502
+ };
503
+
504
+ // Base entries before every `@media`/`@container` block (no specificity). See finding `media-block-ordering`.
505
+ // `${group.set()}` after `${group}` defaults so a stacked override class wins. See finding `local-var-override-ordering`.
506
+ const compareEntriesForEmission = (a: StyleEntry, b: StyleEntry): number => {
507
+ const rankDelta = mediaSortRank(a) - mediaSortRank(b);
508
+ if (rankDelta !== 0) {
509
+ return rankDelta;
510
+ }
511
+ const varDelta = localVarOverrideRank(a) - localVarOverrideRank(b);
512
+ if (varDelta !== 0) {
513
+ return varDelta;
514
+ }
515
+ if (a.moduleName !== b.moduleName) {
516
+ return a.moduleName.localeCompare(b.moduleName);
517
+ }
518
+ return a.key.localeCompare(b.key);
519
+ };
520
+
521
+ const isReadonlyArray = <T>(value: unknown): value is readonly T[] =>
522
+ Array.isArray(value);
523
+
524
+ const isStylesModule = (
525
+ value: StylesModule | StyleHandle | StyleRule | StyleMedia
526
+ ): value is StylesModule => 'handles' in value && 'handleList' in value;