diff --git a/electron.vite.config.ts b/electron.vite.config.ts index 96d5c5b..65079c7 100644 --- a/electron.vite.config.ts +++ b/electron.vite.config.ts @@ -20,7 +20,15 @@ export default defineConfig({ plugins: [externalizeDepsPlugin()] }, preload: { - plugins: [externalizeDepsPlugin()] + plugins: [externalizeDepsPlugin()], + build: { + rollupOptions: { + output: { + format: 'cjs', + entryFileNames: '[name].cjs' + } + } + } }, renderer: { build: { diff --git a/package.json b/package.json index d846cc4..228f00b 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "name": "mihomo-party", "version": "1.9.0", "description": "Clash Party", + "type": "module", "main": "./out/main/index.js", "author": "mihomo-party-org", "homepage": "https://mihomo.party", diff --git a/src/main/resolve/floatingWindow.ts b/src/main/resolve/floatingWindow.ts index 2dfe915..aaa04d6 100644 --- a/src/main/resolve/floatingWindow.ts +++ b/src/main/resolve/floatingWindow.ts @@ -39,7 +39,7 @@ async function createFloatingWindow(): Promise { closable: safeMode, backgroundColor: safeMode ? '#ffffff' : useCompatMode ? '#f0f0f0' : '#00000000', webPreferences: { - preload: join(__dirname, '../preload/index.js'), + preload: join(__dirname, '../preload/index.cjs'), spellcheck: false, sandbox: false, nodeIntegration: false, diff --git a/src/main/window.ts b/src/main/window.ts index a38fafb..f46c50c 100644 --- a/src/main/window.ts +++ b/src/main/window.ts @@ -39,7 +39,7 @@ export async function createWindow(): Promise { autoHideMenuBar: true, ...(process.platform === 'linux' ? { icon: icon } : {}), webPreferences: { - preload: join(__dirname, '../preload/index.js'), + preload: join(__dirname, '../preload/index.cjs'), spellcheck: false, sandbox: false, devTools: true diff --git a/src/native/sysproxy/index.js b/src/native/sysproxy/index.js index a372a49..1db5052 100644 --- a/src/native/sysproxy/index.js +++ b/src/native/sysproxy/index.js @@ -44,11 +44,24 @@ function getBindingName() { } function getResourcesPath() { + // 开发环境:优先使用 process.cwd() + const cwd = process.cwd() + if (existsSync(join(cwd, 'extra', 'sidecar'))) { + return cwd + } // Electron 打包后的路径 - if (process.resourcesPath) { + if (process.resourcesPath && existsSync(join(process.resourcesPath, 'sidecar'))) { return process.resourcesPath } - // 开发环境:查找包含 extra/sidecar 的目录 + // 备选:使用 app.getAppPath() (Electron 特有) + try { + const { app } = require('electron') + const appPath = app.getAppPath() + if (existsSync(join(appPath, 'extra', 'sidecar'))) { + return appPath + } + } catch {} + // 备选:从 __dirname 向上查找 let currentDir = __dirname while (currentDir !== dirname(currentDir)) { if (existsSync(join(currentDir, 'extra', 'sidecar'))) { @@ -56,14 +69,13 @@ function getResourcesPath() { } currentDir = dirname(currentDir) } - return __dirname + return cwd } function loadBinding() { const bindingName = getBindingName() const resourcesPath = getResourcesPath() - // 可能的路径列表 const searchPaths = [ join(resourcesPath, 'sidecar', bindingName), join(resourcesPath, 'extra', 'sidecar', bindingName)