1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
| yarn add --dev electron concurrently wait-on cross-env electron-builder
mkdir -p electron
cat << EOF > electron/main.cjs
const { app, BrowserWindow } = require('electron')
const path = require('path')
const NODE_ENV = process.env.NODE_ENV
function createWindow() {
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
show: false,
autoHideMenuBar: true,
webPreferences: {
preload: path.join(__dirname, 'preload.cjs')
}
})
mainWindow.once('ready-to-show', () => {
mainWindow.show()
})
mainWindow.loadURL(
NODE_ENV === 'development'
? 'http://localhost:5173'
: \`file://\${path.join(__dirname, '../dist/index.html')}\`
);
if (NODE_ENV === "development") {
mainWindow.webContents.openDevTools()
}
}
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
EOF
cat << EOF > electron/preload.cjs
window.addEventListener('DOMContentLoaded', () => {
const replaceText = (selector, text) => {
const element = document.getElementById(selector)
if (element) element.innerText = text
}
for (const dependency of ['chrome', 'node', 'electron']) {
replaceText(\`\${dependency}-version\`, process.versions[dependency])
}
})
EOF
sed -i '/export default/a \ \ \ \ base: "./",' vite.config.cjs
sed -i '/version/a "main": "electron/main.cjs",' package.json
sed -i '/"build/a "electron": "wait-on tcp:5173 && cross-env NODE_ENV=development electron .",' package.json
sed -i '/"build":/a "electron:serve": "concurrently -k \\\"yarn dev\\\" \\\"yarn electron\\\"\",' package.json
sed -i '/"build":/a "electron:build": "vite build && electron-builder",' package.json
sed -i '$d' package.json && sed -i '$d' package.json && cat << EOF >> package.json
},
"build": {
"appId": "com.my-website.my-app",
"productName": "MyApp",
"copyright": "Copyright © 2022 Yuan",
"mac": {
"category": "public.app-category.utilities"
},
"nsis": {
"oneClick": false,
"allowToChangeInstallationDirectory": true
},
"files": [
"dist/**/*",
"electron/**/*"
],
"directories": {
"buildResources": "assets",
"output": "dist_electron"
}
}
}
EOF
yarn electron:serve
|