Skip to content

Commit

Permalink
fix: 🐛 修复 electron 资源路径问题
Browse files Browse the repository at this point in the history
  • Loading branch information
viarotel committed Oct 17, 2023
1 parent 09c8dee commit 955756e
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 44 deletions.
8 changes: 5 additions & 3 deletions electron-builder.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -30,7 +33,6 @@
},
"win": {
"icon": "logo.ico",
"executableName": "escrcpy",
"target": [
{
"target": "nsis",
Expand Down
12 changes: 12 additions & 0 deletions electron/configs/index.js
Original file line number Diff line number Diff line change
@@ -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')
5 changes: 2 additions & 3 deletions electron/events/updater/index.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 3 additions & 1 deletion electron/exposes/adbkit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 1 addition & 2 deletions electron/exposes/scrcpy/index.js
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
11 changes: 11 additions & 0 deletions electron/helpers/index.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
9 changes: 5 additions & 4 deletions electron/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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'))
}

Expand Down
4 changes: 2 additions & 2 deletions jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
}
58 changes: 29 additions & 29 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
],
}),
)

0 comments on commit 955756e

Please sign in to comment.