@hozo/engine 0.4.0 → 0.5.1

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 (76) hide show
  1. package/CHANGELOG.md +59 -0
  2. package/dist/authored-edges.d.ts +24 -0
  3. package/dist/authored-edges.d.ts.map +1 -0
  4. package/dist/authored-edges.js +73 -0
  5. package/dist/authored-edges.js.map +1 -0
  6. package/dist/class-name.native.d.ts +6 -0
  7. package/dist/class-name.native.d.ts.map +1 -1
  8. package/dist/class-name.native.js.map +1 -1
  9. package/dist/class-resolver.d.ts +39 -4
  10. package/dist/class-resolver.d.ts.map +1 -1
  11. package/dist/class-resolver.js +357 -67
  12. package/dist/class-resolver.js.map +1 -1
  13. package/dist/filter-slots.d.ts +11 -0
  14. package/dist/filter-slots.d.ts.map +1 -0
  15. package/dist/filter-slots.js +57 -0
  16. package/dist/filter-slots.js.map +1 -0
  17. package/dist/generated/authored-edges.d.ts +2 -0
  18. package/dist/generated/authored-edges.d.ts.map +1 -0
  19. package/dist/generated/authored-edges.js +3 -0
  20. package/dist/generated/authored-edges.js.map +1 -0
  21. package/dist/generated/class-resolver.d.ts +1 -1
  22. package/dist/generated/class-resolver.d.ts.map +1 -1
  23. package/dist/generated/class-resolver.js +1 -1
  24. package/dist/generated/class-resolver.js.map +1 -1
  25. package/dist/generated/filter-slots.d.ts +2 -0
  26. package/dist/generated/filter-slots.d.ts.map +1 -0
  27. package/dist/generated/filter-slots.js +3 -0
  28. package/dist/generated/filter-slots.js.map +1 -0
  29. package/dist/generated/gradient-slots.d.ts +2 -0
  30. package/dist/generated/gradient-slots.d.ts.map +1 -0
  31. package/dist/generated/gradient-slots.js +3 -0
  32. package/dist/generated/gradient-slots.js.map +1 -0
  33. package/dist/generated/shadow-slots.d.ts +2 -0
  34. package/dist/generated/shadow-slots.d.ts.map +1 -0
  35. package/dist/generated/shadow-slots.js +3 -0
  36. package/dist/generated/shadow-slots.js.map +1 -0
  37. package/dist/gradient-slots.d.ts +19 -0
  38. package/dist/gradient-slots.d.ts.map +1 -0
  39. package/dist/gradient-slots.js +61 -0
  40. package/dist/gradient-slots.js.map +1 -0
  41. package/dist/inline-edges.d.ts +8 -0
  42. package/dist/inline-edges.d.ts.map +1 -0
  43. package/dist/inline-edges.js +13 -0
  44. package/dist/inline-edges.js.map +1 -0
  45. package/dist/shadow-slots.d.ts +26 -0
  46. package/dist/shadow-slots.d.ts.map +1 -0
  47. package/dist/shadow-slots.js +59 -0
  48. package/dist/shadow-slots.js.map +1 -0
  49. package/dist/spread-styles.d.ts +3 -1
  50. package/dist/spread-styles.d.ts.map +1 -1
  51. package/dist/spread-styles.js +6 -5
  52. package/dist/spread-styles.js.map +1 -1
  53. package/dist/text-inheritance.d.ts +0 -5
  54. package/dist/text-inheritance.d.ts.map +1 -1
  55. package/dist/text-inheritance.js +1 -7
  56. package/dist/text-inheritance.js.map +1 -1
  57. package/dist/transform-slots.d.ts +1 -1
  58. package/dist/transform-slots.d.ts.map +1 -1
  59. package/dist/transform-slots.js +12 -5
  60. package/dist/transform-slots.js.map +1 -1
  61. package/package.json +17 -1
  62. package/src/authored-edges.ts +94 -0
  63. package/src/class-name.native.ts +10 -0
  64. package/src/class-resolver.ts +424 -76
  65. package/src/filter-slots.ts +67 -0
  66. package/src/generated/authored-edges.ts +2 -0
  67. package/src/generated/class-resolver.ts +1 -0
  68. package/src/generated/filter-slots.ts +2 -0
  69. package/src/generated/gradient-slots.ts +2 -0
  70. package/src/generated/shadow-slots.ts +2 -0
  71. package/src/gradient-slots.ts +73 -0
  72. package/src/inline-edges.ts +14 -0
  73. package/src/shadow-slots.ts +78 -0
  74. package/src/spread-styles.ts +10 -3
  75. package/src/text-inheritance.ts +1 -16
  76. package/src/transform-slots.ts +11 -3
@@ -1,3 +1,9 @@
1
+ import type { FilterSlots } from './filter-slots.ts'
2
+ import type { GradientSlots } from './gradient-slots.ts'
3
+ import { INLINE_EDGES, type InlineFamily } from './inline-edges.ts'
4
+ import type { ShadowSlots } from './shadow-slots.ts'
5
+ import type { TransformSlots } from './transform-slots.ts'
6
+
1
7
  /** A React Native style object, as the compiler emits it. */
2
8
  export type StyleObject = Record<string, unknown>
3
9
 
@@ -10,7 +16,24 @@ export type StyleObject = Record<string, unknown>
10
16
  export function hozoTextMetricStyles(
11
17
  styles: StyleObject[],
12
18
  defaults: ReadonlyMap<StyleObject, StyleObject>,
19
+ registers?: ReadonlyMap<StyleObject, StyleObject>,
13
20
  ): StyleObject[] {
21
+ if (registers && styles.some((style) => registers.has(style))) {
22
+ let leading: StyleObject | undefined
23
+ let hasDefault = false
24
+ let hasExplicit = false
25
+ for (const style of styles) {
26
+ if (defaults.has(style)) hasDefault = true
27
+ else if (Object.hasOwn(style, 'lineHeight')) hasExplicit = true
28
+ else leading = registers.get(style) ?? leading
29
+ }
30
+ // Until the direct declaration and the reader have a shared assessed
31
+ // order, keep the existing explicit-leading projection. A later
32
+ // register cannot become a direct line-height just by appearing last.
33
+ if (hasDefault && !hasExplicit && leading) {
34
+ return [...styles.map((style) => defaults.get(style) ?? style), leading]
35
+ }
36
+ }
14
37
  if (
15
38
  !styles.some((style) => defaults.has(style)) ||
16
39
  !styles.some((style) => Object.hasOwn(style, 'lineHeight') && !defaults.has(style))
@@ -21,6 +44,48 @@ export function hozoTextMetricStyles(
21
44
  return styles.map((style) => defaults.get(style) ?? style)
22
45
  }
23
46
 
47
+ type TextMetricStyle = StyleObject | readonly TextMetricStyle[] | false | null | undefined
48
+
49
+ /**
50
+ * A compiled text owner sees both its own entries and selected caller entries.
51
+ * Keep the array order for every other field, and keep the last explicit
52
+ * leading if a later font-size fallback would cover it.
53
+ */
54
+ export function hozoMixedTextMetricStyles(
55
+ styles: readonly TextMetricStyle[],
56
+ ownDefaults: readonly StyleObject[],
57
+ candidateDefaults?: ReadonlySet<StyleObject> | ReadonlyMap<StyleObject, StyleObject>,
58
+ candidateRegisters?: ReadonlyMap<StyleObject, StyleObject>,
59
+ ownRegisters?: readonly (readonly [StyleObject, number])[],
60
+ ): readonly TextMetricStyle[] {
61
+ let explicit: StyleObject | undefined
62
+ let endsWithDefault = false
63
+ let hasDefault = false
64
+ let register: number | undefined
65
+ function visit(value: TextMetricStyle): void {
66
+ if (!value || typeof value !== 'object') return
67
+ if (Array.isArray(value)) {
68
+ for (const part of value) visit(part)
69
+ return
70
+ }
71
+ const style = value as StyleObject
72
+ const captured =
73
+ ownRegisters?.find(([entry]) => entry === style)?.[1] ??
74
+ (candidateRegisters?.get(style)?.lineHeight as number | undefined)
75
+ if (captured !== undefined) register = captured
76
+ if (!Object.hasOwn(style, 'lineHeight')) return
77
+ endsWithDefault = ownDefaults.includes(style) || candidateDefaults?.has(style) === true
78
+ hasDefault ||= endsWithDefault
79
+ if (!endsWithDefault) {
80
+ explicit = style
81
+ }
82
+ }
83
+ for (const style of styles) visit(style)
84
+ if (hasDefault && !explicit && register !== undefined)
85
+ return [...styles, { lineHeight: register }]
86
+ return endsWithDefault && explicit ? [...styles, { lineHeight: explicit.lineHeight }] : styles
87
+ }
88
+
24
89
  /**
25
90
  * The state an element is in when its runtime classes are resolved: what
26
91
  * its pressable reports (`pressed`, `hovered`, `focused`, `focusVisible`)
@@ -57,7 +122,31 @@ export type StateGroup = readonly [readonly string[], StyleObject, number?]
57
122
  * in a state is reported as it always was; with it, that class applies
58
123
  * when its state holds, after the unconditional ones.
59
124
  */
60
- export type ClassResolver = (value: unknown, state?: ClassState) => StyleObject[]
125
+ type Composer = (styles: StyleObject[], deferred?: number) => StyleObject[]
126
+
127
+ export type ClassResolver = ((
128
+ value: unknown,
129
+ state?: ClassState,
130
+ deferred?: number,
131
+ ) => StyleObject[]) & {
132
+ /** @internal Compiler-generated identities for mixed effect boundaries. */
133
+ shadowSlots?: ReadonlyMap<StyleObject, ShadowSlots>
134
+ gradientSlots?: ReadonlyMap<StyleObject, GradientSlots>
135
+ transformSlots?: ReadonlyMap<StyleObject, TransformSlots>
136
+ filterSlots?: ReadonlyMap<StyleObject, FilterSlots>
137
+ /** @internal Identity of implicit leading, including on maps without explicit leading. */
138
+ fontDefaults?: ReadonlySet<StyleObject> | ReadonlyMap<StyleObject, StyleObject>
139
+ /** @internal Captured register writes keyed by original generated style. */
140
+ leadingRegisters?: ReadonlyMap<StyleObject, StyleObject>
141
+ /** @internal Bit mask of this namespace's installed deferrable composers. */
142
+ effectMask?: number
143
+ /**
144
+ * @internal Development only. Warn once about each name absent from the map,
145
+ * unless `markers` declares it: an exact name, or a prefix ending in `*`.
146
+ * The bundler calls it behind `__DEV__`, so a release build never does.
147
+ */
148
+ warnUnknownClasses?: (markers: readonly string[]) => void
149
+ }
61
150
 
62
151
  /** @internal Generated-data ABI, not a utility parser or a public cascade API. */
63
152
  export interface ClassResolverPlan {
@@ -66,15 +155,159 @@ export interface ClassResolverPlan {
66
155
  unsupported?: Record<string, string>
67
156
  states?: Record<string, readonly StateGroup[]>
68
157
  ambient?: ClassState
69
- compose?: (styles: StyleObject[]) => StyleObject[]
158
+ compose?: Composer
70
159
  /** Ordinals from one complete compiler namespace. Missing means barrier. */
71
- ordering: { schema: 1; ranks: Readonly<Record<string, readonly number[]>> }
160
+ ordering: {
161
+ schema: 1
162
+ ranks: Readonly<Record<string, readonly number[]>>
163
+ /** Compiler-proven disjoint sets: unranked colours and admitted ranked scalars. */
164
+ independentColors?: readonly [barriers: string, independent: string]
165
+ /** Lists disjoint colour writers. Older schema-1 maps may send a redundant true. */
166
+ rankedStates?: true | string
167
+ }
72
168
  }
73
169
 
74
170
  interface TemplateEntry {
75
171
  keys: readonly string[]
76
172
  style: StyleObject
77
173
  name: string
174
+ ownerRank: number | undefined
175
+ }
176
+
177
+ interface RankedEntry extends TemplateEntry {
178
+ rank: number | undefined
179
+ }
180
+
181
+ /**
182
+ * The families whose physical writes these styles need as logical ones: one
183
+ * style writes the two physical sides unevenly (only one, or two values),
184
+ * which the RTL swap would move (#2050), or, when `mixed` counts, the
185
+ * styles write the family both logically and physically (#2018).
186
+ */
187
+ function inlineEdgesToProject(styles: Iterable<StyleObject>, mixed: boolean): InlineFamily[] {
188
+ let logical = 0
189
+ let physical = 0
190
+ let uneven = 0
191
+ for (const style of styles) {
192
+ INLINE_EDGES.forEach(([start, end, left, right], family) => {
193
+ if (Object.hasOwn(style, start) || Object.hasOwn(style, end)) logical |= 1 << family
194
+ const hasLeft = Object.hasOwn(style, left)
195
+ const hasRight = Object.hasOwn(style, right)
196
+ if (hasLeft || hasRight) physical |= 1 << family
197
+ if (hasLeft !== hasRight || style[left] !== style[right]) uneven |= 1 << family
198
+ })
199
+ }
200
+ const project = uneven | (mixed ? logical & physical : 0)
201
+ return INLINE_EDGES.filter((_, family) => project & (1 << family))
202
+ }
203
+
204
+ /**
205
+ * Tailwind lets the later of `px-8 pl-1` win; React Native does not. Without
206
+ * `swapLeftAndRightInRTL` Yoga prefers `paddingStart` to `paddingLeft` in any
207
+ * order, and with it -- the default -- RTL turns `paddingLeft` into
208
+ * `paddingStart`, overwriting it, so the physical write wins on the other
209
+ * side. Start and end mean the same under both, so in a family the class
210
+ * string writes both ways, physical writes are renamed to the logical side
211
+ * they are in this direction, and array order decides as the cascade did
212
+ * (#2018). The family is chosen per class string, states included, as the
213
+ * static compiler chooses it per element: `pl-1` must not change sides when
214
+ * `hover:ps-8` beside it stops applying. A physical utility on its own
215
+ * keeps its side too (`pl-4` stays on the left in RTL, as in Tailwind), so a
216
+ * family one style writes unevenly is renamed the same way.
217
+ */
218
+ function projectInlineEdges(
219
+ styles: StyleObject[],
220
+ mixed: readonly InlineFamily[] | undefined,
221
+ rtl: boolean,
222
+ ): StyleObject[] {
223
+ if (!mixed?.length) return styles
224
+ return styles.map((style) => {
225
+ let projected: StyleObject | undefined
226
+ for (const [start, end, left, right] of mixed) {
227
+ for (const [physical, logical] of [
228
+ [left, rtl ? end : start],
229
+ [right, rtl ? start : end],
230
+ ] as const) {
231
+ if (!Object.hasOwn(style, physical)) continue
232
+ projected ??= { ...style }
233
+ const value = projected[physical]
234
+ delete projected[physical]
235
+ projected[logical] = value
236
+ }
237
+ }
238
+ return projected ?? style
239
+ })
240
+ }
241
+
242
+ /** An assessed run may not move across a Native specificity boundary or
243
+ * unknown write. The compiler can separately prove that specific holes do
244
+ * not interact with the whole tier. */
245
+ function orderRankedRuns(
246
+ entries: RankedEntry[],
247
+ sharedTier: boolean,
248
+ disjointHoles: boolean,
249
+ start = 0,
250
+ limit = entries.length,
251
+ ) {
252
+ while (start < limit - 1) {
253
+ let end = start
254
+ let previous = -1
255
+ let inverted = false
256
+ const specificity = entries[start]!.ownerRank
257
+ for (; end < limit; end++) {
258
+ const { rank, ownerRank } = entries[end]!
259
+ if (!sharedTier && ownerRank !== specificity) break
260
+ if (rank === undefined) {
261
+ if (!disjointHoles) break
262
+ continue
263
+ }
264
+ inverted ||= previous > rank
265
+ previous = rank
266
+ }
267
+ if (inverted && end === start + 2) {
268
+ // Both entries had ranks, otherwise no inversion was possible.
269
+ const first = entries[start]!
270
+ entries[start] = entries[start + 1]!
271
+ entries[start + 1] = first
272
+ } else if (inverted) {
273
+ const run = entries.slice(start, end)
274
+ const ranked = disjointHoles ? run.filter((entry) => entry.rank !== undefined) : run
275
+ ranked.sort((a, b) => a.rank! - b.rank!)
276
+ let next = 0
277
+ for (let index = start; index < end; index++)
278
+ if (entries[index]!.rank !== undefined) entries[index] = ranked[next++]!
279
+ }
280
+ // A different specificity starts a new run; an unranked write pins
281
+ // its own position and can be skipped.
282
+ start = end + (end < limit && entries[end]!.rank === undefined ? 1 : 0)
283
+ }
284
+ }
285
+
286
+ function orderRestingRuns(
287
+ entries: RankedEntry[],
288
+ barriers: ReadonlySet<string> | undefined,
289
+ independent: ReadonlySet<string> | undefined,
290
+ disjoint: boolean,
291
+ ) {
292
+ if (disjoint || !independent || !barriers) {
293
+ orderRankedRuns(entries, true, disjoint)
294
+ return
295
+ }
296
+ // The full input did not qualify, but the same compiler proof can
297
+ // admit an island bounded by an unrelated or overlapping write.
298
+ let start = 0
299
+ let hasColour = false
300
+ for (let end = 0; end <= entries.length; end++) {
301
+ const entry = entries[end]
302
+ if (entry && (entry.rank === undefined ? barriers : independent).has(entry.name)) {
303
+ hasColour ||= entry.rank === undefined
304
+ continue
305
+ }
306
+ if (hasColour) orderRankedRuns(entries, true, true, start, end)
307
+ start = end + 1
308
+ hasColour = false
309
+ }
310
+ orderRankedRuns(entries, true, false)
78
311
  }
79
312
 
80
313
  interface Resolved {
@@ -82,13 +315,14 @@ interface Resolved {
82
315
  base: StyleObject[]
83
316
  /** The same, before `compose`, for composing again with state styles. */
84
317
  raw: StyleObject[]
85
- groups: StateGroup[]
86
- /** Parallel to groups: retain the candidate name for missing-fact warnings. */
87
- groupClasses: string[]
318
+ /** Inline families this string writes both ways, states included (#2018). */
319
+ edges?: readonly InlineFamily[]
320
+ /** Only materialized for a mixed compiler boundary, using the same selection. */
321
+ deferredBases?: (StyleObject[] | undefined)[]
88
322
  /** State classes in this string, named if it is resolved without a state. */
89
323
  stateClasses: string[]
90
- /** Rank-free cached template: state updates only select applicable entries. */
91
- ordered?: TemplateEntry[]
324
+ /** Cached in final order. Fallback orders all resting writes before it. */
325
+ template?: { entries: TemplateEntry[]; appendToBase: boolean }
92
326
  }
93
327
 
94
328
  /**
@@ -121,19 +355,21 @@ export function createClassResolver(plan: ClassResolverPlan): ClassResolver
121
355
  export function createClassResolver(
122
356
  styles: Record<string, StyleObject>,
123
357
  unsupported?: Record<string, string>,
124
- compose?: (styles: StyleObject[]) => StyleObject[],
358
+ compose?: Composer,
125
359
  states?: Record<string, readonly StateGroup[]>,
126
360
  ambient?: ClassState,
127
361
  ): ClassResolver
128
362
  export function createClassResolver(
129
363
  stylesOrPlan: Record<string, StyleObject> | ClassResolverPlan,
130
364
  unsupported: Record<string, string> = {},
131
- compose?: (styles: StyleObject[]) => StyleObject[],
365
+ compose?: Composer,
132
366
  states: Record<string, readonly StateGroup[]> = {},
133
367
  ambient: ClassState = {},
134
368
  ): ClassResolver {
135
369
  let styles: Record<string, StyleObject>
136
370
  let ranks: ClassResolverPlan['ordering']['ranks'] | undefined
371
+ let independentColors: ClassResolverPlan['ordering']['independentColors']
372
+ let stateColors: Set<string> | undefined
137
373
  if (typeof stylesOrPlan.schema === 'number') {
138
374
  const plan = stylesOrPlan as ClassResolverPlan
139
375
  if (
@@ -149,14 +385,42 @@ export function createClassResolver(
149
385
  states = plan.states ?? {}
150
386
  ambient = plan.ambient ?? {}
151
387
  ranks = plan.ordering.ranks
388
+ independentColors = plan.ordering.independentColors
389
+ if (typeof plan.ordering.rankedStates === 'string')
390
+ stateColors = new Set(plan.ordering.rankedStates.split(' '))
152
391
  } else styles = stylesOrPlan as Record<string, StyleObject>
392
+ // Only mixed namespaces carrying the build-time proof allocate these.
393
+ const barriers = independentColors && new Set(independentColors[0].split(' '))
394
+ const independent = independentColors && new Set(independentColors[1].split(' '))
153
395
  const isAmbient = (key: string) => Object.hasOwn(ambient, key)
396
+ // Checked once per map: most never write an inline family both ways or
397
+ // unevenly, and then no class string can, so none is scanned. A mixed
398
+ // family counts only in a ranked map: an unranked one keeps its written
399
+ // order and Yoga's own precedence.
400
+ const mixed = ranks !== undefined
401
+ const edges =
402
+ inlineEdgesToProject(
403
+ [
404
+ ...Object.values(styles),
405
+ ...Object.values(states).flatMap((groups) => groups.map((group) => group[1])),
406
+ ],
407
+ mixed,
408
+ ).length > 0
409
+ const rtl = Boolean(ambient.rtl)
410
+ const stringEdges = (entries: readonly { style: StyleObject }[], raw: readonly StyleObject[]) =>
411
+ edges
412
+ ? inlineEdgesToProject([...raw, ...entries.map((entry) => entry.style)], mixed)
413
+ : undefined
414
+ const project = (list: StyleObject[], resolved: { edges?: readonly InlineFamily[] }) =>
415
+ projectInlineEdges(list, resolved.edges, rtl)
154
416
  // Class strings repeat constantly across renders and list items, and the
155
417
  // result is immutable, so each distinct string is resolved once.
156
418
  const cache = new Map<string, Resolved>()
157
419
  // Warn once per class, not once per render -- a warning inside a list's
158
420
  // render path would otherwise repeat for every row, every frame.
159
421
  const warned = new Set<string>()
422
+ // Set only by `warnUnknownClasses`, which only development builds call.
423
+ let unknownIsMissing: ((name: string) => boolean) | undefined
160
424
  const warn = (name: string, reason: string) => {
161
425
  if (warned.has(name)) return
162
426
  warned.add(name)
@@ -203,24 +467,36 @@ export function createClassResolver(
203
467
 
204
468
  function resolve(value: string): Resolved {
205
469
  const base: StyleObject[] = []
206
- const groups: StateGroup[] = []
207
- const groupClasses: string[] = []
470
+ const resting: RankedEntry[] | undefined = ranks ? [] : undefined
471
+ const groups: RankedEntry[] = []
208
472
  const stateClasses: string[] = []
209
- const decided: StyleObject[] = []
210
- // Build transient ranked entries only for the generated-plan ABI. A
211
- // single unassessed object makes this whole string use the old path.
212
- let ranked: { rank: number; entry: TemplateEntry }[] | undefined = ranks ? [] : undefined
473
+ const decided: RankedEntry[] = []
474
+ // A compact plan orders all writes together; an unassessed object
475
+ // without a disjointness proof keeps the existing selection tiers.
476
+ let ranked: RankedEntry[] | undefined = ranks ? [] : undefined
477
+ let canKeepRanks = independent !== undefined
478
+ let restingHolesDisjoint = canKeepRanks
479
+ let hasUnrankedState = false
480
+ let colors: TemplateEntry[] | undefined
481
+ const rankFor = (name: string, index: number) => {
482
+ const rank = ranks?.[name]?.[index]
483
+ if (rank !== undefined && (!Number.isSafeInteger(rank) || rank < 0))
484
+ throw new Error('Invalid Hozo class plan rank')
485
+ return rank
486
+ }
213
487
  const take = ranks
214
- ? (name: string, index: number, style: StyleObject, keys: readonly string[]) => {
488
+ ? (entry: RankedEntry) => {
215
489
  if (!ranked) return
216
- const rank = ranks![name]?.[index]
217
- if (rank === undefined) {
218
- ranked = undefined
490
+ if (entry.rank === undefined) {
491
+ if (canKeepRanks && barriers?.has(entry.name)) {
492
+ // Their colours still compete in fallback order. The ranked
493
+ // entries are only independent of these writes, not of one another.
494
+ ;(colors ??= []).push(entry)
495
+ } else ranked = undefined
219
496
  return
220
497
  }
221
- if (!Number.isSafeInteger(rank) || rank < 0)
222
- throw new Error('Invalid Hozo class plan rank')
223
- ranked.push({ rank, entry: { name, style, keys } })
498
+ if (canKeepRanks && !independent?.has(entry.name)) canKeepRanks = false
499
+ ranked.push(entry)
224
500
  }
225
501
  : undefined
226
502
  for (const name of value.split(/\s+/)) {
@@ -229,8 +505,14 @@ export function createClassResolver(
229
505
  }
230
506
  const style = styles[name]
231
507
  if (style) {
232
- base.push(style)
233
- take?.(name, 0, style, [])
508
+ if (resting) {
509
+ const rank = rankFor(name, 0)
510
+ const entry = { name, keys: [], style, ownerRank: undefined, rank }
511
+ resting.push(entry)
512
+ if (restingHolesDisjoint && !(rank === undefined ? barriers : independent)?.has(name))
513
+ restingHolesDisjoint = false
514
+ take?.(entry)
515
+ } else base.push(style)
234
516
  continue
235
517
  }
236
518
  const conditional = states[name]
@@ -238,63 +520,109 @@ export function createClassResolver(
238
520
  let needsState = false
239
521
  for (let index = 0; index < conditional.length; index++) {
240
522
  const group = conditional[index]!
241
- take?.(name, index, group[1], group[0])
242
- if ((negativePredicates?.get(group[0])?.keys ?? group[0]).every(isAmbient)) {
243
- if (matches(group[0], name, ambient)) decided.push(group[1])
523
+ const rank = rankFor(name, index)
524
+ const entry = {
525
+ name,
526
+ keys: group[0],
527
+ style: group[1],
528
+ ownerRank: group[2],
529
+ rank,
530
+ }
531
+ take?.(entry)
532
+ if ((negativePredicates?.get(entry.keys)?.keys ?? entry.keys).every(isAmbient)) {
533
+ if (matches(entry.keys, name, ambient)) decided.push(entry)
244
534
  continue
245
535
  }
246
- groups.push(group)
247
- groupClasses.push(name)
536
+ groups.push(entry)
537
+ if (rank === undefined) {
538
+ hasUnrankedState = true
539
+ if (stateColors && !stateColors.has(name))
540
+ throw new Error('Invalid Hozo class plan state rank')
541
+ }
248
542
  needsState = true
249
543
  }
250
544
  if (needsState) stateClasses.push(name)
251
545
  continue
252
546
  }
253
547
  // An unknown class isn't necessarily a Hozo problem -- it may be an
254
- // app's own non-Tailwind class, or a testID-ish marker -- so only the
255
- // ones the compiler recognized and refused are reported.
548
+ // app's own non-Tailwind class, or a testID-ish marker. So by default
549
+ // only the ones the compiler recognized and refused are reported. In
550
+ // development the bundler can switch on the rest, with the project's
551
+ // declared markers (`markerClasses`) excluded (#2073).
256
552
  const reason = unsupported[name]
257
553
  if (reason) warn(name, reason)
554
+ else if (unknownIsMissing?.(name))
555
+ warn(
556
+ name,
557
+ `\`${name}\` is not in this app's class map, so it does not apply. If it is a ` +
558
+ "Tailwind class, it was assembled at run time or written in a file the bundler's scan " +
559
+ "did not read: write it out whole in a file the project's `content` covers. If it is " +
560
+ 'not meant to style anything, list it in `markerClasses`.',
561
+ )
258
562
  }
259
- if (ranked) {
260
- // Stable ties keep input order. Strip rank objects before caching, so
261
- // neither cache hits nor state-only changes can read/sort priorities.
262
- ranked.sort((a, b) => a.rank - b.rank)
263
- const ordered: TemplateEntry[] = []
264
- for (const { entry } of ranked) {
563
+ if (ranked && (!colors || canKeepRanks)) {
564
+ // Stable ties keep input order. Cache the selected entry order; hot
565
+ // state changes only test guards, never read or sort priorities.
566
+ ranked.sort((a, b) => a.rank! - b.rank!)
567
+ const ordered: TemplateEntry[] = colors ?? []
568
+ for (const entry of ranked) {
265
569
  if ((negativePredicates?.get(entry.keys)?.keys ?? entry.keys).every(isAmbient)) {
266
570
  if (matches(entry.keys, entry.name, ambient)) ordered.push({ ...entry, keys: [] })
267
571
  } else ordered.push(entry)
268
572
  }
269
573
  const raw = ordered.filter((entry) => !entry.keys.length).map((entry) => entry.style)
574
+ const edges = stringEdges(ordered, [])
270
575
  return {
271
- base: compose ? compose(raw) : raw,
576
+ base: projectInlineEdges(compose ? compose(raw) : raw, edges, rtl),
272
577
  raw,
273
- groups,
274
- groupClasses,
578
+ edges,
275
579
  stateClasses,
276
- ordered,
580
+ template: groups.length ? { entries: ordered, appendToBase: false } : undefined,
277
581
  }
278
582
  }
279
- // Build-time Tailwind owner-state rank, sorted once per cached class string,
280
- // never on every state update. Mixed/unranked domains are left alone; ties
281
- // retain authored/caller order. Keep warning provenance beside each group.
282
- if (groups.length > 1 && groups.every((group) => group[2] !== undefined)) {
283
- if (groups.some((group, index) => index > 0 && groups[index - 1]![2]! > group[2]!)) {
284
- const sorted = groups
285
- .map((group, index) => ({ group, name: groupClasses[index]! }))
286
- .sort((a, b) => a.group[2]! - b.group[2]!)
287
- for (const [index, entry] of sorted.entries()) {
288
- groups[index] = entry.group
289
- groupClasses[index] = entry.name
290
- }
291
- }
583
+ // The selected tier (not the whole map) can already have exact CSS
584
+ // priorities. Use Native weights only when there is a real hole.
585
+ const sharedTier = ranks !== undefined && (!hasUnrankedState || stateColors !== undefined)
586
+ // Native can linearize known selector weights, but it must not order
587
+ // unknown weights by pretending they have zero specificity.
588
+ if (
589
+ !sharedTier &&
590
+ groups.length > 1 &&
591
+ groups.every((group) => group.ownerRank !== undefined) &&
592
+ groups.some((group, index) => index > 0 && groups[index - 1]!.ownerRank! > group.ownerRank!)
593
+ ) {
594
+ groups.sort((a, b) => a.ownerRank! - b.ownerRank!)
595
+ }
596
+ if (ranks) {
597
+ // Ambient guards have already been evaluated. They keep their
598
+ // resting tier, but it must not throw away known Tailwind order.
599
+ orderRankedRuns(decided, true, false)
600
+ orderRankedRuns(groups, sharedTier, stateColors !== undefined)
601
+ }
602
+ if (resting) {
603
+ orderRestingRuns(resting, barriers, independent, restingHolesDisjoint)
604
+ for (const { style } of resting) base.push(style)
605
+ }
606
+ for (const { style } of decided) base.push(style)
607
+ const edges = stringEdges(groups, base)
608
+ return {
609
+ base: projectInlineEdges(compose ? compose(base) : base, edges, rtl),
610
+ raw: base,
611
+ edges,
612
+ stateClasses,
613
+ template: groups.length ? { entries: groups, appendToBase: true } : undefined,
292
614
  }
293
- base.push(...decided)
294
- return { base: compose ? compose(base) : base, raw: base, groups, groupClasses, stateClasses }
295
615
  }
296
616
 
297
- return function hozoClasses(value: unknown, state?: ClassState): StyleObject[] {
617
+ function restingWithoutEffects(resolved: Resolved, deferred: number): StyleObject[] {
618
+ const bases = (resolved.deferredBases ??= [])
619
+ return (bases[deferred] ??= project(
620
+ compose ? compose(resolved.raw, deferred) : resolved.raw,
621
+ resolved,
622
+ ))
623
+ }
624
+
625
+ const hozoClasses: ClassResolver = (value, state, deferred) => {
298
626
  // Falsy is the normal case, not an error: `cond && 'p-4'` yields
299
627
  // `false` whenever the condition is off.
300
628
  if (typeof value !== 'string' || value === '') {
@@ -306,7 +634,9 @@ export function createClassResolver(
306
634
  resolved = resolve(value)
307
635
  cache.set(value, resolved)
308
636
  }
309
- if (resolved.groups.length === 0) return resolved.base
637
+ if (deferred) deferred &= hozoClasses.effectMask ?? 0
638
+ const template = resolved.template
639
+ if (!template) return deferred ? restingWithoutEffects(resolved, deferred) : resolved.base
310
640
  if (state === undefined) {
311
641
  for (const name of resolved.stateClasses) {
312
642
  warn(
@@ -316,32 +646,50 @@ export function createClassResolver(
316
646
  'className so it compiles to a real style variant.',
317
647
  )
318
648
  }
319
- return resolved.base
649
+ return deferred ? restingWithoutEffects(resolved, deferred) : resolved.base
320
650
  }
321
651
  // A group can name an ambient fact beside the element's own, as
322
652
  // `rtl:hover:` does; the element supplies only its part.
323
653
  const facts = { ...ambient, ...state }
324
- if (resolved.ordered) {
325
- const all: StyleObject[] = []
326
- let active = false
327
- for (const entry of resolved.ordered) {
328
- if (!entry.keys.length) all.push(entry.style)
329
- else if (matches(entry.keys, entry.name, facts)) {
330
- all.push(entry.style)
331
- active = true
654
+ // Fully ranked writes may interleave with resting styles. The fallback
655
+ // template has only state entries, so delay copying its resting prefix
656
+ // until an actual state matches. Both routes select by the same loop.
657
+ const all: StyleObject[] = []
658
+ let active = false
659
+ for (const entry of template.entries) {
660
+ if (!entry.keys.length) all.push(entry.style)
661
+ else if (matches(entry.keys, entry.name, facts)) {
662
+ if (!active && template.appendToBase) {
663
+ for (const style of resolved.raw) all.push(style)
332
664
  }
665
+ all.push(entry.style)
666
+ active = true
333
667
  }
334
- if (!active) return resolved.base
335
- return compose ? compose(all) : all
336
668
  }
337
- const active = resolved.groups.filter(([keys], index) =>
338
- matches(keys, resolved.groupClasses[index]!, facts),
339
- )
340
- if (active.length === 0) return resolved.base
341
- const all = [...resolved.raw, ...active.map(([, style]) => style)]
669
+ if (!active) return deferred ? restingWithoutEffects(resolved, deferred) : resolved.base
342
670
  // A state style can carry a transform, which `compose` folds into one;
343
671
  // so it runs over the whole list, and the result is per render rather
344
672
  // than cached -- it changes with the state.
345
- return compose ? compose(all) : all
673
+ return project(compose ? compose(all, deferred) : all, resolved)
674
+ }
675
+ hozoClasses.warnUnknownClasses = (markers) => {
676
+ const names = new Set<string>(BUILTIN_MARKERS)
677
+ const prefixes = [...BUILTIN_MARKER_PREFIXES]
678
+ for (const marker of markers) {
679
+ if (marker.endsWith('*')) prefixes.push(marker.slice(0, -1))
680
+ else names.add(marker)
681
+ }
682
+ unknownIsMissing = (name) =>
683
+ !names.has(name) && !prefixes.some((prefix) => name.startsWith(prefix))
684
+ // A string resolved before this call would otherwise keep its silence.
685
+ cache.clear()
346
686
  }
687
+ return hozoClasses
347
688
  }
689
+
690
+ /**
691
+ * Tailwind's own non-styling names: a `group` or `peer` marks an element for
692
+ * its descendants or siblings and writes no style of its own.
693
+ */
694
+ const BUILTIN_MARKERS = ['group', 'peer']
695
+ const BUILTIN_MARKER_PREFIXES = ['group/', 'peer/']