|
| 1 | +--- |
| 2 | +title: Vite |
| 3 | +pcx_content_type: concept |
| 4 | +sidebar: |
| 5 | + order: 7 |
| 6 | + group: |
| 7 | + badge: Beta |
| 8 | +head: [] |
| 9 | +description: Locally develop and test changes with the Cloudflare Vite plugin |
| 10 | +--- |
| 11 | + |
| 12 | +import { |
| 13 | + Badge, |
| 14 | + InlineBadge, |
| 15 | + PackageManagers, |
| 16 | + WranglerConfig, |
| 17 | +} from "~/components"; |
| 18 | + |
| 19 | +The Cloudflare Vite plugin (`@cloudflare/vite-plugin`) integrates your Worker code directly with [Vite](https://vite.dev/). Like `wrangler dev`, this approach uses `workerd` under the hood as well, and provides: |
| 20 | + |
| 21 | +- Direct access to Workers [runtime APIs](/workers/runtime-apis/) and [bindings](/runtime-apis/bindings/) |
| 22 | +- [Hot module replacement (HMR)](https://vite.dev/guide/features.html#hot-module-replacement) for near-instant feedback on code changes. |
| 23 | +- A single workflow (`vite dev`, `vite build`, `vite preview`) that handles both your client-side assets and Worker code. |
| 24 | +- An “input” Wrangler config file and an automatically generated “output” config file (used for preview and deployment). |
| 25 | + |
| 26 | +If you are building anything that involves a front-end using [Workers static assets](/workers/static-assets/), the Vite approach can be particularly powerful. |
| 27 | + |
| 28 | +## Get started |
| 29 | + |
| 30 | +#### 1. Create your project directory and add a minimal package.json: |
| 31 | + |
| 32 | +```json |
| 33 | +{ |
| 34 | + "name": "cloudflare-vite-quick-start", |
| 35 | + "private": true, |
| 36 | + "version": "0.0.0", |
| 37 | + "type": "module", |
| 38 | + "scripts": { |
| 39 | + "dev": "vite", |
| 40 | + "build": "vite build", |
| 41 | + "preview": "vite preview" |
| 42 | + } |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +:::note |
| 47 | +Ensure that you include `"type": "module"` in order to use ES modules by default. |
| 48 | +::: |
| 49 | + |
| 50 | +#### 2. Install dependencies |
| 51 | + |
| 52 | +<PackageManagers type="add" pkg="vite @cloudflare/vite-plugin wrangler" dev /> |
| 53 | + |
| 54 | +#### 3. Create a Vite config file (`vite.config.ts`) and include the Cloudflare plugin: |
| 55 | + |
| 56 | +```ts |
| 57 | +// vite.config.ts |
| 58 | + |
| 59 | +import { defineConfig } from "vite"; |
| 60 | +import { cloudflare } from "@cloudflare/vite-plugin"; |
| 61 | + |
| 62 | +export default defineConfig({ |
| 63 | + plugins: [cloudflare()], |
| 64 | +}); |
| 65 | +``` |
| 66 | + |
| 67 | +#### 4. Create a Worker configuration file (`wrangler.toml` or `wrangler.jsonc`): |
| 68 | + |
| 69 | +<WranglerConfig> |
| 70 | + |
| 71 | +```toml |
| 72 | +name = "cloudflare-vite-quick-start" |
| 73 | +compatibility_date = "2024-12-30" |
| 74 | +main = "./src/index.ts" |
| 75 | +``` |
| 76 | + |
| 77 | +</WranglerConfig> |
| 78 | + |
| 79 | +#### 5. Create a Worker entry file (`src/index.ts`): |
| 80 | + |
| 81 | +```ts |
| 82 | +// src/index.ts |
| 83 | + |
| 84 | +export default { |
| 85 | + fetch() { |
| 86 | + return new Response(`Running in ${navigator.userAgent}!`); |
| 87 | + }, |
| 88 | +}; |
| 89 | +``` |
| 90 | + |
| 91 | +#### 6. Run local development |
| 92 | + |
| 93 | +```sh |
| 94 | +npm run dev |
| 95 | +``` |
| 96 | + |
| 97 | +This starts a local development server, using the Workers runtime behind the scenes. You can build (`npm run build)`, preview (`npm run preview`), or deploy (`npm exec wrangler deploy`) at any time. |
| 98 | + |
| 99 | +## Cloudflare environments |
| 100 | + |
| 101 | +You can specify multiple [Cloudflare environments](/workers/wrangler/environments/) in your Worker confiuration (for example, `[env.staging]` or `[env.production]`). With the Cloudflare Vite plugin, you can select an environment at `dev` or `build` time by providing the `CLOUDFLARE_ENV` environment variable. |
| 102 | + |
| 103 | +For example, let's say you have the following Wrangler configuration file: |
| 104 | + |
| 105 | +<WranglerConfig> |
| 106 | + |
| 107 | +```toml |
| 108 | +name = "my-worker" |
| 109 | +compatibility_date = "2024-12-30" |
| 110 | +main = "./src/index.ts" |
| 111 | + |
| 112 | +vars = { MY_VAR = "Top-level var" } |
| 113 | + |
| 114 | +[env.staging] |
| 115 | +vars = { MY_VAR = "Staging var" } |
| 116 | + |
| 117 | +[env.production] |
| 118 | +vars = { MY_VAR = "Production var" } |
| 119 | +``` |
| 120 | + |
| 121 | +</WranglerConfig> |
| 122 | + |
| 123 | +With the Vite plugin, you can run: |
| 124 | + |
| 125 | +```sh |
| 126 | +CLOUDFLARE_ENV=production vite build |
| 127 | +``` |
| 128 | + |
| 129 | +The output `wrangler.json` file generated by the build will be a flattened configuration for the `'production'` Cloudflare environment. This combines [top-level only](/workers/wrangler/configuration/#top-level-only-keys), [inheritable](/workers/wrangler/configuration/#inheritable-keys), and [non-inheritable](/workers/wrangler/configuration/#non-inheritable-keys) keys. |
| 130 | + |
| 131 | +In this case, |
| 132 | + |
| 133 | +- The value of `MY_VAR` will be `'Production var'`. |
| 134 | +- The name of the Worker will be `'my-worker-production'`. |
| 135 | + |
| 136 | +This is because the environment name is automatically appended to the top-level Worker name. |
| 137 | + |
| 138 | +:::note |
| 139 | +The default Vite environment name for a Worker is always the top-level Worker name. |
| 140 | +This enables you to reference the Worker consistently in your Vite config when using multiple Cloudflare environments. |
| 141 | +::: |
| 142 | + |
| 143 | +### Cloudflare Environments during development |
| 144 | + |
| 145 | +You can also specify Cloudflare environments in development. For example, you could run: |
| 146 | + |
| 147 | +```sh |
| 148 | +CLOUDFLARE_ENV=development vite dev |
| 149 | +``` |
| 150 | + |
| 151 | +It is common to use the default top-level environment as the development environment and then add additional environments as necessary. |
| 152 | + |
| 153 | +:::note |
| 154 | +Running `vite dev` or `vite build` without providing `CLOUDFLARE_ENV` will use the default top-level Cloudflare environment. |
| 155 | +In the above example, the value of `MY_VAR` would be `'Top-level var'`. |
| 156 | +Since Cloudflare environments are applied at `dev` and `build` time, specifying `CLOUDFLARE_ENV` when running `vite preview` or `wrangler deploy` will have no effect. |
| 157 | +::: |
| 158 | + |
| 159 | +### Combining Cloudflare Environments and Vite Modes |
| 160 | + |
| 161 | +You may want to combine the concepts of [Cloudflare environments](/workers/wrangler/environments/) and [Vite modes](https://vite.dev/guide/env-and-mode.html#modes). |
| 162 | + |
| 163 | +With this approach, the Vite mode can be used to select the Cloudflare environment and a single method can be used to determine environment specific configuration and code. |
| 164 | + |
| 165 | +Let's use the previous example: |
| 166 | + |
| 167 | +<WranglerConfig> |
| 168 | + |
| 169 | +```toml |
| 170 | +# wrangler.toml |
| 171 | + |
| 172 | +name = "my-worker" |
| 173 | +compatibility_date = "2024-12-30" |
| 174 | +main = "./src/index.ts" |
| 175 | + |
| 176 | +vars = { MY_VAR = "Top-level var" } |
| 177 | + |
| 178 | +[env.staging] |
| 179 | +vars = { MY_VAR = "Staging var" } |
| 180 | + |
| 181 | +[env.production] |
| 182 | +vars = { MY_VAR = "Production var" } |
| 183 | +``` |
| 184 | + |
| 185 | +</WranglerConfig> |
| 186 | + |
| 187 | +Next, provide `.env.staging` and `.env.production` files: |
| 188 | + |
| 189 | +```sh |
| 190 | +# .env.staging |
| 191 | + |
| 192 | +CLOUDFLARE_ENV=staging |
| 193 | +``` |
| 194 | + |
| 195 | +```sh |
| 196 | +# .env.production |
| 197 | + |
| 198 | +CLOUDFLARE_ENV=production |
| 199 | +``` |
| 200 | + |
| 201 | +By default, `vite build` uses the **'production'** Vite mode, which loads `.env.production` to get the environment variables that are used in the build. |
| 202 | + |
| 203 | +- This sets `CLOUDFLARE_ENV=production`, selecting the production Cloudflare environment. |
| 204 | +- `MY_VAR` becomes `'Production var'`. |
| 205 | + |
| 206 | +To build with the **staging mode**, run: |
| 207 | + |
| 208 | +```sh |
| 209 | +vite build --mode staging |
| 210 | +``` |
| 211 | + |
| 212 | +- This loads `.env.staging`. |
| 213 | +- The value of `MY_VAR` will be `'Staging var'`. |
| 214 | + |
| 215 | +## Secrets in local development |
| 216 | + |
| 217 | +Secrets can be provided to your Worker in local development using a [`.dev.vars`](/workers/configuration/secrets/#local-development-with-secrets) file. If you are using [Cloudflare Environments](../cloudflare-environments) then the relevant `.dev.vars` file will be selected. |
| 218 | + |
| 219 | +For example, |
| 220 | + |
| 221 | +```sh |
| 222 | +CLOUDFLARE_ENV=staging vite dev |
| 223 | +``` |
| 224 | + |
| 225 | +Will load `.dev.vars.staging` if it exists (falling back to `.dev.vars` if otherwise). |
| 226 | + |
| 227 | +:::note |
| 228 | +When you run `vite build`, the relevant `.dev.vars[.env-name]` file is copied to the output directory for use during `vite preview`. It is **not** deployed with your Worker. |
| 229 | +::: |
| 230 | + |
| 231 | +## Migrating from `wrangler dev` |
| 232 | + |
| 233 | +Migrating from `wrangler dev` is a simple process and you can follow the instructions in [Get started](../get-started/). |
| 234 | + |
| 235 | +There are a few key differences to highlight: |
| 236 | + |
| 237 | +#### 1. Input and output Worker configuration files |
| 238 | + |
| 239 | +- Your existing Wrangler configuration file is treated as an **input** configuration. |
| 240 | +- A separate output configuration is generated at build time, capturing a snapshot of your config and referencing your build artifacts. This is the configuration that is used for **preview** and **deployment**. |
| 241 | + |
| 242 | +#### 2. Redundant fields in the Wrangler configuration file |
| 243 | + |
| 244 | +- Some fields are either ignored or replaced by Vite options, as they are either no longer applicable or are replaced by Vite equivalents. |
| 245 | +- If these options are provided, then warnings will be printed to the console with suggestions for how to proceed. For example, you will see console warnings if you use fields like `alias` or `define` that have direct Vite equivalents. |
| 246 | + |
| 247 | +## API |
| 248 | + |
| 249 | +### `cloudflare` |
| 250 | + |
| 251 | +The `cloudflare` plugin should be included in the Vite `plugins` array: |
| 252 | + |
| 253 | +```ts {4, 7} |
| 254 | +// vite.config.ts |
| 255 | + |
| 256 | +import { defineConfig } from "vite"; |
| 257 | +import { cloudflare } from "@cloudflare/vite-plugin"; |
| 258 | + |
| 259 | +export default defineConfig({ |
| 260 | + plugins: [cloudflare()], |
| 261 | +}); |
| 262 | +``` |
| 263 | + |
| 264 | +It accepts an optional `PluginConfig` parameter. |
| 265 | + |
| 266 | +### `interface PluginConfig` |
| 267 | + |
| 268 | +- `configPath?: string` |
| 269 | + |
| 270 | + An optional path to your Worker config file. |
| 271 | + By default, a `wrangler.toml`, `wrangler.json`, or `wrangler.jsonc` file in the root of your application will be used as the Worker config. |
| 272 | + |
| 273 | +- `viteEnvironment?: { name?: string }` |
| 274 | + |
| 275 | + Optional Vite environment options. |
| 276 | + By default, the environment name is the Worker name with `-` characters replaced with `_`. |
| 277 | + Setting the name here will override this. |
| 278 | + |
| 279 | +- `persistState?: boolean | { path: string }` |
| 280 | + |
| 281 | + An optional override for state persistence. |
| 282 | + By default, state is persisted to `.wrangler/state` in a `v3` subdirectory. |
| 283 | + A custom `path` can be provided or, alternatively, persistence can be disabled by setting the value to `false`. |
| 284 | + |
| 285 | +- `auxiliaryWorkers?: Array<AuxiliaryWorkerConfig>` |
| 286 | + |
| 287 | + An optional array of auxiliary Workers. |
| 288 | + You can use [service bindings](/workers/runtime-apis/bindings/service-bindings/) to call auxiliary Workers from your main (entry) Worker. |
| 289 | + All requests are routed through your entry Worker. |
| 290 | + During the build, each Worker is output to a separate subdirectory of `dist`. |
| 291 | + |
| 292 | +:::note |
| 293 | +When running `wrangler deploy`, only your main (entry) Worker will be deployed. |
| 294 | +If using multiple Workers, each must be deployed individually. |
| 295 | +You can inspect the `dist` directory and then run `wrangler deploy -c path-to-worker-output-config` for each. |
| 296 | +::: |
| 297 | + |
| 298 | +### `interface AuxiliaryWorkerConfig` |
| 299 | + |
| 300 | +- `configPath: string` |
| 301 | + |
| 302 | + A required path to your Worker config file. |
| 303 | + |
| 304 | +- `viteEnvironment?: { name?: string }` |
| 305 | + |
| 306 | + Optional Vite environment options. |
| 307 | + By default, the environment name is the Worker name with `-` characters replaced with `_`. |
| 308 | + Setting the name here will override this. |
0 commit comments