web/versioned_docs/version-0.23/project/custom-vite-config.md
import { ShowForTs, ShowForJs } from '@site/src/components/TsJsHelpers' import { Optional } from '@site/src/components/Tag'
Wasp uses Vite to serve the client during development and bundling it for production. If you want to customize the Vite config, you can do that by editing the vite.config.{js,ts} file in your project root directory.
You have full control over your vite.config.ts file. Wasp doesn't manage this file internally. Instead, you must import and use the wasp() plugin from wasp/client/vite in your Vite configuration. This plugin provides all the essential Wasp features:
Here's the minimal required configuration:
<Tabs groupId="js-ts"> <TabItem value="js" label="JavaScript"> ```js title="vite.config.js" import { wasp } from 'wasp/client/vite' import { defineConfig } from 'vite'export default defineConfig({
plugins: [wasp()],
})
```
export default defineConfig({
plugins: [wasp()],
})
```
:::warning Plugin order
The wasp() plugin must be the first plugin in the plugins array. Any other plugins (like Tailwind CSS) should be added after it.
:::
The wasp() plugin enforces certain Vite config values that Wasp needs to function correctly. If you set any of these in your vite.config.ts, Wasp will throw an error asking you to remove them.
| Option | Internal value | Why you can't customize it |
|---|---|---|
base | Based on the client.baseDir option | Wasp sets the React Router's basename to the same value. |
envPrefix | "REACT_APP_" | Wasp's environment variable validation depends on this prefix. |
build.outDir | ".wasp/out/web-app/build" | Build artifacts must go to the location Wasp expects for deployment. |
You can add additional configuration and plugins as needed. The wasp() plugin will use your config and merge it with the built-in defaults.
Vite config customization can be useful for things like:
The wasp() plugin accepts options allowing you to customize the underlying React plugin behavior if needed:
import { wasp } from 'wasp/client/vite'
import { defineConfig } from 'vite'
export default defineConfig({
plugins: [
wasp({
reactOptions: {
// Pass any @vitejs/plugin-react options here
}
})
],
})
Below are some examples of how you can customize the Vite config.
If you want to stop Vite from opening the browser automatically when you run wasp start, you can do that by customizing the open option.
import { wasp } from 'wasp/client/vite'
import { defineConfig } from 'vite'
export default defineConfig({
plugins: [wasp()],
server: {
open: false,
},
})
You have access to all of the Vite dev server options in your custom Vite config. You can change the dev server port by setting the port option.
import { wasp } from 'wasp/client/vite'
import { defineConfig } from 'vite'
export default defineConfig({
plugins: [wasp()],
server: {
port: 4000,
},
})
WASP_WEB_CLIENT_URL=http://localhost:4000
:::warning Changing the dev server port
Be careful when changing the dev server port, you'll need to update the WASP_WEB_CLIENT_URL env var in your .env.server file.
:::
Chrome DevTools support mapping a page's resources to a folder, so any changes you make in the browser are reflected back to your files. To enable it, you can use their Vite plugin: vite-plugin-devtools-json.
npm i -D vite-plugin-devtools-json
vite.config.{ts,js}:import { wasp } from 'wasp/client/vite'
import { defineConfig } from 'vite'
import devtoolsJson from 'vite-plugin-devtools-json'
export default defineConfig({
plugins: [
wasp(),
devtoolsJson({ root: import.meta.dirname })
]
})
wasp start, open Chrome DevTools → Sources → Workspace and you should see your project automatically mapped. Changes you make in DevTools now save to disk and Vite's HMR updates the browser instantly!:::tip Path normalisation
The latest version of vite-plugin-devtools-json includes Windows, WSL and Docker Desktop path fixes contributed by the Wasp community – make sure you are on version 0.4.0 or greater.
:::
import { wasp } from 'wasp/client/vite'
import { defineConfig } from 'vite'
export default defineConfig({
plugins: [
wasp({
reactOptions: {
// ...
},
}),
],
})
The wasp() plugin accepts the following options:
reactOptions: ReactOptions <Optional />Object to customize the underlying @vitejs/plugin-react plugin.
This allows you to configure React-specific options like Babel plugins, Fast Refresh settings, and JSX configuration.