Skip to content
Tauri 中文网

Vite

Vite 是一种构建工具,旨在为现代 Web 项目提供更快、更精简的开发体验。本指南适用于 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

  • tauri.conf.json 中将 dist/ 用作 frontendDist

    ¥Use dist/ as frontendDist in tauri.conf.json.

  • 设置为在 iOS 物理设备上运行时,使用 process.env.TAURI_DEV_HOST 作为开发服务器主机 IP。

    ¥Use process.env.TAURI_DEV_HOST as the development server host IP when set to run on iOS physical devices.

示例配置

¥Example configuration

  1. Assuming you have the following dev and build scripts in your package.json:

    {
    "scripts": {
    "dev": "vite dev",
    "build": "vite build"
    }
    }

    You can configure the Tauri CLI to use your Vite development server and dist folder along with the hooks to automatically run the Vite scripts:

    tauri.conf.json
    {
    "build": {
    "beforeDevCommand": "npm run dev",
    "beforeBuildCommand": "npm run build",
    "devUrl": "http://localhost:5173",
    "frontendDist": "../dist"
    }
    }
  2. Update Vite configuration:
    vite.config.js
    import { defineConfig } from 'vite';
    const host = process.env.TAURI_DEV_HOST;
    export default defineConfig({
    // prevent vite from obscuring rust errors
    clearScreen: false,
    server: {
    // Tauri expects a fixed port, fail if that port is not available
    strictPort: true,
    // if the host Tauri is expecting is set, use it
    host: host || false,
    port: 5173,
    },
    // 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 Linux
    target:
    process.env.TAURI_ENV_PLATFORM == 'windows'
    ? 'chrome105'
    : 'safari13',
    // don't minify for debug builds
    minify: !process.env.TAURI_ENV_DEBUG ? 'esbuild' : false,
    // produce sourcemaps for debug builds
    sourcemap: !!process.env.TAURI_ENV_DEBUG,
    },
    });

Tauri 中文网 - 粤ICP备13048890号