uni-app electron打包成exe应用
1.安装electroncnpm install electron -g
2.安装electron-packagercnpm install electron-packager -g
3.uni-app的manifest.json修改
运行的基础路径修改为:./ 不然打包出来会出现白屏,读取不到,因为打包出来的h5默认加载地址为/static/
去掉启用https协议: 不然会出现网络无法加载,去掉https不影响你请求后端的https协议。
4.打包成H5
5.H5文件夹下新建package.json和main.js
package.json
1 | { "name" : "exam考试系统" , "version" : "0.1.0" , "main" : "main.js" } |
main.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | const path = require( 'path' ) const url = require( 'url' ) // Keep a global reference of the window object, if you don't, the window will // be closed automatically when the JavaScript object is garbage collected. let win function createWindow () { // Create the browser window. //设置窗口大小 //win = new BrowserWindow({width: 400, height: 700}) 设置窗口最大化 // win = new BrowserWindow({width: 400, height: 700}) win = new BrowserWindow({show: false }) win.maximize() win.show() // and load the index.html of the app. win.loadURL(url.format({ pathname: path. join (__dirname, 'index.html' ), protocol: 'file:' , slashes: true })) // Open the DevTools. // win.webContents.openDevTools() // Emitted when the window is closed. win. on ( 'closed' , () => { // Dereference the window object, usually you would store windows // in an array if your app supports multi windows, this is the time // when you should delete the corresponding element. win = null }) } // This method will be called when Electron has finished // initialization and is ready to create browser windows. // Some APIs can only be used after this event occurs. app. on ( 'ready' , createWindow) // Quit when all windows are closed. app. on ( 'window-all-closed' , () => { // On macOS it is common for applications and their menu bar // to stay active until the user quits explicitly with Cmd + Q if (process.platform !== 'darwin' ) { app.quit() } }) app. on ( 'activate' , () => { // On macOS it's common to re-create a window in the app when the // dock icon is clicked and there are no other windows open. if (win === null ) { createWindow() } }) // In this file you can include the rest of your app's specific main process // code. You can also put them in separate files and require them here. |
6.打包命令:
1 | electron-packager . helloWorld --platform=win32 --arch=x64 --icon=computer.ico -- out =./ out --asar --app-version=1.0.0 --overwrite --ignore=node_modules --electron-version 8.2.1 |
参数说明:
HelloWorld :你将要生成的exe文件的名称
–platform=win32:确定了你要构建哪个平台的应用,可取的值有 darwin, linux, mas, win32
–arch=x64:决定了使用 x86 还是 x64 还是两个架构都用
–icon=自定义.ico:自定义设置应用图标
–out=./out:指定打包文件输出的文件夹位置,当前指定的为项目目录下的out文件夹
–asar:该参数可以不加,如果加上,打包之后应用的源码会以.asar格式存在
–app-version=1.0.0:生成应用的版本号
–overwrite:覆盖原有的build,让新生成的包覆盖原来的包
–ignore=node_modules:如果加上该参数,项目里node_modules模块不会被打包进去
–electron-version 8.2.1:指定当前要构建的electron的电子版本(不带"v"),需要和当前的版本一致,具体可以在 package.json文件中查看,可以不加该参数,如果不一致,会自动下载。
打包过慢使 使用淘宝映射:
更换阿里镜像源
npm config set ELECTRON_MIRROR http://npm.taobao.org/mirrors/electron/ electron-packager . appName --platform=win32 --electron-version=10.0.0 --arch=x64 --download.mirrorOptions.mirror=https://npm.taobao.org/mirrors/electron/ --asar --app-version=0.0.0 --build-version=0.0.0 --out=outName --overwrite --no-package-manager --ignore="(.git)" --icon=logo.ico
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂