|
1 | 1 | ---
|
2 | 2 | title: Vite
|
3 |
| -sidebar: |
4 |
| - badge: |
5 |
| - text: Stub |
6 |
| - variant: caution |
7 | 3 | tableOfContents:
|
8 | 4 | minHeadingLevel: 2
|
9 | 5 | maxHeadingLevel: 5
|
10 | 6 | ---
|
11 | 7 |
|
12 |
| -import Stub from '@components/Stub.astro'; |
| 8 | +import { Tabs, TabItem, Steps } from '@astrojs/starlight/components'; |
13 | 9 |
|
14 |
| -<Stub /> |
| 10 | +Vite is a build tool that aims to provide a faster and leaner development experience for modern web projects. |
| 11 | +This guide is accurate as of Vite 5.4.8. |
| 12 | + |
| 13 | +## Checklist |
| 14 | + |
| 15 | +- Use `dist/` as `frontendDist` in `tauri.conf.json`. |
| 16 | +- Use `process.env.TAURI_DEV_HOST` as the development server host IP when set to run on iOS physical devices. |
| 17 | + |
| 18 | +## Example configuration |
| 19 | + |
| 20 | +<Steps> |
| 21 | + |
| 22 | +1. ##### Update Tauri configuration |
| 23 | + |
| 24 | + Assuming you have the following `dev` and `build` scripts in your `package.json`: |
| 25 | + |
| 26 | + ```json |
| 27 | + { |
| 28 | + "scripts": { |
| 29 | + "dev": "vite dev", |
| 30 | + "build": "vite build" |
| 31 | + } |
| 32 | + } |
| 33 | + ``` |
| 34 | + |
| 35 | + You can configura the Tauri CLI to use your Vite development server and dist folder |
| 36 | + along with the hooks to automatically run the Vite scripts: |
| 37 | + |
| 38 | + <Tabs> |
| 39 | + |
| 40 | + <TabItem label="npm"> |
| 41 | + |
| 42 | + ```json |
| 43 | + // tauri.conf.json |
| 44 | + { |
| 45 | + "build": { |
| 46 | + "beforeDevCommand": "npm run dev", |
| 47 | + "beforeBuildCommand": "npm run build", |
| 48 | + "devUrl": "http://localhost:5173", |
| 49 | + "frontendDist": "../dist" |
| 50 | + } |
| 51 | + } |
| 52 | + ``` |
| 53 | + |
| 54 | + </TabItem> |
| 55 | + |
| 56 | + <TabItem label="yarn"> |
| 57 | + |
| 58 | + ```json |
| 59 | + // tauri.conf.json |
| 60 | + { |
| 61 | + "build": { |
| 62 | + "beforeDevCommand": "yarn dev", |
| 63 | + "beforeBuildCommand": "yarn build", |
| 64 | + "devUrl": "http://localhost:5173", |
| 65 | + "frontendDist": "../dist" |
| 66 | + } |
| 67 | + } |
| 68 | + ``` |
| 69 | + |
| 70 | + </TabItem> |
| 71 | + |
| 72 | + <TabItem label="pnpm"> |
| 73 | + |
| 74 | + ```json |
| 75 | + // tauri.conf.json |
| 76 | + { |
| 77 | + "build": { |
| 78 | + "beforeDevCommand": "pnpm dev", |
| 79 | + "beforeBuildCommand": "pnpm build", |
| 80 | + "devUrl": "http://localhost:5173", |
| 81 | + "frontendDist": "../dist" |
| 82 | + } |
| 83 | + } |
| 84 | + ``` |
| 85 | + |
| 86 | + </TabItem> |
| 87 | + |
| 88 | + </Tabs> |
| 89 | + |
| 90 | +1. ##### Update Vite configuration: |
| 91 | + |
| 92 | + ```js title="vite.config.js" |
| 93 | + import { defineConfig } from 'vite'; |
| 94 | + |
| 95 | + export default defineConfig({ |
| 96 | + // prevent vite from obscuring rust errors |
| 97 | + clearScreen: false, |
| 98 | + server: { |
| 99 | + // Tauri expects a fixed port, fail if that port is not available |
| 100 | + strictPort: true, |
| 101 | + // if the host Tauri is expecting is set, use it |
| 102 | + host: host || false, |
| 103 | + port: 5173, |
| 104 | + }, |
| 105 | + // to access the Tauri environment variables set by the CLI with information about the current target |
| 106 | + envPrefix: ['VITE_', 'TAURI_ENV_*'], |
| 107 | + build: { |
| 108 | + // Tauri uses Chromium on Windows and WebKit on macOS and Linux |
| 109 | + target: |
| 110 | + process.env.TAURI_ENV_PLATFORM == 'windows' |
| 111 | + ? 'chrome105' |
| 112 | + : 'safari13', |
| 113 | + // don't minify for debug builds |
| 114 | + minify: !process.env.TAURI_ENV_DEBUG ? 'esbuild' : false, |
| 115 | + // produce sourcemaps for debug builds |
| 116 | + sourcemap: !!process.env.TAURI_ENV_DEBUG, |
| 117 | + }, |
| 118 | + }); |
| 119 | + ``` |
| 120 | + |
| 121 | +</Steps> |
0 commit comments