時(shí)間:2023-05-31 19:18:02 | 來(lái)源:網(wǎng)站運(yùn)營(yíng)
時(shí)間:2023-05-31 19:18:02 來(lái)源:網(wǎng)站運(yùn)營(yíng)
Electron+React+七牛云開發(fā)跨平臺(tái)云文檔:git clone git@github.com:FrontDream/cloud-doc.git注意
cd cloud-doc
npm install (切記在可以科學(xué)上網(wǎng)的情況下安裝,國(guó)內(nèi)即使用淘寶鏡像,雖然能運(yùn)行起來(lái),打包也會(huì)失敗)
npm run dev (運(yùn)行)
npm run dist (打包)
npm run release (發(fā)布)
access_key
,secret_key
, bucket
,才能同步到你自己的七牛云release
時(shí),需要先確定package.json
中的publish
平臺(tái),并在自己的package.json
中設(shè)置發(fā)布平臺(tái)的GH_TOKEN
npx create-react-app my-app
cnpm install electron --save-dev
main.js
,并且在package.json
中增加"main"入口: "main": "main.js",
cnpm install electron-is-dev
const { app ,BrowserWindow } = require('electron')const isDev = require('electron-is-dev')let mainWindow;app.on('ready',()=>{ mainWindow = new BrowserWindow({ width: 1024, height: 680, webPreferences: { nodeIntegration: true } }) const urlLocation = isDev?'http://localhost:3000': 'ddd' mainWindow.loadURL(urlLocation)})
npm install concurrently --save
package.json
為如下,但是因?yàn)檫@是同時(shí)運(yùn)行的,但是正常來(lái)說(shuō)是前一個(gè)命令運(yùn)行起來(lái),再運(yùn)行后一個(gè)命令"scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject", "ele": "electron .", "dev": "concurrently /"npm start/" /"npm run ele/"" }
cnpm install wait-on --save-dev
。并修改package.json
如下:"scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject", "ele": "electron .", "dev": "concurrently /"npm start/" /"wait-on http://localhost:3000 && electron ./"" },
cnpm install cross-env --save-dev
,并修改package.json
修改為如下: "scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject", "ele": "electron .", "dev": "concurrently /"cross-env BROWSER=none npm start/" /"wait-on http://localhost:3000 && electron ./"" },
electron-builder
: npm install electron-builder --save-devnpm run build
const urlLocation = isDev?'http://localhost:3000':
file://${path.join(__dirname, './build/index.html')}package.json
中添加基本配置,package.json
第一層添加如下代碼:"author": { "name": "qiandingwei", "email": "1370336125@qq.com"},"build": { "appId": "cloudDoc", "productName": "七牛云文檔", "copyright": "Copyright ? 2020 ${author}" },
script
中添加:"pack": "electron-builder --dir","prepack": "npm run build","dist": "electron-builder"
npm run pack
"directories": { "buildResources": "assets" },
"mac": { "category": "public.app-category.productivity", "artifactName": "${productName}-${version}-${arch}.${ext}" }, "dmg": { "background": "assets/appdmg.png", "icon": "assets/icon.icns", "iconSize": 100, "contents": [ { "x": 380, "y": 280, "type": "link", "path": "/Applications" }, { "x": 110, "y": 280, "type": "file" } ], "window": { "width": 500, "height": 500 } }, "win": { "target": [ "msi", "nsis" ], "icon": "assets/icon.ico", "artifactName": "${productName}-Web-Setup-${version}.${ext}", "publisherName": "Viking Zhang" }, "nsis": { "allowToChangeInstallationDirectory": true, "oneClick": false, "perMachine": false }
app.asar
是體積過(guò)大的主要罪魁禍?zhǔn)?,解壓后,發(fā)現(xiàn)其實(shí)就是package.json
中build
下files
中的文件內(nèi)容。npm run build
將react相關(guān)的代碼,也就是視圖層的代碼,進(jìn)行了打包到build
文件夾下,因此其實(shí)只需要將main.js中用到的包放在dependencies
中就行了,剩余的包,移動(dòng)到devDependencies
中。因?yàn)?code>electron-builder不會(huì)把devDependencies
中的包打包進(jìn)應(yīng)用程序electron
層。思路:通過(guò)新建webpack.config.js
將main.js
進(jìn)行打包,并配置,將main.js
打包進(jìn)入build
文件夾package.json
中配置release
的平臺(tái)為github
,即在build
中配置如何代碼 "publish": ["github"]
GitHub
中生成該項(xiàng)目所需要的access_key
,并替換如下代碼you_access_key
的對(duì)應(yīng)位置package.json
中配置release
命令并設(shè)置環(huán)境變量,如下:"release": "cross-env GH_TOKEN=you_access_key electron-builder", "prerelease": "npm run build && npm run buildMain"
npm run release
即可。npm install electron-updater --save-dev
并在main.js
中引入:const { autoUpdater} = require('electron-updater')autoUpdater.autoDownload = false autoUpdater.checkForUpdatesAndNotify() autoUpdater.on('error',(error)=>{ dialog.showErrorBox('Error',error===null?"un-known":error) }) autoUpdater.on('update-available',()=>{ dialog.showMessageBox({ type: 'info', title: '應(yīng)用有新的版本', message: '發(fā)現(xiàn)新應(yīng)用,是否現(xiàn)在更新?', buttons: ['是','否'], },(buttonIndex)=>{ if(buttonIndex===0){ autoUpdater.downloadUpdate() } }) }) autoUpdater.on('update-not-available',()=>{ dialog.showMessageBox({ type: 'info', title: '沒(méi)有新的版本', message: '當(dāng)前已經(jīng)是最新版本', }) })
?? 愛(ài)心三連擊關(guān)鍵詞:平臺(tái)
客戶&案例
營(yíng)銷資訊
關(guān)于我們
客戶&案例
營(yíng)銷資訊
關(guān)于我們
微信公眾號(hào)
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。