vite-electron-app 1.2.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/electron/main.ts +26 -20
- package/electron/preload.ts +14 -4
- package/package.json +1 -1
package/electron/main.ts
CHANGED
|
@@ -7,32 +7,36 @@ process.env.APP_ROOT = path.join(__dirname, '..')
|
|
|
7
7
|
const VITE_DEV_SERVER_URL = process.env['VITE_DEV_SERVER_URL']
|
|
8
8
|
const RENDERER_DIST = path.join(process.env.APP_ROOT, 'dist')
|
|
9
9
|
|
|
10
|
+
// 📁 Em dev aponta para /public, em produção para /dist
|
|
10
11
|
const VITE_PUBLIC = VITE_DEV_SERVER_URL
|
|
11
12
|
? path.join(process.env.APP_ROOT, 'public')
|
|
12
13
|
: RENDERER_DIST
|
|
13
14
|
|
|
14
15
|
process.env.VITE_PUBLIC = VITE_PUBLIC
|
|
15
16
|
|
|
16
|
-
let win: BrowserWindow | null
|
|
17
|
+
let win: BrowserWindow | null = null
|
|
17
18
|
|
|
18
19
|
function getIconPath(): string {
|
|
19
|
-
if (process.platform === 'win32') return path.join(VITE_PUBLIC, 'icon.ico')
|
|
20
|
-
if (process.platform === 'linux') return path.join(VITE_PUBLIC, 'icon.png')
|
|
21
|
-
return path.join(VITE_PUBLIC, 'icon.icns')
|
|
20
|
+
if (process.platform === 'win32') return path.join(VITE_PUBLIC, 'icon.ico')
|
|
21
|
+
if (process.platform === 'linux') return path.join(VITE_PUBLIC, 'icon.png')
|
|
22
|
+
return path.join(VITE_PUBLIC, 'icon.icns')
|
|
22
23
|
}
|
|
23
24
|
|
|
24
|
-
function createWindow() {
|
|
25
|
+
function createWindow(): void {
|
|
25
26
|
win = new BrowserWindow({
|
|
26
27
|
icon: getIconPath(),
|
|
27
28
|
webPreferences: {
|
|
28
|
-
preload: path.join(__dirname, 'preload.mjs'),
|
|
29
|
-
contextIsolation: true, // Isola
|
|
30
|
-
nodeIntegration: false, //
|
|
31
|
-
sandbox: true, //
|
|
29
|
+
preload: path.join(__dirname, 'preload.mjs'),
|
|
30
|
+
contextIsolation: true, // 🔒 Isola o contexto do renderer
|
|
31
|
+
nodeIntegration: false, // 🔒 Desabilita Node.js no renderer — use o preload
|
|
32
|
+
sandbox: true, // 🔒 Sandboxing adicional recomendado pelo Electron
|
|
32
33
|
},
|
|
33
34
|
})
|
|
34
35
|
|
|
35
|
-
//
|
|
36
|
+
// Libera a referência ao fechar (evita memory leak)
|
|
37
|
+
win.on('closed', () => { win = null })
|
|
38
|
+
|
|
39
|
+
// Em dev carrega o servidor Vite (HMR), em produção serve o build
|
|
36
40
|
if (VITE_DEV_SERVER_URL) {
|
|
37
41
|
win.loadURL(VITE_DEV_SERVER_URL).catch(console.error)
|
|
38
42
|
} else {
|
|
@@ -40,18 +44,20 @@ function createWindow() {
|
|
|
40
44
|
}
|
|
41
45
|
}
|
|
42
46
|
|
|
43
|
-
// No macOS o app permanece ativo mesmo sem janelas abertas
|
|
47
|
+
// No macOS o app permanece ativo mesmo sem janelas abertas
|
|
44
48
|
app.on('window-all-closed', () => {
|
|
45
|
-
if (process.platform !== 'darwin')
|
|
46
|
-
app.quit()
|
|
47
|
-
win = null
|
|
48
|
-
}
|
|
49
|
+
if (process.platform !== 'darwin') app.quit()
|
|
49
50
|
})
|
|
50
|
-
|
|
51
|
+
|
|
52
|
+
// No macOS, recriar a janela ao clicar no ícone do dock
|
|
51
53
|
app.on('activate', () => {
|
|
52
|
-
if (
|
|
53
|
-
|
|
54
|
-
}
|
|
54
|
+
if (win === null) createWindow()
|
|
55
|
+
else win.show()
|
|
55
56
|
})
|
|
56
57
|
|
|
57
|
-
app.whenReady()
|
|
58
|
+
app.whenReady()
|
|
59
|
+
.then(createWindow)
|
|
60
|
+
.catch((err) => {
|
|
61
|
+
console.error('[main] Falha ao inicializar o app:', err)
|
|
62
|
+
app.quit()
|
|
63
|
+
})
|
package/electron/preload.ts
CHANGED
|
@@ -1,11 +1,21 @@
|
|
|
1
1
|
import { contextBridge, ipcRenderer } from 'electron'
|
|
2
2
|
|
|
3
|
-
//
|
|
3
|
+
// ---------------------------------------------------------------------------
|
|
4
|
+
// 💡 EXEMPLO — sinta-se livre para apagar este bloco e criar o seu próprio
|
|
5
|
+
//
|
|
6
|
+
// O contextBridge é a forma segura de expor APIs do Electron ao renderer.
|
|
7
|
+
// Adicione aqui apenas o que o renderer realmente precisar.
|
|
8
|
+
// Docs: https://www.electronjs.org/docs/latest/api/context-bridge
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
4
10
|
contextBridge.exposeInMainWorld('ipcRenderer', {
|
|
5
11
|
on(...args: Parameters<typeof ipcRenderer.on>) {
|
|
6
12
|
const [channel, listener] = args
|
|
7
13
|
return ipcRenderer.on(channel, (event, ...params) => listener(event, ...params))
|
|
8
|
-
}
|
|
9
|
-
//
|
|
10
|
-
|
|
14
|
+
},
|
|
15
|
+
// 🧹 Remove o listener ao desmontar o componente (evita memory leak)
|
|
16
|
+
off(...args: Parameters<typeof ipcRenderer.off>) {
|
|
17
|
+
const [channel, listener] = args
|
|
18
|
+
return ipcRenderer.off(channel, listener)
|
|
19
|
+
},
|
|
11
20
|
})
|
|
21
|
+
// ---------------------------------------------------------------------------
|