From 955756e114a485ec3595d39d5b45185a3970258f Mon Sep 17 00:00:00 2001 From: viarotel Date: Tue, 17 Oct 2023 15:27:08 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20=E4=BF=AE=E5=A4=8D=20elec?= =?UTF-8?q?tron=20=E8=B5=84=E6=BA=90=E8=B7=AF=E5=BE=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- electron-builder.json | 8 +++-- electron/configs/index.js | 12 +++++++ electron/events/updater/index.js | 5 ++- electron/exposes/adbkit/index.js | 4 ++- electron/exposes/scrcpy/index.js | 3 +- electron/helpers/index.js | 11 ++++++ electron/main.js | 9 ++--- jsconfig.json | 4 +-- vite.config.js | 58 ++++++++++++++++---------------- 9 files changed, 70 insertions(+), 44 deletions(-) create mode 100644 electron/configs/index.js diff --git a/electron-builder.json b/electron-builder.json index c734cd7d..606e0324 100644 --- a/electron-builder.json +++ b/electron-builder.json @@ -1,14 +1,17 @@ { "$schema": "https://raw.githubusercontent.com/electron-userland/electron-builder/master/packages/app-builder-lib/scheme.json", + "productName": "Escrcpy", "appId": "org.viarotel.escrcpy", "asar": true, - "productName": "Escrcpy", "directories": { "output": "dist-release", "buildResources": "electron/resources/build" }, "files": ["dist", "dist-electron"], - "extraResources": ["electron/resources/extra"], + "extraResources": { + "from": "electron/resources/extra", + "to": "extra" + }, "publish": { "provider": "github", "owner": "viarotel-org", @@ -30,7 +33,6 @@ }, "win": { "icon": "logo.ico", - "executableName": "escrcpy", "target": [ { "target": "nsis", diff --git a/electron/configs/index.js b/electron/configs/index.js new file mode 100644 index 00000000..9aab4b72 --- /dev/null +++ b/electron/configs/index.js @@ -0,0 +1,12 @@ +import { resolve } from 'node:path' + +import { buildResolve, extraResolve } from '@electron/helpers/index.js' + +export const devPublishPath = resolve('dev-publish.yml') + +export const logoPath = buildResolve('logo.png') +export const icoLogoPath = buildResolve('logo.ico') +export const icnsLogoPath = buildResolve('logo.icns') + +export const adbPath = extraResolve('core/adb.exe') +export const scrcpyPath = extraResolve('core/scrcpy.exe') diff --git a/electron/events/updater/index.js b/electron/events/updater/index.js index d64d72ad..04ec06f7 100644 --- a/electron/events/updater/index.js +++ b/electron/events/updater/index.js @@ -1,14 +1,13 @@ import { app, ipcMain } from 'electron' import { is } from '@electron-toolkit/utils' import { autoUpdater } from 'electron-updater' -import devPublishPath from '@root/dev-publish.yml?path' +import { devPublishPath } from '@electron/configs/index.js' export default (mainWindow) => { // dev-start, 这里是为了在本地做应用升级测试使用,正式环境请务必删除 // if (is.dev && process.env.ELECTRON_RENDERER_URL) { if (is.dev && process.env.VITE_DEV_SERVER_URL) { - const updateConfigPath = devPublishPath - autoUpdater.updateConfigPath = updateConfigPath + autoUpdater.updateConfigPath = devPublishPath Object.defineProperty(app, 'isPackaged', { get() { return true diff --git a/electron/exposes/adbkit/index.js b/electron/exposes/adbkit/index.js index 41780d71..f98a9118 100644 --- a/electron/exposes/adbkit/index.js +++ b/electron/exposes/adbkit/index.js @@ -4,7 +4,9 @@ import path from 'node:path' import fs from 'node:fs' import dayjs from 'dayjs' import { Adb } from '@devicefarmer/adbkit' -import adbPath from '@resources/extra/core/adb.exe?path' +import { adbPath } from '@electron/configs/index.js' + +console.log('adbPath', adbPath) const exec = util.promisify(child_process.exec) diff --git a/electron/exposes/scrcpy/index.js b/electron/exposes/scrcpy/index.js index 40a253b6..51528302 100644 --- a/electron/exposes/scrcpy/index.js +++ b/electron/exposes/scrcpy/index.js @@ -1,7 +1,6 @@ import util from 'node:util' import child_process from 'node:child_process' -import adbPath from '@resources/extra/core/adb.exe?path' -import scrcpyPath from '@resources/extra/core/scrcpy.exe?path' +import { adbPath, scrcpyPath } from '@electron/configs/index.js' const exec = util.promisify(child_process.exec) diff --git a/electron/helpers/index.js b/electron/helpers/index.js index 2f39b004..68e173e3 100644 --- a/electron/helpers/index.js +++ b/electron/helpers/index.js @@ -1,5 +1,16 @@ +import { resolve } from 'node:path' import { contextBridge } from 'electron' +export const isPackaged = !process.resourcesPath.includes('node_modules') + +export const extraResolve = value => + isPackaged + ? resolve(process.resourcesPath, `extra/${value}`) + : resolve(`electron/resources/extra/${value}`) + +export const buildResolve = value => + resolve(`electron/resources/build/${value}`) + export function exposeContext(key, value) { if (process.contextIsolated) { try { diff --git a/electron/main.js b/electron/main.js index 2a730128..641236b9 100644 --- a/electron/main.js +++ b/electron/main.js @@ -2,9 +2,11 @@ import path from 'node:path' import { BrowserWindow, app, shell } from 'electron' import { electronApp, optimizer } from '@electron-toolkit/utils' -import logoPath from '@resources/build/logo.png?path' -import icoLogoPath from '@resources/build/logo.ico?path' -import icnsLogoPath from '@resources/build/logo.icns?path' +import { + icnsLogoPath, + icoLogoPath, + logoPath, +} from '@electron/configs/index.js' import events from './events/index.js' @@ -70,7 +72,6 @@ function createWindow() { mainWindow.loadURL(VITE_DEV_SERVER_URL) } else { - // win.loadFile('dist/index.html') mainWindow.loadFile(path.join(process.env.DIST, 'index.html')) } diff --git a/jsconfig.json b/jsconfig.json index 5e9c0a5e..bc7e623e 100644 --- a/jsconfig.json +++ b/jsconfig.json @@ -4,9 +4,9 @@ "paths": { "@/*": ["src/*"], "@root/*": ["*"], - "@resources/*": ["electron/resources/*"] + "@electron/*": ["electron/*"] } }, "exclude": ["node_modules", "dist", "dist-electron", "dist-release"], "include": ["src/**/*.js", "src/**/*.jsx", "src/**/*.vue", "electron"] -} \ No newline at end of file +} diff --git a/vite.config.js b/vite.config.js index 213f2a3c..98f577b5 100644 --- a/vite.config.js +++ b/vite.config.js @@ -6,47 +6,47 @@ import useRenderer from 'vite-plugin-electron-renderer' import useVue from '@vitejs/plugin-vue' import useEslint from 'vite-plugin-eslint' import useUnoCSS from 'unocss/vite' -import usePath from '@viarotel-org/vite-plugin-path' const merge = config => mergeConfig( { resolve: { alias: { - '@root': resolve('./'), - '@resources': resolve('./electron/resources'), + '@root': resolve(), + '@electron': resolve('electron'), }, }, - plugins: [usePath()], + plugins: [], }, config, ) // https://vitejs.dev/config/ -export default defineConfig({ - assetsInclude: ['**/*.exe'], - resolve: { - alias: { - '@': resolve('./src'), - }, - }, - plugins: [ - useEslint(), - useUnoCSS(), - useVue(), - useElectron([ - { - entry: 'electron/main.js', - vite: merge({}), +export default merge( + defineConfig({ + resolve: { + alias: { + '@': resolve('src'), }, - { - entry: 'electron/preload.js', - onstart(args) { - args.reload() + }, + plugins: [ + useEslint(), + useUnoCSS(), + useVue(), + useElectron([ + { + entry: 'electron/main.js', + vite: merge({}), }, - vite: merge({}), - }, - ]), - useRenderer(), - ], -}) + { + entry: 'electron/preload.js', + onstart(args) { + args.reload() + }, + vite: merge({}), + }, + ]), + useRenderer(), + ], + }), +)