chore: add type module and fix preload script output format

This commit is contained in:
xmk23333 2026-01-15 19:22:53 +08:00
parent b071154263
commit 1d053fe636
5 changed files with 28 additions and 7 deletions

View File

@ -20,7 +20,15 @@ export default defineConfig({
plugins: [externalizeDepsPlugin()]
},
preload: {
plugins: [externalizeDepsPlugin()]
plugins: [externalizeDepsPlugin()],
build: {
rollupOptions: {
output: {
format: 'cjs',
entryFileNames: '[name].cjs'
}
}
}
},
renderer: {
build: {

View File

@ -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",

View File

@ -39,7 +39,7 @@ async function createFloatingWindow(): Promise<void> {
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,

View File

@ -39,7 +39,7 @@ export async function createWindow(): Promise<void> {
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

View File

@ -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)