新增 现在支持编译为Windows桌面端

This commit is contained in:
2025-10-05 05:45:34 +08:00
parent e30e5b4fe2
commit 29d4152e81
21 changed files with 6008 additions and 3453 deletions

56
electron/dev-runner.js Normal file
View File

@@ -0,0 +1,56 @@
const { spawn } = require('child_process');
const path = require('path');
const fs = require('fs');
// 先编译 TypeScript 文件
const tsc = spawn('npx', ['tsc', '-p', 'tsconfig.electron.json'], {
stdio: 'inherit',
shell: true
});
tsc.on('close', (code) => {
if (code === 0) {
// 编译成功后复制 preload.js 文件
const sourcePreload = path.join(__dirname, 'preload.js');
const destPreload = path.join(__dirname, 'dist', 'preload.js');
try {
fs.copyFileSync(sourcePreload, destPreload);
console.log('preload.js copied successfully');
} catch (err) {
console.error('Failed to copy preload.js:', err);
}
// 编译成功后运行 Electron 应用
const electron = require('electron');
const appPath = path.join(__dirname, 'dist', 'main.js');
// 启动 Vite 开发服务器
const vite = spawn('npm', ['run', 'dev'], {
stdio: 'inherit',
shell: true,
env: {
...process.env,
VITE_DEV_SERVER_URL: 'http://localhost:5173',
},
});
// 等待一段时间让 Vite 服务器启动,然后启动 Electron
setTimeout(() => {
const child = spawn(electron, [appPath], {
stdio: 'inherit',
env: {
...process.env,
VITE_DEV_SERVER_URL: 'http://localhost:5173',
},
});
child.on('close', (code) => {
process.exit(code || 0);
});
}, 5000); // 等待 5 秒钟让 Vite 服务器启动
} else {
console.error('TypeScript compilation failed');
process.exit(code || 1);
}
});

3
electron/index.js Normal file
View File

@@ -0,0 +1,3 @@
// Electron 入口文件
// 使用 require 而不是 import 来避免 ES 模块问题
require('./dist/main.js');

59
electron/main.js Normal file
View File

@@ -0,0 +1,59 @@
import { app, BrowserWindow } from 'electron';
import * as path from 'path';
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) {
app.quit();
}
const createWindow = () => {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 1200,
height: 800,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: false,
contextIsolation: true,
},
});
// and load the index.html of the app.
if (process.env.VITE_DEV_SERVER_URL) {
mainWindow.loadURL(process.env.VITE_DEV_SERVER_URL);
} else {
mainWindow.loadFile(path.join(__dirname, '../dist/index.html'));
}
// Open the DevTools.
if (process.env.VITE_DEV_SERVER_URL) {
mainWindow.webContents.openDevTools();
}
};
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
createWindow();
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and import them here.

93
electron/main.ts Normal file
View File

@@ -0,0 +1,93 @@
import { app, BrowserWindow, ipcMain } from 'electron';
import * as path from 'path';
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) {
app.quit();
}
let mainWindow: BrowserWindow | null = null;
const createWindow = () => {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
frame: false, // 隐藏默认的窗口框架
icon: path.join(__dirname, '../../build/icon.ico'), // 设置应用图标
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: false,
contextIsolation: true,
},
});
// and load the index.html of the app.
if (process.env.VITE_DEV_SERVER_URL) {
mainWindow.loadURL(process.env.VITE_DEV_SERVER_URL);
} else {
// 使用绝对路径从项目根目录的 dist 文件夹加载
const indexPath = path.join(__dirname, '../../dist/index.html');
console.log('Loading index.html from:', indexPath);
mainWindow.loadFile(indexPath);
}
// Open the DevTools.
if (process.env.VITE_DEV_SERVER_URL) {
mainWindow.webContents.openDevTools();
}
};
// IPC 处理程序
ipcMain.handle('window-minimize', () => {
if (mainWindow) {
mainWindow.minimize();
}
});
ipcMain.handle('window-maximize', () => {
if (mainWindow) {
if (mainWindow.isMaximized()) {
mainWindow.unmaximize();
} else {
mainWindow.maximize();
}
}
});
ipcMain.handle('window-close', () => {
if (mainWindow) {
mainWindow.close();
}
});
ipcMain.handle('window-is-maximized', () => {
return mainWindow ? mainWindow.isMaximized() : false;
});
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
createWindow();
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and import them here.

3
electron/package.json Normal file
View File

@@ -0,0 +1,3 @@
{
"type": "commonjs"
}

20
electron/preload.js Normal file
View File

@@ -0,0 +1,20 @@
const { contextBridge, ipcRenderer, remote } = require('electron');
// As an example, we expose a function to the renderer process
// that shows a dialog
contextBridge.exposeInMainWorld('electronAPI', {
showDialog: () => ipcRenderer.invoke('show-dialog'),
});
// Custom APIs for renderer
const api = {};
// 窗口控制 API
contextBridge.exposeInMainWorld('electron', {
minimize: () => ipcRenderer.invoke('window-minimize'),
maximize: () => ipcRenderer.invoke('window-maximize'),
close: () => ipcRenderer.invoke('window-close'),
isMaximized: () => ipcRenderer.invoke('window-is-maximized'),
});
module.exports = api;

52
electron/prod-runner.js Normal file
View File

@@ -0,0 +1,52 @@
const { spawn } = require('child_process');
const path = require('path');
const fs = require('fs');
// 先编译 TypeScript 文件
const tsc = spawn('npx', ['tsc', '-p', 'tsconfig.electron.json'], {
stdio: 'inherit',
shell: true
});
tsc.on('close', (code) => {
if (code === 0) {
// 编译成功后复制 preload.js 文件
const sourcePreload = path.join(__dirname, 'preload.js');
const destPreload = path.join(__dirname, 'dist', 'preload.js');
try {
fs.copyFileSync(sourcePreload, destPreload);
console.log('preload.js copied successfully');
} catch (err) {
console.error('Failed to copy preload.js:', err);
}
// 编译成功后运行 Electron 应用
const electron = require('electron');
const appPath = path.join(__dirname, 'dist', 'main.js');
// 先确保应用已构建
const build = spawn('npm', ['run', 'build'], {
stdio: 'inherit',
shell: true
});
build.on('close', (buildCode) => {
if (buildCode === 0) {
const child = spawn(electron, [appPath], {
stdio: 'inherit'
});
child.on('close', (code) => {
process.exit(code || 0);
});
} else {
console.error('Build failed');
process.exit(buildCode || 1);
}
});
} else {
console.error('TypeScript compilation failed');
process.exit(code || 1);
}
});