打包配置 #

一、打包概述 #

1.1 打包流程 #

text
┌─────────────────────────────────────────────────────────────┐
│                      打包流程                                │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  前端构建 ──► Rust 编译 ──► 资源打包 ──► 安装包生成          │
│      │           │           │           │                  │
│      ▼           ▼           ▼           ▼                  │
│  Vite/Rollup  cargo build  资源整合   MSI/DMG/DEB           │
│                                                             │
└─────────────────────────────────────────────────────────────┘

1.2 打包命令 #

bash
# 开发模式构建
pnpm tauri build --debug

# 生产模式构建
pnpm tauri build

# 构建特定平台
pnpm tauri build --target x86_64-pc-windows-msvc
pnpm tauri build --target aarch64-apple-darwin

二、基础配置 #

2.1 tauri.conf.json #

json
{
    "productName": "My App",
    "version": "1.0.0",
    "identifier": "com.mycompany.myapp",
    "build": {
        "beforeDevCommand": "pnpm dev",
        "devUrl": "http://localhost:1420",
        "beforeBuildCommand": "pnpm build",
        "frontendDist": "../dist"
    },
    "bundle": {
        "active": true,
        "targets": "all",
        "icon": [
            "icons/32x32.png",
            "icons/128x128.png",
            "icons/128x128@2x.png",
            "icons/icon.icns",
            "icons/icon.ico"
        ],
        "resources": [],
        "copyright": "",
        "category": "Utility",
        "shortDescription": "My Application",
        "longDescription": "A powerful desktop application",
        "externalBin": []
    }
}

2.2 应用元数据 #

json
{
    "productName": "My App",
    "version": "1.0.0",
    "identifier": "com.mycompany.myapp",
    "bundle": {
        "publisher": "My Company",
        "copyright": "Copyright © 2024 My Company",
        "category": "Utility",
        "shortDescription": "A powerful desktop application",
        "longDescription": "This is a detailed description of my application.",
        "homepage": "https://myapp.com",
        "license": "MIT",
        "licenseFile": "LICENSE"
    }
}

三、图标配置 #

3.1 图标要求 #

平台 格式 尺寸
Windows .ico 256x256
macOS .icns 512x512, 1024x1024
Linux .png 512x512

3.2 图标配置 #

json
{
    "bundle": {
        "icon": [
            "icons/32x32.png",
            "icons/128x128.png",
            "icons/128x128@2x.png",
            "icons/icon.icns",
            "icons/icon.ico"
        ]
    }
}

3.3 生成图标 #

bash
# 使用 tauri icon 命令生成所有尺寸
pnpm tauri icon /path/to/source-icon.png

四、Windows 配置 #

4.1 Windows 打包选项 #

json
{
    "bundle": {
        "windows": {
            "certificateThumbprint": null,
            "digestAlgorithm": "sha256",
            "timestampUrl": "",
            "webviewInstallMode": {
                "type": "downloadBootstrapper"
            },
            "webviewFixedRuntimePath": null,
            "nsis": {
                "installerIcon": "icons/icon.ico",
                "headerImage": "icons/header.bmp",
                "sidebarImage": "icons/sidebar.bmp",
                "license": null,
                "installMode": "currentUser",
                "languages": ["SimpChinese", "English"],
                "displayLanguageSelector": true
            },
            "wix": {
                "language": "zh-CN",
                "template": null
            }
        }
    }
}

4.2 NSIS 配置 #

json
{
    "nsis": {
        "installerIcon": "icons/icon.ico",
        "headerImage": "icons/header.bmp",
        "sidebarImage": "icons/sidebar.bmp",
        "license": "LICENSE",
        "installMode": "currentUser",
        "languages": ["SimpChinese", "English"],
        "displayLanguageSelector": true,
        "installerHooks": "installer.nsi"
    }
}

4.3 WiX 配置 #

json
{
    "wix": {
        "language": "zh-CN",
        "template": null,
        "fragmentPaths": [],
        "componentRefs": [],
        "featureRefs": [],
        "mergeRefs": [],
        "bannerPath": "icons/banner.bmp",
        "dialogImagePath": "icons/dialog.bmp"
    }
}

五、macOS 配置 #

5.1 macOS 打包选项 #

json
{
    "bundle": {
        "macOS": {
            "minimumSystemVersion": "10.13",
            "entitlements": null,
            "exceptionDomain": "",
            "frameworks": [],
            "providerShortName": null,
            "signingIdentity": null,
            "signingCertificate": null,
            " hardenedRuntime": true,
            "dmg": {
                "appPosition": {
                    "x": 180,
                    "y": 170
                },
                "applicationFolderPosition": {
                    "x": 480,
                    "y": 170
                },
                "windowSize": {
                    "width": 660,
                    "height": 400
                }
            }
        }
    }
}

5.2 DMG 配置 #

json
{
    "dmg": {
        "appPosition": {
            "x": 180,
            "y": 170
        },
        "applicationFolderPosition": {
            "x": 480,
            "y": 170
        },
        "windowSize": {
            "width": 660,
            "height": 400
        },
        "title": "Install My App"
    }
}

六、Linux 配置 #

6.1 Linux 打包选项 #

json
{
    "bundle": {
        "linux": {
            "deb": {
                "depends": ["libwebkit2gtk-4.1-0"],
                "desktopTemplate": null,
                "files": {}
            },
            "appimage": {
                "bundleMediaFramework": false
            },
            "rpm": {
                "depends": [],
                "desktopTemplate": null
            }
        }
    }
}

6.2 DEB 配置 #

json
{
    "deb": {
        "depends": [
            "libwebkit2gtk-4.1-0",
            "libgtk-3-0"
        ],
        "desktopTemplate": "template.desktop",
        "files": {
            "/usr/share/myapp/config": "config/default.conf"
        },
        "section": "utils",
        "priority": "optional"
    }
}

6.3 AppImage 配置 #

json
{
    "appimage": {
        "bundleMediaFramework": false,
        "mediaFramework": "gstreamer"
    }
}

七、资源打包 #

7.1 包含资源 #

json
{
    "bundle": {
        "resources": [
            "data/*",
            "config/*",
            "assets/**/*"
        ]
    }
}

7.2 外部二进制 #

json
{
    "bundle": {
        "externalBin": [
            "binaries/my-tool",
            "binaries/helper"
        ]
    }
}

7.3 访问资源 #

rust
use tauri::Manager;

fn get_resource_path(app: &tauri::AppHandle, name: &str) -> Option<std::path::PathBuf> {
    app.path().resolve(name, tauri::path::BaseDirectory::Resource).ok()
}

八、构建优化 #

8.1 Rust 优化配置 #

toml
# Cargo.toml
[profile.release]
panic = "abort"
codegen-units = 1
lto = true
opt-level = "s"
strip = true

8.2 前端优化 #

typescript
// vite.config.ts
export default defineConfig({
    build: {
        minify: 'esbuild',
        target: ['es2021', 'chrome100', 'safari13'],
        rollupOptions: {
            output: {
                manualChunks: {
                    vendor: ['react', 'react-dom'],
                },
            },
        },
    },
});

8.3 减小体积 #

json
{
    "bundle": {
        "targets": ["msi", "app"],  // 只构建需要的格式
        "externalBin": []  // 避免包含不必要的二进制
    }
}

九、构建脚本 #

9.1 package.json 脚本 #

json
{
    "scripts": {
        "dev": "vite",
        "build": "tsc && vite build",
        "tauri": "tauri",
        "tauri:dev": "tauri dev",
        "tauri:build": "tauri build",
        "tauri:build:debug": "tauri build --debug"
    }
}

9.2 CI/CD 构建 #

yaml
# .github/workflows/build.yml
name: Build

on: [push]

jobs:
    build:
        strategy:
            matrix:
                platform: [macos-latest, ubuntu-latest, windows-latest]
        
        runs-on: ${{ matrix.platform }}
        
        steps:
            - uses: actions/checkout@v3
            
            - name: Setup Node.js
              uses: actions/setup-node@v3
              with:
                  node-version: 20
            
            - name: Setup Rust
              uses: actions-rs/toolchain@v1
              with:
                  toolchain: stable
            
            - name: Install dependencies
              run: pnpm install
            
            - name: Build
              run: pnpm tauri build
            
            - name: Upload artifacts
              uses: actions/upload-artifact@v3
              with:
                  name: ${{ matrix.platform }}-build
                  path: src-tauri/target/release/bundle/

十、总结 #

10.1 核心要点 #

要点 说明
配置文件 tauri.conf.json
图标配置 各平台图标格式
平台配置 Windows/macOS/Linux
资源打包 包含必要资源
构建优化 减小应用体积

10.2 下一步 #

现在你已经掌握了打包配置,接下来让我们学习 自动更新,了解如何实现应用自动更新!

最后更新:2026-03-28