feat(build): Tauri 交叉编译出 Windows 安装包(1.6MB),装完 .md 默认用它打开
老板要求:用 Tauri 打包成 exe,安装后 .md 默认由小银 Markdown 打开。 一、交叉编译(Linux → Windows,无需 Windows 机器) 本机从零装了 rustc 1.100 / mingw-w64(GCC 14)/ NSIS 3.11, target `x86_64-pc-windows-gnu`,crates 走清华镜像: npx tauri build --target x86_64-pc-windows-gnu --bundles nsis 产物: · XiaoyinMarkdown_2.6.1_x64-setup.exe 1.52 MiB(NSIS 安装包) · xiaoyin-markdown.exe 3.5 MiB(免安装版) electron-packager 那版是 152 MiB —— 因为 Tauri 用系统 WebView2,不捆绑运行时。 已核对导入表:只依赖系统 DLL + 安装包内自带的 WebView2Loader.dll, 没有 libgcc_s_seh-1 / libwinpthread 之类的 mingw 运行时依赖,干净 Windows 可跑。 二、默认打开方式(两步,缺一不可) 1. tauri.conf.json 的 fileAssociations 早就配了 md/markdown/mdown/mkd/mdwn/txt; 已核对生成的 installer.nsi:6 条 APP_ASSOCIATE,写 HKCU\Software\Classes\.md = XiaoyinMarkdown.Document(默认 ProgId,不只是「打开方式」列表) 2. 新增 src-tauri/installer-hooks.nsh(NSIS_HOOK_POSTINSTALL): 删掉 HKCU\...\Explorer\FileExts\.md\UserChoice —— 这个键带哈希保护、只能删不能写, 不删的话用户原来的默认程序永远赢;删完再 SHChangeNotify 刷新图标缓存。 已用「故意 !error 探针」证明该钩子确实被 makensis 编译进去(installer.nsi:758), 不是写了没生效。 三、首次双击 .md 能打开文件 原来只有 single_instance 回调处理 argv(= 程序已在运行时才生效)。 新增 on_page_load(Finished) + std::sync::Once:冷启动时把 argv 里的文件路径 emit 给前端(前端 tauri-bridge 监听 open-external → doOpenPath)。 四、官网 下载区改成三档:安装版(1.6MB,推荐)/ 免安装 exe(3.5MB)/ 源码 + 旧 Electron 版。 两个新文件已部署到 /download/,公网 200。 README 补了「用 Tauri 打包」整节(含 UserChoice 这个坑),site/README 更新发布步骤。
This commit is contained in:
1 parent
29fc7ef3ff
commit
793eb56710
6 files changed
+150
-28
No files matched your search
@@ -147,6 +147,41 @@ npx electron-packager . XiaoyinMarkdown --platform=win32 --arch=x64 --out=dist ^
|
||||
|
||||
打包完成后把 `dist\XiaoyinMarkdown-win32-x64` 整个目录拷到 `%USERPROFILE%\Apps\XiaoyinMarkdown` 覆盖即可(exe 依赖同目录的 dll,不能只拷 exe)。
|
||||
|
||||
## 用 Tauri 打包(推荐的安装包)
|
||||
|
||||
Tauri 版走系统 WebView2,**安装包只有 1.6 MB**(Electron 版 152 MB)。
|
||||
在 Linux 上也能交叉编译出 Windows 安装包:
|
||||
|
||||
```bash
|
||||
# 一次性准备
|
||||
apt-get install -y mingw-w64 nsis
|
||||
curl -sSf https://sh.rustup.rs | sh -s -- -y --profile minimal # 国内可设 RUSTUP_DIST_SERVER 用清华镜像
|
||||
rustup target add x86_64-pc-windows-gnu
|
||||
mkdir -p ~/.cargo && cat >> ~/.cargo/config.toml <<'EOF'
|
||||
[target.x86_64-pc-windows-gnu]
|
||||
linker = "x86_64-w64-mingw32-gcc"
|
||||
ar = "x86_64-w64-mingw32-ar"
|
||||
EOF
|
||||
|
||||
# 打包
|
||||
npm install
|
||||
npx tauri build --target x86_64-pc-windows-gnu --bundles nsis
|
||||
# → src-tauri/target/x86_64-pc-windows-gnu/release/bundle/nsis/XiaoyinMarkdown_<版本>_x64-setup.exe
|
||||
```
|
||||
|
||||
### 装完 .md 默认用小银 Markdown 打开
|
||||
|
||||
两步配合,缺一不可:
|
||||
|
||||
1. `tauri.conf.json` 的 `bundle.fileAssociations` → 写 `HKCU\Software\Classes\.md` = `XiaoyinMarkdown.Document`
|
||||
(含 markdown / mdown / mkd / mdwn / txt)
|
||||
2. `src-tauri/installer-hooks.nsh`(NSIS `NSIS_HOOK_POSTINSTALL`)→ 删掉
|
||||
`HKCU\...\Explorer\FileExts\.md\UserChoice`。这个键带哈希保护、**只能删不能写**,
|
||||
删掉后系统才回落到第 1 步写的关联,否则用户原来的默认程序会一直赢。
|
||||
|
||||
首次双击 .md 时是冷启动,文件路径由 Rust 的 `on_page_load` 读 `argv` 发给前端
|
||||
(`main.rs` 里的 `STARTUP_FILE_SENT`);程序已在运行时再双击,走 single-instance 回调。
|
||||
|
||||
## 说明
|
||||
|
||||
- 未做代码签名,首次运行 Windows SmartScreen 可能提示"未知发布者",选"仍要运行"即可。
|
||||
|
||||
Reference in new issue
Block a user