|
| 1 | +--- |
| 2 | +id: vite |
| 3 | +title: Vite Plugin |
| 4 | +sidebar_label: Vite Plugin |
| 5 | +--- |
| 6 | + |
| 7 | +# Build with the Devvit Vite plugin |
| 8 | + |
| 9 | +::::warning Experimental |
| 10 | +The Devvit Vite plugin is experimental and subject to breaking changes. |
| 11 | +:::: |
| 12 | + |
| 13 | +The Devvit [Vite](https://vite.dev/) plugin is a 100% optional plugin for Devvit Web that unifies your client and server builds into a single command using [Vite's Environment API](https://vite.dev/guide/api-environment). |
| 14 | + |
| 15 | +Features: |
| 16 | + |
| 17 | +- Unified build command for client and server |
| 18 | +- Automatic entrypoint bundling based on your `devvit.json` configuration |
| 19 | +- Optimized configuration for building your code for Devvit |
| 20 | + |
| 21 | +The plugin is **completely optional**. You can use Vite without it, or swap in Webpack, esbuild, or any other bundler you prefer. |
| 22 | + |
| 23 | +You can see a full template using it here: https://github.com/reddit/devvit-template-vibe-coding/blob/main/vite.config.ts |
| 24 | + |
| 25 | +## Quick start |
| 26 | + |
| 27 | +Add the plugin to your `vite.config.ts` alongside any UI tooling you already use: |
| 28 | + |
| 29 | +```ts |
| 30 | +import { defineConfig } from "vite"; |
| 31 | +import react from "@vitejs/plugin-react"; |
| 32 | +import tailwind from "@tailwindcss/vite"; |
| 33 | +import { devvit } from "@devvit/start/vite"; |
| 34 | + |
| 35 | +export default defineConfig({ |
| 36 | + plugins: [ |
| 37 | + react(), |
| 38 | + tailwind(), |
| 39 | + // Make sure to add the Devvit plugin last! |
| 40 | + devvit(), |
| 41 | + ], |
| 42 | +}); |
| 43 | +``` |
| 44 | + |
| 45 | +## What the plugin expects |
| 46 | + |
| 47 | +The plugin uses your `devvit.json` as the source of truth for client entry points. Make sure you have at least one entrypoint defined: |
| 48 | + |
| 49 | +```json |
| 50 | +{ |
| 51 | + "post": { |
| 52 | + "dir": "dist/client", |
| 53 | + "entrypoints": { |
| 54 | + "default": { |
| 55 | + "inline": true, |
| 56 | + "entry": "src/splash.html" |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +For the server build, the plugin looks for one of these files: |
| 64 | + |
| 65 | +- `src/server/index.ts` |
| 66 | +- `src/api/index.ts` |
| 67 | + |
| 68 | +If neither file exists, the build fails with a clear error message. |
| 69 | + |
| 70 | +## What it builds |
| 71 | + |
| 72 | +Out of the box, the plugin configures two environments: |
| 73 | + |
| 74 | +- **Client build** outputs to `dist/client` and uses the entry points from `devvit.json`. |
| 75 | +- **Server build** outputs to `dist/server` as `index.cjs` |
| 76 | + |
| 77 | +> Note that Devvit requires a single CJS bundle to run the server code. Please do not mark server dependencies as `external` as it will break your server build. This may change in the future! |
| 78 | +
|
| 79 | +## Customize the build |
| 80 | + |
| 81 | +The plugin accepts a small options object that lets you tweak both environments without redoing the whole config. Each option is merged into the generated Vite environment config. |
| 82 | + |
| 83 | +```ts |
| 84 | +type DevvitPluginOptions = { |
| 85 | + client?: EnvironmentOptions; |
| 86 | + server?: EnvironmentOptions; |
| 87 | + logLevel?: "info" | "warn" | "error" | "silent"; |
| 88 | + verbose?: boolean; |
| 89 | +}; |
| 90 | +``` |
| 91 | + |
| 92 | +For example, if you want to disable sourcemaps for the client build: |
| 93 | + |
| 94 | +```ts |
| 95 | +import { defineConfig } from "vite"; |
| 96 | +import { devvit } from "@devvit/start/vite"; |
| 97 | + |
| 98 | +export default defineConfig({ |
| 99 | + plugins: [ |
| 100 | + devvit({ |
| 101 | + client: { |
| 102 | + build: { |
| 103 | + sourcemap: false, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }), |
| 107 | + ], |
| 108 | +}); |
| 109 | +``` |
| 110 | + |
| 111 | +Any customization you make is automatically merged into the generated environment config so don't worry about spreading our defaults across your config changes. |
| 112 | + |
| 113 | +## Sharing code between client and server |
| 114 | + |
| 115 | +If you need to share code between client and server, keep it outside of `src/server` and `src/api`. Treat those folders as server-only. Put shared modules in a neutral folder (for example `src/shared`) and import them from both environments. |
| 116 | + |
| 117 | +```text |
| 118 | +src/ |
| 119 | + server/ |
| 120 | + index.ts # server entry |
| 121 | + client/ |
| 122 | + main.tsx # client entry |
| 123 | + shared/ |
| 124 | + formatScore.ts # safe to import from client + server |
| 125 | +``` |
| 126 | + |
| 127 | +## Limitations and gotchas |
| 128 | + |
| 129 | +- **Build-only:** The plugin only supports `vite build`. There is no support for `vite dev` or Hot Module Replacement (HMR) at this time. This is because `devvit playtest` works by uploading your build to our servers and running it on Reddit.com. Instead, use `vite build --watch` as your dev command. |
| 130 | +- **Entry points are required:** Building server apps only is not yet supported. If you'd like to request this feature, please [open an issue](https://github.com/reddit/devvit/issues/new). |
0 commit comments