electron打包在官网给了三种方式,分别是electron-packager、electron-builder、electron-forge

1. electron-packager主要打包成可执行文件

2. electron-builder主要是是打包成安装文件(推荐这种方式比较友好)

3. electron-forge笔者用的较少

一、首先先上一个官方文档地址

    1. electron 文档 http://electronjs.org/docs/tutorial/application-packaging

     2. electron-builder文档 https://www.electron.build/configuration/configuration

二、流程介绍

    1. electron-vue的好处,让我们可以使用webpack来开发,webpack加vue开发是多么舒服,大家都是很清楚的。

     2. electron-builder本身是把electron-vue打包的后文件再次编译。所以会存在两次构建过程。这时候,对一些路径的问题,要着重注意,很多时候在开发环境中运行OK,生成环境bug的一般都是二次编译的后的路径不一致问题

三、配置文件解释

   1. electron-builder编译通过指定(package.json / yml-or-json5-or-toml-or-js)来指定编译的参数和过程

   2. 在这里我主要讲一下几个核心的配置

     最基本的必须要指定的参数,productName(产品名称),appId(产品ID),directories(输出目录),files(编译的文件),打包的系统win or mac linux等等

  3. Hooks

     1)afterPack    要在pack之后运行的函数(或文件或模块id的路径)(但在打包成可分发格式和符号之前)。

    (context: AfterPackContext): Promise<any> | any 

afterPack: async (context) => {
  // your code
}

  在打包之后执行的脚本,通过配置这个脚本,你可以很灵活执行一些配置,package.json只需要指定如下即可:

"build": {
  "afterPack": "./myAfterPackHook.js"
}

 2) afterAllArtifactBuild

  构建完所有工件后要运行的函数(或文件或模块id的路径)

  同afterPack配置类似,这个主要的作用是安装完成之后执行一些列操作(无限的想象)

(buildResult: BuildResult): Promise<Array<string>> | Array<string>
exports.default = function () {
  // you can return additional files to publish
  return ["/path/to/additional/result/file"]
}

3)onNodeModuleFile

  要在每个节点模块文件上运行的函数(或文件或模块id的路径)。

  配置和前两项的配置类似

(file: string) => void

4)AfterPackContext

 

interface AfterPackContext {
  outDir: string
  appOutDir: string
  packager: PlatformPackager<any>
  electronPlatformName: string
  arch: Arch
  targets: Array<Target>
}

5) BuildResult

interface BuildResult {
  outDir: string
  artifactPaths: Array<string>
  platformToTargets: Map<Platform, Map<string, Target>>
  configuration: Configuration
}

4. Artifact File Name Template(宏)

[ 
  //匹配所有文件
  “** / *” ,

  //除了foo /目录中的js文件
  “!foo / * .js”之外,

  //除非它是foo / bar.js 
  “foo / bar.js” ,
]

5. asar 

  electron 工作原理是通过一系列的dll来运行JavaScript和html代码动态生成各平台程序,可以理解为模拟器。

 1)  在编译的时候如果我们将asar设为true的时候,那我们的js和html会被编译到到程序安装完成后resources下面的asar文件,相当于一个压缩文件,里面的代码用户是无法直接看到的。

2) 在编译的时候如果我们将asar设为false的时候,那我们的js和html也是会被编译到到程序安装完成后resources下面,但打开之后,可以直接在里面看到我们的所有代码。所以一般情况下,建议设为true

四、总结

  关于electron-builder其他配置,如安装时候的指定路径和logo,这些其实也要注意下这些相对比较简单一些,后面我会详细介绍这些配置!

五、package.json配置实例

{
  "name": "sdpwclient",
  "version": "0.0.1",
  "author": "Falways <1914783590@qq.com>",
  "description": "An electron-vue project",
  "license": null,
  "main": "./dist/electron/main.js",
  "scripts": {
    "build": "node .electron-vue/build.js && electron-builder --platform=win32  --arch=x64",
    "win32": "node .electron-vue/build.js && electron-builder --platform=win32  --arch=x64",
    "mac": "node .electron-vue/build.js && electron-builder --platform=drwin ",
    "build:dir": "node .electron-vue/build.js && electron-builder --dir",
    "build:clean": "cross-env BUILD_TARGET=clean node .electron-vue/build.js",
    "build:web": "cross-env BUILD_TARGET=web node .electron-vue/build.js",
    "dev": "node .electron-vue/dev-runner.js",
    "e2e": "npm run pack && mocha test/e2e",
    "lint": "eslint --ext .js,.vue -f ./node_modules/eslint-friendly-formatter src test",
    "lint:fix": "eslint --ext .js,.vue -f ./node_modules/eslint-friendly-formatter --fix src test",
    "pack": "npm run pack:main && npm run pack:renderer",
    "pack:main": "cross-env NODE_ENV=production webpack --progress --colors --config .electron-vue/webpack.main.config.js",
    "pack:renderer": "cross-env NODE_ENV=production webpack --progress --colors --config .electron-vue/webpack.renderer.config.js",
    "test": "npm run unit && npm run e2e",
    "unit": "karma start test/unit/karma.conf.js",
    "postinstall": "npm run lint:fix",
    "generate":"electron-builder --platform=win32  --arch=x64"
  },
  "build": {
    "productName": "SDP",
    "appId": "com.example.yourapp",
    "directories": {
      "output": "build"
    },
    "files": [
      "dist/electron/**/*"
    ],
    "dmg": {
      "contents": [
        {
          "x": 410,
          "y": 150,
          "type": "link",
          "path": "/Applications"
        },
        {
          "x": 130,
          "y": 150,
          "type": "file"
        }
      ]
    },
    "mac": {
      "icon": "build/icons/icon.icns"
    },
    "win": {
      "asar": false,
      "icon": "build/icons/log1.png"
    },
    "linux": {
      "icon": "build/icons"
    }
  },
  "dependencies": {
    "axios": "^0.18.0",
    "element-ui": "^2.4.11",
    "iconv-lite": "^0.4.24",
    "ini": "^1.3.5",
    "vue": "^2.5.16",
    "vue-electron": "^1.0.6",
    "vue-router": "^3.0.1",
    "vuex": "^3.0.1",
    "vuex-electron": "^1.0.0"
  },
  "devDependencies": {
    "ajv": "^6.5.0",
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.4",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-preset-env": "^1.7.0",
    "babel-preset-stage-0": "^6.24.1",
    "babel-register": "^6.26.0",
    "babili-webpack-plugin": "^0.1.2",
    "cfonts": "^2.1.2",
    "chalk": "^2.4.1",
    "child_process": "^1.0.2",
    "copy-webpack-plugin": "^4.5.1",
    "cross-env": "^5.1.6",
    "css-loader": "^0.28.11",
    "del": "^3.0.0",
    "devtron": "^1.4.0",
    "electron": "^2.0.4",
    "electron-builder": "^20.19.2",
    "electron-debug": "^1.5.0",
    "electron-devtools-installer": "^2.2.4",
    "eslint": "^5.15.2",
    "file-loader": "^1.1.11",
    "fs": "0.0.1-security",
    "html-webpack-plugin": "^3.2.0",
    "less": "^3.9.0",
    "less-loader": "^4.1.0",
    "mini-css-extract-plugin": "0.4.0",
    "multispinner": "^0.2.1",
    "node-loader": "^0.6.0",
    "node-sass": "^4.9.2",
    "sass-loader": "^7.0.3",
    "style-loader": "^0.21.0",
    "url-loader": "^1.0.1",
    "vue-html-loader": "^1.2.4",
    "vue-loader": "^15.2.4",
    "vue-style-loader": "^4.1.0",
    "vue-template-compiler": "^2.5.16",
    "webpack": "^4.15.1",
    "webpack-cli": "^3.0.8",
    "webpack-dev-server": "^3.1.4",
    "webpack-hot-middleware": "^2.22.2",
    "webpack-merge": "^4.1.3"
  },
  "__npminstall_done": false
}

 

Logo

前往低代码交流专区

更多推荐