@kerf-js/start 0.1.2-nightly.20261118.85 → 0.1.3
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.
- package/package.json +2 -2
- package/src/app.mjs +42 -36
- package/src/contract.mjs +3 -11
- package/src/dev-runtime.mjs +103 -34
- package/src/inspect/server.mjs +16 -0
- package/src/inspect.mjs +4 -11
- package/src/server/form.ts +4 -3
- package/src/server/functions.ts +6 -3
- package/src/server/node-preset.mjs +4 -2
- package/src/server/origin.ts +33 -0
- package/src/sidecar/dev-watch.mjs +32 -12
- package/src/vite/build-paths.d.mts +2 -0
- package/src/vite/build-paths.mjs +45 -0
- package/src/vite/config-hooks.mjs +4 -1
- package/src/vite/dev-hooks.mjs +16 -8
- package/src/vite/entries.mjs +7 -2
- package/src/vite/module-hooks.mjs +15 -3
- package/src/vite/nitro-output.d.mts +8 -0
- package/src/vite/nitro-output.mjs +291 -0
- package/src/vite/output-manifests.d.mts +7 -0
- package/src/vite/output-manifests.mjs +75 -0
- package/src/vite/route-modules.mjs +3 -3
- package/src/vite/shared.mjs +18 -1
- package/src/vite/sheets.mjs +13 -8
- package/src/vite/state.mjs +16 -1
- package/src/vite/trace-deps.mjs +77 -0
- package/src/vite.mjs +22 -35
- package/src/watch-tree.mjs +26 -0
package/src/vite/state.mjs
CHANGED
|
@@ -5,6 +5,8 @@ import { scanRoutes } from '../routes.mjs'
|
|
|
5
5
|
import { scanServerModules } from './server-functions.mjs'
|
|
6
6
|
import { usesShapes } from './dev.mjs'
|
|
7
7
|
import { PRELOAD, V } from './shared.mjs'
|
|
8
|
+
import { buildPaths } from './build-paths.mjs'
|
|
9
|
+
import { manifestKey } from './output-manifests.mjs'
|
|
8
10
|
|
|
9
11
|
/** Partial hydration's view of the project, redone in dev when a file it read changed. */
|
|
10
12
|
async function hydration(s, ctx) {
|
|
@@ -27,7 +29,7 @@ async function hydration(s, ctx) {
|
|
|
27
29
|
entries,
|
|
28
30
|
lazy: (options.hydration ?? 'lazy') === 'lazy',
|
|
29
31
|
transform: s.compile,
|
|
30
|
-
rel: (f) => relative(s.root, f).split('\\').join('/'),
|
|
32
|
+
rel: s.dev ? (f) => relative(s.root, f).split('\\').join('/') : s.pathFor,
|
|
31
33
|
cache: s.facts,
|
|
32
34
|
resolve: async (spec, from) => (await ctx.resolve(spec, from, { skipSelf: true }))?.id,
|
|
33
35
|
})
|
|
@@ -80,6 +82,19 @@ export function createState(options) {
|
|
|
80
82
|
}
|
|
81
83
|
s.abs = (f) => resolve(s.root, f)
|
|
82
84
|
s.id = (v) => (v.startsWith('virtual:') ? '\0' + v : v)
|
|
85
|
+
s.resetPaths = () => {
|
|
86
|
+
s.pathFor = buildPaths(s.root)
|
|
87
|
+
s.assetSources = new Map()
|
|
88
|
+
}
|
|
89
|
+
s.resetPaths()
|
|
90
|
+
s.assetKey = (id) => manifestKey(s.pathFor, id)
|
|
91
|
+
s.assetSource = (file) => {
|
|
92
|
+
const id = `virtual:kerf/asset-source/${encodeURIComponent(s.pathFor(file))}`
|
|
93
|
+
const existing = s.assetSources.get(id)
|
|
94
|
+
if (existing && existing !== file) throw new Error(`Duplicate Kerf asset source ${id}`)
|
|
95
|
+
s.assetSources.set(id, file)
|
|
96
|
+
return id
|
|
97
|
+
}
|
|
83
98
|
s.scan = () => {
|
|
84
99
|
s.serverModules = scanServerModules(s.root)
|
|
85
100
|
s.manifest = scanRoutes(
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// The packages a built server loads by name at runtime, which Nitro can't see to copy into `.output`. Nitro's `traceDeps`
|
|
2
|
+
// keeps each external and traces it into the build; nf3 warns for an entry no installed package resolves ("could not
|
|
3
|
+
// resolve `traceInclude` entry … from any root"), so each one here is listed only when the app has it.
|
|
4
|
+
|
|
5
|
+
import { existsSync, readFileSync, realpathSync } from 'node:fs'
|
|
6
|
+
import { dirname, join } from 'node:path'
|
|
7
|
+
|
|
8
|
+
const readJson = (file) => {
|
|
9
|
+
try {
|
|
10
|
+
return JSON.parse(readFileSync(file, 'utf8'))
|
|
11
|
+
} catch {
|
|
12
|
+
return null
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Where `name` is installed for code in any of `from`, Node's way: `node_modules/<name>` in each directory up. */
|
|
17
|
+
export function packageDir(name, from) {
|
|
18
|
+
for (let dir of from) {
|
|
19
|
+
for (;;) {
|
|
20
|
+
const at = join(dir, 'node_modules', name)
|
|
21
|
+
if (existsSync(join(at, 'package.json'))) return realpathSync(at)
|
|
22
|
+
const up = dirname(dir)
|
|
23
|
+
if (up === dir) break
|
|
24
|
+
dir = up
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return null
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The plugin a generated slot file names, or null: `kerf plugins sync` writes `provider = null` for an unfilled slot. */
|
|
31
|
+
function slotProvider(root, slot) {
|
|
32
|
+
const file = join(root, '.kerf', 'slots', slot)
|
|
33
|
+
if (!existsSync(file)) return null
|
|
34
|
+
const text = readFileSync(file, 'utf8')
|
|
35
|
+
if (/^export const provider = null$/m.test(text)) return null
|
|
36
|
+
return /["']?plugin["']?\s*:\s*["']([^"']+)["']/.exec(text)?.[1] ?? ''
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Nitro's `traceDeps` for the app: the media route's sharp, and css-tree and csso under svgo, which read JSON from their
|
|
41
|
+
* packages, when a plugin provides the media slot; and nodemailer for SMTP.
|
|
42
|
+
*/
|
|
43
|
+
export function traceDeps(root) {
|
|
44
|
+
const pkg = readJson(join(root, 'package.json')) ?? {}
|
|
45
|
+
const direct = Object.keys({ ...pkg.dependencies, ...pkg.devDependencies })
|
|
46
|
+
.map((name) => packageDir(name, [root]))
|
|
47
|
+
.filter(Boolean)
|
|
48
|
+
// what nf3 resolves an entry from: the app, and the packages that declare it (one of the app's own, or theirs)
|
|
49
|
+
const bases = [root, ...direct]
|
|
50
|
+
const deps = []
|
|
51
|
+
const media = slotProvider(root, 'media.server.ts')
|
|
52
|
+
if (media !== null) {
|
|
53
|
+
const provider = media && packageDir(media, bases)
|
|
54
|
+
const from = provider ? [provider, ...bases] : bases
|
|
55
|
+
const svgo = packageDir('svgo', from)
|
|
56
|
+
for (const name of ['css-tree', 'csso'])
|
|
57
|
+
if (packageDir(name, svgo ? [svgo, ...from] : from)) deps.push(`${name}*`)
|
|
58
|
+
if (packageDir('sharp', from)) deps.push('sharp*')
|
|
59
|
+
}
|
|
60
|
+
if (existsSync(join(root, 'node_modules/nodemailer'))) deps.push('nodemailer*')
|
|
61
|
+
return deps.length ? { traceDeps: deps } : {}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Takumi's addon, which every server's share cards import by name (`og/native.mjs`), and Bun's built-in modules
|
|
66
|
+
* (`bun:sqlite`, imported only under Bun), as externals of Nitro's prerender bundle. That bundle resolves without
|
|
67
|
+
* tracing and on Node, so it called each import UNRESOLVED_IMPORT: Takumi sits beside @kerf-js/start under pnpm (the
|
|
68
|
+
* prerender's `native()` loads that copy), and Node has no `bun:` modules. The server's bundle is left to Nitro, which
|
|
69
|
+
* traces the addon into `.output`.
|
|
70
|
+
*/
|
|
71
|
+
export const externalAddons = () => ({
|
|
72
|
+
name: 'kerf:start:external-addons',
|
|
73
|
+
resolveId: {
|
|
74
|
+
filter: { id: /^(?:@takumi-rs\/core|bun:[\w/-]+)$/ },
|
|
75
|
+
handler: (id) => ({ id, external: true }),
|
|
76
|
+
},
|
|
77
|
+
})
|
package/src/vite.mjs
CHANGED
|
@@ -1,38 +1,7 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
//
|
|
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.
|
|
1
|
+
// Start supplies Nitro's virtual entries and the file-routes manifest. Client modules reach
|
|
2
|
+
// routes through dynamic imports and drop server-only exports and function bodies.
|
|
3
|
+
// Route CSS follows the matched layouts and routes from the client graph in dev, or Nitro's
|
|
4
|
+
// asset manifest in a build. Dev links let Vite swap the existing sheets during HMR.
|
|
36
5
|
|
|
37
6
|
import { existsSync } from 'node:fs'
|
|
38
7
|
import { relative } from 'node:path'
|
|
@@ -49,6 +18,8 @@ import {
|
|
|
49
18
|
} from './vite/config-hooks.mjs'
|
|
50
19
|
import { configureServer, hotUpdate } from './vite/dev-hooks.mjs'
|
|
51
20
|
import { resolveId, load, transform, api } from './vite/module-hooks.mjs'
|
|
21
|
+
import { normalizeViteAssets } from './vite/nitro-output.mjs'
|
|
22
|
+
import { sourceMapOptions, viteManifests } from './vite/output-manifests.mjs'
|
|
52
23
|
export { assetsOnly } from './vite/assets.mjs'
|
|
53
24
|
export { inlineSheets, sheetPieces } from './vite/sheets.mjs'
|
|
54
25
|
export { stripServerExports } from './vite/syntax.mjs'
|
|
@@ -162,6 +133,7 @@ export default function kerfStart(options = {}) {
|
|
|
162
133
|
config: configHook(state),
|
|
163
134
|
configResolved: configResolved(state),
|
|
164
135
|
api: api(state),
|
|
136
|
+
buildApp: normalizeViteAssets(state),
|
|
165
137
|
},
|
|
166
138
|
{
|
|
167
139
|
name: 'kerf:start:sheets',
|
|
@@ -194,6 +166,9 @@ export default function kerfStart(options = {}) {
|
|
|
194
166
|
entryWatchLifecycle(state, server, modes)
|
|
195
167
|
return configured
|
|
196
168
|
},
|
|
169
|
+
closeServer() {
|
|
170
|
+
state.closeRoutes?.()
|
|
171
|
+
},
|
|
197
172
|
hotUpdate: shapeHotUpdate(state, modes, hotUpdate(state)),
|
|
198
173
|
},
|
|
199
174
|
{
|
|
@@ -203,6 +178,18 @@ export default function kerfStart(options = {}) {
|
|
|
203
178
|
load: load(state),
|
|
204
179
|
transform: transform(state),
|
|
205
180
|
},
|
|
181
|
+
{
|
|
182
|
+
name: 'kerf:start:output-manifests',
|
|
183
|
+
apply: 'build',
|
|
184
|
+
enforce: 'post',
|
|
185
|
+
generateBundle: viteManifests(state),
|
|
186
|
+
outputOptions(output) {
|
|
187
|
+
if (this.environment.config.consumer === 'client')
|
|
188
|
+
return sourceMapOptions(output, state.pathFor)
|
|
189
|
+
if (this.environment.name !== 'nitro' && output.sourcemap)
|
|
190
|
+
return { ...output, sourcemapExcludeSources: true }
|
|
191
|
+
},
|
|
192
|
+
},
|
|
206
193
|
]
|
|
207
194
|
}
|
|
208
195
|
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// A recursive `fs.watch` that can fail without taking the process with it. A sandbox or a busy machine limits
|
|
2
|
+
// watchers (EMFILE, ENOSPC), and an FSWatcher's 'error' with no listener is an uncaught exception.
|
|
3
|
+
|
|
4
|
+
import { watch } from 'node:fs'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* `fs.watch(dir, { recursive: true }, listener)`, or null when it can't watch. Whether it can't start or fails later,
|
|
8
|
+
* `warn` hears why once and the watcher is closed: the caller goes on without it.
|
|
9
|
+
*/
|
|
10
|
+
export function watchTree(dir, listener, warn) {
|
|
11
|
+
let watcher
|
|
12
|
+
try {
|
|
13
|
+
watcher = watch(dir, { recursive: true }, listener)
|
|
14
|
+
} catch (error) {
|
|
15
|
+
warn(error)
|
|
16
|
+
return null
|
|
17
|
+
}
|
|
18
|
+
watcher.on('error', (error) => {
|
|
19
|
+
warn(error)
|
|
20
|
+
watcher.close()
|
|
21
|
+
})
|
|
22
|
+
return watcher
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** What a watcher's failure was, for a warning: its code (EMFILE) when it has one. */
|
|
26
|
+
export const watchFailure = (error) => error?.code ?? error?.message ?? String(error)
|