@kerf-js/start 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (236) hide show
  1. package/LICENSE +16 -0
  2. package/README.md +1468 -2
  3. package/crate/Cargo.lock +1347 -0
  4. package/crate/Cargo.toml +72 -0
  5. package/crate/README.md +322 -0
  6. package/crate/rust-toolchain.toml +3 -0
  7. package/crate/src/build/emit.rs +103 -0
  8. package/crate/src/build.rs +367 -0
  9. package/crate/src/ctx.rs +186 -0
  10. package/crate/src/envelope.rs +163 -0
  11. package/crate/src/functions.rs +173 -0
  12. package/crate/src/lib.rs +118 -0
  13. package/crate/src/native.rs +212 -0
  14. package/crate/src/otel.rs +62 -0
  15. package/crate/src/server.rs +203 -0
  16. package/package.json +123 -3
  17. package/src/app.mjs +136 -0
  18. package/src/asset-receipt.d.mts +13 -0
  19. package/src/asset-receipt.mjs +61 -0
  20. package/src/auth/better-auth.ts +280 -0
  21. package/src/auth/drizzle.ts +17 -0
  22. package/src/auth/emails.ts +44 -0
  23. package/src/auth.ts +62 -0
  24. package/src/boot.ts +57 -0
  25. package/src/builtin.tsx +19 -0
  26. package/src/bus/postgres.ts +104 -0
  27. package/src/bus.ts +51 -0
  28. package/src/cache.ts +4 -0
  29. package/src/capture.ts +50 -0
  30. package/src/changes-sql.d.mts +20 -0
  31. package/src/changes-sql.mjs +115 -0
  32. package/src/changes.ts +352 -0
  33. package/src/channel/connections.ts +216 -0
  34. package/src/channel/socket.ts +396 -0
  35. package/src/channel-fake.ts +70 -0
  36. package/src/channel.ts +250 -0
  37. package/src/client.tsx +27 -0
  38. package/src/contract/describe.mjs +237 -0
  39. package/src/contract.mjs +299 -0
  40. package/src/cron.ts +74 -0
  41. package/src/data/after.ts +57 -0
  42. package/src/data/cache.ts +354 -0
  43. package/src/data/definitions.ts +94 -0
  44. package/src/data/draft.ts +104 -0
  45. package/src/data/foundation.ts +95 -0
  46. package/src/data/live-api.ts +22 -0
  47. package/src/data/mutation.ts +138 -0
  48. package/src/data/primitives.ts +135 -0
  49. package/src/data/query.ts +106 -0
  50. package/src/data/transport.ts +123 -0
  51. package/src/data/types.ts +333 -0
  52. package/src/data.ts +125 -0
  53. package/src/db/bun-sqlite.d.ts +13 -0
  54. package/src/db/changes-mysql.ts +126 -0
  55. package/src/db/changes-postgres.ts +238 -0
  56. package/src/db/changes-sqlite.ts +183 -0
  57. package/src/db/core.ts +95 -0
  58. package/src/db/index.ts +43 -0
  59. package/src/db/mysql.ts +84 -0
  60. package/src/db/postgres.ts +102 -0
  61. package/src/db/sqlite.ts +166 -0
  62. package/src/derive.d.mts +19 -0
  63. package/src/derive.mjs +237 -0
  64. package/src/dev-runtime.mjs +46 -0
  65. package/src/email.ts +146 -0
  66. package/src/exec/errors.ts +87 -0
  67. package/src/exec/impl.ts +21 -0
  68. package/src/exec/landing.ts +66 -0
  69. package/src/exec/limit.ts +25 -0
  70. package/src/exec/once.ts +24 -0
  71. package/src/exec/scope.ts +328 -0
  72. package/src/exec/touch.ts +80 -0
  73. package/src/exec/validation.ts +176 -0
  74. package/src/exec/warm.ts +50 -0
  75. package/src/exec.ts +400 -0
  76. package/src/extension.ts +48 -0
  77. package/src/first-paint.ts +74 -0
  78. package/src/form/value.ts +15 -0
  79. package/src/form.tsx +223 -0
  80. package/src/gate.css +6 -0
  81. package/src/hub/channels.ts +102 -0
  82. package/src/hub/connections.ts +147 -0
  83. package/src/hub/delivery.ts +62 -0
  84. package/src/hub/feed.ts +168 -0
  85. package/src/hub/presence.ts +64 -0
  86. package/src/hub/route.ts +159 -0
  87. package/src/hub/subscriptions.ts +213 -0
  88. package/src/hub/topic.ts +98 -0
  89. package/src/hub/tracked.ts +12 -0
  90. package/src/hub/types.ts +190 -0
  91. package/src/hub/view.ts +147 -0
  92. package/src/hub.ts +196 -0
  93. package/src/index.ts +184 -0
  94. package/src/inline.mjs +359 -0
  95. package/src/inspect/compare.mjs +162 -0
  96. package/src/inspect/routes.mjs +154 -0
  97. package/src/inspect/samples.mjs +59 -0
  98. package/src/inspect/url.mjs +2 -0
  99. package/src/inspect.mjs +239 -0
  100. package/src/jobs.ts +223 -0
  101. package/src/link/election.ts +178 -0
  102. package/src/link/lines.ts +27 -0
  103. package/src/link.ts +385 -0
  104. package/src/list-drizzle.ts +136 -0
  105. package/src/list.ts +315 -0
  106. package/src/listen.ts +54 -0
  107. package/src/live/api.ts +18 -0
  108. package/src/live/backoff.ts +6 -0
  109. package/src/live/frames.ts +42 -0
  110. package/src/live/read.ts +77 -0
  111. package/src/live/sub-types.ts +24 -0
  112. package/src/live/transport.ts +86 -0
  113. package/src/live/types.ts +52 -0
  114. package/src/live-fake.ts +57 -0
  115. package/src/live-target.mjs +231 -0
  116. package/src/live.ts +390 -0
  117. package/src/media.ts +50 -0
  118. package/src/migrations.mjs +27 -0
  119. package/src/mux-session.ts +6 -0
  120. package/src/mux.ts +232 -0
  121. package/src/og/DefaultCard.tsx +91 -0
  122. package/src/og/fonts.mjs +72 -0
  123. package/src/og/images.mjs +124 -0
  124. package/src/og/index.mjs +222 -0
  125. package/src/og/native.mjs +36 -0
  126. package/src/range.mjs +31 -0
  127. package/src/router.ts +199 -0
  128. package/src/routes.mjs +193 -0
  129. package/src/run/admission.ts +67 -0
  130. package/src/run/boot.tsx +115 -0
  131. package/src/run/cache-presentation.ts +67 -0
  132. package/src/run/cache.ts +254 -0
  133. package/src/run/helpers.ts +197 -0
  134. package/src/run/listeners.ts +166 -0
  135. package/src/run/navigation.ts +385 -0
  136. package/src/run/page-skeleton.tsx +35 -0
  137. package/src/run/prefetch.ts +129 -0
  138. package/src/run/retain-view.ts +18 -0
  139. package/src/run/shape-preparation.ts +97 -0
  140. package/src/run/shape-styles.ts +72 -0
  141. package/src/run/source-context/pin.ts +124 -0
  142. package/src/run/source-context.ts +311 -0
  143. package/src/run.tsx +74 -0
  144. package/src/rust/common.ts +163 -0
  145. package/src/rust/native.ts +282 -0
  146. package/src/rust.ts +207 -0
  147. package/src/server/background.ts +34 -0
  148. package/src/server/card.tsx +65 -0
  149. package/src/server/compression.ts +115 -0
  150. package/src/server/core.ts +70 -0
  151. package/src/server/cron.ts +49 -0
  152. package/src/server/dispatch.ts +73 -0
  153. package/src/server/form.ts +92 -0
  154. package/src/server/functions.ts +133 -0
  155. package/src/server/handler.ts +57 -0
  156. package/src/server/head.ts +96 -0
  157. package/src/server/health.ts +36 -0
  158. package/src/server/jobs.ts +112 -0
  159. package/src/server/lifecycle.ts +48 -0
  160. package/src/server/log.ts +42 -0
  161. package/src/server/media.ts +61 -0
  162. package/src/server/navigation-cache.ts +75 -0
  163. package/src/server/navigation.tsx +86 -0
  164. package/src/server/page-cold.ts +96 -0
  165. package/src/server/page-stream.tsx +181 -0
  166. package/src/server/page-types.ts +51 -0
  167. package/src/server/page-warm.ts +131 -0
  168. package/src/server/page.ts +69 -0
  169. package/src/server/payload.ts +89 -0
  170. package/src/server/prerender.ts +64 -0
  171. package/src/server/preview.tsx +67 -0
  172. package/src/server/render.ts +197 -0
  173. package/src/server/request.ts +192 -0
  174. package/src/server/resolver.ts +228 -0
  175. package/src/server/routes.ts +150 -0
  176. package/src/server/security.ts +50 -0
  177. package/src/server/services.ts +69 -0
  178. package/src/server/shutdown.ts +23 -0
  179. package/src/server/text.ts +29 -0
  180. package/src/server/types.ts +170 -0
  181. package/src/server.tsx +138 -0
  182. package/src/shape.d.mts +3 -0
  183. package/src/shape.mjs +97 -0
  184. package/src/share/display.ts +50 -0
  185. package/src/share/network.ts +119 -0
  186. package/src/share/receive.ts +70 -0
  187. package/src/share/state.ts +30 -0
  188. package/src/share/tab-queue.ts +28 -0
  189. package/src/share-server.ts +293 -0
  190. package/src/share.ts +397 -0
  191. package/src/sheets.mjs +87 -0
  192. package/src/sidecar/build.mjs +170 -0
  193. package/src/sidecar/cargo.mjs +164 -0
  194. package/src/sidecar/common.mjs +17 -0
  195. package/src/sidecar/dev-build.mjs +91 -0
  196. package/src/sidecar/dev-watch.mjs +88 -0
  197. package/src/sidecar/manifest.mjs +19 -0
  198. package/src/sidecar/process.mjs +58 -0
  199. package/src/sidecar/refresh.mjs +19 -0
  200. package/src/sidecar.mjs +162 -0
  201. package/src/socket/check.ts +50 -0
  202. package/src/socket/deliver.ts +16 -0
  203. package/src/socket/publish.ts +56 -0
  204. package/src/socket/rooms.ts +41 -0
  205. package/src/socket/route.ts +79 -0
  206. package/src/socket/shared.ts +26 -0
  207. package/src/socket/types.ts +48 -0
  208. package/src/socket.ts +400 -0
  209. package/src/stream.ts +22 -0
  210. package/src/verify/app.ts +39 -0
  211. package/src/verify/cleanup.ts +27 -0
  212. package/src/verify/database.ts +11 -0
  213. package/src/verify/live.ts +63 -0
  214. package/src/verify/notify.ts +35 -0
  215. package/src/verify/skip.ts +19 -0
  216. package/src/verify/support.ts +149 -0
  217. package/src/verify/timing.ts +15 -0
  218. package/src/verify/trigger.ts +14 -0
  219. package/src/verify.ts +152 -0
  220. package/src/view.tsx +132 -0
  221. package/src/vite/asset-bytes.mjs +95 -0
  222. package/src/vite/assets.mjs +117 -0
  223. package/src/vite/config-hooks.mjs +166 -0
  224. package/src/vite/dev-hooks.mjs +103 -0
  225. package/src/vite/dev.mjs +102 -0
  226. package/src/vite/entries.mjs +149 -0
  227. package/src/vite/module-hooks.mjs +290 -0
  228. package/src/vite/route-modules.mjs +289 -0
  229. package/src/vite/server-functions.mjs +222 -0
  230. package/src/vite/shape-entry-css.mjs +187 -0
  231. package/src/vite/shape-override.mjs +226 -0
  232. package/src/vite/shared.mjs +55 -0
  233. package/src/vite/sheets.mjs +92 -0
  234. package/src/vite/state.mjs +89 -0
  235. package/src/vite/syntax.mjs +139 -0
  236. package/src/vite.mjs +202 -0
@@ -0,0 +1,139 @@
1
+ import { parseSync } from 'vite'
2
+
3
+ const SERVER_EXPORTS = new Set(['load', 'meta', 'prerender'])
4
+
5
+ /**
6
+ * The client build of a route module: `load`, `meta` and `prerender` removed (they run on the server; loaders can
7
+ * hold database code and secrets), then imports nothing else uses. Exported for tests.
8
+ */
9
+ function serverCuts(code, file) {
10
+ const ast = parseSync(file, code, {
11
+ lang: file.endsWith('.tsx') ? 'tsx' : file.endsWith('.ts') ? 'ts' : 'jsx',
12
+ sourceType: 'module',
13
+ })
14
+ const cuts = []
15
+ for (const node of ast.program.body) {
16
+ if (node.type !== 'ExportNamedDeclaration' || !node.declaration) continue
17
+ const d = node.declaration
18
+ const names =
19
+ d.type === 'VariableDeclaration' ? d.declarations.map((x) => x.id?.name) : [d.id?.name]
20
+ if (names.length && names.every((n) => SERVER_EXPORTS.has(n))) cuts.push([node.start, node.end])
21
+ }
22
+ return cuts
23
+ }
24
+
25
+ export const serverExports = (code, file) =>
26
+ serverCuts(code, file)
27
+ .map(([a, b]) => code.slice(a, b))
28
+ .join('\n')
29
+
30
+ /** The server-only exports of a route file, cut out of the client's copy. */
31
+ export function stripServerExports(code, file = 'route.tsx') {
32
+ const cuts = serverCuts(code, file)
33
+ if (!cuts.length) return code
34
+ let out = code
35
+ for (const [a, b] of cuts.sort((x, y) => y[0] - x[0])) out = out.slice(0, a) + out.slice(b)
36
+ // Imports whose bindings are no longer used go too (a db client a loader imported must not reach the browser).
37
+ return pruneImports(out, file)
38
+ }
39
+
40
+ export const parse = (file, code) =>
41
+ parseSync(file, code, {
42
+ lang: file.endsWith('.tsx') ? 'tsx' : /\.[cm]?ts$/.test(file) ? 'ts' : 'jsx',
43
+ sourceType: 'module',
44
+ })
45
+
46
+ /** The TS nodes that hold code; every other `TS*` node is a type, gone once TypeScript is stripped. */
47
+ const TS_VALUE =
48
+ /^TS((As|Satisfies|NonNull|Instantiation)Expression|TypeAssertion|Enum\w+|Module\w+|ImportEqualsDeclaration|ExternalModuleReference|ParameterProperty|ExportAssignment)$/
49
+ const NAMED_MEMBER = /^(Property|MethodDefinition|PropertyDefinition|AccessorProperty)$/
50
+ const binds = (p) =>
51
+ !p
52
+ ? []
53
+ : p.type === 'Identifier'
54
+ ? [p.name]
55
+ : (p.properties ?? p.elements ?? [p.value ?? p.left ?? p.argument ?? p.parameter]).flatMap(
56
+ binds,
57
+ )
58
+ const declares = (s) =>
59
+ s.type === 'VariableDeclaration'
60
+ ? s.declarations.flatMap((d) => binds(d.id))
61
+ : /^(Function|Class)Declaration$/.test(s.type) && s.id
62
+ ? [s.id.name]
63
+ : []
64
+
65
+ /**
66
+ * The outer names `ast` reads at run time. Not types (`typeof x` in one reads nothing), property names, or what a
67
+ * function or block binds itself: `optimistic: ({ input }) => …` doesn't use a top-level `input`. It errs towards a name
68
+ * being used: a kept import is a bigger bundle, a dropped one a crash.
69
+ */
70
+ export function valueNames(ast) {
71
+ const out = new Set()
72
+ const walk = (n, bound) => {
73
+ if (Array.isArray(n)) return n.forEach((x) => walk(x, bound))
74
+ if (
75
+ typeof n?.type !== 'string' ||
76
+ n.type === 'ImportDeclaration' ||
77
+ n.exportKind === 'type' ||
78
+ (n.type.startsWith('TS') && !TS_VALUE.test(n.type))
79
+ )
80
+ return
81
+ if (n.type === 'Identifier' || n.type === 'JSXIdentifier') {
82
+ if (!bound.has(n.name)) out.add(n.name)
83
+ return
84
+ }
85
+ const inner = /^(Arrow)?Function/.test(n.type)
86
+ ? [...(n.id ? [n.id.name] : []), ...n.params.flatMap(binds)]
87
+ : n.type === 'BlockStatement'
88
+ ? n.body.flatMap(declares)
89
+ : []
90
+ const scope = inner.length ? new Set([...bound, ...inner]) : bound
91
+ const skip = n.computed
92
+ ? undefined
93
+ : n.type === 'MemberExpression'
94
+ ? 'property'
95
+ : NAMED_MEMBER.test(n.type)
96
+ ? 'key'
97
+ : undefined
98
+ for (const k in n)
99
+ if (k !== skip && k !== 'parent' && n[k] && typeof n[k] === 'object') walk(n[k], scope)
100
+ }
101
+ walk(ast.program.body, new Set())
102
+ return out
103
+ }
104
+
105
+ /** Drop value import specifiers nothing in `out` reads (and imports left empty). */
106
+ export function pruneImports(out, file) {
107
+ const again = parse(file, out)
108
+ const imports = again.program.body.filter(
109
+ (n) => n.type === 'ImportDeclaration' && n.specifiers.length,
110
+ )
111
+ const used = valueNames(again)
112
+ let rest = out
113
+ for (const imp of imports)
114
+ rest = rest.slice(0, imp.start) + ' '.repeat(imp.end - imp.start) + rest.slice(imp.end)
115
+ const spelled = new Set(rest.match(/[A-Za-z_$][\w$]*/g)) // a type import is erased, so a type may keep it
116
+ for (const imp of imports.sort((x, y) => y.start - x.start)) {
117
+ const keep = imp.specifiers.filter((s) =>
118
+ (imp.importKind === 'type' || s.importKind === 'type' ? spelled : used).has(s.local.name),
119
+ )
120
+ if (keep.length === imp.specifiers.length) continue
121
+ const text = keep.length ? rewriteImport(imp, keep, out) : ''
122
+ out = out.slice(0, imp.start) + text + out.slice(imp.end)
123
+ }
124
+ return out
125
+ }
126
+
127
+ function rewriteImport(imp, keep, code) {
128
+ const def = keep.find((s) => s.type === 'ImportDefaultSpecifier')
129
+ const ns = keep.find((s) => s.type === 'ImportNamespaceSpecifier')
130
+ const named = keep
131
+ .filter((s) => s.type === 'ImportSpecifier')
132
+ .map((s) => code.slice(s.start, s.end))
133
+ const parts = [
134
+ def && def.local.name,
135
+ ns && `* as ${ns.local.name}`,
136
+ named.length && `{ ${named.join(', ')} }`,
137
+ ].filter(Boolean)
138
+ return `import ${imp.importKind === 'type' ? 'type ' : ''}${parts.join(', ')} from ${code.slice(imp.source.start, imp.source.end)}`
139
+ }
package/src/vite.mjs ADDED
@@ -0,0 +1,202 @@
1
+ // kerfStart() for Vite 8 (research 25 §6): file routes as virtual modules, the server and client entries Nitro's Vite
2
+ // plugin builds (`nitro()` from `nitro/vite` goes next to it), and route modules without their server exports in
3
+ // the client build. Used through `kerf dev` / `kerf build`, which add kerf() and nitro() and pass the config's `site`.
4
+ //
5
+ // virtual:kerf/routes server: the manifest, every route, layout and card module, and each one's stylesheets
6
+ // client: the route table and a chunk loader per route id (load/meta/prerender stripped)
7
+ // virtual:kerf/app server: site config, build id, card fonts (bytes, so every preset can read them)
8
+ // virtual:kerf/entry-server the Nitro SSR entry: `export default { fetch }`
9
+ // virtual:kerf/entry-client the client entry: `start()`, unless the app has src/entry.client.ts
10
+ // virtual:kerf/fns server: imports every server module, so `/_kerf/fn/<id>` knows every function
11
+ // virtual:kerf/context server: `src/context.server.ts`'s default export (the app's part of `ctx`)
12
+ // virtual:kerf/auth server: `src/auth.server.ts`'s default export, mounted at its base path
13
+ // virtual:kerf/migrations server: `drizzle/`'s migrations, embedded (a bundle can't read a folder on every preset)
14
+ // virtual:kerf/media server: the `media.server` provider when one is bound (research 29 §6)
15
+ // virtual:kerf/realtime server: `src/realtime.server.ts`'s default export (the bus, origins and limits)
16
+ // virtual:kerf/cache server: `src/cache.server.ts`'s default export (the shared cache's store)
17
+ // virtual:kerf/cron server: `src/cron.server.ts`'s default export (jobs on a schedule)
18
+ // virtual:kerf/email server: `src/email.server.ts`'s default export (the mailer)
19
+ // virtual:kerf/jobs server: `src/jobs.server.ts`'s default export (the job queue)
20
+ // virtual:kerf/extensions server: each `server.extension` provider with its `src/<key>.server.ts` (extension.ts)
21
+ //
22
+ // Server modules (`src/server/**`, `*.server.ts`, research 27 §2): each `query`/`mutation`/`stream`/`channel` export
23
+ // gets an `$id` on both sides; the client build replaces the module with stubs that keep only `optimistic`, the
24
+ // query's shape and a shared state's `initial`.
25
+ //
26
+ // Stylesheets: routes are chunks the entry only reaches through `import()`, so `?assets=client` names just the entry's
27
+ // CSS. Each route and layout's own CSS comes from Nitro's assets manifest (build) or the client module graph (dev);
28
+ // the server links the entry's and those of the levels it matched, so nothing paints before its styles and other
29
+ // routes' CSS stays out of the page. In dev the links stand in for Vite's injected `<style>`s and HMR updates them.
30
+ // A build keeps each chunk's sheet for client navigation and writes a shared sheet (what every route has) and one sheet
31
+ // per route for the rest, hashed and cached for good. Each page carries the part of those its first paint needs
32
+ // (`firstPaint`) and links them at the end of its shell, where they block nothing above and hold whatever streams after.
33
+ // `site: { criticalCss: false }` splits by chunk and puts them in the HTML (`inlineSheets`, inline.mjs): whole while the
34
+ // document fits the first flight, else the rules the page's HTML reaches, with the sheets behind them as links that
35
+ // don't hold the paint. `site: { inlineCss: false }` keeps plain links.
36
+
37
+ import { existsSync } from 'node:fs'
38
+ import { relative } from 'node:path'
39
+ import { createState } from './vite/state.mjs'
40
+ import { shapeSignature } from './vite/route-modules.mjs'
41
+ import { entryCandidate, entryCssSignature, entryWatchLifecycle } from './vite/shape-entry-css.mjs'
42
+ import { V } from './vite/shared.mjs'
43
+ import { configHook, writeBundle, renderChunk, configResolved } from './vite/config-hooks.mjs'
44
+ import { configureServer, hotUpdate } from './vite/dev-hooks.mjs'
45
+ import { resolveId, load, transform, api } from './vite/module-hooks.mjs'
46
+ export { assetsOnly } from './vite/assets.mjs'
47
+ export { inlineSheets, sheetPieces } from './vite/sheets.mjs'
48
+ export { stripServerExports } from './vite/syntax.mjs'
49
+ export { isServerModule, fnId, withIds, clientStubs } from './vite/server-functions.mjs'
50
+ export { scanRoutes, routeTypes } from './routes.mjs'
51
+ export { here as startDir } from './vite/shared.mjs'
52
+
53
+ /**
54
+ * A signature of what a save means for the shapes, or undefined when it doesn't parse. A broken save takes ordinary HMR
55
+ * and its overlay: reloading onto it would leave the tab on the server's error page, which no fix reaches.
56
+ */
57
+ const parsed = (signature, file) => {
58
+ try {
59
+ return signature(file)
60
+ } catch {
61
+ return undefined
62
+ }
63
+ }
64
+
65
+ /** Reload when an edit changes shape metadata embedded in the client route module. */
66
+ export function shapeHotUpdate(state, modes, next) {
67
+ return async function (update) {
68
+ if (this.environment.config.consumer === 'client') {
69
+ const pending =
70
+ state.shapeScanActive === state.shapeScanGeneration && state.shapeScanActive !== undefined
71
+ if (pending) state.shapeScanGeneration++
72
+ const owners = [...(state.shapeOverrideDependencies ?? [])].flatMap(([file, routes]) =>
73
+ entryCandidate(new Set([file]), update.file) ? [...routes] : [],
74
+ )
75
+ if (owners.length) {
76
+ if (
77
+ update.type === 'update' &&
78
+ entryCandidate(state.shapeEntryMissing, update.file) &&
79
+ existsSync(update.file)
80
+ )
81
+ await this.environment.pluginContainer.watchChange(update.file, { event: 'create' })
82
+ const graph = this.environment.moduleGraph
83
+ for (const file of owners) {
84
+ const id = `\0virtual:kerf/shape-override/${encodeURIComponent(file)}.tsx`
85
+ const asset = graph.getModuleById(id)
86
+ if (asset) graph.invalidateModule(asset)
87
+ }
88
+ const mod = graph.getModuleById('\0' + V.routes)
89
+ if (mod) graph.invalidateModule(mod)
90
+ state.vite?.ws.send({ type: 'full-reload' })
91
+ return []
92
+ }
93
+ const rel = relative(state.root, update.file).split('\\').join('/')
94
+ const entrySignatures = state.shapeEntrySignatures
95
+ if (entrySignatures?.has(update.file)) {
96
+ const signature = parsed(entryCssSignature, update.file)
97
+ const previous = entrySignatures.get(update.file)
98
+ if (signature !== undefined) entrySignatures.set(update.file, signature)
99
+ if (signature !== undefined && previous !== signature) {
100
+ const graph = this.environment.moduleGraph
101
+ const mod = graph.getModuleById('\0' + V.routes)
102
+ if (mod) graph.invalidateModule(mod)
103
+ state.vite?.ws.send({ type: 'full-reload' })
104
+ return []
105
+ }
106
+ }
107
+ const candidateEvent =
108
+ (update.type === 'create' || update.type === 'delete') &&
109
+ entryCandidate(state.shapeEntryCandidates, update.file)
110
+ const missingUpdate =
111
+ update.type === 'update' && entryCandidate(state.shapeEntryMissing, update.file)
112
+ if (candidateEvent || missingUpdate) {
113
+ // A watched missing path may arrive as an update; Vite clears negative resolution on create.
114
+ if (missingUpdate && existsSync(update.file))
115
+ await this.environment.pluginContainer.watchChange(update.file, { event: 'create' })
116
+ const graph = this.environment.moduleGraph
117
+ const mod = graph.getModuleById('\0' + V.routes)
118
+ if (mod) graph.invalidateModule(mod)
119
+ state.vite?.ws.send({ type: 'full-reload' })
120
+ return []
121
+ }
122
+ const levels = [...state.manifest.routes, ...state.manifest.layouts]
123
+ if (levels.some((level) => level.file === rel) || state.clientEntry?.() === update.file) {
124
+ const mode = parsed(shapeSignature, update.file)
125
+ const before = modes.get(rel)
126
+ if (mode !== undefined) modes.set(rel, mode)
127
+ if (mode !== undefined && before !== mode) {
128
+ const graph = this.environment.moduleGraph
129
+ const mod = graph.getModuleById('\0' + V.routes)
130
+ if (mod) graph.invalidateModule(mod)
131
+ state.vite?.ws.send({ type: 'full-reload' })
132
+ return []
133
+ }
134
+ }
135
+ if (pending) {
136
+ const graph = this.environment.moduleGraph
137
+ const mod = graph.getModuleById('\0' + V.routes)
138
+ if (mod) graph.invalidateModule(mod)
139
+ state.vite?.ws.send({ type: 'full-reload' })
140
+ return []
141
+ }
142
+ }
143
+ return next.call(this, update)
144
+ }
145
+ }
146
+
147
+ /**
148
+ * @param {{ root?: string, site?: import('@kerf-js/core/ssr').Site, routesDir?: string, timeout?: number, hold?: number, fonts?: string[], hydration?: 'lazy' | 'partial' | 'full' }} [options]
149
+ */
150
+ export default function kerfStart(options = {}) {
151
+ const state = createState(options)
152
+ const modes = new Map()
153
+ const configure = configureServer(state)
154
+ return [
155
+ {
156
+ name: 'kerf:start',
157
+ enforce: 'pre',
158
+ config: configHook(state),
159
+ configResolved: configResolved(state),
160
+ api: api(state),
161
+ },
162
+ {
163
+ name: 'kerf:start:sheets',
164
+ enforce: 'pre',
165
+ writeBundle: writeBundle(state),
166
+ renderChunk: renderChunk(state),
167
+ },
168
+ {
169
+ name: 'kerf:start:dev',
170
+ enforce: 'pre',
171
+ configureServer(server) {
172
+ for (const level of [...state.manifest.routes, ...state.manifest.layouts]) {
173
+ try {
174
+ modes.set(level.file, shapeSignature(state.abs(level.file)))
175
+ } catch {
176
+ modes.set(level.file, 'invalid')
177
+ }
178
+ }
179
+ const entry = state.clientEntry()
180
+ if (!entry.startsWith('virtual:')) {
181
+ const rel = relative(state.root, entry).split('\\').join('/')
182
+ try {
183
+ modes.set(rel, shapeSignature(entry))
184
+ } catch {
185
+ modes.set(rel, 'invalid')
186
+ }
187
+ }
188
+ const configured = configure(server)
189
+ entryWatchLifecycle(state, server, modes)
190
+ return configured
191
+ },
192
+ hotUpdate: shapeHotUpdate(state, modes, hotUpdate(state)),
193
+ },
194
+ {
195
+ name: 'kerf:start:modules',
196
+ enforce: 'pre',
197
+ resolveId: resolveId(state),
198
+ load: load(state),
199
+ transform: transform(state),
200
+ },
201
+ ]
202
+ }