fondu 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.
- package/LICENSE +21 -0
- package/README.md +168 -2
- package/dist/client.d.ts +19 -0
- package/dist/client.js +4 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +9 -0
- package/dist/paths.d.ts +15 -0
- package/dist/paths.js +46 -0
- package/dist/server/app.d.ts +32 -0
- package/dist/server/app.js +130 -0
- package/dist/server/document.d.ts +23 -0
- package/dist/server/document.js +34 -0
- package/dist/server/errors.d.ts +8 -0
- package/dist/server/errors.js +73 -0
- package/dist/server/flash.d.ts +18 -0
- package/dist/server/flash.js +63 -0
- package/dist/server/handler.d.ts +25 -0
- package/dist/server/handler.js +20 -0
- package/dist/server/http.d.ts +13 -0
- package/dist/server/http.js +25 -0
- package/dist/server/props.d.ts +81 -0
- package/dist/server/props.js +28 -0
- package/dist/server/protocol.d.ts +50 -0
- package/dist/server/protocol.js +174 -0
- package/dist/server/validate.d.ts +58 -0
- package/dist/server/validate.js +110 -0
- package/dist/types.d.ts +15 -0
- package/dist/types.js +1 -0
- package/dist/virtual.d.ts +13 -0
- package/dist/vite/index.d.ts +19 -0
- package/dist/vite/index.js +271 -0
- package/dist/vite/typegen.d.ts +5 -0
- package/dist/vite/typegen.js +64 -0
- package/package.json +63 -4
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Filled in by the generated `app/fondu.gen.d.ts` (written by the Vite plugin, committed with the app):
|
|
3
|
+
* `pages`: page name → its module, `routes`: URL pattern → its route module.
|
|
4
|
+
*
|
|
5
|
+
* `c.get()` / `c.set()` are typed by Hono's own `ContextVariableMap`; shared props and flash by Inertia's
|
|
6
|
+
* `InertiaConfig`.
|
|
7
|
+
*/
|
|
8
|
+
export interface Register {
|
|
9
|
+
}
|
|
10
|
+
export type Pages = Register extends {
|
|
11
|
+
pages: infer P;
|
|
12
|
+
} ? P : Record<string, unknown>;
|
|
13
|
+
export type Routes = Register extends {
|
|
14
|
+
routes: infer R;
|
|
15
|
+
} ? R : Record<string, unknown>;
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Modules generated by the fondu Vite plugin.
|
|
2
|
+
declare module 'virtual:fondu/assets' {
|
|
3
|
+
/** client asset version (production builds); a page visit from other assets is reloaded as a document */
|
|
4
|
+
export const version: string | null
|
|
5
|
+
/** the page names of a build (from the client manifest); null in development */
|
|
6
|
+
export const pages: string[] | null
|
|
7
|
+
/** the `<head>` tags a page needs: the client entry and its styles, the page component's chunk */
|
|
8
|
+
export function assetTags(component: string, nonce?: string): string
|
|
9
|
+
}
|
|
10
|
+
declare module 'virtual:fondu/ssr' {
|
|
11
|
+
/** the app's `createInertiaApp(...)` rendering a page on the server (null: `fondu({ ssr: false })`) */
|
|
12
|
+
export const renderPage: ((page: import('@inertiajs/core').Page) => Promise<{ head: string[]; body: string }>) | null
|
|
13
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type Options as ReactOptions } from '@vitejs/plugin-react';
|
|
2
|
+
import type { PluginOption } from 'vite';
|
|
3
|
+
/**
|
|
4
|
+
* `vite.config.ts`: `plugins: [fondu({ target: 'node' })]` (or `'bun'`; for Cloudflare: `cloudflare()` and
|
|
5
|
+
* `fondu({ target: 'cloudflare' })`). Composes the packages that do the work:
|
|
6
|
+
* - `@vitejs/plugin-react`, and `@inertiajs/vite` (pages from `app/pages/`, the server render of `app/app.tsx`)
|
|
7
|
+
* - Node / Bun: `@hono/vite-dev-server` (dev) and `@hono/vite-build` (`vite build --mode client && vite build`)
|
|
8
|
+
* - Cloudflare: `@cloudflare/vite-plugin` (yours: dev in workerd, one `vite build`)
|
|
9
|
+
* and adds what none of them does: the document's asset tags, the generated types, the server-only guard.
|
|
10
|
+
*/
|
|
11
|
+
export interface FonduOptions {
|
|
12
|
+
target: 'node' | 'bun' | 'cloudflare';
|
|
13
|
+
/** production port (Node / Bun), default 3000 */
|
|
14
|
+
port?: number;
|
|
15
|
+
/** `false`: pages render in the browser only, no server render is built (an admin app). Per route: `withoutSsr()` */
|
|
16
|
+
ssr?: boolean;
|
|
17
|
+
react?: ReactOptions;
|
|
18
|
+
}
|
|
19
|
+
export default function fondu(options: FonduOptions): PluginOption[];
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { existsSync, readdirSync, readFileSync } from 'node:fs';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
import { dirname, join, resolve } from 'node:path';
|
|
5
|
+
import { pathToFileURL } from 'node:url';
|
|
6
|
+
import devServer, { defaultOptions as devServerDefaults } from '@hono/vite-dev-server';
|
|
7
|
+
import inertia from '@inertiajs/vite';
|
|
8
|
+
import react, {} from '@vitejs/plugin-react';
|
|
9
|
+
import { typegen } from './typegen.js';
|
|
10
|
+
const ENTRY = 'app/app.tsx';
|
|
11
|
+
const SERVER = './app/server.ts';
|
|
12
|
+
const DEV_CSS = '/@fondu/dev.css';
|
|
13
|
+
const V = { assets: 'virtual:fondu/assets', ssr: 'virtual:fondu/ssr' };
|
|
14
|
+
const resolved = (id) => '\0' + id;
|
|
15
|
+
/**
|
|
16
|
+
* `@inertiajs/vite` wraps `createInertiaApp(...)` for the server; its React template also starts a standalone
|
|
17
|
+
* SSR server (Laravel's model). Same wrap, rendering in this process: the template only exports the render.
|
|
18
|
+
*/
|
|
19
|
+
const inertiaInProcess = {
|
|
20
|
+
package: '@inertiajs/react',
|
|
21
|
+
extensions: ['.tsx', '.jsx'],
|
|
22
|
+
extractDefault: true,
|
|
23
|
+
ssr: (configureCall) => `
|
|
24
|
+
import { renderToString } from 'react-dom/server'
|
|
25
|
+
const renderPromise = ${configureCall}
|
|
26
|
+
renderPromise.catch((error) => console.error(error))
|
|
27
|
+
export default async function renderPage(page) {
|
|
28
|
+
return (await renderPromise)(page, renderToString)
|
|
29
|
+
}
|
|
30
|
+
`,
|
|
31
|
+
};
|
|
32
|
+
export default function fondu(options) {
|
|
33
|
+
const node = options.target !== 'cloudflare';
|
|
34
|
+
return [
|
|
35
|
+
react(options.react),
|
|
36
|
+
// `ssr: {}`: the server transform without an entry, so without @inertiajs/vite's dev SSR endpoint (Laravel's model)
|
|
37
|
+
inertia({ ssr: options.ssr === false ? false : {}, frameworks: inertiaInProcess }),
|
|
38
|
+
core(options),
|
|
39
|
+
typegen(),
|
|
40
|
+
...(node ? runtimePlugins(options) : []),
|
|
41
|
+
];
|
|
42
|
+
}
|
|
43
|
+
async function buildPlugin(options) {
|
|
44
|
+
const { default: build } = options.target === 'bun' ? await import('@hono/vite-build/bun') : await import('@hono/vite-build/node');
|
|
45
|
+
// the app's dependencies stay in node_modules (as with any Node server: native modules cannot be bundled)
|
|
46
|
+
const pkg = JSON.parse(readFileSync(resolve('package.json'), 'utf8'));
|
|
47
|
+
const external = Object.keys(pkg.dependencies ?? {}).filter((d) => d !== 'fondu');
|
|
48
|
+
const plugin = build({
|
|
49
|
+
entry: SERVER,
|
|
50
|
+
external,
|
|
51
|
+
outputDir: './dist',
|
|
52
|
+
// Node: graceful shutdown on SIGTERM / SIGINT; the port is read when the server starts (`PORT`, else the
|
|
53
|
+
// option): the adapter writes the value into the entry as code. Bun reads `PORT` itself.
|
|
54
|
+
...(options.target === 'node' && {
|
|
55
|
+
shutdownTimeoutMs: 10_000,
|
|
56
|
+
port: { toString: () => `(Number(process.env.PORT) || ${options.port ?? 3000})` },
|
|
57
|
+
}),
|
|
58
|
+
// the client build's files (dist/client), found next to the server bundle wherever it runs from; hashed files
|
|
59
|
+
// never change
|
|
60
|
+
entryContentBeforeHooks: [
|
|
61
|
+
(app) => {
|
|
62
|
+
const dir = resolve('dist/client'); // vite runs in the project root
|
|
63
|
+
const paths = existsSync(dir)
|
|
64
|
+
? readdirSync(dir, { withFileTypes: true })
|
|
65
|
+
.filter((e) => e.name !== '.vite')
|
|
66
|
+
.map((e) => (e.isDirectory() ? `/${e.name}/*` : `/${e.name}`))
|
|
67
|
+
: [];
|
|
68
|
+
return [
|
|
69
|
+
`import { serveStatic } from '${options.target === 'bun' ? 'hono/bun' : '@hono/node-server/serve-static'}'`,
|
|
70
|
+
`import { fileURLToPath } from 'node:url'`,
|
|
71
|
+
`const clientRoot = fileURLToPath(new URL('./client', import.meta.url))`,
|
|
72
|
+
`${app}.use('/assets/*', async (c, next) => { await next(); if (c.res.ok) c.header('Cache-Control', 'public, max-age=31536000, immutable') })`,
|
|
73
|
+
...paths.map((p) => `${app}.use(${JSON.stringify(p)}, serveStatic({ root: clientRoot }))`),
|
|
74
|
+
].join('\n');
|
|
75
|
+
},
|
|
76
|
+
],
|
|
77
|
+
});
|
|
78
|
+
return plugin;
|
|
79
|
+
}
|
|
80
|
+
function runtimePlugins(options) {
|
|
81
|
+
return [
|
|
82
|
+
{
|
|
83
|
+
// dev: the same Request / Response globals as the production server (@hono/node-server's), which the dev
|
|
84
|
+
// server's requests are made with: middleware that re-creates the request (hono/body-limit) needs them
|
|
85
|
+
name: 'fondu:node-globals',
|
|
86
|
+
apply: 'serve',
|
|
87
|
+
async configureServer() {
|
|
88
|
+
// the dev server's own copy, as an ES module (its CommonJS build would be another class)
|
|
89
|
+
const fromDevServer = createRequire(createRequire(import.meta.url).resolve('@hono/vite-dev-server'));
|
|
90
|
+
const dir = dirname(dirname(fromDevServer.resolve('@hono/node-server'))); // …/dist/index.js
|
|
91
|
+
const pkg = JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8'));
|
|
92
|
+
const entry = pkg.exports['.'].import;
|
|
93
|
+
const { getRequestListener } = (await import(pathToFileURL(join(dir, typeof entry === 'string' ? entry : entry.default)).href));
|
|
94
|
+
getRequestListener(() => new Response());
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
devServer({
|
|
98
|
+
entry: SERVER,
|
|
99
|
+
injectClientScript: false, // the document has the client tags (with the React refresh preamble)
|
|
100
|
+
adapter: undefined,
|
|
101
|
+
exclude: devServerDefaults.exclude,
|
|
102
|
+
handleHotUpdate: () => { }, // fondu's plugin does it, for every target
|
|
103
|
+
}),
|
|
104
|
+
buildPlugin(options),
|
|
105
|
+
];
|
|
106
|
+
}
|
|
107
|
+
function core(options) {
|
|
108
|
+
let config;
|
|
109
|
+
let isBuild = false;
|
|
110
|
+
return {
|
|
111
|
+
name: 'fondu',
|
|
112
|
+
enforce: 'pre',
|
|
113
|
+
config(_user, env) {
|
|
114
|
+
isBuild = env.command === 'build';
|
|
115
|
+
const client = { manifest: true, rollupOptions: { input: ENTRY } };
|
|
116
|
+
if (options.target === 'cloudflare') {
|
|
117
|
+
return {
|
|
118
|
+
environments: { client: { build: client } },
|
|
119
|
+
// the worker reads the client manifest: client first
|
|
120
|
+
builder: {
|
|
121
|
+
async buildApp(builder) {
|
|
122
|
+
await builder.build(builder.environments.client);
|
|
123
|
+
for (const env of Object.values(builder.environments))
|
|
124
|
+
if (env.name !== 'client')
|
|
125
|
+
await builder.build(env);
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
ssr: { noExternal: ['fondu'] },
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
// Node / Bun: `vite build --mode client` builds the client into dist/client, `vite build` the server
|
|
132
|
+
return env.mode === 'client'
|
|
133
|
+
? { build: { ...client, outDir: 'dist/client', emptyOutDir: true } }
|
|
134
|
+
: {
|
|
135
|
+
ssr: { noExternal: ['fondu'] },
|
|
136
|
+
build: { copyPublicDir: false, rollupOptions: { output: { chunkFileNames: 'server/[name]-[hash].js' } } },
|
|
137
|
+
};
|
|
138
|
+
},
|
|
139
|
+
configResolved(c) {
|
|
140
|
+
config = c;
|
|
141
|
+
},
|
|
142
|
+
// dev: server code changed (a module of the server only: routes/, *.server.ts…): reload the page; a page component:
|
|
143
|
+
// React Fast Refresh (the client's HMR)
|
|
144
|
+
handleHotUpdate({ server, file }) {
|
|
145
|
+
const modules = (env) => [...(server.environments[env]?.moduleGraph.getModulesByFile(file) ?? [])];
|
|
146
|
+
// the browser's code (not a file a stylesheet only watches, as Tailwind's sources), or no server's: leave it
|
|
147
|
+
if (modules('client').some((m) => m.type === 'js'))
|
|
148
|
+
return;
|
|
149
|
+
if (!Object.keys(server.environments).some((env) => env !== 'client' && modules(env).length))
|
|
150
|
+
return;
|
|
151
|
+
server.hot.send({ type: 'full-reload' });
|
|
152
|
+
return [];
|
|
153
|
+
},
|
|
154
|
+
// dev: the CSS the client entry imports, as a stylesheet in the document, so a server-rendered page is styled
|
|
155
|
+
// at first paint (Vite would add it only once the JS runs); removed once the entry has run
|
|
156
|
+
configureServer(server) {
|
|
157
|
+
server.middlewares.use(DEV_CSS, async (_req, res) => {
|
|
158
|
+
const env = server.environments.client;
|
|
159
|
+
const css = [];
|
|
160
|
+
try {
|
|
161
|
+
await env.transformRequest('/' + ENTRY);
|
|
162
|
+
const seen = new Set();
|
|
163
|
+
const walk = (mod) => {
|
|
164
|
+
if (!mod?.id || seen.has(mod.id))
|
|
165
|
+
return;
|
|
166
|
+
seen.add(mod.id);
|
|
167
|
+
if (/\.(css|scss|sass|less|styl|pcss|postcss)(\?|$)/.test(mod.url))
|
|
168
|
+
css.push(mod.url);
|
|
169
|
+
for (const m of mod.importedModules)
|
|
170
|
+
walk(m);
|
|
171
|
+
};
|
|
172
|
+
walk(env.moduleGraph.getModuleById(resolve(config.root, ENTRY)));
|
|
173
|
+
const parts = await Promise.all(css.map(async (url) => (await env.transformRequest(url + (url.includes('?') ? '&' : '?') + 'direct'))?.code ?? ''));
|
|
174
|
+
res.setHeader('Content-Type', 'text/css');
|
|
175
|
+
res.end(parts.join('\n'));
|
|
176
|
+
}
|
|
177
|
+
catch (error) {
|
|
178
|
+
res.statusCode = 500;
|
|
179
|
+
res.end(`/* ${String(error)} */`);
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
},
|
|
183
|
+
// Cloudflare: Workers Static Assets serve the client build; hashed files never change (unless you wrote your own
|
|
184
|
+
// public/_headers)
|
|
185
|
+
generateBundle() {
|
|
186
|
+
if (options.target !== 'cloudflare' ||
|
|
187
|
+
this.environment.name !== 'client' ||
|
|
188
|
+
existsSync(resolve(config.publicDir, '_headers')))
|
|
189
|
+
return;
|
|
190
|
+
this.emitFile({
|
|
191
|
+
type: 'asset',
|
|
192
|
+
fileName: '_headers',
|
|
193
|
+
source: '/assets/*\n Cache-Control: public, max-age=31536000, immutable\n',
|
|
194
|
+
});
|
|
195
|
+
},
|
|
196
|
+
resolveId(id) {
|
|
197
|
+
if (id === V.assets || id === V.ssr)
|
|
198
|
+
return resolved(id);
|
|
199
|
+
},
|
|
200
|
+
load(id) {
|
|
201
|
+
if (id === resolved(V.ssr))
|
|
202
|
+
return options.ssr === false
|
|
203
|
+
? 'export const renderPage = null'
|
|
204
|
+
: `export { default as renderPage } from '/${ENTRY}'`;
|
|
205
|
+
if (id === resolved(V.assets))
|
|
206
|
+
return isBuild ? productionAssets(config, options) : devAssets(config.base);
|
|
207
|
+
// the server-only guard: browser code must never import server code
|
|
208
|
+
if (this.environment.config.consumer === 'client' && isServerOnly(id)) {
|
|
209
|
+
this.error(`fondu: ${id.replace(config.root, '')} is server code (routes/, server.ts, *.server.ts) and cannot be imported by browser code`);
|
|
210
|
+
}
|
|
211
|
+
},
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
const isServerOnly = (id) => /\.server\.[cm]?[jt]sx?(\?.*)?$/.test(id) || /\/app\/(routes\/|server\.ts)/.test(id);
|
|
215
|
+
const nonceAttr = `\${nonce ? \` nonce="\${nonce}"\` : ''}`;
|
|
216
|
+
function devAssets(base) {
|
|
217
|
+
const preamble = `import RefreshRuntime from "${base}@react-refresh";RefreshRuntime.injectIntoGlobalHook(window);window.$RefreshReg$=()=>{};window.$RefreshSig$=()=>(type)=>type;window.__vite_plugin_react_preamble_installed__=true`;
|
|
218
|
+
return `export const version = null
|
|
219
|
+
export const pages = null
|
|
220
|
+
export function assetTags(_component, nonce) {
|
|
221
|
+
return '<link rel="stylesheet" href="${base}${DEV_CSS.slice(1)}" data-fondu-dev-css>'
|
|
222
|
+
+ \`<script type="module"${nonceAttr}>${preamble}</script><script type="module" src="${base}@vite/client"${nonceAttr}></script><script type="module" src="${base}${ENTRY}"${nonceAttr}></script><script type="module"${nonceAttr}>document.querySelector('[data-fondu-dev-css]')?.remove()</script>\`
|
|
223
|
+
}`;
|
|
224
|
+
}
|
|
225
|
+
function productionAssets(config, options) {
|
|
226
|
+
const outDir = options.target === 'cloudflare'
|
|
227
|
+
? resolve(config.root, config.environments.client?.build.outDir ?? 'dist/client')
|
|
228
|
+
: resolve(config.root, 'dist/client');
|
|
229
|
+
const file = resolve(outDir, '.vite/manifest.json');
|
|
230
|
+
if (!existsSync(file))
|
|
231
|
+
throw new Error(`fondu: the client manifest (${file}) is missing: build the client first (\`vite build --mode client\`)`);
|
|
232
|
+
const text = readFileSync(file, 'utf8');
|
|
233
|
+
const manifest = JSON.parse(text);
|
|
234
|
+
const collect = (key, css, js, seen = new Set()) => {
|
|
235
|
+
const chunk = manifest[key];
|
|
236
|
+
if (!chunk || seen.has(key))
|
|
237
|
+
return;
|
|
238
|
+
seen.add(key);
|
|
239
|
+
js.add(chunk.file);
|
|
240
|
+
for (const c of chunk.css ?? [])
|
|
241
|
+
css.add(c);
|
|
242
|
+
for (const i of chunk.imports ?? [])
|
|
243
|
+
collect(i, css, js, seen);
|
|
244
|
+
};
|
|
245
|
+
const tags = (key) => {
|
|
246
|
+
const css = new Set(), js = new Set();
|
|
247
|
+
collect(key, css, js);
|
|
248
|
+
return { css: [...css], js: [...js] };
|
|
249
|
+
};
|
|
250
|
+
const entry = tags(ENTRY);
|
|
251
|
+
const pages = {};
|
|
252
|
+
for (const key of Object.keys(manifest)) {
|
|
253
|
+
const m = key.match(/^app\/pages\/(.+)\.[jt]sx$/);
|
|
254
|
+
if (m)
|
|
255
|
+
pages[m[1]] = tags(key);
|
|
256
|
+
}
|
|
257
|
+
const version = createHash('sha256').update(text).digest('hex').slice(0, 16);
|
|
258
|
+
return `const base = ${JSON.stringify(config.base)}
|
|
259
|
+
const entry = ${JSON.stringify(entry)}
|
|
260
|
+
const byPage = ${JSON.stringify(pages)}
|
|
261
|
+
export const version = ${JSON.stringify(version)}
|
|
262
|
+
export const pages = ${JSON.stringify(Object.keys(pages))}
|
|
263
|
+
export function assetTags(component, nonce) {
|
|
264
|
+
const page = byPage[component] ?? { css: [], js: [] }
|
|
265
|
+
const css = [...new Set([...entry.css, ...page.css])]
|
|
266
|
+
const [main, ...preload] = new Set([...entry.js, ...page.js])
|
|
267
|
+
return css.map((f) => \`<link rel="stylesheet" href="\${base}\${f}">\`).join('')
|
|
268
|
+
+ preload.map((f) => \`<link rel="modulepreload" href="\${base}\${f}"${nonceAttr}>\`).join('')
|
|
269
|
+
+ \`<script type="module" src="\${base}\${main}"${nonceAttr}></script>\`
|
|
270
|
+
}`;
|
|
271
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Plugin } from 'vite';
|
|
2
|
+
/** The registry the types are read from: page name → module, URL pattern → route module. Only `typeof import`s. */
|
|
3
|
+
export declare function generateTypes(root: string): string;
|
|
4
|
+
export declare function writeTypes(root: string): void;
|
|
5
|
+
export declare function typegen(): Plugin;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { join, relative, sep } from 'node:path';
|
|
3
|
+
import { isRouteFile, routePattern } from '../paths.js';
|
|
4
|
+
const OUT = 'app/fondu.gen.d.ts';
|
|
5
|
+
function walk(dir, out = [], base = dir) {
|
|
6
|
+
if (!existsSync(dir))
|
|
7
|
+
return out;
|
|
8
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
9
|
+
const file = join(dir, entry.name);
|
|
10
|
+
if (entry.isDirectory())
|
|
11
|
+
walk(file, out, base);
|
|
12
|
+
else
|
|
13
|
+
out.push(relative(base, file).split(sep).join('/'));
|
|
14
|
+
}
|
|
15
|
+
return out;
|
|
16
|
+
}
|
|
17
|
+
/** The registry the types are read from: page name → module, URL pattern → route module. Only `typeof import`s. */
|
|
18
|
+
export function generateTypes(root) {
|
|
19
|
+
const pages = walk(join(root, 'app/pages'))
|
|
20
|
+
.filter((f) => /\.[jt]sx$/.test(f))
|
|
21
|
+
.sort();
|
|
22
|
+
const routes = walk(join(root, 'app/routes')).filter(isRouteFile).sort();
|
|
23
|
+
const line = (key, path) => ` ${JSON.stringify(key)}: typeof import(${JSON.stringify(path)})`;
|
|
24
|
+
return `// Generated by fondu (vite dev / vite build). Commit it; do not edit.
|
|
25
|
+
declare module 'fondu' {
|
|
26
|
+
interface Register {
|
|
27
|
+
pages: {
|
|
28
|
+
${pages.map((f) => line(f.replace(/\.[jt]sx$/, ''), `./pages/${f.replace(/\.[jt]sx$/, '')}`)).join('\n')}
|
|
29
|
+
}
|
|
30
|
+
routes: {
|
|
31
|
+
${routes.map((f) => line(routePattern(f), `./routes/${f.replace(/\.ts$/, '')}`)).join('\n')}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export {}
|
|
36
|
+
`;
|
|
37
|
+
}
|
|
38
|
+
export function writeTypes(root) {
|
|
39
|
+
const file = join(root, OUT);
|
|
40
|
+
const content = generateTypes(root);
|
|
41
|
+
if (!existsSync(file) || readFileSync(file, 'utf8') !== content)
|
|
42
|
+
writeFileSync(file, content);
|
|
43
|
+
}
|
|
44
|
+
export function typegen() {
|
|
45
|
+
let root = process.cwd();
|
|
46
|
+
return {
|
|
47
|
+
name: 'fondu:typegen',
|
|
48
|
+
configResolved(c) {
|
|
49
|
+
root = c.root;
|
|
50
|
+
},
|
|
51
|
+
buildStart() {
|
|
52
|
+
writeTypes(root);
|
|
53
|
+
},
|
|
54
|
+
configureServer(server) {
|
|
55
|
+
const onChange = (file) => {
|
|
56
|
+
const rel = relative(root, file).split(sep).join('/');
|
|
57
|
+
if (rel.startsWith('app/pages/') || rel.startsWith('app/routes/'))
|
|
58
|
+
writeTypes(root);
|
|
59
|
+
};
|
|
60
|
+
server.watcher.on('add', onChange);
|
|
61
|
+
server.watcher.on('unlink', onChange);
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,65 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fondu",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Inertia v3 on Hono, with file routes (loader, action, actions) and pages. Node, Bun and Cloudflare Workers.",
|
|
5
|
+
"homepage": "https://github.com/fondujs/fondu#readme",
|
|
6
|
+
"bugs": "https://github.com/fondujs/fondu/issues",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/fondujs/fondu.git",
|
|
11
|
+
"directory": "packages/fondu"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"type": "module",
|
|
17
|
+
"sideEffects": false,
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js"
|
|
22
|
+
},
|
|
23
|
+
"./client": {
|
|
24
|
+
"types": "./dist/client.d.ts",
|
|
25
|
+
"import": "./dist/client.js"
|
|
26
|
+
},
|
|
27
|
+
"./vite": {
|
|
28
|
+
"types": "./dist/vite/index.d.ts",
|
|
29
|
+
"import": "./dist/vite/index.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json && cp src/virtual.d.ts dist/",
|
|
34
|
+
"dev": "tsc -p tsconfig.build.json --watch",
|
|
35
|
+
"test": "vitest run",
|
|
36
|
+
"typecheck": "tsc -p tsconfig.json"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@hono/vite-build": "1.11.1",
|
|
40
|
+
"@hono/vite-dev-server": "0.26.1",
|
|
41
|
+
"@inertiajs/vite": "3.9.1",
|
|
42
|
+
"@vitejs/plugin-react": "6.1.2"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@inertiajs/core": "3.9.1",
|
|
46
|
+
"@inertiajs/react": "3.9.1",
|
|
47
|
+
"@types/node": "26.6.4",
|
|
48
|
+
"hono": "4.13.13",
|
|
49
|
+
"typescript": "7.0.2",
|
|
50
|
+
"vite": "8.3.4",
|
|
51
|
+
"vitest": "5.0.3",
|
|
52
|
+
"zod": "4.6.5"
|
|
53
|
+
},
|
|
54
|
+
"peerDependencies": {
|
|
55
|
+
"@inertiajs/react": "^3.9.0",
|
|
56
|
+
"hono": "^4.13.0",
|
|
57
|
+
"react": "^19.0.0",
|
|
58
|
+
"react-dom": "^19.0.0",
|
|
59
|
+
"vite": "^8.3.0"
|
|
60
|
+
},
|
|
61
|
+
"engines": {
|
|
62
|
+
"bun": ">=1.3.14",
|
|
63
|
+
"node": ">=20.19"
|
|
64
|
+
}
|
|
65
|
+
}
|