@warlock.js/web 5.1.0 → 5.2.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 (219) hide show
  1. package/CHANGELOG.md +174 -0
  2. package/README.md +126 -12
  3. package/esm/build/contribution.d.mts +11 -14
  4. package/esm/build/contribution.mjs +46 -58
  5. package/esm/build/contribution.mjs.map +1 -1
  6. package/esm/build/discover-pages.mjs +105 -45
  7. package/esm/build/discover-pages.mjs.map +1 -1
  8. package/esm/build/generate-client-registry.mjs +47 -13
  9. package/esm/build/generate-client-registry.mjs.map +1 -1
  10. package/esm/build/generate-pages-barrel.mjs +55 -13
  11. package/esm/build/generate-pages-barrel.mjs.map +1 -1
  12. package/esm/build/page-default-export.mjs +48 -0
  13. package/esm/build/page-default-export.mjs.map +1 -0
  14. package/esm/build/page-routes-manifest.mjs +21 -0
  15. package/esm/build/page-routes-manifest.mjs.map +1 -0
  16. package/esm/build/public-files.mjs +45 -0
  17. package/esm/build/public-files.mjs.map +1 -0
  18. package/esm/build/read-route-exports.mjs +1 -1
  19. package/esm/build/read-route-exports.mjs.map +1 -1
  20. package/esm/client/build-hydrated-tree.mjs +40 -9
  21. package/esm/client/build-hydrated-tree.mjs.map +1 -1
  22. package/esm/client/hydrate-page.mjs +1 -1
  23. package/esm/client/hydrate-page.mjs.map +1 -1
  24. package/esm/client/navigation/current-route.d.mts +1 -1
  25. package/esm/client/navigation/current-route.mjs +1 -1
  26. package/esm/client/navigation/current-route.mjs.map +1 -1
  27. package/esm/client/navigation/fetch-page-data.mjs +1 -1
  28. package/esm/client/navigation/fetch-page-data.mjs.map +1 -1
  29. package/esm/client/navigation/navigation-root.mjs +1 -1
  30. package/esm/client/navigation/navigation-root.mjs.map +1 -1
  31. package/esm/client/navigation/prefetch.mjs +1 -1
  32. package/esm/client/navigation/prefetch.mjs.map +1 -1
  33. package/esm/client/navigation/refresh.d.mts +1 -1
  34. package/esm/client/navigation/refresh.mjs +1 -1
  35. package/esm/client/navigation/refresh.mjs.map +1 -1
  36. package/esm/client/navigation/scroll-to-fragment.mjs +1 -1
  37. package/esm/client/navigation/scroll-to-fragment.mjs.map +1 -1
  38. package/esm/client/navigation/verbs.d.mts +1 -1
  39. package/esm/client/navigation/verbs.mjs +1 -1
  40. package/esm/client/navigation/verbs.mjs.map +1 -1
  41. package/esm/client/runtime/index.d.mts +2 -1
  42. package/esm/client/runtime/index.mjs +2 -1
  43. package/esm/client/runtime/manifest.d.mts +1 -1
  44. package/esm/client/runtime/manifest.mjs +3 -2
  45. package/esm/client/runtime/manifest.mjs.map +1 -1
  46. package/esm/client/runtime/matcher.d.mts +13 -1
  47. package/esm/client/runtime/matcher.mjs +13 -1
  48. package/esm/client/runtime/matcher.mjs.map +1 -1
  49. package/esm/client/runtime/types.d.mts +15 -2
  50. package/esm/components/default-app.mjs +1 -1
  51. package/esm/components/default-app.mjs.map +1 -1
  52. package/esm/components/document-context.d.mts +40 -2
  53. package/esm/components/document-context.mjs +1 -1
  54. package/esm/components/document-context.mjs.map +1 -1
  55. package/esm/components/head.d.mts +1 -1
  56. package/esm/components/head.mjs +1 -1
  57. package/esm/components/head.mjs.map +1 -1
  58. package/esm/components/link.d.mts +1 -1
  59. package/esm/components/link.mjs +1 -1
  60. package/esm/components/link.mjs.map +1 -1
  61. package/esm/components/scripts.d.mts +1 -1
  62. package/esm/components/scripts.mjs +4 -2
  63. package/esm/components/scripts.mjs.map +1 -1
  64. package/esm/context.d.mts +6 -102
  65. package/esm/hydration/index.mjs +1 -1
  66. package/esm/hydration/index.mjs.map +1 -1
  67. package/esm/hydration-payload.d.mts +1 -1
  68. package/esm/hydration-payload.mjs +37 -8
  69. package/esm/hydration-payload.mjs.map +1 -1
  70. package/esm/index.d.mts +4 -3
  71. package/esm/loaders.d.mts +7 -36
  72. package/esm/metadata.d.mts +1 -1
  73. package/esm/metadata.mjs +1 -1
  74. package/esm/metadata.mjs.map +1 -1
  75. package/esm/props.d.mts +16 -8
  76. package/esm/route.d.mts +2 -17
  77. package/esm/routing/compose-route-path.d.mts +1 -1
  78. package/esm/routing/compose-route-path.mjs +1 -1
  79. package/esm/routing/compose-route-path.mjs.map +1 -1
  80. package/esm/routing/data-request.mjs +1 -1
  81. package/esm/routing/data-request.mjs.map +1 -1
  82. package/esm/routing/filesystem-route.mjs +45 -0
  83. package/esm/routing/filesystem-route.mjs.map +1 -0
  84. package/esm/routing/layout-policy.mjs +1 -1
  85. package/esm/routing/layout-policy.mjs.map +1 -1
  86. package/esm/routing/navigator.mjs +1 -1
  87. package/esm/routing/navigator.mjs.map +1 -1
  88. package/esm/routing/query-string.d.mts +1 -1
  89. package/esm/routing/query-string.mjs +1 -1
  90. package/esm/routing/query-string.mjs.map +1 -1
  91. package/esm/routing/route-identity.mjs +1 -1
  92. package/esm/routing/route-identity.mjs.map +1 -1
  93. package/esm/routing/route-table.d.mts +1 -1
  94. package/esm/routing/route-table.mjs +1 -1
  95. package/esm/routing/route-table.mjs.map +1 -1
  96. package/esm/routing/router-events.d.mts +1 -1
  97. package/esm/routing/router-events.mjs +1 -1
  98. package/esm/routing/router-events.mjs.map +1 -1
  99. package/esm/routing/url-fragment.mjs +1 -1
  100. package/esm/routing/url-fragment.mjs.map +1 -1
  101. package/esm/runtime/register-modules.d.mts +19 -0
  102. package/esm/runtime/register-modules.mjs +22 -0
  103. package/esm/runtime/register-modules.mjs.map +1 -0
  104. package/esm/server/build-hydration-payload.mjs +2 -1
  105. package/esm/server/build-hydration-payload.mjs.map +1 -1
  106. package/esm/server/client-asset-url-prefix.d.mts +1 -1
  107. package/esm/server/client-asset-url-prefix.mjs +1 -1
  108. package/esm/server/client-asset-url-prefix.mjs.map +1 -1
  109. package/esm/server/create-page-module-loader.d.mts +1 -1
  110. package/esm/server/create-page-module-loader.mjs +2 -1
  111. package/esm/server/create-page-module-loader.mjs.map +1 -1
  112. package/esm/server/create-page-route-handler.d.mts +31 -6
  113. package/esm/server/create-page-route-handler.mjs +128 -43
  114. package/esm/server/create-page-route-handler.mjs.map +1 -1
  115. package/esm/server/dev-server.mjs +2 -13
  116. package/esm/server/dev-server.mjs.map +1 -1
  117. package/esm/server/error-page.d.mts +14 -0
  118. package/esm/server/error-page.mjs +39 -0
  119. package/esm/server/error-page.mjs.map +1 -0
  120. package/esm/server/execute-page-request.d.mts +7 -4
  121. package/esm/server/execute-page-request.mjs +79 -80
  122. package/esm/server/execute-page-request.mjs.map +1 -1
  123. package/esm/server/execute-page-request.types.d.mts +33 -92
  124. package/esm/server/hydration-client-url.d.mts +1 -1
  125. package/esm/server/hydration-client-url.mjs +2 -2
  126. package/esm/server/hydration-client-url.mjs.map +1 -1
  127. package/esm/server/index.d.mts +5 -5
  128. package/esm/server/index.mjs +3 -4
  129. package/esm/server/install-page-routes-from-manifest.d.mts +17 -7
  130. package/esm/server/install-page-routes-from-manifest.mjs +71 -10
  131. package/esm/server/install-page-routes-from-manifest.mjs.map +1 -1
  132. package/esm/server/install-page-routes.d.mts +32 -15
  133. package/esm/server/install-page-routes.mjs +91 -39
  134. package/esm/server/install-page-routes.mjs.map +1 -1
  135. package/esm/server/install-production-page-routes.mjs +3 -36
  136. package/esm/server/install-production-page-routes.mjs.map +1 -1
  137. package/esm/server/match-page-route.mjs +2 -2
  138. package/esm/server/match-page-route.mjs.map +1 -1
  139. package/esm/server/not-found-page.d.mts +1 -1
  140. package/esm/server/not-found-page.mjs +2 -2
  141. package/esm/server/not-found-page.mjs.map +1 -1
  142. package/esm/server/page-context.d.mts +3 -9
  143. package/esm/server/page-context.mjs +3 -16
  144. package/esm/server/page-context.mjs.map +1 -1
  145. package/esm/server/page-file-change.mjs +77 -0
  146. package/esm/server/page-file-change.mjs.map +1 -0
  147. package/esm/server/page-manifest.d.mts +25 -10
  148. package/esm/server/page-manifest.mjs +1 -1
  149. package/esm/server/page-manifest.mjs.map +1 -1
  150. package/esm/server/page-render-bundle.mjs +38 -0
  151. package/esm/server/page-render-bundle.mjs.map +1 -0
  152. package/esm/server/page-route-reload.mjs +54 -0
  153. package/esm/server/page-route-reload.mjs.map +1 -0
  154. package/esm/server/register-production-public-files.mjs +40 -0
  155. package/esm/server/register-production-public-files.mjs.map +1 -0
  156. package/esm/server/render-page.d.mts +11 -14
  157. package/esm/server/render-page.mjs +154 -28
  158. package/esm/server/render-page.mjs.map +1 -1
  159. package/esm/server/resolve-page-metadata.mjs +2 -2
  160. package/esm/server/resolve-page-metadata.mjs.map +1 -1
  161. package/esm/server/resolve-validation-data.mjs +1 -1
  162. package/esm/server/resolve-validation-data.mjs.map +1 -1
  163. package/esm/server/settle-page-response.d.mts +58 -1
  164. package/esm/server/settle-page-response.mjs +111 -46
  165. package/esm/server/settle-page-response.mjs.map +1 -1
  166. package/esm/server/stylesheet-urls.d.mts +41 -30
  167. package/esm/server/stylesheet-urls.mjs +137 -52
  168. package/esm/server/stylesheet-urls.mjs.map +1 -1
  169. package/esm/server/unregistered-pages.mjs +42 -0
  170. package/esm/server/unregistered-pages.mjs.map +1 -0
  171. package/esm/server/web-connector-factory.d.mts +2 -3
  172. package/esm/server/web-connector-factory.mjs +7 -6
  173. package/esm/server/web-connector-factory.mjs.map +1 -1
  174. package/esm/server/web-connector.d.mts +1 -1
  175. package/esm/server/web-connector.mjs +138 -18
  176. package/esm/server/web-connector.mjs.map +1 -1
  177. package/esm/shared.d.mts +18 -3
  178. package/esm/shared.mjs +3 -7
  179. package/esm/shared.mjs.map +1 -1
  180. package/esm/validation.d.mts +1 -1
  181. package/esm/vite/app-convention-aliases.mjs +1 -1
  182. package/esm/vite/app-convention-aliases.mjs.map +1 -1
  183. package/esm/vite/build-client.d.mts +1 -1
  184. package/esm/vite/build-client.mjs +2 -2
  185. package/esm/vite/build-client.mjs.map +1 -1
  186. package/esm/vite/gate-a-resolve.d.mts +1 -1
  187. package/esm/vite/gate-a-resolve.mjs +67 -13
  188. package/esm/vite/gate-a-resolve.mjs.map +1 -1
  189. package/esm/vite/gate-b-secrets.d.mts +1 -1
  190. package/esm/vite/gate-b-secrets.mjs +35 -17
  191. package/esm/vite/gate-b-secrets.mjs.map +1 -1
  192. package/esm/vite/gate-c-verify.d.mts +1 -1
  193. package/esm/vite/gate-c-verify.mjs +6 -6
  194. package/esm/vite/gate-c-verify.mjs.map +1 -1
  195. package/esm/vite/hydration-entries.d.mts +1 -1
  196. package/esm/vite/hydration-entries.mjs +2 -2
  197. package/esm/vite/hydration-entries.mjs.map +1 -1
  198. package/esm/vite/index.d.mts +9 -6
  199. package/esm/vite/index.mjs +124 -11
  200. package/esm/vite/index.mjs.map +1 -1
  201. package/esm/vite/page-registry-plugin.d.mts +22 -3
  202. package/esm/vite/page-registry-plugin.mjs +48 -14
  203. package/esm/vite/page-registry-plugin.mjs.map +1 -1
  204. package/esm/vite/projection.d.mts +2 -2
  205. package/esm/vite/projection.mjs +38 -15
  206. package/esm/vite/projection.mjs.map +1 -1
  207. package/llms-full.txt +220 -56
  208. package/llms.txt +5 -3
  209. package/package.json +3 -3
  210. package/skills/add-web-to-an-app/SKILL.md +3 -3
  211. package/skills/create-a-page/SKILL.md +117 -10
  212. package/skills/load-page-data/SKILL.md +92 -12
  213. package/skills/navigate-on-the-client/SKILL.md +1 -1
  214. package/skills/serve-styles/SKILL.md +13 -12
  215. package/skills/use-layouts/SKILL.md +14 -8
  216. package/skills/write-the-root/SKILL.md +3 -1
  217. package/esm/server/buffered-response.d.mts +0 -58
  218. package/esm/server/buffered-response.mjs +0 -115
  219. package/esm/server/buffered-response.mjs.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":[],"sources":["../../../../../../../web/src/vite/index.ts"],"sourcesContent":["/**\n * `@warlock.js/web/vite` — build-tooling subpath, kept separate from the\n * runtime barrel (`@warlock.js/web`) so importing it never pulls `vite` into\n * a project that doesn't build with Vite.\n */\nimport type { Plugin } from \"vite\";\nimport {\n buildHydrationClient,\n type BuildHydrationClientOptions,\n type BuildHydrationClientResult,\n} from \"./build-client\";\nimport { gateAResolve } from \"./gate-a-resolve\";\nimport { createPublicEnvTracker, gateBSecrets } from \"./gate-b-secrets\";\nimport { gateCVerify } from \"./gate-c-verify\";\nimport { clientPageRegistry } from \"./page-registry-plugin\";\nimport { projection } from \"./projection\";\n\nexport { buildHydrationClient } from \"./build-client\";\nexport type {\n BuildHydrationClientOptions,\n BuildHydrationClientResult,\n HydrationClientBuildOutput,\n} from \"./build-client\";\nexport { gateAResolve } from \"./gate-a-resolve\";\nexport type { EnvironmentClassifier, EnvironmentClassifierOptions, WarlockEnvironment } from \"./gate-a-resolve\";\nexport { createPublicEnvTracker, gateBSecrets } from \"./gate-b-secrets\";\nexport type { PublicEnvTracker } from \"./gate-b-secrets\";\nexport {\n buildPublicEnvManifest,\n findLeakedServerExports,\n findLeakedServerImportEdges,\n gateCVerify,\n} from \"./gate-c-verify\";\nexport type {\n GateCOptions,\n PublicEnvManifestEntry,\n ServerExportLeak,\n ServerImportEdgeLeak,\n} from \"./gate-c-verify\";\nexport { HYDRATION_CLIENT_ENTRY_NAME, createHydrationClientEntry } from \"./hydration-entries\";\nexport type { HydrationClientEntry } from \"./hydration-entries\";\nexport {\n CLIENT_PAGE_REGISTRY_ID,\n clientPageRegistry,\n RESOLVED_CLIENT_PAGE_REGISTRY_ID,\n} from \"./page-registry-plugin\";\nexport type { ClientPageRegistryPluginOptions } from \"./page-registry-plugin\";\nexport { projection, ProjectionAmbiguityError } from \"./projection\";\nexport type { ProjectionResult } from \"./projection\";\n\nexport type WarlockClientBoundaryOptions = Parameters<typeof gateAResolve>[0];\n\nexport type BuildWarlockHydrationClientOptions = Readonly<{\n appRoot: string;\n webRoot: string;\n /** Absolute client output dir — threaded to `buildHydrationClient` (`<outdir>/client`). */\n outDir: string;\n resolveAliases: BuildHydrationClientOptions[\"resolveAliases\"];\n external?: BuildHydrationClientOptions[\"external\"];\n}>;\n\nfunction clientEnvironmentOnly(plugin: Plugin): Plugin {\n return {\n ...plugin,\n applyToEnvironment(environment) {\n return environment.config.consumer === \"client\";\n },\n };\n}\n\n/**\n * The composed client-build pipeline: projection\n * strips the 5 server exports first, THEN Gate B's `transform` checks\n * whatever source remains for inline secret reads, THEN Gate A's\n * `resolveId` judges whatever imports remain. Array order here is\n * `[projection(), gateBSecrets(), gateAResolve()]` to match Vite's own\n * pipeline shape (`transform` before `resolveId`), but array order alone\n * does not guarantee this — see the hook-ordering fact below, which is what\n * actually makes the composition correct.\n *\n * Empirically observed fact (via an instrumented real `vite.build()`, not\n * assumed from plugin array order): for a given module M, Vite/Rollup calls\n * `transform(M)` BEFORE it calls `resolveId` for any of M's own import\n * specifiers — because Rollup must parse M's post-transform source to even\n * discover which specifiers to resolve next. Concretely: `transform` ran on\n * `entry.page.tsx` first, and only after that did `resolveId(\"./dep\", ...)`\n * fire for the import statement still present in the transformed code. A\n * consequence follows directly: if projection's `transform` removes an\n * import statement from a page module entirely (e.g. `loader`'s\n * `@warlock.js/core` import, stripped because `loader` itself is removed),\n * `resolveId` is never invoked for that specifier at all — Gate A doesn't\n * \"let it pass\", it never sees it. Gate A's `resolveId` only fires for\n * imports that survive projection's `transform`, which is exactly why a\n * component-level `@warlock.js/core` import (never touched by projection)\n * still reaches and is refused by Gate A.\n *\n * This is OBSERVED Rollup behavior, not a documented contract — a future\n * Vite/Rollup upgrade could invert it. Pinned by a\n * regression test (`index.spec.ts`, \"D.3 hook ordering pin\") that fails\n * loudly if the ordering ever inverts, and by the `vite` peer floor in\n * `web/package.json` (`>=7.3.5`, the version this was verified against). If\n * that test ever fails after a Vite bump: the failure mode of the ordering\n * assumption breaking is SAFE — projection would stop removing an import\n * statement Gate A still sees, so Gate A would refuse an import it used to\n * silently let a stripped server export take with it. That is a loud build\n * failure (\"Gate A refused an import\"), never a silent client-bundle leak.\n * Do NOT \"fix\" an apparent Gate A false-positive after a Vite upgrade by\n * weakening Gate A (e.g. widening what it lets through) — investigate\n * whether this ordering assumption broke instead; loosening Gate A to work\n * around it would turn a loud failure into the exact silent leak this\n * pipeline exists to prevent.\n *\n * Gate B is placed between the two for the same reason, but for a `transform`\n * hook rather than `resolveId`: within a plugin array, Rollup runs each\n * module's registered `transform` hooks in array order, each one receiving\n * the PREVIOUS plugin's output. Running Gate B after projection means it\n * inspects the POST-projection source — a `process.env.SECRET` read inside\n * `loader` (a server export, legitimately reading a real secret server-side)\n * is invisible to Gate B once projection has already removed `loader`\n * entirely, exactly as it should be: Gate B's job is to fence client-bound\n * code, and projection is what decides what counts as client-bound. A\n * component-level secret read is untouched by projection and still reaches\n * Gate B, which refuses it. Gate B does not depend on Gate A's `resolveId`\n * output at all (orthogonal concern, raw source vs. import paths), so its\n * position relative to Gate A is not load-bearing — it is placed before Gate\n * A only to keep both `transform` hooks adjacent in the array.\n *\n * Gate C (`gate-c-verify.ts`) runs last and only at `generateBundle` — after\n * the entire `transform`/`resolveId` build phase has completed for every\n * plugin, regardless of array position (a Rollup lifecycle fact, not\n * something this array order enforces). It verifies the EMITTED output the\n * other three produced: no server export survived as a top-level binding, no\n * import edge into a server-only package survived into the module graph, and\n * it emits the reviewable `PUBLIC_*` inlined-value manifest. `gateBSecrets` and `gateCVerify` share\n * one `PublicEnvTracker` instance so the manifest and Gate B's own unread-key\n * exclusion check agree by construction, not by coincidence.\n *\n * `clientPageRegistry()` is FIRST, ahead of projection. It contributes no\n * `transform` at all — only a `resolveId`/`load` pair for one synthetic id —\n * so it cannot displace or pre-empt any gate's inspection of any real file.\n * Two reasons for the position, one of which is not load-bearing and is\n * labelled as such:\n *\n * 1. Load-bearing: it must own `virtual:warlock/pages` before Gate A's\n * `resolveId` (also `enforce: \"pre\"`) reaches its `this.resolve(...)` call\n * for that specifier. Gate A's nested resolve would find it anyway, but\n * routing the id through Gate A's importer-chain bookkeeping only to have\n * it come back means a synthetic id can surface in a user-facing \"Import\n * chain:\" message. Resolving it first keeps ownership of the id in one\n * place.\n * 2. NOT load-bearing: the position relative to `projection()`. Projection is\n * `enforce: \"pre\"` and selects by file BASENAME (`projection.ts:242-248`),\n * so it transforms every `*.page.tsx` / `layout.tsx` / `root.tsx` that\n * enters the graph regardless of who imported it or where this plugin sits.\n * The registry emits absolute POSIX specifiers that Rollup resolves and\n * loads as ORDINARY file modules — they are not inlined into the virtual\n * module — so each one is transformed exactly as a page imported from a\n * real file would be. Projection declines the virtual module itself\n * (`\\0virtual:warlock/pages` has no matching basename), which is correct:\n * generated code has no server exports to strip.\n *\n * Point 2 is asserted, not assumed, by a real `vite.build()` in\n * `page-registry-plugin.spec.ts`: a fixture page whose `loader` — and only its\n * `loader` — imports a marker module that Gate A independently PERMITS, built\n * through this exact array, with the marker proven absent from every emitted\n * chunk while the page's own component text is proven present. Inspecting this\n * array's order would prove nothing about what reaches the browser.\n */\nexport function warlockClientBoundary(options: Parameters<typeof gateAResolve>[0] = {}): Plugin[] {\n const tracker = createPublicEnvTracker();\n return [\n clientPageRegistry({ appRoot: options.appRoot }),\n projection(),\n gateBSecrets({ tracker }),\n gateAResolve(options),\n gateCVerify({ ...options, tracker }),\n ].map(clientEnvironmentOnly);\n}\n\n/**\n * Callable production seam: callers own their exact source aliases and the\n * app-root classification boundary, while this module owns the one canonical\n * projection/Gate B/Gate A/Gate C composition.\n */\nexport async function buildWarlockHydrationClient(\n options: BuildWarlockHydrationClientOptions,\n): Promise<BuildHydrationClientResult> {\n return buildHydrationClient({\n webRoot: options.webRoot,\n outDir: options.outDir,\n resolveAliases: options.resolveAliases,\n external: options.external,\n plugins: warlockClientBoundary({ appRoot: options.appRoot }),\n });\n}\n"],"mappings":";;;;;;;;;AA6DA,SAAS,sBAAsB,QAAwB;CACrD,OAAO;EACL,GAAG;EACH,mBAAmB,aAAa;GAC9B,OAAO,YAAY,OAAO,aAAa;EACzC;CACF;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoGA,SAAgB,sBAAsB,UAA8C,CAAC,GAAa;CAChG,MAAM,UAAU,uBAAuB;CACvC,OAAO;EACL,mBAAmB,EAAE,SAAS,QAAQ,QAAQ,CAAC;EAC/C,WAAW;EACX,aAAa,EAAE,QAAQ,CAAC;EACxB,aAAa,OAAO;EACpB,YAAY;GAAE,GAAG;GAAS;EAAQ,CAAC;CACrC,CAAC,CAAC,IAAI,qBAAqB;AAC7B;;;;;;AAOA,eAAsB,4BACpB,SACqC;CACrC,OAAO,qBAAqB;EAC1B,SAAS,QAAQ;EACjB,QAAQ,QAAQ;EAChB,gBAAgB,QAAQ;EACxB,UAAU,QAAQ;EAClB,SAAS,sBAAsB,EAAE,SAAS,QAAQ,QAAQ,CAAC;CAC7D,CAAC;AACH"}
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["C:/Work/Warlock.js/web/src/vite/index.ts"],"sourcesContent":["/**\n * `@warlock.js/web/vite` — build-tooling subpath, kept separate from the\n * runtime barrel (`@warlock.js/web`) so importing it never pulls `vite` into\n * a project that doesn't build with Vite.\n */\nimport { parse } from \"@babel/parser\";\nimport path from \"node:path\";\nimport type { Plugin } from \"vite\";\nimport {\n buildHydrationClient,\n type BuildHydrationClientOptions,\n type BuildHydrationClientResult,\n} from \"./build-client\";\nimport {\n gateAResolve,\n isAppSourcePath,\n isRecognizedUniversalSurface,\n isServerFile,\n isWithinModuleWebFolder,\n} from \"./gate-a-resolve\";\nimport { createPublicEnvTracker, gateBSecrets } from \"./gate-b-secrets\";\nimport { gateCVerify } from \"./gate-c-verify\";\nimport {\n clientPageRegistry,\n type ClientPageRegistryPluginOptions,\n} from \"./page-registry-plugin\";\nimport { isProjectableFile, projectModule, projection } from \"./projection\";\n\nexport { buildHydrationClient } from \"./build-client\";\nexport type {\n BuildHydrationClientOptions,\n BuildHydrationClientResult,\n HydrationClientBuildOutput,\n} from \"./build-client\";\nexport { gateAResolve } from \"./gate-a-resolve\";\nexport type {\n EnvironmentClassifier,\n EnvironmentClassifierOptions,\n WarlockEnvironment,\n} from \"./gate-a-resolve\";\nexport { createPublicEnvTracker, gateBSecrets } from \"./gate-b-secrets\";\nexport type { PublicEnvTracker } from \"./gate-b-secrets\";\nexport {\n buildPublicEnvManifest,\n findLeakedServerExports,\n findLeakedServerImportEdges,\n gateCVerify,\n} from \"./gate-c-verify\";\nexport type {\n GateCOptions,\n PublicEnvManifestEntry,\n ServerExportLeak,\n ServerImportEdgeLeak,\n} from \"./gate-c-verify\";\nexport {\n HYDRATION_CLIENT_ENTRY_NAME,\n createHydrationClientEntry,\n} from \"./hydration-entries\";\nexport type { HydrationClientEntry } from \"./hydration-entries\";\nexport {\n CLIENT_PAGE_REGISTRY_ID,\n clientPageRegistry,\n invalidateClientPageRegistry,\n RESOLVED_CLIENT_PAGE_REGISTRY_ID,\n} from \"./page-registry-plugin\";\nexport type { ClientPageRegistryPluginOptions } from \"./page-registry-plugin\";\nexport { projection, ProjectionAmbiguityError } from \"./projection\";\nexport type { ProjectionResult } from \"./projection\";\n\nexport type WarlockClientBoundaryOptions = Parameters<\n typeof gateAResolve\n>[0] & {\n beforePageHotUpdate?: ClientPageRegistryPluginOptions[\"beforePageHotUpdate\"];\n};\n\nexport type BuildWarlockHydrationClientOptions = Readonly<{\n appRoot: string;\n webRoot: string;\n /** Absolute client output dir — threaded to `buildHydrationClient` (`<outdir>/client`). */\n outDir: string;\n resolveAliases: BuildHydrationClientOptions[\"resolveAliases\"];\n external?: BuildHydrationClientOptions[\"external\"];\n /** App-configured plugins, appended after Warlock's client-boundary pipeline. */\n plugins?: BuildHydrationClientOptions[\"plugins\"];\n}>;\n\ntype SsrBoundaryState = {\n readonly appRoot: string;\n readonly clientBoundModules: Set<string>;\n readonly clientImportsByModule: Map<string, Set<string>>;\n};\n\nfunction moduleKey(id: string): string {\n return id.split(\"?\")[0].replace(/\\\\/g, \"/\");\n}\n\nconst CODE_MODULE_EXTENSION = /\\.([cm]?[jt]sx?)$/;\n\nfunction isStatelessClientSurface(id: string, appRoot: string): boolean {\n const bare = moduleKey(id);\n if (!isAppSourcePath(bare, appRoot)) return false;\n if (isServerFile(bare, appRoot)) return false;\n\n return (\n isProjectableFile(bare) ||\n isRecognizedUniversalSurface(bare) ||\n isWithinModuleWebFolder(bare, appRoot)\n );\n}\n\nfunction collectImportSpecifiers(code: string): Set<string> {\n const ast = parse(code, {\n sourceType: \"module\",\n plugins: [\"typescript\", \"jsx\"],\n });\n const imports = new Set<string>();\n\n function walk(node: unknown): void {\n if (!node || typeof node !== \"object\") return;\n if (Array.isArray(node)) {\n for (const child of node) walk(child);\n return;\n }\n\n const record = node as Record<string, any>;\n if (\n (record.type === \"ImportDeclaration\" ||\n record.type === \"ExportNamedDeclaration\" ||\n record.type === \"ExportAllDeclaration\") &&\n record.source?.type === \"StringLiteral\"\n ) {\n imports.add(record.source.value);\n }\n if (\n record.type === \"CallExpression\" &&\n record.callee?.type === \"Import\" &&\n record.arguments?.[0]?.type === \"StringLiteral\"\n ) {\n imports.add(record.arguments[0].value);\n }\n if (\n record.type === \"ImportExpression\" &&\n record.source?.type === \"StringLiteral\"\n ) {\n imports.add(record.source.value);\n }\n\n for (const [key, child] of Object.entries(record)) {\n if (\n key === \"type\" ||\n key === \"start\" ||\n key === \"end\" ||\n key === \"loc\" ||\n key === \"range\" ||\n key.endsWith(\"Comments\") ||\n key === \"extra\"\n ) {\n continue;\n }\n walk(child);\n }\n }\n\n walk(ast.program);\n return imports;\n}\n\nfunction clientViewOf(\n state: SsrBoundaryState,\n code: string,\n id: string,\n): string | undefined {\n const key = moduleKey(id);\n if (\n !state.clientBoundModules.has(key) &&\n !isStatelessClientSurface(key, state.appRoot)\n ) {\n return undefined;\n }\n\n state.clientBoundModules.add(key);\n if (!CODE_MODULE_EXTENSION.test(key)) {\n state.clientImportsByModule.set(key, new Set());\n return code;\n }\n\n const clientCode = isProjectableFile(key)\n ? projectModule(code, key).code\n : code;\n state.clientImportsByModule.set(key, collectImportSpecifiers(clientCode));\n return clientCode;\n}\n\nfunction markResolvedClientModule(\n state: SsrBoundaryState,\n resolved: { id: string } | string | null | false | void,\n): void {\n if (!resolved) return;\n const id = typeof resolved === \"string\" ? resolved : resolved.id;\n if (!id.includes(\"\\0\")) state.clientBoundModules.add(moduleKey(id));\n}\n\nfunction isServerEnvironment(context: {\n environment?: { config: { consumer?: string } };\n}): boolean {\n return context.environment?.config.consumer === \"server\";\n}\n\n/**\n * Keeps the production/client pipeline byte-identical, while giving Gate A\n * and Gate B a validation-only view in Vite's development SSR environment.\n * SSR still evaluates the original source: only the gates receive the\n * projected client view, so loader/server exports retain their legitimate\n * server access while component-visible code is refused before evaluation.\n */\nfunction clientEnvironmentOnly(\n plugin: Plugin,\n ssrState: SsrBoundaryState,\n): Plugin {\n const validatesDevSsr =\n plugin.name === \"warlock:gate-a-resolve\" ||\n plugin.name === \"warlock:gate-b-secrets\";\n const originalTransform =\n typeof plugin.transform === \"function\"\n ? plugin.transform\n : plugin.transform?.handler;\n const originalResolveId =\n typeof plugin.resolveId === \"function\"\n ? plugin.resolveId\n : plugin.resolveId?.handler;\n const originalBuildStart =\n typeof plugin.buildStart === \"function\"\n ? plugin.buildStart\n : plugin.buildStart?.handler;\n\n return {\n ...plugin,\n applyToEnvironment(environment) {\n return (\n environment.config.consumer === \"client\" ||\n (validatesDevSsr && environment.config.consumer === \"server\")\n );\n },\n buildStart: originalBuildStart\n ? function (...args) {\n if (isServerEnvironment(this)) return;\n return originalBuildStart.apply(this, args);\n }\n : undefined,\n transform: originalTransform\n ? async function (code, id, options) {\n if (!isServerEnvironment(this)) {\n return originalTransform.call(this, code, id, options);\n }\n\n const clientCode = clientViewOf(ssrState, code, id);\n if (clientCode === undefined) return null;\n\n const transformed = await originalTransform.call(\n this,\n clientCode,\n id,\n {\n ...options,\n ssr: false,\n },\n );\n\n // Vite can externalize a package in the SSR environment before its\n // normal resolver walk offers that edge to a plugin. Gate A cannot\n // wait for that walk: validate every import that survived projection\n // now, while the original TypeScript source and importer are known.\n if (plugin.name === \"warlock:gate-a-resolve\" && originalResolveId) {\n for (const source of ssrState.clientImportsByModule.get(\n moduleKey(id),\n ) ?? []) {\n const resolved = await originalResolveId.call(this, source, id, {\n attributes: {},\n isEntry: false,\n ssr: false,\n });\n markResolvedClientModule(ssrState, resolved);\n }\n }\n\n return transformed;\n }\n : undefined,\n resolveId: originalResolveId\n ? async function (source, importer, options) {\n if (!isServerEnvironment(this)) {\n return originalResolveId.call(this, source, importer, options);\n }\n\n if (!importer) return null;\n const importerKey = moduleKey(importer);\n const isClientBound =\n ssrState.clientBoundModules.has(importerKey) ||\n isStatelessClientSurface(importerKey, ssrState.appRoot);\n if (!isClientBound) return null;\n\n const survivingImports =\n ssrState.clientImportsByModule.get(importerKey);\n if (survivingImports && !survivingImports.has(source)) return null;\n\n const resolved = await originalResolveId.call(\n this,\n source,\n importer,\n {\n ...options,\n ssr: false,\n },\n );\n markResolvedClientModule(ssrState, resolved);\n return resolved;\n }\n : undefined,\n };\n}\n\n/**\n * The composed client-build pipeline: projection\n * strips the 5 server exports first, THEN Gate B's `transform` checks\n * whatever source remains for inline secret reads, THEN Gate A's\n * `resolveId` judges whatever imports remain. Array order here is\n * `[projection(), gateBSecrets(), gateAResolve()]` to match Vite's own\n * pipeline shape (`transform` before `resolveId`), but array order alone\n * does not guarantee this — see the hook-ordering fact below, which is what\n * actually makes the composition correct.\n *\n * Empirically observed fact (via an instrumented real `vite.build()`, not\n * assumed from plugin array order): for a given module M, Vite/Rollup calls\n * `transform(M)` BEFORE it calls `resolveId` for any of M's own import\n * specifiers — because Rollup must parse M's post-transform source to even\n * discover which specifiers to resolve next. Concretely: `transform` ran on\n * `entry.page.tsx` first, and only after that did `resolveId(\"./dep\", ...)`\n * fire for the import statement still present in the transformed code. A\n * consequence follows directly: if projection's `transform` removes an\n * import statement from a page module entirely (e.g. `loader`'s\n * `@warlock.js/core` import, stripped because `loader` itself is removed),\n * `resolveId` is never invoked for that specifier at all — Gate A doesn't\n * \"let it pass\", it never sees it. Gate A's `resolveId` only fires for\n * imports that survive projection's `transform`, which is exactly why a\n * component-level `@warlock.js/core` import (never touched by projection)\n * still reaches and is refused by Gate A.\n *\n * This is OBSERVED Rollup behavior, not a documented contract — a future\n * Vite/Rollup upgrade could invert it. Pinned by a\n * regression test (`index.spec.ts`, \"D.3 hook ordering pin\") that fails\n * loudly if the ordering ever inverts, and by the `vite` peer floor in\n * `web/package.json` (`>=7.3.5`, the version this was verified against). If\n * that test ever fails after a Vite bump: the failure mode of the ordering\n * assumption breaking is SAFE — projection would stop removing an import\n * statement Gate A still sees, so Gate A would refuse an import it used to\n * silently let a stripped server export take with it. That is a loud build\n * failure (\"Gate A refused an import\"), never a silent client-bundle leak.\n * Do NOT \"fix\" an apparent Gate A false-positive after a Vite upgrade by\n * weakening Gate A (e.g. widening what it lets through) — investigate\n * whether this ordering assumption broke instead; loosening Gate A to work\n * around it would turn a loud failure into the exact silent leak this\n * pipeline exists to prevent.\n *\n * Gate B is placed between the two for the same reason, but for a `transform`\n * hook rather than `resolveId`: within a plugin array, Rollup runs each\n * module's registered `transform` hooks in array order, each one receiving\n * the PREVIOUS plugin's output. Running Gate B after projection means it\n * inspects the POST-projection source — a `process.env.SECRET` read inside\n * `loader` (a server export, legitimately reading a real secret server-side)\n * is invisible to Gate B once projection has already removed `loader`\n * entirely, exactly as it should be: Gate B's job is to fence client-bound\n * code, and projection is what decides what counts as client-bound. A\n * component-level secret read is untouched by projection and still reaches\n * Gate B, which refuses it. Gate B does not depend on Gate A's `resolveId`\n * output at all (orthogonal concern, raw source vs. import paths), so its\n * position relative to Gate A is not load-bearing — it is placed before Gate\n * A only to keep both `transform` hooks adjacent in the array.\n *\n * Gate C (`gate-c-verify.ts`) runs last and only at `generateBundle` — after\n * the entire `transform`/`resolveId` build phase has completed for every\n * plugin, regardless of array position (a Rollup lifecycle fact, not\n * something this array order enforces). It verifies the EMITTED output the\n * other three produced: no server export survived as a top-level binding, no\n * import edge into a server-only package survived into the module graph, and\n * it emits the reviewable `PUBLIC_*` inlined-value manifest. `gateBSecrets` and `gateCVerify` share\n * one `PublicEnvTracker` instance so the manifest and Gate B's own unread-key\n * exclusion check agree by construction, not by coincidence.\n *\n * `clientPageRegistry()` is FIRST, ahead of projection. It contributes no\n * `transform` at all — only a `resolveId`/`load` pair for one synthetic id —\n * so it cannot displace or pre-empt any gate's inspection of any real file.\n * Two reasons for the position, one of which is not load-bearing and is\n * labelled as such:\n *\n * 1. Load-bearing: it must own `virtual:warlock/pages` before Gate A's\n * `resolveId` (also `enforce: \"pre\"`) reaches its `this.resolve(...)` call\n * for that specifier. Gate A's nested resolve would find it anyway, but\n * routing the id through Gate A's importer-chain bookkeeping only to have\n * it come back means a synthetic id can surface in a user-facing \"Import\n * chain:\" message. Resolving it first keeps ownership of the id in one\n * place.\n * 2. NOT load-bearing: the position relative to `projection()`. Projection is\n * `enforce: \"pre\"` and selects by file BASENAME (`projection.ts:242-248`),\n * so it transforms every `*.page.tsx` / `layout.tsx` / `root.tsx` that\n * enters the graph regardless of who imported it or where this plugin sits.\n * The registry emits absolute POSIX specifiers that Rollup resolves and\n * loads as ORDINARY file modules — they are not inlined into the virtual\n * module — so each one is transformed exactly as a page imported from a\n * real file would be. Projection declines the virtual module itself\n * (`\\0virtual:warlock/pages` has no matching basename), which is correct:\n * generated code has no server exports to strip.\n *\n * Point 2 is asserted, not assumed, by a real `vite.build()` in\n * `page-registry-plugin.spec.ts`: a fixture page whose `loader` — and only its\n * `loader` — imports a marker module that Gate A independently PERMITS, built\n * through this exact array, with the marker proven absent from every emitted\n * chunk while the page's own component text is proven present. Inspecting this\n * array's order would prove nothing about what reaches the browser.\n */\nexport function warlockClientBoundary(\n options: WarlockClientBoundaryOptions = {},\n): Plugin[] {\n const tracker = createPublicEnvTracker();\n const ssrState: SsrBoundaryState = {\n appRoot: path.resolve(options.appRoot ?? process.cwd()),\n clientBoundModules: new Set(),\n clientImportsByModule: new Map(),\n };\n return [\n clientPageRegistry({\n appRoot: options.appRoot,\n beforePageHotUpdate: options.beforePageHotUpdate,\n }),\n projection(),\n gateBSecrets({ tracker }),\n gateAResolve(options),\n gateCVerify({ ...options, tracker }),\n ].map((plugin) => clientEnvironmentOnly(plugin, ssrState));\n}\n\n/**\n * Callable production seam: callers own their exact source aliases and the\n * app-root classification boundary, while this module owns the one canonical\n * projection/Gate B/Gate A/Gate C composition.\n */\nexport async function buildWarlockHydrationClient(\n options: BuildWarlockHydrationClientOptions,\n): Promise<BuildHydrationClientResult> {\n return buildHydrationClient({\n webRoot: options.webRoot,\n outDir: options.outDir,\n resolveAliases: options.resolveAliases,\n external: options.external,\n plugins: [\n ...warlockClientBoundary({ appRoot: options.appRoot }),\n ...(options.plugins ?? []),\n ],\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;AA4FA,SAAS,UAAU,IAAoB;CACrC,OAAO,GAAG,MAAM,GAAG,EAAE,GAAG,QAAQ,OAAO,GAAG;AAC5C;AAEA,MAAM,wBAAwB;AAE9B,SAAS,yBAAyB,IAAY,SAA0B;CACtE,MAAM,OAAO,UAAU,EAAE;CACzB,IAAI,CAAC,gBAAgB,MAAM,OAAO,GAAG,OAAO;CAC5C,IAAI,aAAa,MAAM,OAAO,GAAG,OAAO;CAExC,OACE,kBAAkB,IAAI,KACtB,6BAA6B,IAAI,KACjC,wBAAwB,MAAM,OAAO;AAEzC;AAEA,SAAS,wBAAwB,MAA2B;CAC1D,MAAM,MAAM,MAAM,MAAM;EACtB,YAAY;EACZ,SAAS,CAAC,cAAc,KAAK;CAC/B,CAAC;CACD,MAAM,0BAAU,IAAI,IAAY;CAEhC,SAAS,KAAK,MAAqB;EACjC,IAAI,CAAC,QAAQ,OAAO,SAAS,UAAU;EACvC,IAAI,MAAM,QAAQ,IAAI,GAAG;GACvB,KAAK,MAAM,SAAS,MAAM,KAAK,KAAK;GACpC;EACF;EAEA,MAAM,SAAS;EACf,KACG,OAAO,SAAS,uBACf,OAAO,SAAS,4BAChB,OAAO,SAAS,2BAClB,OAAO,QAAQ,SAAS,iBAExB,QAAQ,IAAI,OAAO,OAAO,KAAK;EAEjC,IACE,OAAO,SAAS,oBAChB,OAA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ACJ,SAAS,sBAAsB,IAAI,WAAW;GAChD,IAAI,oBAAoB,CAAC,iBAAiB,IAAI,MAAM,GAAG,OAAO;GAE9D,MAAM,WAAW,MAAM,kBAAkB,KACvC,MACA,QACA,UACA;IACE,GAAG;IACH,KAAK;GACP,CACF;GACA,yBAAyB,UAAU,QAAQ;GAC3C,OAAO;EACT,IACA;CACN;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoGA,SAAgB,sBACd,UAAwC,CAAC,GAC/B;CACV,MAAM,UAAU,uBAAuB;CACvC,MAAM,WAA6B;EACjC,SAAS,KAAK,QAAQ,QAAQ,WAAW,QAAQ,IAAI,CAAC;EACtD,oCAAoB,IAAI,IAAI;EAC5B,uCAAuB,IAAI,IAAI;CACjC;CACA,OAAO;EACL,mBAAmB;GACjB,SAAS,QAAQ;GACjB,qBAAqB,QAAQ;EAC/B,CAAC;EACD,WAAW;EACX,aAAa,EAAE,QAAQ,CAAC;EACxB,aAAa,OAAO;EACpB,YAAY;GAAE,GAAG;GAAS;EAAQ,CAAC;CACrC,EAAE,KAAK,WAAW,sBAAsB,QAAQ,QAAQ,CAAC;AAC3D;;;;;;AAOA,eAAsB,4BACpB,SACqC;CACrC,OAAO,qBAAqB;EAC1B,SAAS,QAAQ;EACjB,QAAQ,QAAQ;EAChB,gBAAgB,QAAQ;EACxB,UAAU,QAAQ;EAClB,SAAS,CACP,GAAG,sBAAsB,EAAE,SAAS,QAAQ,QAAQ,CAAC,GACrD,GAAI,QAAQ,WAAW,CAAC,CAC1B;CACF,CAAC;AACH"}
@@ -1,6 +1,6 @@
1
- import { Plugin } from "vite";
1
+ import { Plugin, ViteDevServer } from "vite";
2
2
 
3
- //#region ../web/src/vite/page-registry-plugin.d.ts
3
+ //#region web/src/vite/page-registry-plugin.d.ts
4
4
  /**
5
5
  * The specifier application code writes.
6
6
  *
@@ -15,9 +15,28 @@ declare const CLIENT_PAGE_REGISTRY_ID = "virtual:warlock/pages";
15
15
  * (and no filesystem watcher) mistakes it for a real path.
16
16
  */
17
17
  declare const RESOLVED_CLIENT_PAGE_REGISTRY_ID = "\0virtual:warlock/pages";
18
+ /**
19
+ * Evicts the client registry so its next request re-runs page discovery, then
20
+ * reloads the document so hydration consumes that fresh registry.
21
+ *
22
+ * Vite 7 keeps separate module graphs per environment. Pages are imported by
23
+ * the browser, so only the resolved virtual module in the client graph is the
24
+ * cache entry this operation owns.
25
+ */
26
+ declare function invalidateClientPageRegistry(vite: ViteDevServer): void;
18
27
  type ClientPageRegistryPluginOptions = {
19
28
  /** Absolute path to the application root. Defaults to `process.cwd()`, matching Vite's own default `root` and Gate A's `appRoot` default. */appRoot?: string; /** Source directory name under `appRoot`; forwarded verbatim to `discoverPages`, which defaults it to `"src"`. */
20
29
  srcDir?: string;
30
+ /**
31
+ * Optional server-side barrier run before this plugin decides how the browser
32
+ * receives a page update. `true` means the callback already published the
33
+ * new route graph and sent the required reload, so this hook emits no second
34
+ * update for the same filesystem event.
35
+ */
36
+ beforePageHotUpdate?: (context: {
37
+ file: string;
38
+ type: "create" | "update" | "delete";
39
+ }) => boolean | Promise<boolean>;
21
40
  };
22
41
  /**
23
42
  * Serves the client page registry at {@link CLIENT_PAGE_REGISTRY_ID}.
@@ -39,5 +58,5 @@ type ClientPageRegistryPluginOptions = {
39
58
  */
40
59
  declare function clientPageRegistry(options?: ClientPageRegistryPluginOptions): Plugin;
41
60
  //#endregion
42
- export { CLIENT_PAGE_REGISTRY_ID, ClientPageRegistryPluginOptions, RESOLVED_CLIENT_PAGE_REGISTRY_ID, clientPageRegistry };
61
+ export { CLIENT_PAGE_REGISTRY_ID, ClientPageRegistryPluginOptions, RESOLVED_CLIENT_PAGE_REGISTRY_ID, clientPageRegistry, invalidateClientPageRegistry };
43
62
  //# sourceMappingURL=page-registry-plugin.d.mts.map
@@ -5,7 +5,7 @@ import path from "node:path";
5
5
  import { parse } from "@babel/parser";
6
6
  import MagicString from "magic-string";
7
7
 
8
- //#region ../web/src/vite/page-registry-plugin.ts
8
+ //#region web/src/vite/page-registry-plugin.ts
9
9
  /**
10
10
  * The wire between the two halves that already existed and never met:
11
11
  * `discoverPages` (the page graph, read off disk) and `generateClientRegistry`
@@ -34,6 +34,23 @@ const CLIENT_PAGE_REGISTRY_ID = "virtual:warlock/pages";
34
34
  */
35
35
  const RESOLVED_CLIENT_PAGE_REGISTRY_ID = `\0${CLIENT_PAGE_REGISTRY_ID}`;
36
36
  /**
37
+ * Evicts the client registry so its next request re-runs page discovery, then
38
+ * reloads the document so hydration consumes that fresh registry.
39
+ *
40
+ * Vite 7 keeps separate module graphs per environment. Pages are imported by
41
+ * the browser, so only the resolved virtual module in the client graph is the
42
+ * cache entry this operation owns.
43
+ */
44
+ function invalidateClientPageRegistry(vite) {
45
+ const moduleGraph = vite.environments.client.moduleGraph;
46
+ const registryModule = moduleGraph.getModuleById(RESOLVED_CLIENT_PAGE_REGISTRY_ID);
47
+ if (registryModule) moduleGraph.invalidateModule(registryModule);
48
+ vite.hot.send({
49
+ type: "full-reload",
50
+ path: "*"
51
+ });
52
+ }
53
+ /**
37
54
  * The import specifiers the emitted registry names must be ABSOLUTE POSIX file
38
55
  * paths, never relative ones.
39
56
  *
@@ -112,11 +129,11 @@ function isServerPageModule(file) {
112
129
  return false;
113
130
  }
114
131
  /**
115
- * What replaces a component body in the skeleton. Its content is irrelevant —
132
+ * What replaces a refresh-safe body in the skeleton. Its content is irrelevant —
116
133
  * only that it is CONSTANT, so two sources that differ solely inside a masked
117
134
  * body serialise identically.
118
135
  */
119
- const MASKED_COMPONENT_BODY = "/*warlock:component-body*/";
136
+ const MASKED_REFRESH_BODY = "/*warlock:refresh-body*/";
120
137
  /** React's own convention, and the one `react-refresh` itself uses: components are PascalCase. */
121
138
  function isComponentName(name) {
122
139
  return typeof name === "string" && /^[A-Z]/.test(name);
@@ -162,7 +179,7 @@ function topLevelBoundNames(stmt) {
162
179
  return names;
163
180
  }
164
181
  /**
165
- * Every module-scope name reachable from one of the five server exports.
182
+ * Every module-scope name reachable from one of the six server exports.
166
183
  *
167
184
  * Used ONLY to UNMASK: a PascalCase function that `metadata` or `loader` can
168
185
  * reach is not a component for this purpose, it is a server-side helper that
@@ -201,13 +218,16 @@ function serverReachableNames(body) {
201
218
  }
202
219
  /**
203
220
  * The body node to mask for a top-level statement, or `undefined` if this
204
- * statement is not a component declaration.
221
+ * statement is neither a component declaration nor an exported `register`
222
+ * declaration.
205
223
  *
206
224
  * Recognised shapes, and only these:
207
225
  * - `export default function () {…}` / `export default () => …` — the page
208
226
  * component, whatever it is called.
209
227
  * - `function Name() {…}` / `const Name = () => …` (PascalCase, optionally
210
228
  * `export`ed) — a component declared alongside it.
229
+ * - `export function register() {…}` / `export const register = () => …` —
230
+ * the lifecycle hook whose replacement namespace is invoked by projection.
211
231
  *
212
232
  * Everything else — `memo(...)`/`forwardRef(...)` wrappers, classes,
213
233
  * lowercase helpers, every server export — is left UNMASKED and therefore
@@ -216,6 +236,14 @@ function serverReachableNames(body) {
216
236
  */
217
237
  function componentBodyToMask(stmt, serverReachable) {
218
238
  if (stmt.type === "ExportDefaultDeclaration") return functionBody(stmt.declaration);
239
+ if (stmt.type === "ExportNamedDeclaration") {
240
+ const exported = stmt.declaration;
241
+ if (exported?.type === "FunctionDeclaration" && exported.id?.name === "register") return functionBody(exported);
242
+ if (exported?.type === "VariableDeclaration" && exported.declarations.length === 1) {
243
+ const declarator = exported.declarations[0];
244
+ if (declarator.id?.type === "Identifier" && declarator.id.name === "register") return functionBody(declarator.init);
245
+ }
246
+ }
219
247
  const declaration = stmt.type === "ExportNamedDeclaration" ? stmt.declaration : stmt;
220
248
  if (!declaration) return void 0;
221
249
  const named = (name, node) => isComponentName(name) && !serverReachable.has(name) ? functionBody(node) : void 0;
@@ -227,13 +255,13 @@ function componentBodyToMask(stmt, serverReachable) {
227
255
  }
228
256
  }
229
257
  /**
230
- * The module source with every component body replaced by a constant — the
231
- * ONE value the reload decision compares across an edit.
258
+ * The module source with every component and exported `register` body replaced
259
+ * by a constant — the ONE value the reload decision compares across an edit.
232
260
  *
233
- * Everything outside a component body survives verbatim: imports, module-level
234
- * declarations, all five server exports, and the comments and whitespace
235
- * between them. So the skeleton is unchanged iff the save touched nothing but
236
- * component bodies, which is exactly the ruling.
261
+ * Everything outside those bodies survives verbatim: imports, signatures,
262
+ * module-level declarations, all six server exports, and the comments and
263
+ * whitespace between them. So the skeleton is unchanged iff the save touched
264
+ * nothing but refresh-safe bodies.
237
265
  *
238
266
  * Returns `undefined` when the source does not parse — a half-typed file whose
239
267
  * error Vite is already reporting from projection's real `transform`. The
@@ -258,7 +286,7 @@ function captureSkeleton(code) {
258
286
  if (!bodyNode) continue;
259
287
  const start = bodyNode.start;
260
288
  const end = bodyNode.end;
261
- if (end > start) magic.overwrite(start, end, MASKED_COMPONENT_BODY);
289
+ if (end > start) magic.overwrite(start, end, MASKED_REFRESH_BODY);
262
290
  }
263
291
  return magic.toString();
264
292
  }
@@ -314,7 +342,7 @@ function clientPageRegistry(options = {}) {
314
342
  },
315
343
  /**
316
344
  * Applies the ruling (canon `6b240682`): Fast Refresh ONLY when the only
317
- * changes are inside component bodies.
345
+ * changes are inside component or exported `register` bodies.
318
346
  *
319
347
  * - Skeleton moved (an import, a module-level declaration, ANY server
320
348
  * export — with or without a simultaneous JSX change) → full reload.
@@ -327,6 +355,12 @@ function clientPageRegistry(options = {}) {
327
355
  * construction.
328
356
  */
329
357
  async hotUpdate(context) {
358
+ if (isServerPageModule(context.file)) {
359
+ if (await options.beforePageHotUpdate?.({
360
+ file: context.file,
361
+ type: context.type
362
+ })) return [];
363
+ }
330
364
  if (context.type !== "update") return void 0;
331
365
  if (!isServerPageModule(context.file)) return void 0;
332
366
  const next = captureSkeleton(await context.read());
@@ -345,5 +379,5 @@ function clientPageRegistry(options = {}) {
345
379
  }
346
380
 
347
381
  //#endregion
348
- export { CLIENT_PAGE_REGISTRY_ID, RESOLVED_CLIENT_PAGE_REGISTRY_ID, clientPageRegistry };
382
+ export { CLIENT_PAGE_REGISTRY_ID, RESOLVED_CLIENT_PAGE_REGISTRY_ID, clientPageRegistry, invalidateClientPageRegistry };
349
383
  //# sourceMappingURL=page-registry-plugin.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"page-registry-plugin.mjs","names":[],"sources":["../../../../../../../web/src/vite/page-registry-plugin.ts"],"sourcesContent":["/**\n * The wire between the two halves that already existed and never met:\n * `discoverPages` (the page graph, read off disk) and `generateClientRegistry`\n * (the module SOURCE that carries that graph into the browser). Neither one\n * touches Vite; this plugin is the only place they are joined, and it joins\n * them as a VIRTUAL module so nothing is ever written to the user's tree.\n *\n * Identical in dev and build — no `apply`/`command` gating, matching the rest\n * of `warlockClientBoundary`'s composition (`index.ts`), which is also\n * mode-agnostic. A registry that differed between `vite dev` and `vite build`\n * would make every dev-only or prod-only page bug unreproducible in the other\n * mode.\n */\nimport { parse } from \"@babel/parser\";\nimport MagicString from \"magic-string\";\nimport path from \"node:path\";\nimport type { Plugin } from \"vite\";\nimport { discoverPages, toPosix } from \"../build/discover-pages\";\nimport { generateClientRegistry } from \"../build/generate-client-registry\";\nimport { SERVER_EXPORT_NAMES } from \"./projection\";\n\n/**\n * The specifier application code writes.\n *\n * Exported so the client runtime imports this constant instead of retyping the\n * string: a constant two sides must agree on is a guard, and a guard duplicated\n * at a second site fails open at the third — a typo'd re-spelling doesn't fail\n * loudly, it resolves to \"no such module\" or, worse, to a stale real file.\n */\nexport const CLIENT_PAGE_REGISTRY_ID = \"virtual:warlock/pages\";\n\n/**\n * The resolved id, `\\0`-prefixed per Vite/Rollup convention so no other plugin\n * (and no filesystem watcher) mistakes it for a real path.\n */\nexport const RESOLVED_CLIENT_PAGE_REGISTRY_ID = `\\0${CLIENT_PAGE_REGISTRY_ID}`;\n\nexport type ClientPageRegistryPluginOptions = {\n /** Absolute path to the application root. Defaults to `process.cwd()`, matching Vite's own default `root` and Gate A's `appRoot` default. */\n appRoot?: string;\n /** Source directory name under `appRoot`; forwarded verbatim to `discoverPages`, which defaults it to `\"src\"`. */\n srcDir?: string;\n};\n\n/**\n * The import specifiers the emitted registry names must be ABSOLUTE POSIX file\n * paths, never relative ones.\n *\n * A relative specifier resolves against its IMPORTER, and the importer here is\n * `\\0virtual:warlock/pages` — a synthetic id whose `dirname` is not a real\n * directory. `./blog.page.tsx` from that importer resolves to nonsense that\n * fails at bundle time with a path no user authored and no user can act on.\n *\n * Separator normalization is `hydration-entries.ts`'s\n * (`hydration-entries.ts:12-14`) and `discover-pages.ts`'s single\n * `.replace(/\\\\/g, \"/\")` rule, reused via the already-exported `toPosix` rather\n * than spelled a third time — keeping the drive colon (`D:/...`) is exactly\n * what Vite's resolver wants on Windows.\n */\nfunction toImportSpecifier(absoluteFilePath: string): string {\n return toPosix(path.resolve(absoluteFilePath));\n}\n\n/**\n * Erases the generated module's TypeScript down to plain JavaScript.\n *\n * NOT optional, and not a style choice. Vite's `vite:esbuild` transform is\n * gated behind `createFilter`, which refuses ANY id containing a NUL byte\n * (`node_modules/vite/dist/node/chunks/config.js:1512` — `if\n * (id.includes(\"\\0\")) return false`). So the one module in this build that is\n * `\\0`-prefixed by convention is precisely the one module esbuild will never\n * transform, while `generateClientRegistry` always emits TypeScript (a\n * type-only `ClientPageEntry` import plus the array's type annotation). Handed\n * to Rollup verbatim, `import type { ClientPageEntry } from ...` is a\n * JavaScript syntax error.\n *\n * Done with the AST rather than a regex, using the same `@babel/parser` +\n * `MagicString` pair `projection.ts` already uses in this directory — a regex\n * over generated source is a second grammar that drifts from the generator's\n * silently. If the generator ever emits a TS construct outside these two\n * shapes, the result is a Rollup parse error naming the virtual module: loud,\n * not silent. `page-registry-plugin.spec.ts` pins that the erased output\n * re-parses as plain JavaScript with the TypeScript plugin switched OFF.\n */\nfunction eraseTypes(source: string): string {\n const ast = parse(source, { sourceType: \"module\", plugins: [\"typescript\"] });\n const magic = new MagicString(source);\n\n for (const statement of ast.program.body as any[]) {\n if (statement.type === \"ImportDeclaration\" && statement.importKind === \"type\") {\n let end = statement.end as number;\n if (source[end] === \"\\r\" && source[end + 1] === \"\\n\") end += 2;\n else if (source[end] === \"\\n\") end += 1;\n magic.remove(statement.start as number, end);\n continue;\n }\n\n const declaration =\n statement.type === \"ExportNamedDeclaration\" ? statement.declaration : statement;\n\n if (declaration?.type !== \"VariableDeclaration\") continue;\n\n for (const declarator of declaration.declarations as any[]) {\n const annotation = declarator.id?.typeAnnotation;\n if (annotation) magic.remove(annotation.start as number, annotation.end as number);\n }\n }\n\n return magic.toString();\n}\n\n/**\n * The four file shapes that carry SERVER data (`metadata` chief among them)\n * and are therefore projected before the client graph forms — the exact set\n * `projection.ts`'s `isProjectableFile` matches, spelled here by BASENAME so\n * the two agree by construction on what \"a server-side page module\" is. A\n * change to one of these is the only kind of change whose SERVER half\n * (`metadata`, `loader`, …) can move without the client half moving at all.\n */\nfunction isServerPageModule(file: string): boolean {\n const base = path.basename(file.split(\"?\")[0]);\n if (/\\.page\\.tsx?$/.test(base)) return true;\n if (base === \"layout.tsx\" || base === \"layout.ts\") return true;\n if (/\\.layout\\.tsx?$/.test(base)) return true;\n if (base === \"root.tsx\") return true;\n return false;\n}\n\n/**\n * The server-vs-client reload seam.\n *\n * A page module carries TWO halves. The CLIENT half is the projected code the\n * browser actually runs; Fast Refresh can hot-swap it with zero reloads. The\n * SERVER half — `metadata`, `loader`, `route`, `middleware`, `validation`, plus\n * the imports/locals orphaned with them — is stripped by projection\n * (`projection.ts:49`) and set to `undefined` on hydration\n * (`client/hydrate-page.tsx`), so the browser never holds it and there is\n * nothing on the client to hot-swap. Its effect is felt only when SSR re-runs\n * and re-renders `<head>`; the honest way to apply a change to it is a full\n * document reload.\n *\n * THE RULING (canon `6b240682`), stated as the invariant it is:\n *\n * FAST REFRESH ONLY WHEN THE ONLY CHANGES ARE INSIDE COMPONENT BODIES.\n * EVERYTHING ELSE RELOADS.\n *\n * Concretely: any change to an import statement, to a module-level\n * declaration, or to a server export forces a full document reload — whether\n * or not the JSX moved in the same save.\n *\n * WHY AN OVER-APPROXIMATION, AND WHY NOBODY SHOULD \"IMPROVE\" IT BACK\n *\n * Two earlier cuts tried to name the server half EXACTLY and both shipped a\n * stale `<head>`:\n *\n * 1. Comparing only the projected CLIENT code. A save that changed the JSX\n * *and* `metadata` moved the client half, which was read as proof that\n * only the JSX moved. Mixed saves took the Fast Refresh branch.\n * 2. Adding the complement — the stripped server half, recovered by\n * subsequence diff. `projection.ts:447-448` KEEPS an import when the\n * client reads it, so an import read by BOTH `metadata` and the JSX\n * lives in the projection and appears in NEITHER half exclusively.\n * Change its specifier and the complement is byte-identical → Fast\n * Refresh, stale `<title>`. Shared module-level LOCALS have the same\n * shape, so extending the complement a third time is a third bug.\n *\n * Both failures were UNDER-approximations, and under-approximating is the\n * unsafe direction. A precise reachability analysis over shared imports and\n * locals is the correct answer and is a later refinement; getting it subtly\n * wrong reproduces this bug again. Over-approximating can only err toward\n * RELOADING. A needless reload costs component state; a missed one ships a\n * stale `<head>` and calls it a hot update.\n *\n * THE ACCEPTED COST, which is not a bug to be optimised away: editing a\n * module-level helper read only by the JSX now reloads.\n *\n * The skeleton has to be captured BEFORE the edit, because by the time\n * `hotUpdate` runs Vite has already hard-invalidated the module and cleared its\n * `transformResult` (`onFileChange` → `invalidateModule`, which runs before any\n * `hotUpdate` hook). The `transform` spy below is that capture: it runs first in\n * the client environment, records the skeleton, and returns nothing so\n * projection still performs the real transform.\n */\ntype SkeletonCache = Map<string, string>;\n\n/**\n * What replaces a component body in the skeleton. Its content is irrelevant —\n * only that it is CONSTANT, so two sources that differ solely inside a masked\n * body serialise identically.\n */\nconst MASKED_COMPONENT_BODY = \"/*warlock:component-body*/\";\n\n/** React's own convention, and the one `react-refresh` itself uses: components are PascalCase. */\nfunction isComponentName(name: string | undefined): boolean {\n return typeof name === \"string\" && /^[A-Z]/.test(name);\n}\n\n/** The `body` node of a function-shaped expression/declaration, or `undefined` for anything else. */\nfunction functionBody(node: any): any | undefined {\n if (!node) return undefined;\n if (\n node.type === \"FunctionDeclaration\" ||\n node.type === \"FunctionExpression\" ||\n node.type === \"ArrowFunctionExpression\"\n ) {\n return node.body;\n }\n return undefined;\n}\n\n/**\n * Generic duck-typed identifier walk, the same shape `projection.ts`'s\n * `collectIdentifierNames` uses (it is not exported, and re-deriving one\n * OVER-collecting walk is safe here for the same reason it is safe there).\n *\n * Over-collecting — counting an object property key or a shadowing parameter\n * as a \"read\" — can only make the reachable set BIGGER, which can only UNMASK\n * more component bodies, which can only produce more reloads. The safe\n * direction.\n */\nfunction collectIdentifierNames(node: unknown, names: Set<string>): void {\n if (!node || typeof node !== \"object\") return;\n if (Array.isArray(node)) {\n for (const item of node) collectIdentifierNames(item, names);\n return;\n }\n const record = node as Record<string, unknown>;\n if (typeof record.type !== \"string\") return;\n if (record.type === \"Identifier\" || record.type === \"JSXIdentifier\") {\n names.add((record as any).name);\n }\n for (const key of Object.keys(record)) {\n if (key === \"type\" || key === \"start\" || key === \"end\" || key === \"loc\" || key === \"range\") continue;\n if (key === \"leadingComments\" || key === \"trailingComments\" || key === \"innerComments\" || key === \"extra\") {\n continue;\n }\n collectIdentifierNames(record[key], names);\n }\n}\n\n/** The module-scope names a top-level statement binds (the `export` wrapper looked through). */\nfunction topLevelBoundNames(stmt: any): Set<string> {\n const names = new Set<string>();\n const declaration = stmt.type === \"ExportNamedDeclaration\" ? stmt.declaration : stmt;\n if (!declaration) return names;\n if (declaration.type === \"VariableDeclaration\") {\n for (const declarator of declaration.declarations) {\n if (declarator.id?.type === \"Identifier\") names.add(declarator.id.name);\n }\n } else if (declaration.id?.type === \"Identifier\") {\n names.add(declaration.id.name);\n }\n return names;\n}\n\n/**\n * Every module-scope name reachable from one of the five server exports.\n *\n * Used ONLY to UNMASK: a PascalCase function that `metadata` or `loader` can\n * reach is not a component for this purpose, it is a server-side helper that\n * merely looks like one, and a change inside its body must reload. Seeded from\n * any top-level statement binding a `SERVER_EXPORT_NAMES` name — deliberately\n * looser than `projection.ts`'s own `isServerExportDeclaration` (no export\n * requirement, no single-declarator requirement), because seeding from MORE\n * statements can only unmask more, i.e. reload more.\n *\n * Fixpoint, not one pass, for the same reason projection's is: a server-only\n * helper can be reached only through another server-only helper.\n */\nfunction serverReachableNames(body: any[]): Set<string> {\n const reached = new Set<string>();\n const declarations = body\n .filter((stmt) => stmt.type !== \"ImportDeclaration\")\n .map((stmt) => ({ stmt, names: topLevelBoundNames(stmt) }));\n\n for (const { stmt, names } of declarations) {\n let isServerExport = false;\n for (const name of names) {\n if (SERVER_EXPORT_NAMES.has(name)) isServerExport = true;\n }\n if (isServerExport) collectIdentifierNames(stmt, reached);\n }\n\n for (let changed = true; changed; ) {\n changed = false;\n for (const { stmt, names } of declarations) {\n let isReached = false;\n for (const name of names) {\n if (reached.has(name)) isReached = true;\n }\n if (!isReached) continue;\n const before = reached.size;\n collectIdentifierNames(stmt, reached);\n if (reached.size !== before) changed = true;\n }\n }\n\n return reached;\n}\n\n/**\n * The body node to mask for a top-level statement, or `undefined` if this\n * statement is not a component declaration.\n *\n * Recognised shapes, and only these:\n * - `export default function () {…}` / `export default () => …` — the page\n * component, whatever it is called.\n * - `function Name() {…}` / `const Name = () => …` (PascalCase, optionally\n * `export`ed) — a component declared alongside it.\n *\n * Everything else — `memo(...)`/`forwardRef(...)` wrappers, classes,\n * lowercase helpers, every server export — is left UNMASKED and therefore\n * compared byte-for-byte. That costs Fast Refresh on those shapes and buys the\n * guarantee; see this seam's header.\n */\nfunction componentBodyToMask(stmt: any, serverReachable: Set<string>): any | undefined {\n if (stmt.type === \"ExportDefaultDeclaration\") return functionBody(stmt.declaration);\n\n const declaration = stmt.type === \"ExportNamedDeclaration\" ? stmt.declaration : stmt;\n if (!declaration) return undefined;\n\n const named = (name: string | undefined, node: any) =>\n isComponentName(name) && !serverReachable.has(name as string) ? functionBody(node) : undefined;\n\n if (declaration.type === \"FunctionDeclaration\") {\n return named(declaration.id?.name, declaration);\n }\n if (declaration.type === \"VariableDeclaration\" && declaration.declarations.length === 1) {\n const declarator = declaration.declarations[0];\n if (declarator.id?.type !== \"Identifier\") return undefined;\n return named(declarator.id.name, declarator.init);\n }\n return undefined;\n}\n\n/**\n * The module source with every component body replaced by a constant — the\n * ONE value the reload decision compares across an edit.\n *\n * Everything outside a component body survives verbatim: imports, module-level\n * declarations, all five server exports, and the comments and whitespace\n * between them. So the skeleton is unchanged iff the save touched nothing but\n * component bodies, which is exactly the ruling.\n *\n * Returns `undefined` when the source does not parse — a half-typed file whose\n * error Vite is already reporting from projection's real `transform`. The\n * caller leaves the cache holding the last GOOD skeleton, so the next\n * successful save is still compared against the right baseline.\n */\nfunction captureSkeleton(code: string): string | undefined {\n let ast: ReturnType<typeof parse>;\n try {\n ast = parse(code, { sourceType: \"module\", plugins: [\"typescript\", \"jsx\"] });\n } catch {\n return undefined;\n }\n\n const body = ast.program.body as any[];\n const serverReachable = serverReachableNames(body);\n const magic = new MagicString(code);\n\n for (const stmt of body) {\n const bodyNode = componentBodyToMask(stmt, serverReachable);\n if (!bodyNode) continue;\n const start = bodyNode.start as number;\n const end = bodyNode.end as number;\n if (end > start) magic.overwrite(start, end, MASKED_COMPONENT_BODY);\n }\n\n return magic.toString();\n}\n\n/**\n * Serves the client page registry at {@link CLIENT_PAGE_REGISTRY_ID}.\n *\n * Discovery runs INSIDE `load`, once per `load` call, and its result is NOT\n * cached across builds — the plugin holds no state at all. A registry cached\n * past the moment a page file appears is a page that silently 404s until\n * someone restarts the dev server, which is a far more expensive bug than\n * re-walking a source tree. Rollup calls `load` once per module per build, and\n * in dev Vite's module graph caches the transformed result until the module is\n * invalidated, so the walk is not per-request either way. (Invalidating that\n * dev-server cache when a page file is ADDED needs a `handleHotUpdate`/watcher\n * hook that belongs with the dev provider slice — see the followup.)\n *\n * `enforce: \"pre\"` and placed FIRST in `warlockClientBoundary`'s array — see\n * that function's comment in `index.ts` for why position is what it is, and\n * `page-registry-plugin.spec.ts` for the real-build proof that the pages this\n * module names still reach `projection()`.\n */\nexport function clientPageRegistry(options: ClientPageRegistryPluginOptions = {}): Plugin {\n const appRoot = path.resolve(options.appRoot ?? process.cwd());\n\n // Per-plugin-instance, so two composed pipelines never cross-contaminate.\n // Holds the last captured SKELETON (source with component bodies masked) of\n // each server page module the client environment transformed — the \"before\"\n // side of the comparison in `hotUpdate`. See `captureSkeleton` above.\n const skeletonCache: SkeletonCache = new Map();\n\n return {\n name: \"warlock:client-page-registry\",\n enforce: \"pre\",\n resolveId(source) {\n if (source === CLIENT_PAGE_REGISTRY_ID) return RESOLVED_CLIENT_PAGE_REGISTRY_ID;\n return undefined;\n },\n load(id) {\n if (id !== RESOLVED_CLIENT_PAGE_REGISTRY_ID) return undefined;\n\n const pages = discoverPages({ appRoot, srcDir: options.srcDir });\n\n return eraseTypes(generateClientRegistry({ pages, toImportSpecifier }));\n },\n /**\n * Capture-only spy. Records the SKELETON of every server page module the\n * CLIENT environment transforms, and returns nothing so projection's own\n * `transform` still does the real work. SERVE-ONLY:\n * `this.environment.mode !== \"dev\"` skips it during `vite build`, where\n * there is no `hotUpdate` to feed and the extra parse would be pure cost.\n */\n transform(code, id) {\n if (this.environment?.mode !== \"dev\") return undefined;\n if (!isServerPageModule(id)) return undefined;\n\n const skeleton = captureSkeleton(code);\n if (skeleton !== undefined) skeletonCache.set(id, skeleton);\n\n return undefined;\n },\n /**\n * Applies the ruling (canon `6b240682`): Fast Refresh ONLY when the only\n * changes are inside component bodies.\n *\n * - Skeleton moved (an import, a module-level declaration, ANY server\n * export — with or without a simultaneous JSX change) → full reload.\n * - Skeleton unchanged → defer to Fast Refresh, zero reloads.\n *\n * Note what is NOT here: no attempt to name which half a shared import or\n * local belongs to. That question is what produced the two previous stale\n * `<head>` bugs; this seam refuses to answer it and reloads instead.\n * `hotUpdate` exists only on the dev server, so this is serve-only by\n * construction.\n */\n async hotUpdate(context) {\n // `create`/`delete` are page graph churn, not in-place edits — leave them\n // to Vite's normal handling (a new/removed module reloads on its own).\n if (context.type !== \"update\") return undefined;\n if (!isServerPageModule(context.file)) return undefined;\n\n const nextSource = await context.read();\n const next = captureSkeleton(nextSource);\n const prev = skeletonCache.get(context.file);\n\n // Refresh the cache for the next edit regardless of the decision below.\n if (next !== undefined) skeletonCache.set(context.file, next);\n\n // Could not parse the new source (Vite is already reporting that error),\n // or the client environment never transformed this module — which means\n // the browser is not holding this page, so there is no stale `<head>` to\n // ship and nothing a reload of some OTHER page would fix.\n if (next === undefined || prev === undefined) return undefined;\n\n // Anything outside a component body moved: the browser cannot hot-swap\n // it, so reload the document to re-run SSR and re-render `<head>`.\n // `path: \"*\"` matches Vite's own middleware-mode reload.\n if (prev !== next) {\n this.environment.hot.send({ type: \"full-reload\", path: \"*\" });\n\n // Empty module list: we've issued the update ourselves, so Vite should\n // not additionally push a Fast Refresh for the client module.\n return [];\n }\n\n // Only component bodies moved: defer to Vite's Fast Refresh with zero\n // reloads. A no-op re-save falls through the same harmless path.\n return undefined;\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BA,MAAa,0BAA0B;;;;;AAMvC,MAAa,mCAAmC,KAAK;;;;;;;;;;;;;;;;AAwBrD,SAAS,kBAAkB,kBAAkC;CAC3D,OAAO,QAAQ,KAAK,QAAQ,gBAAgB,CAAC;AAC/C;;;;;;;;;;;;;;;;;;;;;;AAuBA,SAAS,WAAW,QAAwB;CAC1C,MAAM,MAAM,MAAM,QAAQ;EAAE,YAAY;EAAU,SAAS,CAAC,YAAY;CAAE,CAAC;CAC3E,MAAM,QAAQ,IAAI,YAAY,MAAM;CAEpC,KAAK,MAAM,aAAa,IAAI,QAAQ,MAAe;EACjD,IAAI,UAAU,SAAS,uBAAuB,UAAU,eAAe,QAAQ;GAC7E,IAAI,MAAM,UAAU;GACpB,IAAI,OAAO,SAAS,QAAQ,OAAO,MAAM,OAAO,MAAM,OAAO;QACxD,IAAI,OAAO,SAAS,MAAM,OAAO;G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,OAAO;EACL,MAAM;EACN,SAAS;EACT,UAAU,QAAQ;GAChB,IAAI,oCAAoC,OAAO;EAEjD;EACA,KAAK,IAAI;GACP,IAAI,OAAO,kCAAkC,OAAO;GAIpD,OAAO,WAAW,uBAAuB;IAAE,OAF7B,cAAc;KAAE;KAAS,QAAQ,QAAQ;IAAO,CAEf;IAAG;GAAkB,CAAC,CAAC;EACxE;;;;;;;;EAQA,UAAU,MAAM,IAAI;GAClB,IAAI,KAAK,aAAa,SAAS,OAAO,OAAO;GAC7C,IAAI,CAAC,mBAAmB,EAAE,GAAG,OAAO;GAEpC,MAAM,WAAW,gBAAgB,IAAI;GACrC,IAAI,aAAa,QAAW,cAAc,IAAI,IAAI,QAAQ;EAG5D;;;;;;;;;;;;;;;EAeA,MAAM,UAAU,SAAS;GAGvB,IAAI,QAAQ,SAAS,UAAU,OAAO;GACtC,IAAI,CAAC,mBAAmB,QAAQ,IAAI,GAAG,OAAO;GAG9C,MAAM,OAAO,gBAAgB,MADJ,QAAQ,KAAK,CACC;GACvC,MAAM,OAAO,cAAc,IAAI,QAAQ,IAAI;GAG3C,IAAI,SAAS,QAAW,cAAc,IAAI,QAAQ,MAAM,IAAI;GAM5D,IAAI,SAAS,UAAa,SAAS,QAAW,OAAO;GAKrD,IAAI,SAAS,MAAM;IACjB,KAAK,YAAY,IAAI,KAAK;KAAE,MAAM;KAAe,MAAM;IAAI,CAAC;IAI5D,OAAO,CAAC;GACV;EAKF;CACF;AACF"}
1
+ {"version":3,"file":"page-registry-plugin.mjs","names":[],"sources":["C:/Work/Warlock.js/web/src/vite/page-registry-plugin.ts"],"sourcesContent":["/**\n * The wire between the two halves that already existed and never met:\n * `discoverPages` (the page graph, read off disk) and `generateClientRegistry`\n * (the module SOURCE that carries that graph into the browser). Neither one\n * touches Vite; this plugin is the only place they are joined, and it joins\n * them as a VIRTUAL module so nothing is ever written to the user's tree.\n *\n * Identical in dev and build — no `apply`/`command` gating, matching the rest\n * of `warlockClientBoundary`'s composition (`index.ts`), which is also\n * mode-agnostic. A registry that differed between `vite dev` and `vite build`\n * would make every dev-only or prod-only page bug unreproducible in the other\n * mode.\n */\nimport { parse } from \"@babel/parser\";\nimport MagicString from \"magic-string\";\nimport path from \"node:path\";\nimport type { Plugin, ViteDevServer } from \"vite\";\nimport { discoverPages, toPosix } from \"../build/discover-pages\";\nimport { generateClientRegistry } from \"../build/generate-client-registry\";\nimport { SERVER_EXPORT_NAMES } from \"./projection\";\n\n/**\n * The specifier application code writes.\n *\n * Exported so the client runtime imports this constant instead of retyping the\n * string: a constant two sides must agree on is a guard, and a guard duplicated\n * at a second site fails open at the third — a typo'd re-spelling doesn't fail\n * loudly, it resolves to \"no such module\" or, worse, to a stale real file.\n */\nexport const CLIENT_PAGE_REGISTRY_ID = \"virtual:warlock/pages\";\n\n/**\n * The resolved id, `\\0`-prefixed per Vite/Rollup convention so no other plugin\n * (and no filesystem watcher) mistakes it for a real path.\n */\nexport const RESOLVED_CLIENT_PAGE_REGISTRY_ID = `\\0${CLIENT_PAGE_REGISTRY_ID}`;\n\n/**\n * Evicts the client registry so its next request re-runs page discovery, then\n * reloads the document so hydration consumes that fresh registry.\n *\n * Vite 7 keeps separate module graphs per environment. Pages are imported by\n * the browser, so only the resolved virtual module in the client graph is the\n * cache entry this operation owns.\n */\nexport function invalidateClientPageRegistry(vite: ViteDevServer): void {\n const moduleGraph = vite.environments.client.moduleGraph;\n const registryModule = moduleGraph.getModuleById(RESOLVED_CLIENT_PAGE_REGISTRY_ID);\n\n if (registryModule) moduleGraph.invalidateModule(registryModule);\n\n vite.hot.send({ type: \"full-reload\", path: \"*\" });\n}\n\nexport type ClientPageRegistryPluginOptions = {\n /** Absolute path to the application root. Defaults to `process.cwd()`, matching Vite's own default `root` and Gate A's `appRoot` default. */\n appRoot?: string;\n /** Source directory name under `appRoot`; forwarded verbatim to `discoverPages`, which defaults it to `\"src\"`. */\n srcDir?: string;\n /**\n * Optional server-side barrier run before this plugin decides how the browser\n * receives a page update. `true` means the callback already published the\n * new route graph and sent the required reload, so this hook emits no second\n * update for the same filesystem event.\n */\n beforePageHotUpdate?: (context: {\n file: string;\n type: \"create\" | \"update\" | \"delete\";\n }) => boolean | Promise<boolean>;\n};\n\n/**\n * The import specifiers the emitted registry names must be ABSOLUTE POSIX file\n * paths, never relative ones.\n *\n * A relative specifier resolves against its IMPORTER, and the importer here is\n * `\\0virtual:warlock/pages` — a synthetic id whose `dirname` is not a real\n * directory. `./blog.page.tsx` from that importer resolves to nonsense that\n * fails at bundle time with a path no user authored and no user can act on.\n *\n * Separator normalization is `hydration-entries.ts`'s\n * (`hydration-entries.ts:12-14`) and `discover-pages.ts`'s single\n * `.replace(/\\\\/g, \"/\")` rule, reused via the already-exported `toPosix` rather\n * than spelled a third time — keeping the drive colon (`D:/...`) is exactly\n * what Vite's resolver wants on Windows.\n */\nfunction toImportSpecifier(absoluteFilePath: string): string {\n return toPosix(path.resolve(absoluteFilePath));\n}\n\n/**\n * Erases the generated module's TypeScript down to plain JavaScript.\n *\n * NOT optional, and not a style choice. Vite's `vite:esbuild` transform is\n * gated behind `createFilter`, which refuses ANY id containing a NUL byte\n * (`node_modules/vite/dist/node/chunks/config.js:1512` — `if\n * (id.includes(\"\\0\")) return false`). So the one module in this build that is\n * `\\0`-prefixed by convention is precisely the one module esbuild will never\n * transform, while `generateClientRegistry` always emits TypeScript (a\n * type-only `ClientPageEntry` import plus the array's type annotation). Handed\n * to Rollup verbatim, `import type { ClientPageEntry } from ...` is a\n * JavaScript syntax error.\n *\n * Done with the AST rather than a regex, using the same `@babel/parser` +\n * `MagicString` pair `projection.ts` already uses in this directory — a regex\n * over generated source is a second grammar that drifts from the generator's\n * silently. If the generator ever emits a TS construct outside these two\n * shapes, the result is a Rollup parse error naming the virtual module: loud,\n * not silent. `page-registry-plugin.spec.ts` pins that the erased output\n * re-parses as plain JavaScript with the TypeScript plugin switched OFF.\n */\nfunction eraseTypes(source: string): string {\n const ast = parse(source, { sourceType: \"module\", plugins: [\"typescript\"] });\n const magic = new MagicString(source);\n\n for (const statement of ast.program.body as any[]) {\n if (statement.type === \"ImportDeclaration\" && statement.importKind === \"type\") {\n let end = statement.end as number;\n if (source[end] === \"\\r\" && source[end + 1] === \"\\n\") end += 2;\n else if (source[end] === \"\\n\") end += 1;\n magic.remove(statement.start as number, end);\n continue;\n }\n\n const declaration =\n statement.type === \"ExportNamedDeclaration\" ? statement.declaration : statement;\n\n if (declaration?.type !== \"VariableDeclaration\") continue;\n\n for (const declarator of declaration.declarations as any[]) {\n const annotation = declarator.id?.typeAnnotation;\n if (annotation) magic.remove(annotation.start as number, annotation.end as number);\n }\n }\n\n return magic.toString();\n}\n\n/**\n * The four file shapes that carry SERVER data (`metadata` chief among them)\n * and are therefore projected before the client graph forms — the exact set\n * `projection.ts`'s `isProjectableFile` matches, spelled here by BASENAME so\n * the two agree by construction on what \"a server-side page module\" is. A\n * change to one of these is the only kind of change whose SERVER half\n * (`metadata`, `loader`, …) can move without the client half moving at all.\n */\nfunction isServerPageModule(file: string): boolean {\n const base = path.basename(file.split(\"?\")[0]);\n if (/\\.page\\.tsx?$/.test(base)) return true;\n if (base === \"layout.tsx\" || base === \"layout.ts\") return true;\n if (/\\.layout\\.tsx?$/.test(base)) return true;\n if (base === \"root.tsx\") return true;\n return false;\n}\n\n/**\n * The server-vs-client reload seam.\n *\n * A page module carries TWO halves. The CLIENT half is the projected code the\n * browser actually runs; Fast Refresh can hot-swap it with zero reloads. The\n * SERVER half — `metadata`, `loader`, `route`, `middleware`, `validation`, `prefix`, plus\n * the imports/locals orphaned with them — is stripped by projection\n * (`projection.ts:49`) and set to `undefined` on hydration\n * (`client/hydrate-page.tsx`), so the browser never holds it and there is\n * nothing on the client to hot-swap. Its effect is felt only when SSR re-runs\n * and re-renders `<head>`; the honest way to apply a change to it is a full\n * document reload.\n *\n * THE RULING (canon `6b240682`), stated as the invariant it is:\n *\n * FAST REFRESH ONLY WHEN THE ONLY CHANGES ARE INSIDE COMPONENT BODIES.\n * EVERYTHING ELSE RELOADS.\n *\n * Concretely: any change to an import statement, to a module-level\n * declaration, or to a server export forces a full document reload — whether\n * or not the JSX moved in the same save.\n *\n * WHY AN OVER-APPROXIMATION, AND WHY NOBODY SHOULD \"IMPROVE\" IT BACK\n *\n * Two earlier cuts tried to name the server half EXACTLY and both shipped a\n * stale `<head>`:\n *\n * 1. Comparing only the projected CLIENT code. A save that changed the JSX\n * *and* `metadata` moved the client half, which was read as proof that\n * only the JSX moved. Mixed saves took the Fast Refresh branch.\n * 2. Adding the complement — the stripped server half, recovered by\n * subsequence diff. `projection.ts:447-448` KEEPS an import when the\n * client reads it, so an import read by BOTH `metadata` and the JSX\n * lives in the projection and appears in NEITHER half exclusively.\n * Change its specifier and the complement is byte-identical → Fast\n * Refresh, stale `<title>`. Shared module-level LOCALS have the same\n * shape, so extending the complement a third time is a third bug.\n *\n * Both failures were UNDER-approximations, and under-approximating is the\n * unsafe direction. A precise reachability analysis over shared imports and\n * locals is the correct answer and is a later refinement; getting it subtly\n * wrong reproduces this bug again. Over-approximating can only err toward\n * RELOADING. A needless reload costs component state; a missed one ships a\n * stale `<head>` and calls it a hot update.\n *\n * THE ACCEPTED COST, which is not a bug to be optimised away: editing a\n * module-level helper read only by the JSX now reloads.\n *\n * The skeleton has to be captured BEFORE the edit, because by the time\n * `hotUpdate` runs Vite has already hard-invalidated the module and cleared its\n * `transformResult` (`onFileChange` → `invalidateModule`, which runs before any\n * `hotUpdate` hook). The `transform` spy below is that capture: it runs first in\n * the client environment, records the skeleton, and returns nothing so\n * projection still performs the real transform.\n */\ntype SkeletonCache = Map<string, string>;\n\n/**\n * What replaces a refresh-safe body in the skeleton. Its content is irrelevant —\n * only that it is CONSTANT, so two sources that differ solely inside a masked\n * body serialise identically.\n */\nconst MASKED_REFRESH_BODY = \"/*warlock:refresh-body*/\";\n\n/** React's own convention, and the one `react-refresh` itself uses: components are PascalCase. */\nfunction isComponentName(name: string | undefined): boolean {\n return typeof name === \"string\" && /^[A-Z]/.test(name);\n}\n\n/** The `body` node of a function-shaped expression/declaration, or `undefined` for anything else. */\nfunction functionBody(node: any): any | undefined {\n if (!node) return undefined;\n if (\n node.type === \"FunctionDeclaration\" ||\n node.type === \"FunctionExpression\" ||\n node.type === \"ArrowFunctionExpression\"\n ) {\n return node.body;\n }\n return undefined;\n}\n\n/**\n * Generic duck-typed identifier walk, the same shape `projection.ts`'s\n * `collectIdentifierNames` uses (it is not exported, and re-deriving one\n * OVER-collecting walk is safe here for the same reason it is safe there).\n *\n * Over-collecting — counting an object property key or a shadowing parameter\n * as a \"read\" — can only make the reachable set BIGGER, which can only UNMASK\n * more component bodies, which can only produce more reloads. The safe\n * direction.\n */\nfunction collectIdentifierNames(node: unknown, names: Set<string>): void {\n if (!node || typeof node !== \"object\") return;\n if (Array.isArray(node)) {\n for (const item of node) collectIdentifierNames(item, names);\n return;\n }\n const record = node as Record<string, unknown>;\n if (typeof record.type !== \"string\") return;\n if (record.type === \"Identifier\" || record.type === \"JSXIdentifier\") {\n names.add((record as any).name);\n }\n for (const key of Object.keys(record)) {\n if (key === \"type\" || key === \"start\" || key === \"end\" || key === \"loc\" || key === \"range\") continue;\n if (key === \"leadingComments\" || key === \"trailingComments\" || key === \"innerComments\" || key === \"extra\") {\n continue;\n }\n collectIdentifierNames(record[key], names);\n }\n}\n\n/** The module-scope names a top-level statement binds (the `export` wrapper looked through). */\nfunction topLevelBoundNames(stmt: any): Set<string> {\n const names = new Set<string>();\n const declaration = stmt.type === \"ExportNamedDeclaration\" ? stmt.declaration : stmt;\n if (!declaration) return names;\n if (declaration.type === \"VariableDeclaration\") {\n for (const declarator of declaration.declarations) {\n if (declarator.id?.type === \"Identifier\") names.add(declarator.id.name);\n }\n } else if (declaration.id?.type === \"Identifier\") {\n names.add(declaration.id.name);\n }\n return names;\n}\n\n/**\n * Every module-scope name reachable from one of the six server exports.\n *\n * Used ONLY to UNMASK: a PascalCase function that `metadata` or `loader` can\n * reach is not a component for this purpose, it is a server-side helper that\n * merely looks like one, and a change inside its body must reload. Seeded from\n * any top-level statement binding a `SERVER_EXPORT_NAMES` name — deliberately\n * looser than `projection.ts`'s own `isServerExportDeclaration` (no export\n * requirement, no single-declarator requirement), because seeding from MORE\n * statements can only unmask more, i.e. reload more.\n *\n * Fixpoint, not one pass, for the same reason projection's is: a server-only\n * helper can be reached only through another server-only helper.\n */\nfunction serverReachableNames(body: any[]): Set<string> {\n const reached = new Set<string>();\n const declarations = body\n .filter((stmt) => stmt.type !== \"ImportDeclaration\")\n .map((stmt) => ({ stmt, names: topLevelBoundNames(stmt) }));\n\n for (const { stmt, names } of declarations) {\n let isServerExport = false;\n for (const name of names) {\n if (SERVER_EXPORT_NAMES.has(name)) isServerExport = true;\n }\n if (isServerExport) collectIdentifierNames(stmt, reached);\n }\n\n for (let changed = true; changed; ) {\n changed = false;\n for (const { stmt, names } of declarations) {\n let isReached = false;\n for (const name of names) {\n if (reached.has(name)) isReached = true;\n }\n if (!isReached) continue;\n const before = reached.size;\n collectIdentifierNames(stmt, reached);\n if (reached.size !== before) changed = true;\n }\n }\n\n return reached;\n}\n\n/**\n * The body node to mask for a top-level statement, or `undefined` if this\n * statement is neither a component declaration nor an exported `register`\n * declaration.\n *\n * Recognised shapes, and only these:\n * - `export default function () {…}` / `export default () => …` — the page\n * component, whatever it is called.\n * - `function Name() {…}` / `const Name = () => …` (PascalCase, optionally\n * `export`ed) — a component declared alongside it.\n * - `export function register() {…}` / `export const register = () => …` —\n * the lifecycle hook whose replacement namespace is invoked by projection.\n *\n * Everything else — `memo(...)`/`forwardRef(...)` wrappers, classes,\n * lowercase helpers, every server export — is left UNMASKED and therefore\n * compared byte-for-byte. That costs Fast Refresh on those shapes and buys the\n * guarantee; see this seam's header.\n */\nfunction componentBodyToMask(stmt: any, serverReachable: Set<string>): any | undefined {\n if (stmt.type === \"ExportDefaultDeclaration\") return functionBody(stmt.declaration);\n\n if (stmt.type === \"ExportNamedDeclaration\") {\n const exported = stmt.declaration;\n if (exported?.type === \"FunctionDeclaration\" && exported.id?.name === \"register\") {\n return functionBody(exported);\n }\n if (exported?.type === \"VariableDeclaration\" && exported.declarations.length === 1) {\n const declarator = exported.declarations[0];\n if (declarator.id?.type === \"Identifier\" && declarator.id.name === \"register\") {\n return functionBody(declarator.init);\n }\n }\n }\n\n const declaration = stmt.type === \"ExportNamedDeclaration\" ? stmt.declaration : stmt;\n if (!declaration) return undefined;\n\n const named = (name: string | undefined, node: any) =>\n isComponentName(name) && !serverReachable.has(name as string) ? functionBody(node) : undefined;\n\n if (declaration.type === \"FunctionDeclaration\") {\n return named(declaration.id?.name, declaration);\n }\n if (declaration.type === \"VariableDeclaration\" && declaration.declarations.length === 1) {\n const declarator = declaration.declarations[0];\n if (declarator.id?.type !== \"Identifier\") return undefined;\n return named(declarator.id.name, declarator.init);\n }\n return undefined;\n}\n\n/**\n * The module source with every component and exported `register` body replaced\n * by a constant — the ONE value the reload decision compares across an edit.\n *\n * Everything outside those bodies survives verbatim: imports, signatures,\n * module-level declarations, all six server exports, and the comments and\n * whitespace between them. So the skeleton is unchanged iff the save touched\n * nothing but refresh-safe bodies.\n *\n * Returns `undefined` when the source does not parse — a half-typed file whose\n * error Vite is already reporting from projection's real `transform`. The\n * caller leaves the cache holding the last GOOD skeleton, so the next\n * successful save is still compared against the right baseline.\n */\nfunction captureSkeleton(code: string): string | undefined {\n let ast: ReturnType<typeof parse>;\n try {\n ast = parse(code, { sourceType: \"module\", plugins: [\"typescript\", \"jsx\"] });\n } catch {\n return undefined;\n }\n\n const body = ast.program.body as any[];\n const serverReachable = serverReachableNames(body);\n const magic = new MagicString(code);\n\n for (const stmt of body) {\n const bodyNode = componentBodyToMask(stmt, serverReachable);\n if (!bodyNode) continue;\n const start = bodyNode.start as number;\n const end = bodyNode.end as number;\n if (end > start) magic.overwrite(start, end, MASKED_REFRESH_BODY);\n }\n\n return magic.toString();\n}\n\n/**\n * Serves the client page registry at {@link CLIENT_PAGE_REGISTRY_ID}.\n *\n * Discovery runs INSIDE `load`, once per `load` call, and its result is NOT\n * cached across builds — the plugin holds no state at all. A registry cached\n * past the moment a page file appears is a page that silently 404s until\n * someone restarts the dev server, which is a far more expensive bug than\n * re-walking a source tree. Rollup calls `load` once per module per build, and\n * in dev Vite's module graph caches the transformed result until the module is\n * invalidated, so the walk is not per-request either way. (Invalidating that\n * dev-server cache when a page file is ADDED needs a `handleHotUpdate`/watcher\n * hook that belongs with the dev provider slice — see the followup.)\n *\n * `enforce: \"pre\"` and placed FIRST in `warlockClientBoundary`'s array — see\n * that function's comment in `index.ts` for why position is what it is, and\n * `page-registry-plugin.spec.ts` for the real-build proof that the pages this\n * module names still reach `projection()`.\n */\nexport function clientPageRegistry(options: ClientPageRegistryPluginOptions = {}): Plugin {\n const appRoot = path.resolve(options.appRoot ?? process.cwd());\n\n // Per-plugin-instance, so two composed pipelines never cross-contaminate.\n // Holds the last captured SKELETON (source with refresh-safe bodies masked) of\n // each server page module the client environment transformed — the \"before\"\n // side of the comparison in `hotUpdate`. See `captureSkeleton` above.\n const skeletonCache: SkeletonCache = new Map();\n\n return {\n name: \"warlock:client-page-registry\",\n enforce: \"pre\",\n resolveId(source) {\n if (source === CLIENT_PAGE_REGISTRY_ID) return RESOLVED_CLIENT_PAGE_REGISTRY_ID;\n return undefined;\n },\n load(id) {\n if (id !== RESOLVED_CLIENT_PAGE_REGISTRY_ID) return undefined;\n\n const pages = discoverPages({ appRoot, srcDir: options.srcDir });\n\n return eraseTypes(generateClientRegistry({ pages, toImportSpecifier }));\n },\n /**\n * Capture-only spy. Records the SKELETON of every server page module the\n * CLIENT environment transforms, and returns nothing so projection's own\n * `transform` still does the real work. SERVE-ONLY:\n * `this.environment.mode !== \"dev\"` skips it during `vite build`, where\n * there is no `hotUpdate` to feed and the extra parse would be pure cost.\n */\n transform(code, id) {\n if (this.environment?.mode !== \"dev\") return undefined;\n if (!isServerPageModule(id)) return undefined;\n\n const skeleton = captureSkeleton(code);\n if (skeleton !== undefined) skeletonCache.set(id, skeleton);\n\n return undefined;\n },\n /**\n * Applies the ruling (canon `6b240682`): Fast Refresh ONLY when the only\n * changes are inside component or exported `register` bodies.\n *\n * - Skeleton moved (an import, a module-level declaration, ANY server\n * export — with or without a simultaneous JSX change) → full reload.\n * - Skeleton unchanged → defer to Fast Refresh, zero reloads.\n *\n * Note what is NOT here: no attempt to name which half a shared import or\n * local belongs to. That question is what produced the two previous stale\n * `<head>` bugs; this seam refuses to answer it and reloads instead.\n * `hotUpdate` exists only on the dev server, so this is serve-only by\n * construction.\n */\n async hotUpdate(context) {\n if (isServerPageModule(context.file)) {\n const routeGraphHandled = await options.beforePageHotUpdate?.({\n file: context.file,\n type: context.type,\n });\n\n if (routeGraphHandled) return [];\n }\n\n // `create`/`delete` are page graph churn, not in-place edits — leave them\n // to Vite's normal handling (a new/removed module reloads on its own).\n if (context.type !== \"update\") return undefined;\n if (!isServerPageModule(context.file)) return undefined;\n\n const nextSource = await context.read();\n const next = captureSkeleton(nextSource);\n const prev = skeletonCache.get(context.file);\n\n // Refresh the cache for the next edit regardless of the decision below.\n if (next !== undefined) skeletonCache.set(context.file, next);\n\n // Could not parse the new source (Vite is already reporting that error),\n // or the client environment never transformed this module — which means\n // the browser is not holding this page, so there is no stale `<head>` to\n // ship and nothing a reload of some OTHER page would fix.\n if (next === undefined || prev === undefined) return undefined;\n\n // Anything outside a component body moved: the browser cannot hot-swap\n // it, so reload the document to re-run SSR and re-render `<head>`.\n // `path: \"*\"` matches Vite's own middleware-mode reload.\n if (prev !== next) {\n this.environment.hot.send({ type: \"full-reload\", path: \"*\" });\n\n // Empty module list: we've issued the update ourselves, so Vite should\n // not additionally push a Fast Refresh for the client module.\n return [];\n }\n\n // Only component bodies moved: defer to Vite's Fast Refresh with zero\n // reloads. A no-op re-save falls through the same harmless path.\n return undefined;\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BA,MAAa,0BAA0B;;;;;AAMvC,MAAa,mCAAmC,KAAK;;;;;;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AO,CAEf;IAAG;GAAkB,CAAC,CAAC;EACxE;;;;;;;;EAQA,UAAU,MAAM,IAAI;GAClB,IAAI,KAAK,aAAa,SAAS,OAAO,OAAO;GAC7C,IAAI,CAAC,mBAAmB,EAAE,GAAG,OAAO;GAEpC,MAAM,WAAW,gBAAgB,IAAI;GACrC,IAAI,aAAa,QAAW,cAAc,IAAI,IAAI,QAAQ;EAG5D;;;;;;;;;;;;;;;EAeA,MAAM,UAAU,SAAS;GACvB,IAAI,mBAAmB,QAAQ,IAAI,GAMjC;QAAI,MAL4B,QAAQ,sBAAsB;KAC5D,MAAM,QAAQ;KACd,MAAM,QAAQ;IAChB,CAAC,GAEsB,OAAO,CAAC;GAAC;GAKlC,IAAI,QAAQ,SAAS,UAAU,OAAO;GACtC,IAAI,CAAC,mBAAmB,QAAQ,IAAI,GAAG,OAAO;GAG9C,MAAM,OAAO,gBAAgB,MADJ,QAAQ,KAAK,CACC;GACvC,MAAM,OAAO,cAAc,IAAI,QAAQ,IAAI;GAG3C,IAAI,SAAS,QAAW,cAAc,IAAI,QAAQ,MAAM,IAAI;GAM5D,IAAI,SAAS,UAAa,SAAS,QAAW,OAAO;GAKrD,IAAI,SAAS,MAAM;IACjB,KAAK,YAAY,IAAI,KAAK;KAAE,MAAM;KAAe,MAAM;IAAI,CAAC;IAI5D,OAAO,CAAC;GACV;EAKF;CACF;AACF"}
@@ -1,7 +1,7 @@
1
1
  import MagicString from "magic-string";
2
2
  import { Plugin } from "vite";
3
3
 
4
- //#region ../web/src/vite/projection.d.ts
4
+ //#region web/src/vite/projection.d.ts
5
5
  /**
6
6
  * Thrown when projection encounters an attribution-ambiguous top-level
7
7
  * statement (`c604f0bc` §3: "the compiler must not guess"). Carries the
@@ -23,7 +23,7 @@ interface ProjectionResult {
23
23
  /**
24
24
  * The client-build Vite plugin. Scoped to `*.page.tsx`/`layout.tsx`/`root.tsx`
25
25
  * and skipped entirely for the SSR build (`options.ssr`) — the server still
26
- * needs `route`/`middleware`/`validation`/`loader`/`metadata` intact.
26
+ * needs `route`/`middleware`/`validation`/`loader`/`metadata`/`prefix` intact.
27
27
  */
28
28
  declare function projection(): Plugin;
29
29
  //#endregion
@@ -2,18 +2,19 @@ import path from "node:path";
2
2
  import { parse } from "@babel/parser";
3
3
  import MagicString from "magic-string";
4
4
 
5
- //#region ../web/src/vite/projection.ts
5
+ //#region web/src/vite/projection.ts
6
6
  /**
7
7
  * Projection — the compile-time AST transform that strips a page module's
8
- * five server exports before the CLIENT graph forms.
8
+ * six server exports before the CLIENT graph forms.
9
9
  *
10
- * Removes `export const route/middleware/validation/loader/metadata = ...`
10
+ * Removes `export const route/middleware/validation/loader/metadata/prefix = ...`
11
11
  * (const-arrow form) and `export async function loader(...) {...}`
12
12
  * (function-declaration form — a page declares these as separate named
13
13
  * exports, not one fused object, so both forms are real), plus any import
14
14
  * OR top-level declaration that becomes
15
15
  * unreferenced ONLY as a result of that removal. The default export (the
16
- * page component) and every other non-server-named export survive
16
+ * page component) and every other non-server-named export — including the
17
+ * synchronous, no-argument `register()` lifecycle hook — survive
17
18
  * unconditionally, regardless of what they reference — classification is by
18
19
  * FILE, not by what an export does with data (`c604f0bc` §9).
19
20
  *
@@ -51,7 +52,8 @@ const SERVER_EXPORT_NAMES = new Set([
51
52
  "middleware",
52
53
  "validation",
53
54
  "loader",
54
- "metadata"
55
+ "metadata",
56
+ "prefix"
55
57
  ]);
56
58
  /**
57
59
  * Recognized client-safe assets that always survive projection untouched,
@@ -65,14 +67,18 @@ const ASSET_EXTENSION_RE = /\.(css|scss|sass|less|styl|stylus|svg|png|jpe?g|gif|
65
67
  /**
66
68
  * Top-level statement types that need no ambiguity check and are never
67
69
  * touched by removal: import declarations are handled by their own
68
- * survives/orphaned logic below, and every export (other than the 5 server
70
+ * survives/orphaned logic below, and every export (other than the 6 server
69
71
  * names) plus type-only declarations survive unconditionally per
70
72
  * `c604f0bc` §9 ("classify FILES, not the data they touch").
73
+ *
74
+ * `ExportAllDeclaration` (`export * from "./x"` / `export * as ns from
75
+ * "./x"`) is deliberately NOT in this set — it can forward ANY name from its
76
+ * source module, including a server export, and is refused explicitly below
77
+ * rather than assumed safe.
71
78
  */
72
79
  const ALWAYS_SAFE_STATEMENT_TYPES = new Set([
73
80
  "ExportNamedDeclaration",
74
81
  "ExportDefaultDeclaration",
75
- "ExportAllDeclaration",
76
82
  "TSInterfaceDeclaration",
77
83
  "TSTypeAliasDeclaration",
78
84
  "EmptyStatement"
@@ -134,7 +140,7 @@ function isServerExportDeclaration(stmt) {
134
140
  * Generic duck-typed AST walk (no `@babel/traverse` dependency — this
135
141
  * package only needs `@babel/parser` + `@babel/types`-shaped nodes).
136
142
  * Collects every `Identifier`/`JSXIdentifier` name reachable from `node`,
137
- * used to decide whether an import binding still has a reader once the 5
143
+ * used to decide whether an import binding still has a reader once the 6
138
144
  * server exports are gone. Over-collecting (e.g. counting an object
139
145
  * property key as a "use") only ever biases toward KEEPING an import, never
140
146
  * toward dropping one that is still needed — the safe direction for a
@@ -253,7 +259,7 @@ function statementSnippet(code, node) {
253
259
  return code.slice(node.start, node.end).split("\n")[0].trim();
254
260
  }
255
261
  /**
256
- * The transform itself: parse, remove the 5 server exports and every import
262
+ * The transform itself: parse, remove the 6 server exports and every import
257
263
  * orphaned only by that removal, fail closed on anything attribution-
258
264
  * ambiguous. `filePath` is only used for error messages (`c604f0bc` §7 —
259
265
  * fence errors must name the file).
@@ -273,6 +279,8 @@ function projectModule(code, filePath) {
273
279
  importDeclarations.push(stmt);
274
280
  continue;
275
281
  }
282
+ const isNamespaceReexport = stmt.type === "ExportNamedDeclaration" && stmt.source != null && stmt.specifiers?.some((specifier) => specifier.type === "ExportNamespaceSpecifier");
283
+ if (stmt.type === "ExportAllDeclaration" || isNamespaceReexport) throw new ProjectionAmbiguityError(filePath, statementSnippet(code, stmt), stmt.loc.start.line, `a star re-export forwards every name the source module exports, including possibly one of the 6 known server exports (route, middleware, validation, loader, metadata, prefix) — projection cannot inspect the source module's exports without parsing a second file, so it can't tell whether this leaks a server-only binding into the client bundle`, `replace the star re-export with explicit named re-exports (export { ComponentA, ComponentB } from "./source"), listing only the client-safe names`);
276
284
  if (isServerExportDeclaration(stmt)) {
277
285
  removedServerExports.push(stmt);
278
286
  continue;
@@ -287,7 +295,7 @@ function projectModule(code, filePath) {
287
295
  });
288
296
  continue;
289
297
  }
290
- throw new ProjectionAmbiguityError(filePath, statementSnippet(code, stmt), stmt.loc.start.line, `top-level executable code that declares nothing — outside the 5 known server exports (route, middleware, validation, loader, metadata), and binding no name, so projection has no reader to attribute it by and can't tell whether it belongs to the server or the client`, `mark it with an explicit .server/.client file, or move it inside one of the 5 declared server exports (if server-only) or the default export/a component (if client-safe)`);
298
+ throw new ProjectionAmbiguityError(filePath, statementSnippet(code, stmt), stmt.loc.start.line, `top-level executable code that declares nothing — outside the 6 known server exports (route, middleware, validation, loader, metadata, prefix), and binding no name, so projection has no reader to attribute it by and can't tell whether it belongs to the server or the client`, `move universal static declarations and their imports into export function register(), or mark the code with an explicit .server/.client file; server-only work can instead move inside one of the 6 declared server exports`);
291
299
  }
292
300
  const removedLocals = /* @__PURE__ */ new Set();
293
301
  /**
@@ -327,12 +335,12 @@ function projectModule(code, filePath) {
327
335
  }
328
336
  for (const local of localDeclarations) {
329
337
  if (local.removed || hasSurvivingReader(local, survivingNames)) continue;
330
- throw new ProjectionAmbiguityError(filePath, statementSnippet(code, local.stmt), local.stmt.loc.start.line, `a top-level declaration read only by the server exports being removed, but whose initializer executes code rather than just defining a value — projection can't tell whether that work is server-only or a side effect the client depends on`, `move the initializer inside the server export that reads it, or split it into an explicit .server/.client file`);
338
+ throw new ProjectionAmbiguityError(filePath, statementSnippet(code, local.stmt), local.stmt.loc.start.line, `a top-level declaration read only by the server exports being removed, but whose initializer executes code rather than just defining a value — projection can't tell whether that work is server-only or a side effect the client depends on`, `move universal static declarations and their imports into export function register(), move a server-only initializer inside the export that reads it, or split it into an explicit .server/.client file`);
331
339
  }
332
340
  for (const decl of importDeclarations) {
333
341
  const source = decl.source.value;
334
342
  if (isKnownSafeAsset(source)) continue;
335
- if (decl.specifiers.length === 0) throw new ProjectionAmbiguityError(filePath, statementSnippet(code, decl), decl.loc.start.line, `a bare side-effect import with no recognized client-safe asset extension — projection can't tell if it belongs only to the server exports being removed or must ship to the client`, `mark it with an explicit .server/.client file, or move it inside one of the 5 declared server exports if it's server-only`);
343
+ if (decl.specifiers.length === 0) throw new ProjectionAmbiguityError(filePath, statementSnippet(code, decl), decl.loc.start.line, `a bare side-effect import with no recognized client-safe asset extension — projection can't tell if it belongs only to the server exports being removed or must ship to the client`, `move universal static declarations and their imports into export function register(), or mark it with an explicit .server/.client file; server-only work can instead move inside one of the 6 declared server exports`);
336
344
  if (!decl.specifiers.some((spec) => survivingNames.has(spec.local.name))) removeStatement(s, code, decl);
337
345
  }
338
346
  for (const stmt of removedServerExports) removeStatement(s, code, stmt);
@@ -353,10 +361,24 @@ function isProjectableFile(id) {
353
361
  if (base === "root.tsx") return true;
354
362
  return false;
355
363
  }
364
+ const HMR_RUNTIME_SPECIFIER = "@warlock.js/web/client/runtime";
365
+ /**
366
+ * The projected module shares its scope with application source, so the helper
367
+ * import must not redeclare a name the application already owns. A suffix is
368
+ * only needed for the deliberately unlikely collision, but making it
369
+ * deterministic keeps the generated HMR module valid for every page shape.
370
+ */
371
+ function hmrRegisterModulesBinding(code) {
372
+ const base = "__warlockRegisterModules";
373
+ let binding = base;
374
+ let index = 2;
375
+ while (new RegExp(`\\b${binding}\\b`).test(code)) binding = `${base}${index++}`;
376
+ return binding;
377
+ }
356
378
  /**
357
379
  * The client-build Vite plugin. Scoped to `*.page.tsx`/`layout.tsx`/`root.tsx`
358
380
  * and skipped entirely for the SSR build (`options.ssr`) — the server still
359
- * needs `route`/`middleware`/`validation`/`loader`/`metadata` intact.
381
+ * needs `route`/`middleware`/`validation`/`loader`/`metadata`/`prefix` intact.
360
382
  */
361
383
  function projection() {
362
384
  return {
@@ -367,8 +389,9 @@ function projection() {
367
389
  if (!isProjectableFile(id)) return null;
368
390
  try {
369
391
  const { code: transformed, map } = projectModule(code, id);
392
+ const registerModules = hmrRegisterModulesBinding(transformed);
370
393
  return {
371
- code: transformed,
394
+ code: `import { registerModules as ${registerModules} } from "${HMR_RUNTIME_SPECIFIER}";\n${transformed}\nif (import.meta.hot) import.meta.hot.accept((replacement) => { if (replacement) ${registerModules}([replacement]); });\n`,
372
395
  map
373
396
  };
374
397
  } catch (error) {
@@ -380,5 +403,5 @@ function projection() {
380
403
  }
381
404
 
382
405
  //#endregion
383
- export { ProjectionAmbiguityError, SERVER_EXPORT_NAMES, projection };
406
+ export { ProjectionAmbiguityError, SERVER_EXPORT_NAMES, isProjectableFile, projectModule, projection };
384
407
  //# sourceMappingURL=projection.mjs.map