1- import { app , BrowserWindow } from 'electron'
1+ // Modules to control application life and create native browser window
2+
3+ import { app , net , protocol , BrowserWindow } from 'electron'
24import path from 'path'
3- import { fileURLToPath } from 'node: url'
5+ import { fileURLToPath } from 'url'
46import log from 'electron-log'
57import Store from 'electron-store'
8+ import fs from 'fs/promises'
69
710const store = new Store ( )
811
@@ -23,8 +26,14 @@ log.scope.labelPadding = 8
2326
2427log . info ( 'Hello from Electron 👋' )
2528
26- let devTools = store . get ( 'devTools' )
27- if ( ! ( devTools instanceof Boolean ) ) {
29+ // 创建一个新的日志记录器
30+ const loader = log . create ( { logId : 'loader' } )
31+ loader . transports . file . fileName = 'loader.log'
32+ loader . scope . defaultLabel = 'loader'
33+ loader . scope . labelPadding = 8
34+
35+ const devTools = store . get ( 'devTools' )
36+ if ( typeof devTools !== 'boolean' ) {
2837 store . set ( 'devTools' , process . env . NODE_ENV === 'development' )
2938}
3039
@@ -38,7 +47,35 @@ if (!(devTools instanceof Boolean)) {
3847// Linux 安装 *.snap:~/snap/项目名/x1/.config/项目名/config.json
3948log . info ( 'electron-store path' , store . path )
4049
50+ // 协议名称,自定义
51+ const scheme = 'vvt'
52+
53+ // 新增协议注册函数
54+ const registerProtocol = ( ) => {
55+ protocol . handle ( scheme , async ( request ) => {
56+ const url = request . url
57+ const parsedUrl = url . substring ( scheme . length + 3 ) . replace ( / ^ ( \. \. ( \/ | \\ | $ ) ) + / , '' )
58+ const requestedPath = path . join ( __dirname , parsedUrl )
59+ const normalizedPath = path . normalize ( requestedPath )
60+
61+ try {
62+ await fs . access ( normalizedPath , fs . constants . R_OK )
63+ loader . info (
64+ `url: ${ url } , parsedUrl: ${ parsedUrl } , requestedPath: ${ requestedPath } , normalizedPath: ${ normalizedPath } ` ,
65+ )
66+ return net . fetch ( `file://${ normalizedPath } ` )
67+ } catch {
68+ const fallbackPath = path . join ( __dirname , 'dist' , 'index.html' )
69+ loader . warn (
70+ `url: ${ url } , parsedUrl: ${ parsedUrl } , requestedPath: ${ requestedPath } , fallbackPath: ${ fallbackPath } ` ,
71+ )
72+ return net . fetch ( `file://${ fallbackPath } ` )
73+ }
74+ } )
75+ }
76+
4177const createWindow = ( ) => {
78+ // Create the browser window.
4279 const mainWindow = new BrowserWindow ( {
4380 width : 800 ,
4481 height : 600 ,
@@ -48,23 +85,49 @@ const createWindow = () => {
4885 } ,
4986 } )
5087
51- mainWindow . loadURL ( process . env . VITE_SERVER_URL )
88+ if ( process . env . VITE_SERVER_URL ) {
89+ mainWindow . loadURL ( process . env . VITE_SERVER_URL ) . catch ( ( err ) => {
90+ log . error ( 'mainWindow.loadURL' , process . env . VITE_SERVER_URL , err )
91+ } )
5292
53- mainWindow . webContents . openDevTools ( )
93+ // Open the DevTools.
94+ mainWindow . webContents . openDevTools ( )
95+ } else {
96+ // and load the index.html of the app.
97+ mainWindow . loadURL ( `${ scheme } ://dist/index.html` ) . catch ( ( err ) => {
98+ log . error ( 'mainWindow.loadURL' , err )
99+ } )
100+ }
54101}
55102
103+ // This method will be called when Electron has finished
104+ // initialization and is ready to create browser windows.
105+ // Some APIs can only be used after this event occurs.
56106app . whenReady ( ) . then ( ( ) => {
107+ // 只在生产环境注册协议
108+ if ( ! process . env . VITE_SERVER_URL ) {
109+ registerProtocol ( )
110+ }
111+
57112 createWindow ( )
58113
59- app . on ( 'activate' , ( ) => {
114+ app . on ( 'activate' , function ( ) {
115+ // On macOS it's common to re-create a window in the app when the
116+ // dock icon is clicked and there are no other windows open.
60117 if ( BrowserWindow . getAllWindows ( ) . length === 0 ) {
61118 createWindow ( )
62119 }
63120 } )
64121} )
65122
123+ // Quit when all windows are closed, except on macOS. There, it's common
124+ // for applications and their menu bar to stay active until the user quits
125+ // explicitly with Cmd + Q.
66126app . on ( 'window-all-closed' , ( ) => {
67127 if ( process . platform !== 'darwin' ) {
68128 app . quit ( )
69129 }
70130} )
131+
132+ // In this file you can include the rest of your app's specific main process
133+ // code. You can also put them in separate files and require them here.
0 commit comments