const fs = require('fs'); const path = require('path'); console.log('=== Electron环境路由诊断工具 ==='); // 检查Electron主进程配置 function checkElectronMain() { console.log('\n1. 检查Electron主进程配置:'); try { const mainPath = path.join(__dirname, 'src/main/main.js'); if (fs.existsSync(mainPath)) { console.log(' ✅ 主进程文件存在'); const mainContent = fs.readFileSync(mainPath, 'utf8'); // 检查webPreferences配置 if (mainContent.includes('contextIsolation: true')) { console.log(' ✅ 上下文隔离已启用'); } else { console.log(' ⚠️ 上下文隔离未启用'); } // 检查preload配置 if (mainContent.includes('preload:')) { console.log(' ✅ Preload脚本已配置'); } else { console.log(' ❌ Preload脚本未配置'); } } else { console.log(' ❌ 主进程文件不存在'); } } catch (error) { console.error(' ❌ 检查主进程配置失败:', error); } } // 检查Preload脚本 function checkPreload() { console.log('\n2. 检查Preload脚本:'); try { const preloadPath = path.join(__dirname, 'src/main/preload.js'); if (fs.existsSync(preloadPath)) { console.log(' ✅ Preload脚本存在'); const preloadContent = fs.readFileSync(preloadPath, 'utf8'); // 检查API暴露 if (preloadContent.includes('contextBridge.exposeInMainWorld')) { console.log(' ✅ API已正确暴露到渲染进程'); } else { console.log(' ❌ API未正确暴露'); } } else { console.log(' ❌ Preload脚本不存在'); } } catch (error) { console.error(' ❌ 检查Preload脚本失败:', error); } } // 检查Webpack配置 function checkWebpack() { console.log('\n3. 检查Webpack配置:'); try { const webpackPath = path.join(__dirname, 'webpack.config.js'); if (fs.existsSync(webpackPath)) { console.log(' ✅ Webpack配置文件存在'); const webpackContent = fs.readFileSync(webpackPath, 'utf8'); // 检查target配置 if (webpackContent.includes('target: \'electron-renderer\'')) { console.log(' ✅ Electron渲染器目标已设置'); } else { console.log(' ⚠️ Electron渲染器目标未设置'); } // 检查publicPath配置 if (webpackContent.includes('publicPath: \'./\'')) { console.log(' ✅ 相对路径已正确配置'); } else { console.log(' ⚠️ 相对路径可能需要调整'); } } else { console.log(' ❌ Webpack配置文件不存在'); } } catch (error) { console.error(' ❌ 检查Webpack配置失败:', error); } } // 检查HTML模板 function checkHtmlTemplate() { console.log('\n4. 检查HTML模板:'); try { const htmlPath = path.join(__dirname, 'src/renderer/index.html'); if (fs.existsSync(htmlPath)) { console.log(' ✅ HTML模板文件存在'); const htmlContent = fs.readFileSync(htmlPath, 'utf8'); // 检查base标签 if (htmlContent.includes('')) { console.log(' ✅ Base标签已正确设置'); } else { console.log(' ⚠️ 可能需要添加标签'); } } else { console.log(' ❌ HTML模板文件不存在'); } } catch (error) { console.error(' ❌ 检查HTML模板失败:', error); } } // 检查React Router配置 function checkReactRouter() { console.log('\n5. 检查React Router配置:'); try { const appPath = path.join(__dirname, 'src/renderer/App.js'); if (fs.existsSync(appPath)) { console.log(' ✅ App.js文件存在'); const appContent = fs.readFileSync(appPath, 'utf8'); // 检查BrowserRouter if (appContent.includes('BrowserRouter') || appContent.includes('Router')) { console.log(' ✅ Router组件已引入'); } else { console.log(' ❌ Router组件未引入'); } // 检查路由配置 if (appContent.includes('Routes') && appContent.includes('Route')) { console.log(' ✅ Routes和Route组件已配置'); } else { console.log(' ❌ Routes和Route组件未配置'); } } else { console.log(' ❌ App.js文件不存在'); } } catch (error) { console.error(' ❌ 检查React Router配置失败:', error); } } // 运行所有诊断 function runAllDiagnostics() { checkElectronMain(); checkPreload(); checkWebpack(); checkHtmlTemplate(); checkReactRouter(); console.log('\n=== 诊断完成 ==='); console.log('如果发现❌错误,请根据提示进行修复。'); } // 执行诊断 runAllDiagnostics();