Vue3高级-第二十八篇:Vue3 在跨平台开发中的应用

Vue3高级-第二十八篇:Vue3 在跨平台开发中的应用

一、Vue3 与移动端开发(以 Weex 为例)

(一)深入剖析 Weex 的原理与架构及与 Vue3 的集成方式

  1. 探秘 Weex 原理与架构
    Weex 作为一款开源的跨平台开发框架,赋予开发者运用 JavaScript 结合类似 Vue.js 的语法来打造原生移动应用的能力。其核心原理在于,将开发者基于 Vue 语法编写的 Weex 页面,借助编译工具转化为适用于不同平台(如 iOS、Android)的原生代码。

从架构层面看,Weex 主要由三部分构成。最上层是 Weex SDK,它为开发者提供了统一的 API 用于开发跨平台应用,隐藏了底层不同平台的差异。中间层是 Weex 编译器,负责将 Weex 页面的代码编译成对应平台可识别的代码结构。最底层则是各平台的原生渲染引擎,如 iOS 的 UIKit 和 Android 的 View 系统,用于最终的页面渲染。

  1. Vue3 与 Weex 的集成方式
    在 Vue3 环境下集成 Weex,首先需要安装 Weex 相关的开发工具和依赖。通过 npm 安装 @weex-vue-compiler@weex-vue-renderer 等核心依赖包。

在项目配置方面,Webpack 是常用的构建工具。

D:\admin-project\guangxi-quan-vue3>npm run build:linux > flowoutdemo@0.0.0 build:linux > electron-builder --linux • electron-builder version=26.0.12 os=10.0.26100 • loaded configuration file=package.json ("build" field) • writing effective config file=app_dist\builder-effective-config.yaml • executing @electron/rebuild electronVersion=36.4.0 arch=x64 buildFromSource=false appDir=./ • installing native dependencies arch=x64 • completed installing native dependencies • packaging platform=linux arch=x64 electron=36.4.0 appOutDir=app_dist\linux-unpacked • building target=AppImage arch=x64 file=app_dist\Electron + Vue3 开发桌面应用-0.0.0.AppImage • building target=snap arch=x64 file=app_dist\flowoutdemo_0.0.0_amd64.snap • default Electron icon is used reason=application icon is not set • cannot execute chmod error=exec: "chmod": executable file not found in %PATH% • cannot execute chmod error=exec: "chmod": executable file not found in %PATH% • cannot execute chmod error=exec: "chmod": executable file not found in %PATH% ⨯ cannot execute cause=exec: "C:\\Users\\49118\\AppData\\Local\\electron-builder\\Cache\\appimage\\appimage-12.0.1\\linux-x64\\mksquashfs": file does not exist command=&#39;C:\Users\49118\AppData\Local\electron-builder\Cache\appimage\appimage-12.0.1\linux-x64\mksquashfs&#39; &#39;C:\Users\49118\AppData\Local\electron-builder\Cache\snap\snap-template-electron-4.0-2-amd64\data-dir&#39; &#39;C:\Users\49118\AppData\Local\electron-builder\Cache\snap\snap-template-electron-4.0-2-amd64\desktop-common.sh&#39; &#39;C:\Users\49118\AppData\Local\electron-builder\Cache\snap\snap-template-electron-4.0-2-amd64\desktop-gnome-specific.sh&#39; &#39;C:\Users\49118\AppData\Local\electron-builder\Cache\snap\snap-template-electron-4.0-2-amd64\desktop-init.sh&#39; &#39;C:\Users\49118\AppData\Local\electron-builder\Cache\snap\snap-template-electron-4.0-2-amd64\gnome-platform&#39; &#39;C:\Users\49118\AppData\Local\electron-builder\Cache\snap\snap-template-electron-4.0-2-amd64\lib&#39; &#39;C:\Users\49118\AppData\Local\electron-builder\Cache\snap\snap-template-electron-4.0-2-amd64\usr&#39; &#39;D:\admin-project\guangxi-quan-vue3\app_dist\__snap-amd64\command.sh&#39; &#39;D:\admin-project\guangxi-quan-vue3\app_dist\__snap-amd64\meta&#39; &#39;D:\admin-project\guangxi-quan-vue3\app_dist\__snap-amd64\scripts&#39; &#39;D:\admin-project\guangxi-quan-vue3\app_dist\linux-unpacked\chrome_100_percent.pak&#39; &#39;D:\admin-project\guangxi-quan-vue3\app_dist\linux-unpacked\chrome_200_percent.pak&#39; &#39;D:\admin-project\guangxi-quan-vue3\app_dist\linux-unpacked\chrome_crashpad_handler&#39; &#39;D:\admin-project\guangxi-quan-vue3\app_dist\linux-unpacked\flowoutdemo&#39; &#39;D:\admin-project\guangxi-quan-vue3\app_dist\linux-unpacked\icudtl.dat&#39; &#39;D:\admin-project\guangxi-quan-vue3\app_dist\linux-unpacked\libEGL.so&#39; &#39;D:\admin-project\guangxi-quan-vue3\app_dist\linux-unpacked\libffmpeg.so&#39; &#39;D:\admin-project\guangxi-quan-vue3\app_dist\linux-unpacked\libGLESv2.so&#39; &#39;D:\admin-project\guangxi-quan-vue3\app_dist\linux-unpacked\libvk_swiftshader.so&#39; &#39;D:\admin-project\guangxi-quan-vue3\app_dist\linux-unpacked\libvulkan.so.1&#39; &#39;D:\admin-project\guangxi-quan-vue3\app_dist\linux-unpacked\locales&#39; &#39;D:\admin-project\guangxi-quan-vue3\app_dist\linux-unpacked\resources&#39; &#39;D:\admin-project\guangxi-quan-vue3\app_dist\linux-unpacked\resources.pak&#39; &#39;D:\admin-project\guangxi-quan-vue3\app_dist\linux-unpacked\snapshot_blob.bin&#39; &#39;D:\admin-project\guangxi-quan-vue3\app_dist\linux-unpacked\v8_context_snapshot.bin&#39; &#39;D:\admin-project\guangxi-quan-vue3\app_dist\linux-unpacked\vk_swiftshader_icd.json&#39; &#39;D:\admin-project\guangxi-quan-vue3\app_dist\flowoutdemo_0.0.0_amd64.snap&#39; -no-progress -quiet -noappend -comp xz -no-xattrs -no-fragments -all-root workingDir= ⨯ cannot execute cause=exec: "C:\\Users\\49118\\AppData\\Local\\electron-builder\\Cache\\appimage\\appimage-12.0.1\\linux-x64\\mksquashfs": file does not exist command=&#39;C:\Users\49118\AppData\Local\electron-builder\Cache\appimage\appimage-12.0.1\linux-x64\mksquashfs&#39; &#39;D:\admin-project\guangxi-quan-vue3\app_dist\__appImage-x64&#39; &#39;D:\admin-project\guangxi-quan-vue3\app_dist\Electron + Vue3 开发桌面应用-0.0.0.AppImage&#39; -offset 188392 -all-root -noappend -no-progress -quiet -no-xattrs -no-fragments workingDir=D:\admin-project\guangxi-quan-vue3\app_dist\__appImage-x64 ⨯ Cannot cleanup: Error #1 -------------------------------------------------------------------------------- Error: D:\admin-project\guangxi-quan-vue3\node_modules\app-builder-bin\win\x64\app-builder.exe process failed ERR_ELECTRON_BUILDER_CANNOT_EXECUTE Exit code: 2 at ChildProcess.<anonymous> (D:\admin-project\guangxi-quan-vue3\node_modules\builder-util\src\util.ts:259:14) at Object.onceWrapper (node:events:632:26) at ChildProcess.emit (node:events:517:28) at ChildProcess.cp.emit (D:\admin-project\guangxi-quan-vue3\node_modules\cross-spawn\lib\enoent.js:34:29) at maybeClose (node:internal/child_process:1098:16) at Process.ChildProcess._handle.onexit (node:internal/child_process:303:5) Error #2 -------------------------------------------------------------------------------- Error: D:\admin-project\guangxi-quan-vue3\node_modules\app-builder-bin\win\x64\app-builder.exe process failed ERR_ELECTRON_BUILDER_CANNOT_EXECUTE Exit code: 2 at ChildProcess.<anonymous> (D:\admin-project\guangxi-quan-vue3\node_modules\builder-util\src\util.ts:259:14) at Object.onceWrapper (node:events:632:26) at ChildProcess.emit (node:events:517:28) at ChildProcess.cp.emit (D:\admin-project\guangxi-quan-vue3\node_modules\cross-spawn\lib\enoent.js:34:29) at maybeClose (node:internal/child_process:1098:16) at Process.ChildProcess._handle.onexit (node:internal/child_process:303:5) failedTask=build stackTrace=Error: Cannot cleanup: Error #1 -------------------------------------------------------------------------------- Error: D:\admin-project\guangxi-quan-vue3\node_modules\app-builder-bin\win\x64\app-builder.exe process failed ERR_ELECTRON_BUILDER_CANNOT_EXECUTE Exit code: 2 at ChildProcess.<anonymous> (D:\admin-project\guangxi-quan-vue3\node_modules\builder-util\src\util.ts:259:14) at Object.onceWrapper (node:events:632:26) at ChildProcess.emit (node:events:517:28) at ChildProcess.cp.emit (D:\admin-project\guangxi-quan-vue3\node_modules\cross-spawn\lib\enoent.js:34:29) at maybeClose (node:internal/child_process:1098:16) at Process.ChildProcess._handle.onexit (node:internal/child_process:303:5) Error #2 -------------------------------------------------------------------------------- Error: D:\admin-project\guangxi-quan-vue3\node_modules\app-builder-bin\win\x64\app-builder.exe process failed ERR_ELECTRON_BUILDER_CANNOT_EXECUTE Exit code: 2 at ChildProcess.<anonymous> (D:\admin-project\guangxi-quan-vue3\node_modules\builder-util\src\util.ts:259:14) at Object.onceWrapper (node:events:632:26) at ChildProcess.emit (node:events:517:28) at ChildProcess.cp.emit (D:\admin-project\guangxi-quan-vue3\node_modules\cross-spawn\lib\enoent.js:34:29) at maybeClose (node:internal/child_process:1098:16) at Process.ChildProcess._handle.onexit (node:internal/child_process:303:5) at throwError (D:\admin-project\guangxi-quan-vue3\node_modules\builder-util\src\asyncTaskManager.ts:88:11) at checkErrors (D:\admin-project\guangxi-quan-vue3\node_modules\builder-util\src\asyncTaskManager.ts:53:9) at AsyncTaskManager.awaitTasks (D:\admin-project\guangxi-quan-vue3\node_modules\builder-util\src\asyncTaskManager.ts:67:7) at processTicksAndRejections (node:internal/process/task_queues:95:5) at D:\admin-project\guangxi-quan-vue3\node_modules\app-builder-lib\src\platformPackager.ts:189:7 at async Promise.all (index 0) at AsyncTaskManager.awaitTasks (D:\admin-project\guangxi-quan-vue3\node_modules\builder-util\src\asyncTaskManager.ts:65:25) at Packager.doBuild (D:\admin-project\guangxi-quan-vue3\node_modules\app-builder-lib\src\packager.ts:507:5) at executeFinally (D:\admin-project\guangxi-quan-vue3\node_modules\builder-util\src\promise.ts:12:14) at Packager.build (D:\admin-project\guangxi-quan-vue3\node_modules\app-builder-lib\src\packager.ts:425:31)
06-07
<think>我们遇到的问题是:在Windows系统上使用electron-builder构建Linux应用时出现了&#39;chmod&#39;和&#39;mksquashfs&#39;执行错误。根本原因是:构建Linux应用所需的工具链(如chmod和mksquashfs)是Linux环境特有的,在Windows上不可用。解决思路:使用WSL(WindowsSubsystemforLinux)在Windows内运行Linux环境来执行构建。###解决方案####第一步:安装WSL(若未安装)1.以管理员身份打开PowerShell,执行:```powershellwsl--install```2.重启电脑[^1]####第二步:在WSL中安装构建依赖1.启动WSL终端(如Ubuntu)2.安装必需工具:```bashsudoaptupdatesudoaptinstall-ybuild-essentialfakerootdpkgsquashfs-tools```>`squashfs-tools`提供`mksquashfs`命令,`fakeroot`和`dpkg`用于deb包构建[^2]####第三步:在WSL中执行构建1.将项目目录映射到WSL(假设项目在Windows的`D:\admin-project\guangxi-quan-vue3`):```bashcd/mnt/d/admin-project/guangxi-quan-vue3```2.重新安装依赖(确保node_modules兼容Linux):```bashnpminstall```3.执行Linux构建命令:```bashnpmrunbuild:linux```###错误处理补充若出现文件权限问题(如`ENOENT:nosuchfileordirectory,chmod...`),在`package.json`中添加:```json{"build":{"linux":{"executableName":"your-app","artifactName":"${productName}-${version}.${ext}"},"afterPack":"./fix-permissions.js"}}```创建`fix-permissions.js`:```javascriptimportfsfrom&#39;fs&#39;;exportdefault(context)=>{fs.chmodSync(context.appOutDir+&#39;/your-app&#39;,0o755);};```###技术原理-**WSL作用**:提供完整的Linux环境,使Linux专用工具链(如`mksquashfs`)可用-**权限修复**:Electron应用在Linux下需要可执行权限(0755),Windows构建时可能丢失-**路径映射**:`/mnt/`是WSL自动挂载Windows驱动器的位置###构建流程对比|步骤|Windows直接构建|WSL构建||--------------------|----------------------------------|---------------------------------||环境工具链|❌缺少Linux工具|✅完整Linux环境||`chmod`处理|❌失败|✅自动处理权限||`mksquashfs`可用性|❌不可用|✅通过`squashfs-tools`安装提供|>💡**提示**:使用WSL构建时,输出文件会生成在项目`dist/`目录(同时适用于Linux和Windows访问)§§相关问题§§1.如何为WSL配置持久化环境变量?2.electron-builder如何实现多平台打包的配置管理?3.如何优化Electron应用的Linux打包体积?</think> ### 解决 electron-builder 在 Windows 构建 Linux 应用时 "chmod" 和 "mksquashfs" 错误的完整方案 #### 🔧 核心问题分析 错误关键点: 1. **`chmod` 缺失**:Windows 没有 Linux 的权限系统,导致权限设置失败 2. **`mksquashfs` 缺失**:AppImage 打包需要 Linux 专有工具链 3. **缓存污染**:历史缓存可能包含跨平台冲突的文件[^1] #### ⚡ 终极解决方案:使用 WSL 构建 ```mermaid graph LR A[Windows主机] --> B[WSL Linux环境] B --> C[安装squashfs-tools] B --> D[执行npm build:linux] C --> E[生成AppImage/snap] ``` ##### 步骤 1:安装 WSL ```powershell # Windows PowerShell 管理员模式 wsl --install wsl --set-default-version 2 ``` ##### 步骤 2:设置 WSL 构建环境 ```bash # WSL 终端中执行 sudo apt update sudo apt install -y build-essential fakeroot squashfs-tools ``` ##### 步骤 3:执行 Linux 构建 ```bash # 映射 Windows 项目路径 cd /mnt/d/admin-project/guangxi-quan-vue3 # 清理旧依赖 rm -rf node_modules # 安装依赖并构建 npm install npm run build:linux ``` #### 🛠️ 替代方案:Windows 直接修复(不推荐) 若必须使用 Windows: 1. 修改 `package.json`: ```json { "build": { "linux": { "target": ["dir"], // 禁用 AppImage/Snap "executableName": "your-app" }, "afterPack": "./fix-permissions.js" } } ``` 2. 创建 `fix-permissions.js`: ```javascript const fs = require(&#39;fs&#39;) module.exports = async (context) => { const exePath = `${context.appOutDir}/your-app` if (fs.existsSync(exePath)) { try { fs.chmodSync(exePath, 0o755) console.log(`✅ 权限修复成功: ${exePath}`) } catch (e) { console.error(&#39;❌ 权限修复失败&#39;, e) } } } ``` #### ✅ 验证构建结果 ```bash # 在 WSL 中检查 ls dist/linux-unpacked/ file dist/linux-unpacked/your-app # 应显示 ELF 可执行文件 ``` #### ⚠️ 注意事项 1. 缓存清理建议: ```powershell Remove-Item -Recurse -Force $env:LOCALAPPDATA/electron-builder npm cache clean --force ``` 2. 图标强制指定: ```json "build": { "linux": { "icon": "build/icons" } } ``` > 💡 技术原理:Windows 缺少 Linux 的底层工具链[^2]。通过 WSL 我们实现了: > $$ > \text{Windows} \xrightarrow{\text{WSL}} \text{Linux} \xrightarrow{\text{mksquashfs}} \text{AppImage} > $$
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员勇哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值