zyzz 0.0.7 → 0.0.9

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 (78) hide show
  1. package/README.md +7 -7
  2. package/dist/Style.d.ts +2 -2
  3. package/dist/Style.d.ts.map +1 -1
  4. package/dist/Style.js +4 -4
  5. package/dist/Style.js.map +1 -1
  6. package/dist/babel/index.d.ts +4 -1
  7. package/dist/babel/index.d.ts.map +1 -1
  8. package/dist/babel/index.js +6 -3
  9. package/dist/babel/index.js.map +1 -1
  10. package/dist/babel/internal/Compilation.d.ts +12 -0
  11. package/dist/babel/internal/Compilation.d.ts.map +1 -0
  12. package/dist/babel/internal/Compilation.js +3 -0
  13. package/dist/babel/internal/Compilation.js.map +1 -0
  14. package/dist/compiler/Graph.d.ts +6 -1
  15. package/dist/compiler/Graph.d.ts.map +1 -1
  16. package/dist/compiler/Graph.js +160 -36
  17. package/dist/compiler/Graph.js.map +1 -1
  18. package/dist/compiler/Transform.d.ts.map +1 -1
  19. package/dist/compiler/Transform.js +2 -1
  20. package/dist/compiler/Transform.js.map +1 -1
  21. package/dist/compiler/internal/Stylesheets.d.ts +8 -0
  22. package/dist/compiler/internal/Stylesheets.d.ts.map +1 -1
  23. package/dist/compiler/internal/Stylesheets.js +4 -0
  24. package/dist/compiler/internal/Stylesheets.js.map +1 -1
  25. package/dist/internal/Value.d.ts +5 -5
  26. package/dist/internal/Value.d.ts.map +1 -1
  27. package/dist/internal/Value.js +18 -64
  28. package/dist/internal/Value.js.map +1 -1
  29. package/dist/metro/Graph.d.ts +9 -1
  30. package/dist/metro/Graph.d.ts.map +1 -1
  31. package/dist/metro/Graph.js +5 -4
  32. package/dist/metro/Graph.js.map +1 -1
  33. package/dist/metro/transformer.d.ts +0 -1
  34. package/dist/metro/transformer.d.ts.map +1 -1
  35. package/dist/metro/transformer.js +19 -1
  36. package/dist/metro/transformer.js.map +1 -1
  37. package/dist/next/index.d.ts +3 -3
  38. package/dist/next/index.d.ts.map +1 -1
  39. package/dist/next/index.js +25 -30
  40. package/dist/next/index.js.map +1 -1
  41. package/dist/next/internal/Project.d.ts +4 -4
  42. package/dist/next/internal/Project.d.ts.map +1 -1
  43. package/dist/next/internal/Project.js +153 -107
  44. package/dist/next/internal/Project.js.map +1 -1
  45. package/dist/next/loader.d.ts +3 -1
  46. package/dist/next/loader.d.ts.map +1 -1
  47. package/dist/next/loader.js +152 -99
  48. package/dist/next/loader.js.map +1 -1
  49. package/dist/node/Host.d.ts.map +1 -1
  50. package/dist/node/Host.js +11 -3
  51. package/dist/node/Host.js.map +1 -1
  52. package/dist/node/internal/Snapshot.d.ts +12 -0
  53. package/dist/node/internal/Snapshot.d.ts.map +1 -0
  54. package/dist/node/internal/Snapshot.js +74 -0
  55. package/dist/node/internal/Snapshot.js.map +1 -0
  56. package/dist/unplugin/index.d.ts.map +1 -1
  57. package/dist/unplugin/index.js +13 -9
  58. package/dist/unplugin/index.js.map +1 -1
  59. package/dist/vite/index.d.ts.map +1 -1
  60. package/dist/vite/index.js +130 -27
  61. package/dist/vite/index.js.map +1 -1
  62. package/package.json +1 -1
  63. package/src/Style.ts +6 -6
  64. package/src/babel/index.ts +10 -4
  65. package/src/babel/internal/Compilation.ts +13 -0
  66. package/src/compiler/Graph.ts +213 -47
  67. package/src/compiler/Transform.ts +2 -1
  68. package/src/compiler/internal/Stylesheets.ts +9 -0
  69. package/src/internal/Value.ts +40 -88
  70. package/src/metro/Graph.ts +5 -3
  71. package/src/metro/transformer.ts +26 -1
  72. package/src/next/index.ts +26 -31
  73. package/src/next/internal/Project.ts +196 -155
  74. package/src/next/loader.ts +181 -124
  75. package/src/node/Host.ts +15 -3
  76. package/src/node/internal/Snapshot.ts +79 -0
  77. package/src/unplugin/index.ts +12 -8
  78. package/src/vite/index.ts +161 -35
@@ -38,6 +38,8 @@ export declare namespace compile {
38
38
 
39
39
  /** Source modules available for relative import resolution. */
40
40
  type Options = {
41
+ /** Host entrypoints emit owned stylesheet effects without packed contracts. */
42
+ readonly [Stylesheets.entry]?: string | undefined
41
43
  /** Host syntax from the same immutable source snapshot. */
42
44
  readonly [Syntax.cache]?:
43
45
  | ReadonlyMap<string, Parser.ParseResult>
@@ -71,6 +73,8 @@ export declare namespace compile {
71
73
 
72
74
  /** Compiled modules and their direct source dependencies. */
73
75
  type ReturnType = {
76
+ /** Packed resources deduplicated by the host across scoped source outputs. */
77
+ readonly [Stylesheets.packed]?: readonly Stylesheets.Resource[]
74
78
  /** Versioned compiler-only JSON per module; publish beside the compiled entrypoint as <entry>.zyzz.json. */
75
79
  readonly contracts: Readonly<Record<string, string>>
76
80
  /** Direct static runtime source and library-contract dependencies, keyed by module identity. */
@@ -88,7 +92,7 @@ export declare namespace compile {
88
92
  }
89
93
  }
90
94
 
91
- /** Creates an isolated compiler that retains only the last successful graph. */
95
+ /** Creates an isolated compiler with bounded extraction reuse across source graphs. */
92
96
  export function create(): create.ReturnType {
93
97
  let previous: Cache | undefined
94
98
 
@@ -112,6 +116,26 @@ export declare namespace create {
112
116
  }
113
117
  }
114
118
 
119
+ type Extraction = {
120
+ dependencies: readonly string[]
121
+ extracted: Source.extract.ReturnType
122
+ inputs: ReadonlyMap<
123
+ string,
124
+ Source.extract.ReturnType | ReturnType<typeof Contract.read>
125
+ >
126
+ resolution: string
127
+ source: string
128
+ }
129
+
130
+ type Transformed = {
131
+ classes: Readonly<Record<string, string | undefined>>
132
+ extracted: Source.extract.ReturnType
133
+ owners: string
134
+ output: Transform.compile.ReturnType
135
+ schemes: boolean
136
+ themes: Readonly<Record<string, Theme.Definition>>
137
+ }
138
+
115
139
  type Cache = {
116
140
  compiler: boolean
117
141
  composition: Css.compile.Options['composition']
@@ -119,9 +143,12 @@ type Cache = {
119
143
 
120
144
  contracts: string
121
145
  development: boolean
146
+ entry: string | undefined
122
147
  extracted: ReadonlyMap<string, Source.extract.ReturnType>
123
148
  libraries: Readonly<Record<string, ReturnType<typeof Contract.read>>>
124
149
  reset: string | undefined
150
+ retained: ReadonlyMap<string, Extraction>
151
+ transformed: ReadonlyMap<string, Transformed>
125
152
  resolutions: Readonly<Record<string, string>>
126
153
  native: compile.Options['native']
127
154
  result: compile.ReturnType
@@ -195,14 +222,15 @@ function build(options: compile.Options, cache?: Cache): Cache {
195
222
  ]),
196
223
  )
197
224
 
198
- // File-set changes can alter extensionless resolution even without source edits.
225
+ // Host resolutions remain explicit when generated modules enter or leave the graph.
199
226
  const previous = (() => {
200
227
  if (
201
228
  cache &&
202
229
  cache.contracts === contracts &&
203
230
  cache.development === !!options.development &&
204
- ids.length === Object.keys(cache.sources).length &&
205
- ids.every((id) => Object.hasOwn(cache.sources, id))
231
+ (options.imports !== undefined ||
232
+ (ids.length === Object.keys(cache.sources).length &&
233
+ ids.every((id) => Object.hasOwn(cache.sources, id))))
206
234
  ) {
207
235
  return cache
208
236
  }
@@ -211,6 +239,8 @@ function build(options: compile.Options, cache?: Cache): Cache {
211
239
  })()
212
240
  if (
213
241
  previous &&
242
+ previous.entry === options[Stylesheets.entry] &&
243
+ ids.length === Object.keys(previous.sources).length &&
214
244
  Object.keys(resolutions).length ===
215
245
  Object.keys(previous.resolutions).length &&
216
246
  Object.entries(resolutions).every(
@@ -224,6 +254,8 @@ function build(options: compile.Options, cache?: Cache): Cache {
224
254
  )
225
255
  return previous
226
256
 
257
+ const retained = new Map(previous?.retained)
258
+ const transformed = new Map(previous?.transformed)
227
259
  const dependencies: Record<string, readonly string[]> = Object.create(null)
228
260
  const extracted = new Map<string, Source.extract.ReturnType>()
229
261
  const identifiers = new Map<string, Set<string>>()
@@ -365,29 +397,6 @@ function build(options: compile.Options, cache?: Cache): Cache {
365
397
  let direct: Ast.Node | undefined
366
398
  const stars: string[] = []
367
399
  for (const statement of program.body) {
368
- if (
369
- statement.type === 'ImportDeclaration' &&
370
- statement.importKind !== 'type'
371
- ) {
372
- const target = resolve(moduleId, statement.source.value, statement)
373
- if (!target) continue
374
- for (const specifier of statement.specifiers) {
375
- if (
376
- specifier.type === 'ImportNamespaceSpecifier' ||
377
- (specifier.type === 'ImportSpecifier' &&
378
- specifier.importKind === 'type')
379
- )
380
- continue
381
- const imported =
382
- specifier.type === 'ImportDefaultSpecifier'
383
- ? 'default'
384
- : specifier.imported.type === 'Identifier'
385
- ? specifier.imported.name
386
- : specifier.imported.value
387
- const value = constant(target, imported, next)
388
- if (value) imports[specifier.local.name] = relocate(value, specifier)
389
- }
390
- }
391
400
  if (statement.type === 'ExportDefaultDeclaration' && name === 'default') {
392
401
  direct = statement.declaration
393
402
  exported = true
@@ -436,7 +445,47 @@ function build(options: compile.Options, cache?: Cache): Cache {
436
445
  .filter((value) => value !== undefined),
437
446
  ),
438
447
  ]
439
- return candidates.length === 1 ? candidates[0] : undefined
448
+ const value = candidates.length === 1 ? candidates[0] : undefined
449
+ if (value || !stars.length) constants.set(key, value)
450
+ return value
451
+ }
452
+ if (
453
+ direct &&
454
+ [
455
+ 'FunctionDeclaration',
456
+ 'FunctionExpression',
457
+ 'ArrowFunctionExpression',
458
+ 'ClassDeclaration',
459
+ 'ClassExpression',
460
+ ].includes(direct.type)
461
+ ) {
462
+ constants.set(key, undefined)
463
+ return undefined
464
+ }
465
+ for (const statement of program.body) {
466
+ if (
467
+ statement.type === 'ImportDeclaration' &&
468
+ statement.importKind !== 'type'
469
+ ) {
470
+ const target = resolve(moduleId, statement.source.value, statement)
471
+ if (!target) continue
472
+ for (const specifier of statement.specifiers) {
473
+ if (
474
+ specifier.type === 'ImportNamespaceSpecifier' ||
475
+ (specifier.type === 'ImportSpecifier' &&
476
+ specifier.importKind === 'type')
477
+ )
478
+ continue
479
+ const imported =
480
+ specifier.type === 'ImportDefaultSpecifier'
481
+ ? 'default'
482
+ : specifier.imported.type === 'Identifier'
483
+ ? specifier.imported.name
484
+ : specifier.imported.value
485
+ const value = constant(target, imported, next)
486
+ if (value) imports[specifier.local.name] = relocate(value, specifier)
487
+ }
488
+ }
440
489
  }
441
490
  const scope = new Scope.Tracker({ preserveExitedScopes: true })
442
491
  Walker.walk(program, { scopeTracker: scope })
@@ -602,21 +651,34 @@ function build(options: compile.Options, cache?: Cache): Cache {
602
651
 
603
652
  const source = options.modules[moduleId]!
604
653
 
654
+ const snapshot =
655
+ previous?.retained.get(moduleId) ??
656
+ (previous && Object.hasOwn(previous.sources, moduleId)
657
+ ? {
658
+ dependencies: previous.result.dependencies[moduleId]!,
659
+ extracted: previous.extracted.get(moduleId)!,
660
+ inputs: new Map(
661
+ previous.result.dependencies[moduleId]!.map((target) => [
662
+ target,
663
+ previous.libraries[target] ?? previous.extracted.get(target)!,
664
+ ]),
665
+ ),
666
+ resolution: previous.resolutions[moduleId]!,
667
+ source: previous.sources[moduleId]!,
668
+ }
669
+ : undefined)
605
670
  if (
606
- previous &&
607
- source === previous.sources[moduleId] &&
608
- resolutions[moduleId] === previous.resolutions[moduleId] &&
609
- previous.result.dependencies[moduleId]!.every(
610
- (target) =>
611
- Object.hasOwn(libraries, target) ||
612
- visit(target) === previous.extracted.get(target),
671
+ snapshot &&
672
+ source === snapshot.source &&
673
+ resolutions[moduleId] === snapshot.resolution &&
674
+ snapshot.dependencies.every((target) =>
675
+ Object.hasOwn(libraries, target)
676
+ ? libraries[target] === snapshot.inputs.get(target)
677
+ : Object.hasOwn(options.modules, target) &&
678
+ visit(target) === snapshot.inputs.get(target),
613
679
  )
614
680
  ) {
615
- return retain(
616
- moduleId,
617
- previous.extracted.get(moduleId)!,
618
- previous.result.dependencies[moduleId]!,
619
- )
681
+ return retain(moduleId, snapshot.extracted, snapshot.dependencies)
620
682
  }
621
683
 
622
684
  // Validate identity and syntax through the public source boundary before linking.
@@ -957,6 +1019,20 @@ function build(options: compile.Options, cache?: Cache): Cache {
957
1019
 
958
1020
  extracted.set(moduleId, result)
959
1021
  dependencies[moduleId] = imports
1022
+ retained.delete(moduleId)
1023
+ retained.set(moduleId, {
1024
+ dependencies: imports,
1025
+ extracted: result,
1026
+ inputs: new Map(
1027
+ imports.map((target) => [
1028
+ target,
1029
+ libraries[target] ?? extracted.get(target)!,
1030
+ ]),
1031
+ ),
1032
+ resolution: resolutions[moduleId]!,
1033
+ source: options.modules[moduleId]!,
1034
+ })
1035
+ if (retained.size > 256) retained.delete(retained.keys().next().value!)
960
1036
 
961
1037
  for (const call of result.themeCalls)
962
1038
  for (const name of new Set([
@@ -1072,6 +1148,9 @@ function build(options: compile.Options, cache?: Cache): Cache {
1072
1148
  ...(options.native.units && { units: { ...options.native.units } }),
1073
1149
  },
1074
1150
  reset: options.reset,
1151
+ entry: options[Stylesheets.entry],
1152
+ retained,
1153
+ transformed,
1075
1154
  resolutions: Object.freeze(resolutions),
1076
1155
  result: Object.freeze({
1077
1156
  contracts: Object.freeze(
@@ -1248,16 +1327,58 @@ function build(options: compile.Options, cache?: Cache): Cache {
1248
1327
  }
1249
1328
 
1250
1329
  const sharedVisited = new Set<string>()
1330
+ const packed: Stylesheets.Resource[] = []
1251
1331
 
1252
1332
  const shared = (() => {
1253
1333
  try {
1254
- const sharedSections = ids.flatMap((id) => reachable(id, sharedVisited))
1334
+ const entry = options[Stylesheets.entry]
1335
+ const packedSections =
1336
+ entry === undefined
1337
+ ? []
1338
+ : (dependencies[entry] ?? [])
1339
+ .filter((id) => Object.hasOwn(libraries, id))
1340
+ .flatMap((id) => reachable(id, sharedVisited))
1341
+ const sharedSections =
1342
+ entry === undefined
1343
+ ? ids.flatMap((id) => reachable(id, sharedVisited))
1344
+ : (sections.get(entry) ?? [])
1345
+ if (packedSections.length) {
1346
+ // Validate combined ownership and ordering before splitting independently loaded resources.
1347
+ Stylesheets.render([...packedSections, ...sharedSections])
1348
+ for (const section of packedSections) {
1349
+ packed.push({
1350
+ id: JSON.stringify([section.owner, section.source, section.key]),
1351
+ ...Stylesheets.render([section]),
1352
+ })
1353
+ }
1354
+ }
1255
1355
  const resetSource =
1256
1356
  (options.reset === undefined ? undefined : 'zyzz/reset.css') ??
1257
1357
  resetOwners[0] ??
1258
1358
  sharedSections.find((section) => section.key === 'optional-reset-order')
1259
1359
  ?.source
1260
1360
 
1361
+ if (packed.length) {
1362
+ const order = Stylesheets.render([
1363
+ ...(resetSource === undefined
1364
+ ? []
1365
+ : [
1366
+ {
1367
+ source: resetSource,
1368
+ css: '',
1369
+ layers: layerNames.length
1370
+ ? layerNames.map((name) => ['reset', name])
1371
+ : [['reset']],
1372
+ },
1373
+ ]),
1374
+ ...[...packedSections, ...sharedSections].map((section) => ({
1375
+ ...section,
1376
+ css: '',
1377
+ })),
1378
+ ])
1379
+ if (order.css) packed.unshift({ ...order, id: order.css })
1380
+ }
1381
+
1261
1382
  return Stylesheets.render([
1262
1383
  ...(resetSource === undefined
1263
1384
  ? []
@@ -1406,12 +1527,26 @@ function build(options: compile.Options, cache?: Cache): Cache {
1406
1527
  ),
1407
1528
  )
1408
1529
 
1530
+ const ownerSignature = JSON.stringify(owners)
1531
+
1409
1532
  // Extraction visits dependencies first; their emitted classes must precede consumers.
1410
1533
  for (const moduleId of extracted.keys()) {
1411
- modules[moduleId] =
1412
- sameThemes &&
1413
- previous!.schemes === schemes &&
1414
- extracted.get(moduleId) === previous!.extracted.get(moduleId)
1534
+ const cached = transformed.get(moduleId)
1535
+ const reusable =
1536
+ cached &&
1537
+ cached.extracted === extracted.get(moduleId) &&
1538
+ cached.schemes === schemes &&
1539
+ Object.keys(cached.themes).length === names.length &&
1540
+ names.every((name) => cached.themes[name] === themes[name]) &&
1541
+ Object.entries(cached.classes).every(
1542
+ ([name, value]) => styleClasses[name] === value,
1543
+ ) &&
1544
+ cached.owners === ownerSignature
1545
+ modules[moduleId] = reusable
1546
+ ? cached.output
1547
+ : sameThemes &&
1548
+ previous!.schemes === schemes &&
1549
+ extracted.get(moduleId) === previous!.extracted.get(moduleId)
1415
1550
  ? previous!.result.modules[moduleId]!
1416
1551
  : Transform.compile({
1417
1552
  compiler: options.compiler,
@@ -1422,6 +1557,7 @@ function build(options: compile.Options, cache?: Cache): Cache {
1422
1557
  schemes,
1423
1558
  source: options.modules[moduleId]!,
1424
1559
  [Themes.context]: {
1560
+ parsed: parse({ moduleId, source: options.modules[moduleId]! }),
1425
1561
  extracted: Object.freeze({
1426
1562
  ...extracted.get(moduleId)!,
1427
1563
  vars: sharedThemes,
@@ -1432,13 +1568,39 @@ function build(options: compile.Options, cache?: Cache): Cache {
1432
1568
  styleClasses,
1433
1569
  },
1434
1570
  })
1571
+ transformed.delete(moduleId)
1572
+ transformed.set(moduleId, {
1573
+ classes: Object.fromEntries(
1574
+ extracted
1575
+ .get(moduleId)!
1576
+ .calls.flatMap((call) =>
1577
+ (call.runtimeComposition ?? []).map((input) => [
1578
+ input.name,
1579
+ styleClasses[input.name],
1580
+ ]),
1581
+ ),
1582
+ ),
1583
+ extracted: extracted.get(moduleId)!,
1584
+ owners: ownerSignature,
1585
+ output: modules[moduleId]!,
1586
+ schemes,
1587
+ themes: sharedThemes,
1588
+ })
1589
+ if (transformed.size > 256)
1590
+ transformed.delete(transformed.keys().next().value!)
1435
1591
  // Module-local checks cannot detect truncated ownership hashes colliding across files.
1592
+ const emitted = new Set(
1593
+ Array.from(
1594
+ modules[moduleId]!.css.matchAll(/\.(z-[\w-]+)\{/g),
1595
+ (match) => match[1],
1596
+ ),
1597
+ )
1436
1598
  for (const value of Object.values(modules[moduleId]!.classes))
1437
1599
  for (const name of value.split(' ')) {
1438
1600
  if (
1439
1601
  options.compiler === false ||
1440
1602
  !name.startsWith('z-') ||
1441
- !modules[moduleId]!.css.includes(`.${name}{`)
1603
+ !emitted.has(name)
1442
1604
  )
1443
1605
  continue
1444
1606
 
@@ -1531,8 +1693,12 @@ function build(options: compile.Options, cache?: Cache): Cache {
1531
1693
  libraries: Object.freeze(libraries),
1532
1694
  native: undefined,
1533
1695
  reset: options.reset,
1696
+ entry: options[Stylesheets.entry],
1697
+ retained,
1698
+ transformed,
1534
1699
  resolutions: Object.freeze(resolutions),
1535
1700
  result: Object.freeze({
1701
+ ...(packed.length ? { [Stylesheets.packed]: packed } : {}),
1536
1702
  ...(sharedCss
1537
1703
  ? {
1538
1704
  sharedCss,
@@ -1543,7 +1709,7 @@ function build(options: compile.Options, cache?: Cache): Cache {
1543
1709
  : {}),
1544
1710
  contracts: Object.freeze(
1545
1711
  Object.fromEntries(
1546
- ids
1712
+ (options[Stylesheets.entry] === undefined ? ids : [])
1547
1713
  .filter(
1548
1714
  (id) =>
1549
1715
  Object.keys(extracted.get(id)!.themeExports ?? {}).length ||
@@ -96,7 +96,8 @@ export function compile(options: compile.Options): compile.ReturnType {
96
96
  }
97
97
 
98
98
  const module = new MagicString(options.source)
99
- const program = Syntax.parse(options).program
99
+ const program = (options[Themes.context]?.parsed ?? Syntax.parse(options))
100
+ .program
100
101
 
101
102
  for (const call of extracted.contributionCalls ?? [])
102
103
  module.overwrite(
@@ -17,6 +17,15 @@ export class ConflictError extends Error {
17
17
  override name = 'Stylesheets.ConflictError'
18
18
  }
19
19
 
20
+ /** Selects module-owned effects when the host loads source dependencies separately. */
21
+ export const entry = Symbol('stylesheet entry')
22
+
23
+ /** Independently shared packed sections for hosts that load source modules separately. */
24
+ export const packed = Symbol('packed stylesheets')
25
+
26
+ /** One packed resource with stable source ownership for development updates. */
27
+ export type Resource = ReturnType<typeof render> & { readonly id: string }
28
+
20
29
  /** Ordered CSS owned by one portable source module. */
21
30
  export type Section = {
22
31
  /** Isolated selector namespaces belonging to the source owner. */
@@ -29,7 +29,10 @@ export type Accepted<style, properties> = {
29
29
  }
30
30
 
31
31
  type AcceptedValue<value, property extends PropertyKey, properties> =
32
- Extract<value, `[${string}]` | `[${string}] !important`> extends never
32
+ Extract<
33
+ value,
34
+ `${string} !custom` | `${string} !custom !important`
35
+ > extends never
33
36
  ? AcceptedMember<Record<property, value>, properties>[property]
34
37
  : value extends unknown
35
38
  ? AcceptedMember<Record<property, value>, properties>[property]
@@ -37,22 +40,15 @@ type AcceptedValue<value, property extends PropertyKey, properties> =
37
40
 
38
41
  type AcceptedMember<style, properties> = {
39
42
  [property in keyof style]: property extends keyof properties
40
- ? style[property] extends `[${infer value}]` | `[${infer value}] !important`
41
- ?
42
- | (property extends 'gridTemplateColumns' | 'gridTemplateRows'
43
- ? style[property] extends LiteralAccepted<
44
- Pick<style, property>,
45
- Literal.Properties
46
- >[property]
47
- ? style[property]
48
- : never
49
- : never)
50
- | (ScalarValue<value, property> extends LiteralAccepted<
51
- Record<property, ScalarValue<value, property>>,
52
- Literal.Properties
53
- >[property]
54
- ? style[property]
55
- : never)
43
+ ? style[property] extends
44
+ | `${infer value} !custom`
45
+ | `${infer value} !custom !important`
46
+ ? ScalarValue<value, property> extends LiteralAccepted<
47
+ Record<property, ScalarValue<value, property>>,
48
+ Literal.Properties
49
+ >[property]
50
+ ? style[property]
51
+ : never
56
52
  : style[property] extends readonly [unknown, ...unknown[]]
57
53
  ? AcceptedArray<style[property], property, properties>
58
54
  : LiteralAccepted<Pick<style, property>, properties>[property]
@@ -135,15 +131,11 @@ type Fold<value> = value extends string
135
131
  /** Refines concrete scalar spellings; already-broad property contracts need no literal refinement. */
136
132
  export type Checked<style, tokens = {}> = {
137
133
  [property in keyof style]: style[property] extends
138
- | `[${infer value}]`
139
- | `[${infer value}] !important`
140
- ?
141
- | (property extends 'gridTemplateColumns' | 'gridTemplateRows'
142
- ? LiteralChecked<Pick<style, property>>[property]
143
- : never)
144
- | LiteralChecked<
145
- Record<property, ScalarValue<value, property>>
146
- >[property]
134
+ | `${infer value} !custom`
135
+ | `${infer value} !custom !important`
136
+ ? value extends `${string} !${'custom' | 'important'}`
137
+ ? never
138
+ : LiteralChecked<Record<property, ScalarValue<value, property>>>[property]
147
139
  : style[property] extends readonly [unknown, ...unknown[]]
148
140
  ? CheckedArray<style[property], property, tokens>
149
141
  : LiteralChecked<Pick<style, property>, tokens>[property]
@@ -214,8 +206,8 @@ export type Tokens<
214
206
  | Token.Reference
215
207
  | Token.Variable
216
208
  | `${Token.Variable} !important`
217
- | `[${string}]`
218
- | `[${string}] !important`
209
+ | `${string} !custom`
210
+ | `${string} !custom !important`
219
211
  ? unknown
220
212
  : value extends Atom<Token.Names<tokens, property>>
221
213
  ? unknown
@@ -361,7 +353,7 @@ export type Fallbacks<atom> = atom | readonly [atom, ...atom[]]
361
353
  /** One declaration or a nonempty ordered sequence of declaration fallbacks. */
362
354
  export type Input<value> = Fallbacks<Atom<Exclude<value, undefined>>>
363
355
 
364
- /** Splits a trailing importance marker without interpreting quoted or escaped text. */
356
+ /** Splits trailing custom and importance markers without interpreting quoted or escaped text. */
365
357
  export function parse(
366
358
  input: unknown,
367
359
  property: keyof Literal.Properties,
@@ -383,16 +375,6 @@ export function parse(
383
375
 
384
376
  const parts = [...input.parts]
385
377
  let remaining = text.length - String(parsed.value).length
386
- if (parsed.custom) {
387
- for (let index = 0; index < parts.length; index++) {
388
- const part = parts[index]
389
- if (part === '') continue
390
- if (typeof part !== 'string') return undefined
391
- parts[index] = part.slice(1)
392
- break
393
- }
394
- remaining--
395
- }
396
378
 
397
379
  for (let index = parts.length - 1; remaining > 0 && index >= 0; index--) {
398
380
  const part = parts[index]
@@ -434,37 +416,38 @@ export function parse(
434
416
  '',
435
417
  )
436
418
 
437
- if (!escaped(marker) && (suffix === '' || suffix === 'important')) break
419
+ if (
420
+ !escaped(marker) &&
421
+ (suffix === '' || suffix === 'important' || suffix === 'custom')
422
+ )
423
+ break
438
424
 
439
425
  marker = marker === 0 ? -1 : input.lastIndexOf('!', marker - 1)
440
426
  }
441
427
 
442
- if (marker < 0) {
443
- if (bracketed(input))
444
- return {
445
- custom: true,
446
- important: false,
447
- value: scalar(input.slice(1, -1)),
448
- }
449
- return undefined
450
- }
451
- if (input.slice(marker - 1) !== ' !important' || escaped(marker - 1))
428
+ if (marker < 0) return undefined
429
+ const suffix = input.slice(marker - 1)
430
+ if (
431
+ (suffix !== ' !important' && suffix !== ' !custom') ||
432
+ escaped(marker - 1)
433
+ )
452
434
  return { invalid: true }
453
435
 
454
436
  let end = marker
455
-
456
437
  while (end > 0 && /[ \t\n\r\f]/.test(input[end - 1]!) && !escaped(end - 1))
457
438
  end--
458
439
 
459
440
  const body = input.slice(0, end)
460
- const custom = bracketed(body)
461
- const text = custom ? body.slice(1, -1) : body
462
- const value = scalar(text)
441
+ if (!body) return { invalid: true }
442
+ const nested = parse(body, property)
443
+ if (nested && 'invalid' in nested) return nested
444
+ const custom = suffix === ' !custom'
445
+ if (nested && (custom || nested.important)) return { invalid: true }
463
446
 
464
447
  return {
465
- ...(custom ? { custom: true } : {}),
466
- important: true as const,
467
- value,
448
+ ...(custom || nested?.custom ? { custom: true } : {}),
449
+ important: !custom,
450
+ value: nested?.value ?? scalar(body),
468
451
  }
469
452
 
470
453
  function scalar(text: string): number | string {
@@ -476,34 +459,3 @@ export function parse(
476
459
  : text
477
460
  }
478
461
  }
479
-
480
- /** Recognizes one enclosing pair without consuming native grid line lists. */
481
- function bracketed(value: string): boolean {
482
- if (!value.startsWith('[') || !value.endsWith(']')) return false
483
- let depth = 0
484
- let quote = ''
485
- for (let index = 0; index < value.length; index++) {
486
- const character = value[index]!
487
- if (character === '\\') {
488
- index++
489
- continue
490
- }
491
- if (quote) {
492
- if (character === quote) quote = ''
493
- continue
494
- }
495
- if (character === '/' && value[index + 1] === '*') {
496
- const end = value.indexOf('*/', index + 2)
497
- if (end < 0) return false
498
- index = end + 1
499
- continue
500
- }
501
- if (character === '"' || character === "'") {
502
- quote = character
503
- continue
504
- }
505
- if (character === '[') depth++
506
- if (character === ']' && --depth === 0) return index === value.length - 1
507
- }
508
- return false
509
- }
@@ -1,7 +1,7 @@
1
1
  /** Reads a closed local source graph for native authoring without evaluating modules. @module */
2
2
  import * as Fs from 'node:fs'
3
3
  import * as Path from 'node:path'
4
- import * as Parser from 'oxc-parser'
4
+ import * as Snapshot from '../node/internal/Snapshot.js'
5
5
  import type * as Graph from '../compiler/Graph.js'
6
6
 
7
7
  /** Collects relative source imports; package and asset imports remain owned by Metro. */
@@ -10,6 +10,7 @@ export function read(
10
10
  source: string,
11
11
  platform: string,
12
12
  root: string,
13
+ snapshot = Snapshot.create(),
13
14
  ) {
14
15
  const files = new Set<string>()
15
16
  const modules: Record<string, string> = Object.create(null)
@@ -31,7 +32,8 @@ export function read(
31
32
  modules[id] = source
32
33
  const resolved: Record<string, string | null> = Object.create(null)
33
34
  imports[id] = resolved
34
- for (const statement of Parser.parseSync(filename, source).program.body) {
35
+ for (const statement of snapshot.parse({ moduleId: id, source }).program
36
+ .body) {
35
37
  if (
36
38
  !('source' in statement) ||
37
39
  !statement.source ||
@@ -62,7 +64,7 @@ export function read(
62
64
  )
63
65
  if (!target || !/\.[cm]?[jt]sx?$/.test(target)) continue
64
66
  resolved[specifier] = identity(target)
65
- visit(target, Fs.readFileSync(target, 'utf8'))
67
+ visit(target, snapshot.readSync(target))
66
68
  }
67
69
  }
68
70
  visit(filename, source)