Electron uses electron-packager to package linux-x64 package and linux-arm64 package to solve the problem of slow packaging

Use electron-packager to play linux-x64 package and linux-arm64 package to solve the problem of slow download zip packaging

In the process of using electron-packager to package, you need to download the zip file corresponding to the electron version for the first time. The download is very slow, and it may happen that you download the zip every time. The solution is to download the zip file in advance and modify
electron —The packager source code specifies the zip path to the file directory where you put the zip, to avoid the packager to download the zip file by itself. Achieve faster package completion

The first step, download the zip file

You need to download the zip file of the corresponding version of your electrn on the Internet, and you need to pay attention to download the corresponding SHASUMS256.txt file and put it in the same directory as the zip. Electron zip download address
In order to be consistent with the effect of the article and avoid some path problems, you can find the corresponding version of the zip file and download it to the project root directory at the same level as node_modules, and create a cache folder

insert image description here

The second step, install packager and configure

install packager

npm install electron-packager

Configure the package.json scripts command

"scripts": {
    
    
    "packager:win": "electron-packager ./ winApp --platform=win32 --arch=x64   --overwrite --no-prune --ignore=/node_modules",
    "packager:linux-x64": "electron-packager ./ linuxApp --platform=linux --arch=x64   --overwrite --no-prune --ignore=/node_modules",
    "packager:linux-arm64": "electron-packager ./ linuxApp --platform=linux --arch=arm64   --overwrite --no-prune --ignore=/node_modules"
  },

Modify the electron-packager source code

Find the electron-packager package in node_modules, locate node_modules\electron-packager\src\index.js, find the packageForPlatformAndArchWithOpts method and modify zipPath to get the code

downloadOpts.arch corresponds to --arch=arm64 in the scripts command, and the windows package is not processed here. The implementation process is consistent with the linux package, and the win judgment can be added below. The downloadOpts parameter can get the parameters in the script command

async packageForPlatformAndArchWithOpts (comboOpts, downloadOpts) {
    
    
    // const zipPath = await this.getElectronZipPath(downloadOpts)  ---
    const arch = downloadOpts.arch // +++
    const zipPath = arch === 'arm64' ? './cache/electron-v22.0.0-linux-arm64.zip' : './cache/electron-v22.0.0-linux-x64.zip' // +++

    if (!this.useTempDir) {
    
    
      return this.createApp(comboOpts, zipPath)
    }

    if (common.isPlatformMac(comboOpts.platform)) {
    
    
      /* istanbul ignore else */
      if (this.canCreateSymlinks === undefined) {
    
    
        return this.testSymlink(comboOpts, zipPath)
      } else if (!this.canCreateSymlinks) {
    
    
        return this.skipHostPlatformSansSymlinkSupport(comboOpts)
      }
    }

    return this.checkOverwrite(comboOpts, zipPath)
}

The third step, packing

linux-x64 package

npm run packager:linux-x64

linux-arm64 package

npm run packager:linux-arm64

After the packaging is successful, electron-packager will merge the cache download file into the resources\app file under the package file
insert image description here

Guess you like

Origin blog.csdn.net/weixin_42508580/article/details/130760495