AppView in a box as a Vite plugin thing hatk.dev
at main 111 lines 3.6 kB view raw view rendered
1--- 2title: Frontend Setup 3description: How hatk integrates with SvelteKit — the Vite plugin, generated files, and import aliases. 4--- 5 6# Frontend Setup 7 8hatk uses SvelteKit for its frontend, but any Vite SSR framework could be supported. The hatk CLI generates typed client code from your lexicons, so your components get autocomplete and type checking for every API call. The Vite plugin handles import resolution, the dev server, and the server-side bridge for `callXrpc()`. 9 10## Vite plugin 11 12Add the `hatk()` plugin to your `vite.config.ts` alongside `sveltekit()`: 13 14```typescript 15// vite.config.ts 16import { sveltekit } from "@sveltejs/kit/vite"; 17import { hatk } from "@hatk/hatk/vite-plugin"; 18import { defineConfig } from "vite-plus"; 19 20export default defineConfig({ 21 plugins: [hatk(), sveltekit()], 22}); 23``` 24 25The `hatk()` plugin resolves `$hatk` and `$hatk/client` imports to the generated files, boots the dev server and PDS, and sets up the server-side bridge that lets `callXrpc()` work in both server and client contexts. 26 27## Generated files 28 29When you run `hatk dev` or `hatk generate types`, hatk produces two files in your project root: 30 31| File | Purpose | 32|---|---| 33| `hatk.generated.ts` | Server-side types, helpers, and lexicon definitions. Exports `defineQuery`, `defineProcedure`, `defineFeed`, `callXrpc` (server variant), and all your record/view types. | 34| `hatk.generated.client.ts` | Client-safe subset. Exports `callXrpc` (browser variant), `getViewer`, `login`, `logout`, `parseViewer`, and re-exports types from the server file without pulling in server-only dependencies. | 35 36These files are auto-generated -- don't edit them. Add them to your lint/format ignore patterns: 37 38```typescript 39// vite.config.ts 40export default defineConfig({ 41 // ... 42 lint: { 43 ignorePatterns: ["hatk.generated.ts", "hatk.generated.client.ts"], 44 }, 45 fmt: { 46 ignorePatterns: ["hatk.generated.ts", "hatk.generated.client.ts"], 47 }, 48}); 49``` 50 51## Import aliases 52 53SvelteKit aliases map `$hatk` and `$hatk/client` to the generated files. These are configured in `svelte.config.js`: 54 55```javascript 56// svelte.config.js 57export default { 58 kit: { 59 alias: { 60 $hatk: "./hatk.generated.ts", 61 "$hatk/client": "./hatk.generated.client.ts", 62 }, 63 }, 64}; 65``` 66 67**When to use which:** 68 69- **`$hatk`** -- Use in server-only code: XRPC handler files, seeds, feeds, hooks. Contains server-side `callXrpc` that talks directly to your XRPC layer without HTTP. 70- **`$hatk/client`** -- Use in components, `+page.server.ts`, `+layout.server.ts`, `.remote.ts` files, and anywhere that might run in the browser. Contains the client `callXrpc` that routes through HTTP on the client and uses a server bridge during SSR. 71 72The rule is simple: if the file can be imported by a Svelte component (even indirectly), use `$hatk/client`. 73 74## The `app/` directory 75 76hatk projects use `app/` instead of `src/` for the SvelteKit source directory: 77 78```javascript 79// svelte.config.js 80export default { 81 kit: { 82 files: { 83 src: "app", 84 }, 85 }, 86}; 87``` 88 89Your routes, components, and lib code live under `app/`: 90 91``` 92app/ 93 routes/ 94 +page.svelte 95 +page.server.ts 96 +layout.server.ts 97 lib/ 98 components/ 99``` 100 101This is a convention, not a requirement -- but all hatk templates use it and the CLI scaffolding expects it. 102 103## Regenerating types 104 105After adding or changing lexicons, regenerate the typed files: 106 107```bash 108npx hatk generate types 109``` 110 111This updates both `hatk.generated.ts` and `hatk.generated.client.ts` with new types, XRPC schema entries, and helper functions matching your lexicons.