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
bb794b7d81
commit
81c90ed060
6 files changed
+150
-28
No files matched your search
@@ -9,6 +9,9 @@ use tauri_plugin_dialog::DialogExt;
|
||||
|
||||
const MD_EXTS: [&str; 6] = ["md", "markdown", "mdown", "mkd", "mdwn", "txt"];
|
||||
|
||||
/// 首次启动时 argv 里的文件只发一次(页面重载/多窗口不重复发)
|
||||
static STARTUP_FILE_SENT: std::sync::Once = std::sync::Once::new();
|
||||
|
||||
/* ---------------- 数据结构 ---------------- */
|
||||
|
||||
#[derive(Serialize, Clone)]
|
||||
@@ -461,6 +464,22 @@ fn migrate_apply(app: AppHandle, mode: String, docs: Vec<serde_json::Value>, kee
|
||||
fn main() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_dialog::init())
|
||||
// 双击 .md 用本程序打开时的「首次启动」路径:
|
||||
// · 首次启动(进程还不存在)→ 页面加载完成后把 argv 里的文件发给前端
|
||||
// · 已在运行(再双击另一个文件)→ 走下面的 single_instance 回调
|
||||
// 用 Once 保证只发一次,避免页面重载时反复打开同一个文件。
|
||||
.on_page_load(|webview, payload| {
|
||||
if payload.event() == tauri::webview::PageLoadEvent::Finished {
|
||||
STARTUP_FILE_SENT.call_once(|| {
|
||||
if let Some(path) = std::env::args()
|
||||
.skip(1)
|
||||
.find(|a| PathBuf::from(a).is_file())
|
||||
{
|
||||
let _ = webview.emit("open-external", path);
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
.plugin(tauri_plugin_single_instance::init(|app, argv, _cwd| {
|
||||
if let Some(win) = app.get_webview_window("main") {
|
||||
let _ = win.set_focus();
|
||||
|
||||
Reference in new issue
Block a user