快
Vite 是一个构建工具,旨在为现代网络项目提供更快、更精简的开发体验。 本指南适用于 Vite 5.4.8 版本。
🌐 Vite is a build tool that aims to provide a faster and leaner development experience for modern web projects. This guide is accurate as of Vite 5.4.8.
🌐 Checklist
- 在
src-tauri/tauri.conf.json中将../dist用作frontendDist。 - 当设置为在 iOS 实体设备上运行时,使用
process.env.TAURI_DEV_HOST作为开发服务器主机 IP。
🌐 Example configuration
-
假设你在你的
package.json中有以下dev和build脚本:{"scripts": {"dev": "vite","build": "tsc && vite build","preview": "vite preview","tauri": "tauri"}}你可以配置 Tauri CLI 使用你的 Vite 开发服务器和 dist 文件夹,以及钩子来自动运行 Vite 脚本:
tauri.conf.json {"build": {"beforeDevCommand": "npm run dev","beforeBuildCommand": "npm run build","devUrl": "http://localhost:5173","frontendDist": "../dist"}}tauri.conf.json {"build": {"beforeDevCommand": "yarn dev","beforeBuildCommand": "yarn build","devUrl": "http://localhost:5173","frontendDist": "../dist"}}tauri.conf.json {"build": {"beforeDevCommand": "pnpm dev","beforeBuildCommand": "pnpm build","devUrl": "http://localhost:5173","frontendDist": "../dist"}}tauri.conf.json {"build": {"beforeDevCommand": "deno task dev","beforeBuildCommand": "deno task build","devUrl": "http://localhost:5173","frontendDist": "../dist"}} -
vite.config.js import { defineConfig } from 'vite';const host = process.env.TAURI_DEV_HOST;export default defineConfig({// prevent vite from obscuring rust errorsclearScreen: false,server: {// make sure this port matches the devUrl port in tauri.conf.json fileport: 5173,// Tauri expects a fixed port, fail if that port is not availablestrictPort: true,// if the host Tauri is expecting is set, use ithost: host || false,hmr: host? {protocol: 'ws',host,port: 1421,}: undefined,watch: {// tell vite to ignore watching `src-tauri`ignored: ['**/src-tauri/**'],},},// Env variables starting with the item of `envPrefix` will be exposed in tauri's source code through `import.meta.env`.envPrefix: ['VITE_', 'TAURI_ENV_*'],build: {// Tauri uses Chromium on Windows and WebKit on macOS and Linuxtarget:process.env.TAURI_ENV_PLATFORM == 'windows'? 'chrome105': 'safari13',// don't minify for debug buildsminify: !process.env.TAURI_ENV_DEBUG ? 'esbuild' : false,// produce sourcemaps for debug buildssourcemap: !!process.env.TAURI_ENV_DEBUG,},});
Tauri 中文网 - 粤ICP备13048890号
Nodejs.cn 旗下网站