Electron:Win环境下-Vue项目打包

2023-05-29  本文已影响0人  CodeMT

准备工作

注意:Electron不能跨架构跨平台打包,windows下只能打包windows包,如需要打包,MacOS、Linux等需要去对应平台

建议先设置国内镜像

npm config edit

执行后会弹出npm的配置文档,将以下类容复制到文件末尾(格式跟文档中保持一致)

electron_mirror=https://npm.taobao.org/mirrors/electron/
    
electron-builder-binaries_mirror=https://npm.taobao.org/mirrors/electron-builder-binaries/

一、安装依赖

项目根目录下安装如下依赖包

npm install electron -g  // 全局安装Electron,加g是全局安装,自动添加到环境变量中
vue add electron-builder  // 安装Electron-builder打包工具  

可使用输入electron命令验证

如果出现安装出错?

二、electron配置文件

2.1 新建electron文件夹

在项目根目录下新建一个electron文件夹,与package.json平级

2.2 新建main.js文件

electron文件夹下新建一个main.js文件,并复制以下内容

// Modules to control application life and create native browser window
const { app, BrowserWindow, Menu } = require('electron');
const path = require('path');
const mode = process.env.NODE_ENV;

// 隐藏electron创听的菜单栏
Menu.setApplicationMenu(null);

function createWindow() {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800, // 窗口打开的宽度
    height: 600,  // 窗口打开的高度
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
      //  渲染进程 开启node模块,使得JS中可以使用node的model
      nodeIntegration: true,
    }
  });

  // and load the index.html of the app.
  mainWindow.loadURL(
    mode === 'development' ? 'http://localhost:2103' : `file://${path.join(__dirname, '../dist/index.html')}`
  );

  // Open the DevTools.
  if (mode === 'development') {
    mainWindow.webContents.openDevTools();
  }
}

// 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.whenReady().then(() => {
  createWindow();

  app.on('activate', function () {
    // 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 (BrowserWindow.getAllWindows().length === 0) createWindow();
  });
});

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') app.quit()
  BrowserWindow.addDevToolsExtension('node_modules/vue-devtools/vender')
});

// 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.

2.3 新建preload.js文件

electron文件夹下新建一个preload.js文件,并复制以下内容

// All of the Node.js APIs are available in the preload process.
// It has the same sandbox as a Chrome extension.
window.addEventListener('DOMContentLoaded', () => {
  const replaceText = (selector, text) => {
    const element = document.getElementById(selector)
    if (element) element.innerText = text
  }

  for (const type of ['chrome', 'node', 'electron']) {
    replaceText(`${type}-version`, process.versions[type])
  }
})

备注

mainpreload文件来自Electron快速启动示例代码,可以直接将项目拉取下来查看

git clone https://github.com/electron/electron-quick-start

三、package.json配置

在package.json文件中添加一下内容

"main": "electron/main.js",
"scripts": {
  "electron:build": "npm run build && electron-builder"
},

注意:如果package.json中有"type": "module",字段则需要去掉,避免报错

四、打包完成

npm run electron:build 
// 构建打包客户端-会在根目录生成dist_electron文件夹,其中的XXX Setup XXX.exe就是安装包

五、其他配置

package.json文件中,有一个build属性,可以配置打包软件的参数

"build": {
  "appId": "xxxxxxxx",
  "copyright": "xxx",
  "productName": "xxx",
  "win": {
    "files":{
      "filter": ["!doc/*"]
    },
  "icon": "res/icon.ico"
  },
  "mac": {
    "files":{
      "filter": ["!doc/*"]
    },
  "icon": "res/icon.icns"
  }
},
上一篇 下一篇

猜你喜欢

热点阅读