找个喜欢的目录,执行以下命令,创建vue项目:
(这里把项目名称定为electron-vue-demo)
vue create electron-vue-demo
会出现以下选项(如果熟悉此步骤可跳过本节内容):
Vue CLI v3.8.4
? Please pick a preset: (Use arrow keys)
default (babel, eslint)
> Manually select features
选择“Manually select features” (自定义安装)。
? Check the features needed for your project: (Press <space> to select, <a> to t
oggle all, <i> to invert selection)
❯◉ Babel
◯ TypeScript
◯ Progressive Web App (PWA) Support
◉ Router
◉ Vuex
◉ CSS Pre-processors
◉ Linter / Formatter
◯ Unit Testing
◯ E2E Testing
这里选择了常用的模块,请根据实际需求进行选择。
? Use history mode for router? (Requires proper server setup for index fallback
in production) (Y/n) n
如果选择了router,这里会询问是否使用history模式。
vue-router 默认使用hash模式(即通过url#hash来跳转页面),使用URL的hash来模拟一个完整的 URL,当URL改变时,页面不会重新加载。
如果使用history,URL就像正常的url,例如http://yoursite.com/user/id
,比较好看。但是还需要后台配置支持。
这里我们选择“n”。
? Pick a CSS pre-processor (PostCSS, Autoprefixer and CSS Modules are supported
by default): (Use arrow keys)
Sass/SCSS (with dart-sass)
Sass/SCSS (with node-sass)
Less
❯ Stylus
选择CSS预处理模块,这里我们使用Stylus
。
? Pick a linter / formatter config: (Use arrow keys)
ESLint with error prevention only
ESLint + Airbnb config
❯ ESLint + Standard config
ESLint + Prettier
选择ESLint代码格式检查工具的配置,选择“ESLint + Standard config”,标准配置。
? Pick additional lint features: (Press <space> to select, <a> to toggle all, <i
> to invert selection)
❯◉ Lint on save
◯ Lint and fix on commit
Line on save
表示在保存代码的时候,进行格式检查。
Lint and fix on commit
表示在git commit的时候自动纠正格式。
这里只选择Lint on save
? Where do you prefer placing config for Babel, PostCSS, ESLint, etc.?
In dedicated config files
❯ In package.json
这里问把 babel, postcss, eslint 这些配置文件放哪?
In dedicated config files
表示独立文件
In package.json
表示放在package.json里
这里选择In package.json
。
? Save this as a preset for future projects? (y/N) N
是否为以后的项目保留这些设置?选择“N”。
然后耐心等待项目安装完成。
由于源在国外,安装会特别慢,可以在项目根目录新建文件.npmrc
,写入如下源
electron_mirror = https://registry.npmmirror.com/-/binary/electron/
进入项目根目录,安装
vue add electron-builder
安装过程选择electron版本,选最高的就行
项目根目录下执行命令
npm run electron:serve
启动会有点慢,需要在src/background.js文件中,注释掉如下代码
// 去掉安装 VUEJS3_DEVTOOLS
// if (isDevelopment && !process.env.IS_TEST) {
// // Install Vue Devtools
// try {
// await installExtension(VUEJS3_DEVTOOLS)
// } catch (e) {
// console.error('Vue Devtools failed to install:', e.toString())
// }
// }
在项目根目录下创建vue.config.js,粘贴以下代码:
const path = require('path')
function resolve (dir) {
return path.join(__dirname, dir);
}
module.exports = {
publicPath: './',
devServer: {
// can be overwritten by process.env.HOST
host: '0.0.0.0',
port: 8080
},
chainWebpack: config => {
config.resolve.alias
.set('@', resolve('src'))
.set('src', resolve('src'))
.set('common', resolve('src/common'))
.set('components', resolve('src/components'));
}
};
devServer 用于设置开发环境的服务,这里表示在本地8080端口启动web服务。
chainWebpack我们给项目目录起了“别名(alias)”,在代码中,我们可以直接用“别名”访问资源,省去了每次输入完整相对路径的麻烦。
function createWindow () {
// Create the browser window.
win = new BrowserWindow({
width: 1200,
height: 620,
webPreferences: {
//取消跨域限制
webSecurity: false,
nodeIntegration: true
}
})
windows: app.ico 最小尺寸:256x256
macOS: app.png或app.icns 最小尺寸:512x512
将图标放在public文件夹下,添加图标,${__static}
代表public文件夹
function createWindow () {
// Create the browser window.
win = new BrowserWindow({
width: 1200,
height: 620,
webPreferences: {
nodeIntegration: true
},
icon: `${__static}/app.ico`
})
}
修改public/index.html
文件的tittle
就可以了
根目录创建electron-build.yml
,写入如下内容
# 唯一的应用程序标识符,用于操作系统级别的识别
appId: top.ygang.dbee
# 应用程序的名称,显示在用户界面上
productName: DBee
# 版权声明,通常包含版权年份和名称
copyright: Copyright © 2024
# 定义构建资源目录,放置图标、证书等资源文件
directories:
buildResources: assets # 构建过程中使用的资源文件夹路径
output: dist_electron # 指定构建输出的目录
# 启用 ASAR 打包,将应用文件打包成单个压缩文件以提高安全性
asar: true
nsis:
oneClick: false # 是否一键安装,如果为 false,则显示安装向导
allowElevation: true # 是否允许请求提升(以管理员身份运行)
allowToChangeInstallationDirectory: true # 是否允许用户更改安装目录
createDesktopShortcut: true # 是否在桌面上创建快捷方式
createStartMenuShortcut: true # 是否在开始菜单中创建快捷方式
shortcutName: ${productName} # 快捷方式的名称
uninstallDisplayName: ${productName}-UnInstall # 卸载时显示的名称
installerIcon: public/favicon.ico # 安装程序图标的路径
uninstallerIcon: public/favicon.ico # 卸载程序图标的路径
installerHeaderIcon: public/favicon.ico # 安装向导头部的图标路径
runAfterFinish: true # 安装完成后是否运行应用
perMachine: true # 是否为所有用户安装(而非仅当前用户)
artifactName: ${productName}-${version}-Setup.${ext} # 自定义输出文件的名称
win:
# must be at least 256x256
icon: public/favicon.ico
target:
- target: nsis # 使用nsis打成安装包,或者portable打包成免安装版
arch:
- ia32
- x64
legalTrademarks: www.ygang.top
mac:
# must be at least 512x512
icon: public/favicon.ico
linux:
icon: public/favicon.ico
切记!!!项目不要放在中文路径上,不然打包会报错
执行命令进行打包
npm run electron:build
注意:在mac上使用electron-builder打包的时候需要依赖 Python2,如果没有配置环境变量则需要使用如下命令临时创建环境变量
export PYTHON_PATH=/Library/Frameworks/Python.framework/Versions/2.7/bin/python
npm run build
在上面配置中buildResources
的目录下创建license.txt
文件即可,如果displayLanguageSelector
为true
的话,创建license_en.txt
和license_zh_CN.txt
,使用字符集编码带有BOM的UTF-8
兼容win、mac