zyzz 0.0.7 → 0.0.8

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 (67) hide show
  1. package/dist/babel/index.d.ts +4 -1
  2. package/dist/babel/index.d.ts.map +1 -1
  3. package/dist/babel/index.js +6 -3
  4. package/dist/babel/index.js.map +1 -1
  5. package/dist/babel/internal/Compilation.d.ts +12 -0
  6. package/dist/babel/internal/Compilation.d.ts.map +1 -0
  7. package/dist/babel/internal/Compilation.js +3 -0
  8. package/dist/babel/internal/Compilation.js.map +1 -0
  9. package/dist/compiler/Graph.d.ts +6 -1
  10. package/dist/compiler/Graph.d.ts.map +1 -1
  11. package/dist/compiler/Graph.js +160 -36
  12. package/dist/compiler/Graph.js.map +1 -1
  13. package/dist/compiler/Transform.d.ts.map +1 -1
  14. package/dist/compiler/Transform.js +2 -1
  15. package/dist/compiler/Transform.js.map +1 -1
  16. package/dist/compiler/internal/Stylesheets.d.ts +8 -0
  17. package/dist/compiler/internal/Stylesheets.d.ts.map +1 -1
  18. package/dist/compiler/internal/Stylesheets.js +4 -0
  19. package/dist/compiler/internal/Stylesheets.js.map +1 -1
  20. package/dist/metro/Graph.d.ts +9 -1
  21. package/dist/metro/Graph.d.ts.map +1 -1
  22. package/dist/metro/Graph.js +5 -4
  23. package/dist/metro/Graph.js.map +1 -1
  24. package/dist/metro/transformer.d.ts +0 -1
  25. package/dist/metro/transformer.d.ts.map +1 -1
  26. package/dist/metro/transformer.js +19 -1
  27. package/dist/metro/transformer.js.map +1 -1
  28. package/dist/next/index.d.ts +3 -3
  29. package/dist/next/index.d.ts.map +1 -1
  30. package/dist/next/index.js +25 -30
  31. package/dist/next/index.js.map +1 -1
  32. package/dist/next/internal/Project.d.ts +4 -4
  33. package/dist/next/internal/Project.d.ts.map +1 -1
  34. package/dist/next/internal/Project.js +153 -107
  35. package/dist/next/internal/Project.js.map +1 -1
  36. package/dist/next/loader.d.ts +3 -1
  37. package/dist/next/loader.d.ts.map +1 -1
  38. package/dist/next/loader.js +152 -99
  39. package/dist/next/loader.js.map +1 -1
  40. package/dist/node/Host.d.ts.map +1 -1
  41. package/dist/node/Host.js +11 -3
  42. package/dist/node/Host.js.map +1 -1
  43. package/dist/node/internal/Snapshot.d.ts +12 -0
  44. package/dist/node/internal/Snapshot.d.ts.map +1 -0
  45. package/dist/node/internal/Snapshot.js +74 -0
  46. package/dist/node/internal/Snapshot.js.map +1 -0
  47. package/dist/unplugin/index.d.ts.map +1 -1
  48. package/dist/unplugin/index.js +10 -5
  49. package/dist/unplugin/index.js.map +1 -1
  50. package/dist/vite/index.d.ts.map +1 -1
  51. package/dist/vite/index.js +19 -4
  52. package/dist/vite/index.js.map +1 -1
  53. package/package.json +1 -1
  54. package/src/babel/index.ts +10 -4
  55. package/src/babel/internal/Compilation.ts +13 -0
  56. package/src/compiler/Graph.ts +213 -47
  57. package/src/compiler/Transform.ts +2 -1
  58. package/src/compiler/internal/Stylesheets.ts +9 -0
  59. package/src/metro/Graph.ts +5 -3
  60. package/src/metro/transformer.ts +26 -1
  61. package/src/next/index.ts +26 -31
  62. package/src/next/internal/Project.ts +196 -155
  63. package/src/next/loader.ts +181 -124
  64. package/src/node/Host.ts +15 -3
  65. package/src/node/internal/Snapshot.ts +79 -0
  66. package/src/unplugin/index.ts +10 -5
  67. package/src/vite/index.ts +24 -4
@@ -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. */
@@ -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)
@@ -1,4 +1,7 @@
1
1
  /** Chains native authoring compilation into the configured Metro Babel transformer. @module */
2
+ import * as Compilation from '../babel/internal/Compilation.js'
3
+ import * as Compiler from '../compiler/Graph.js'
4
+ import * as Snapshot from '../node/internal/Snapshot.js'
2
5
  import type * as Babel from '@babel/core'
3
6
  import * as Crypto from 'node:crypto'
4
7
  import * as Fs from 'node:fs'
@@ -28,6 +31,8 @@ export function create(
28
31
  'platform' | 'target' | 'moduleId' | 'modules' | 'imports' | 'colorScheme'
29
32
  > & { readonly root: string },
30
33
  ) {
34
+ const compilers = new Map<string, ReturnType<typeof Compiler.create>>()
35
+ const snapshot = Snapshot.create()
31
36
  const upstream: Upstream = Module.createRequire(import.meta.url)(upstreamPath)
32
37
  return {
33
38
  getCacheKey(...args: readonly unknown[]) {
@@ -77,6 +82,21 @@ export function create(
77
82
  Path.relative(options.root, filename).startsWith(`..${Path.sep}`)
78
83
  )
79
84
  return upstream.transform(input)
85
+
86
+ const graph = Graph.read(
87
+ filename,
88
+ input.src,
89
+ platform,
90
+ options.root,
91
+ snapshot,
92
+ )
93
+ const key = `${platform}:${filename}`
94
+ let compiler = compilers.get(key)
95
+ if (!compiler) {
96
+ compiler = Compiler.create()
97
+ compilers.set(key, compiler)
98
+ }
99
+
80
100
  return upstream.transform({
81
101
  ...input,
82
102
  plugins: [
@@ -86,7 +106,12 @@ export function create(
86
106
  ...options,
87
107
  platform,
88
108
  target: 'native',
89
- ...Graph.read(filename, input.src, platform, options.root),
109
+ ...graph,
110
+ [Compilation.key]: {
111
+ compile: compiler.compile,
112
+ parse: snapshot.parse,
113
+ programs: snapshot.programs(graph.modules),
114
+ },
90
115
  },
91
116
  ],
92
117
  ...(input.plugins ?? []),
package/src/next/index.ts CHANGED
@@ -5,9 +5,9 @@ import * as Module from 'node:module'
5
5
  import * as Path from 'node:path'
6
6
 
7
7
  /**
8
- * Adds source transformation, CSS delivery, and project dependency watching to both bundlers.
9
- * Preserves existing configuration hooks and creates `.zyzz/next` beneath the working directory.
10
- * Throws file-system errors during setup; compilation errors are reported by the bundler.
8
+ * Adds source transformation, CSS delivery, and imported dependency watching to both bundlers.
9
+ * Preserves configuration hooks. Compilation emits stylesheets beneath `.zyzz/next`.
10
+ * Compilation and file-system errors are reported by the bundler.
11
11
  */
12
12
  export function zyzz(config: NextConfig, options?: zyzz.Options): NextConfig
13
13
  export function zyzz(config: zyzz.Factory, options?: zyzz.Options): zyzz.Factory
@@ -27,24 +27,32 @@ export function zyzz(
27
27
  const loader = Module.createRequire(import.meta.url).resolve(
28
28
  'zyzz/next/loader',
29
29
  )
30
- const loaderOptions = { reset: options.reset ?? false, root: process.cwd() }
31
- const shared = Path.join(loaderOptions.root, '.zyzz', 'next', 'shared.css')
32
- Fs.mkdirSync(Path.dirname(shared), { recursive: true })
33
- try {
34
- Fs.writeFileSync(shared, '', { flag: 'wx' })
35
- } catch (error) {
36
- if ((error as NodeJS.ErrnoException).code !== 'EEXIST') throw error
30
+ const loaderOptions = {
31
+ development: process.env.NODE_ENV === 'development',
32
+ reset: options.reset ?? false,
33
+ root: process.cwd(),
34
+ }
35
+ // Turbopack must observe the output directory before taking its filesystem snapshot.
36
+ const directory = Path.join(loaderOptions.root, '.zyzz', 'next')
37
+ Fs.mkdirSync(directory, { recursive: true })
38
+ for (const [name, source] of Object.entries({
39
+ 'package.json': JSON.stringify({ sideEffects: true, type: 'module' }),
40
+ 'style.css': '',
41
+ })) {
42
+ try {
43
+ Fs.writeFileSync(Path.join(directory, name), source, { flag: 'wx' })
44
+ } catch (error) {
45
+ if ((error as NodeJS.ErrnoException).code !== 'EEXIST') throw error
46
+ }
37
47
  }
38
48
  const pattern = '*.{ts,tsx,js,jsx,mts,cts,mjs,cjs}'
39
49
  const existing = config.turbopack?.rules?.[pattern]
40
50
  const rule = {
41
- condition: {
42
- all: [{ not: 'foreign' as const }, { not: { query: /zyzz-style/ } }],
43
- },
51
+ condition: { not: 'foreign' as const },
44
52
  loaders: [
45
53
  {
46
54
  loader,
47
- options: { ...loaderOptions, bundler: 'turbopack', mode: 'source' },
55
+ options: { ...loaderOptions, bundler: 'turbopack' },
48
56
  },
49
57
  ],
50
58
  }
@@ -70,29 +78,16 @@ export function zyzz(
70
78
  [pattern]: [
71
79
  ...(Array.isArray(existing) ? existing : existing ? [existing] : []),
72
80
  rule,
73
- {
74
- condition: { query: /zyzz-style/ },
75
- loaders: [
76
- {
77
- loader,
78
- options: {
79
- ...loaderOptions,
80
- bundler: 'turbopack',
81
- mode: 'style',
82
- },
83
- },
84
- ],
85
- as: '*.css',
86
- },
87
81
  ],
88
- '**/.zyzz/next/shared.css': {
82
+ '**/.zyzz/next/style.css': {
83
+ condition: { query: /zyzz=/ },
89
84
  loaders: [
90
85
  {
91
86
  loader,
92
87
  options: {
93
88
  ...loaderOptions,
94
89
  bundler: 'turbopack',
95
- mode: 'shared',
90
+ mode: 'style',
96
91
  },
97
92
  },
98
93
  ],
@@ -111,7 +106,7 @@ export function zyzz(
111
106
  use: [
112
107
  {
113
108
  loader,
114
- options: { ...loaderOptions, bundler: 'webpack', mode: 'source' },
109
+ options: { ...loaderOptions, bundler: 'webpack' },
115
110
  },
116
111
  ],
117
112
  })