electron下载依赖慢解决办法

搭建一个梯子

在安装的过程中可能会安装失败或者下载时间过长

配置yarn使用梯子下载

$ yarn config set proxy http://127.0.0.1:1080
 

安装之后如果不想使用梯子的话可以删除

 $ yarn config delete proxy

创建目录并安装

$ mkdir electron-demo
$ cd electron-demo
$ yarn init
$ yarn add electron --dev
electron-demo/
├── package.json
├── main.js
└── index.html

package.json中修改scripts

 "main": "main.js",
  "scripts": {
    "start": "electron ."
  }

Hello World

main.js

const { app, BrowserWindow } = require('electron')

function createWindow () {   
 // 创建浏览器窗口
 let win = new BrowserWindow({
   width: 800,
   height: 600,
   webPreferences: {
     nodeIntegration: true
   }
 })

 // 加载index.html文件
 win.loadFile('index.html')
}

app.on('ready', createWindow)

index.html

<html>
 <head>
   <meta charset="UTF-8">
   <title>Hello World!</title>
 </head>
 <body>
   <h1>Hello World!</h1>
   We are using node <script>document.write(process.versions.node)</script>,
   Chrome <script>document.write(process.versions.chrome)</script>,
   and Electron <script>document.write(process.versions.electron)</script>.
 </body>
</html>

# Start

yarn start

# Build

安装electron-builder

$ yarn add electron-builder --dev

package.json

"build": {
  "appId": "your.id",
  "mac": {
    "category": "your.app.category.type"
  }
},
"scripts": {
  "pack": "electron-builder --dir",
  "dist": "electron-builder"
}

run

$ yarn dist

打包过程中也需要配置梯子,因为终端默认是不走梯子的

下载Proxifier让终端可以使用梯子

增加以上规则即可

阅读 171

推荐阅读