Skip to content

Tauri 打包发布

构建配置

基础配置

json
// tauri.conf.json
{
  "build": {
    "beforeBuildCommand": "npm run build",
    "beforeDevCommand": "npm run dev",
    "frontendDist": "../dist"
  },
  "bundle": {
    "active": true,
    "icon": [
      "icons/32x32.png",
      "icons/128x128.png",
      "icons/128x128@2x.png",
      "icons/icon.icns",
      "icons/icon.ico"
    ],
    "identifier": "com.mycompany.myapp",
    "targets": "all"
  },
  "productName": "My App",
  "version": "1.0.0"
}

平台特定配置

json
{
  "bundle": {
    "macOS": {
      "minimumSystemVersion": "10.13",
      "entitlements": "./entitlements.plist",
      "signingIdentity": "Developer ID Application: Company Name",
      "providerShortName": "TEAMID"
    },
    "windows": {
      "wix": {
        "language": ["zh-CN", "en-US"]
      },
      "nsis": {
        "languages": ["SimpChinese", "English"],
        "displayLanguageSelector": true
      }
    },
    "linux": {
      "deb": {
        "depends": ["libwebkit2gtk-4.1-0"]
      },
      "appimage": {
        "bundleMediaFramework": true
      }
    }
  }
}

多平台构建

GitHub Actions

yaml
# .github/workflows/release.yml
name: Release

on:
  push:
    tags:
      - 'v*'

jobs:
  build:
    strategy:
      matrix:
        include:
          - platform: macos-latest
            target: aarch64-apple-darwin
          - platform: macos-latest
            target: x86_64-apple-darwin
          - platform: ubuntu-22.04
            target: x86_64-unknown-linux-gnu
          - platform: windows-latest
            target: x86_64-pc-windows-msvc

    runs-on: ${{ matrix.platform }}
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 20
          
      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}
          
      - name: Install dependencies (Linux)
        if: matrix.platform == 'ubuntu-22.04'
        run: |
          sudo apt-get update
          sudo apt-get install -y libwebkit2gtk-4.1-dev
          
      - name: Install frontend dependencies
        run: npm ci
        
      - name: Build
        uses: tauri-apps/tauri-action@v0
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY }}
        with:
          tagName: v__VERSION__
          releaseName: 'v__VERSION__'
          releaseBody: 'See the assets for download.'

macOS 通用二进制

bash
# 构建 Intel 版本
npm run tauri build -- --target x86_64-apple-darwin

# 构建 Apple Silicon 版本
npm run tauri build -- --target aarch64-apple-darwin

# 合并为通用二进制
npm run tauri build -- --target universal-apple-darwin

代码签名

macOS 签名

bash
# 1. 导出开发者证书
# 2. 配置环境变量
export APPLE_SIGNING_IDENTITY="Developer ID Application: Your Name (TEAMID)"
export APPLE_ID="your@email.com"
export APPLE_PASSWORD="app-specific-password"
export APPLE_TEAM_ID="TEAMID"

# 3. 构建并公证
npm run tauri build
xml
<!-- entitlements.plist -->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "...">
<plist version="1.0">
<dict>
    <key>com.apple.security.app-sandbox</key>
    <true/>
    <key>com.apple.security.network.client</key>
    <true/>
    <key>com.apple.security.files.user-selected.read-write</key>
    <true/>
</dict>
</plist>

Windows 签名

powershell
# 使用 SignTool
$env:TAURI_SIGNING_PRIVATE_KEY = "path/to/certificate.pfx"
$env:TAURI_SIGNING_PRIVATE_KEY_PASSWORD = "certificate-password"

npm run tauri build

自动更新

配置更新端点

json
// tauri.conf.json
{
  "plugins": {
    "updater": {
      "active": true,
      "endpoints": [
        "https://releases.myapp.com/{{target}}/{{arch}}/{{current_version}}"
      ],
      "pubkey": "dW50cnVzdGVkIGNvbW1lbnQ6..."
    }
  }
}

更新服务器响应

json
// https://releases.myapp.com/darwin/aarch64/1.0.0
{
  "version": "1.1.0",
  "notes": "Bug fixes and performance improvements",
  "pub_date": "2024-01-15T00:00:00Z",
  "platforms": {
    "darwin-aarch64": {
      "signature": "dW50cnVzdGVkIGNvbW1lbnQ6...",
      "url": "https://releases.myapp.com/MyApp_1.1.0_aarch64.app.tar.gz"
    }
  }
}

前端更新逻辑

typescript
import { check } from '@tauri-apps/plugin-updater';
import { relaunch } from '@tauri-apps/plugin-process';

async function checkForUpdates() {
  const update = await check();
  
  if (update?.available) {
    console.log(`Update available: ${update.version}`);
    
    // 下载并安装
    await update.downloadAndInstall((event) => {
      switch (event.event) {
        case 'Started':
          console.log(`Downloading ${event.data.contentLength} bytes`);
          break;
        case 'Progress':
          console.log(`Downloaded ${event.data.chunkLength} bytes`);
          break;
        case 'Finished':
          console.log('Download complete');
          break;
      }
    });
    
    // 重启应用
    await relaunch();
  }
}

调试与分析

bash
# 开发模式调试
npm run tauri dev

# 构建时包含调试符号
npm run tauri build -- --debug

# 分析包体积
npm run tauri build -- --verbose

面试要点

  1. 多平台构建:GitHub Actions 矩阵构建
  2. 代码签名:macOS 公证、Windows 证书
  3. 自动更新:签名验证、增量更新
  4. 包体积优化:Rust release 优化、前端代码分割

前端面试知识库