Skip to content

Commit

Permalink
Merge pull request #31 from baizeteam/dev
Browse files Browse the repository at this point in the history
性能优化
  • Loading branch information
sulgweb authored Jun 19, 2024
2 parents 9972b64 + c5c8102 commit d1e4abd
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 111 deletions.
1 change: 1 addition & 0 deletions electron-builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,4 @@ npmRebuild: false
publish:
provider: generic
url: https://example.com/auto-updates
afterPack: "scripts/clean-locales.js"
8 changes: 7 additions & 1 deletion electron.vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ export default defineConfig({
"@preload": resolve("src/preload"),
},
},
build: {
minify: "esbuild",
},
},
preload: {
plugins: [externalizeDepsPlugin()],
build: {
minify: "esbuild",
},
},
renderer: {
resolve: {
Expand Down Expand Up @@ -57,7 +63,7 @@ export default defineConfig({
},
},
build: {
minify: "terser",
minify: "esbuild",
cssMinify: "esbuild",
rollupOptions: {
input: {
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "baize-toolbox",
"version": "0.0.1-beta3",
"version": "0.0.1-beta4",
"description": "An Electron application with React and TypeScript",
"main": "./out/main/index.js",
"author": "[email protected]",
Expand Down Expand Up @@ -64,8 +64,8 @@
"bufferutil": "^4.0.8",
"dayjs": "^1.11.10",
"easy-auto-launch": "^6.0.2",
"electron": "^29.1.6",
"electron-builder": "^24.9.1",
"electron": "^31.0.1",
"electron-builder": "^24.13.3",
"electron-vite": "^2.0.0",
"generic-names": "^4.0.0",
"husky": "^9.0.11",
Expand All @@ -82,7 +82,6 @@
"react-router-dom": "^6.21.2",
"rollup-plugin-copy": "^3.5.0",
"systeminformation": "^5.22.7",
"terser": "^5.30.0",
"typescript": "^5.3.3",
"utf-8-validate": "^6.0.3",
"vite": "5.0.12",
Expand Down
50 changes: 21 additions & 29 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions scripts/clean-locales.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const fs = require("fs")
const path = require("path")

module.exports = async function (context) {
const appOutDir = context.appOutDir
const localesDir = path.join(appOutDir, "locales")

const localesToKeep = [
"en-US.pak",
"zh-CN.pak",
// 其他需要保留的语言包
]

fs.readdir(localesDir, (err, files) => {
if (err) throw err

files.forEach((file) => {
if (!localesToKeep.includes(file)) {
fs.unlink(path.join(localesDir, file), (err) => {
if (err) throw err
// console.log(`Deleted locale: ${file}`)
})
}
})
})
}
66 changes: 3 additions & 63 deletions src/main/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,11 @@ import { InjectData } from "@main/utils/inject"
import { getSystemInfo } from "@main/utils/systemHelper"

let mainWindow: BrowserWindow
let loadingWin: BrowserWindow
export enum START_STATUS {
pending = "pending",
success = "success",
fail = "fail",
}
const mainWinStart = {
web: START_STATUS.pending,
ffmpeg: START_STATUS.pending,
}
const handler = {
set(target, p, value) {
Reflect.set(target, p, value)
onMainWinStartChange()
return true
},
}
export const mainWinStartProxy = new Proxy(mainWinStart, handler)
export function onMainWinStartChange() {
let tag = true
Object.keys(mainWinStart).forEach((key) => {
if (mainWinStart[key] !== START_STATUS.success) {
tag = false
}
})
if (tag) {
mainWindow.show()
loadingWin.hide()
}
}

export function createLoadingWin() {
loadingWin = new BrowserWindow({
width: 300,
height: 100,
frame: false, // 隐藏窗口边框
transparent: true, // 设置窗口背景透明
alwaysOnTop: true, // 置顶显示
hasShadow: false, // 隐藏窗口阴影
webPreferences: {
nodeIntegration: true,
webSecurity: false,
preload: join(__dirname, "../preload/index.js"),
},
})

initWinUrl(loadingWin, "/siteElectronLoading/index.html")
}

// 加载的url
export function initWinUrl(win: BrowserWindow, url: string) {
Expand Down Expand Up @@ -100,7 +57,7 @@ export async function createWin({ config, url, injectData, route }: ICreateWin):
return win
}

export async function createMainWin(): Promise<void> {
export async function createMainWin(): Promise<BrowserWindow> {
mainWindow = await createWin({
config: {
width: 1200,
Expand All @@ -117,26 +74,9 @@ export async function createMainWin(): Promise<void> {
mainWindow["customId"] = "main"
mainWindow.on("ready-to-show", () => {
showCustomMenu(mainWindow)
mainWindow.show()
})
mainWindow.webContents.setWindowOpenHandler((details) => {
shell.openExternal(details.url)
return { action: "deny" }
})
mainWindow.webContents.on("did-start-loading", () => {
if (!loadingWin) {
createLoadingWin()
}
if (mainWinStartProxy.web === START_STATUS.pending) {
loadingWin.show()
}
})
mainWindow.webContents.on("did-stop-loading", () => {
if (mainWinStart.web !== START_STATUS.success) {
setTimeout(() => {
mainWinStartProxy.web = START_STATUS.success
}, 200)
}
})
return mainWindow
// initWinUrl(mainWindow, "/siteMain/index.html");
}

Expand Down
19 changes: 15 additions & 4 deletions src/main/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
import { app, BrowserWindow } from "electron"
import { electronApp, optimizer } from "@electron-toolkit/utils"
import "@main/plugin"
import "@main/plugin/mainModule"
import { createMainWin } from "@main/helper"

const addSubModel = () => {
setTimeout(() => {
import("@main/plugin/subModule")
}, 500)
}

app.whenReady().then(async () => {
electronApp.setAppUserModelId("com.electron")
app.on("browser-window-created", (_, window) => {
optimizer.watchWindowShortcuts(window)
})
createMainWin()
app.on("activate", function () {

createMainWin().then((win) => {
win.on("show", addSubModel)
})
app.on("activate", async function () {
if (BrowserWindow.getAllWindows().length === 0) {
createMainWin()
createMainWin().then((win) => {
addSubModel()
})
}
})
})
Expand Down
5 changes: 5 additions & 0 deletions src/main/plugin/mainModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import "./modules/baseConfig"
import "./modules/MenuManger"
import "./modules/ipcWin"
import "./modules/store"
import "./modules/autoLuanch"
Loading

0 comments on commit d1e4abd

Please sign in to comment.