From f129465d28b7fb9fa0fc2643d803854b145c39e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8D=89=E9=9E=8B=E6=B2=A1=E5=8F=B7?= <308487730@qq.com> Date: Wed, 29 Jun 2022 08:35:32 +0800 Subject: [PATCH 1/6] `preload/splash.ts` -> `preload/index.ts` --- electron/preload/{splash.ts => index.ts} | 8 ++++++-- vite.config.ts | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) rename electron/preload/{splash.ts => index.ts} (95%) diff --git a/electron/preload/splash.ts b/electron/preload/index.ts similarity index 95% rename from electron/preload/splash.ts rename to electron/preload/index.ts index ea48210..0ee2dcc 100644 --- a/electron/preload/splash.ts +++ b/electron/preload/index.ts @@ -1,4 +1,3 @@ - function domReady(condition: DocumentReadyState[] = ['complete', 'interactive']) { return new Promise(resolve => { if (condition.includes(document.readyState)) { @@ -84,5 +83,10 @@ function useLoading() { // ---------------------------------------------------------------------- const { appendLoading, removeLoading } = useLoading() -window.removeLoading = removeLoading domReady().then(appendLoading) + +window.onmessage = ev => { + ev.data.payload === 'removeLoading' && removeLoading() +} + +setTimeout(removeLoading, 4999) diff --git a/vite.config.ts b/vite.config.ts index be4c762..d566654 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -23,7 +23,7 @@ export default defineConfig({ preload: { input: { // You can configure multiple preload here - splash: join(__dirname, 'electron/preload/splash.ts'), + index: join(__dirname, 'electron/preload/index.ts'), }, vite: { build: { From c861aaeb331869172a6245f06a29fad8125d6e94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8D=89=E9=9E=8B=E6=B2=A1=E5=8F=B7?= <308487730@qq.com> Date: Wed, 29 Jun 2022 08:36:04 +0800 Subject: [PATCH 2/6] chore: optimize code --- electron/main/index.ts | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/electron/main/index.ts b/electron/main/index.ts index 76cf34d..1b376f1 100644 --- a/electron/main/index.ts +++ b/electron/main/index.ts @@ -12,32 +12,42 @@ if (!app.requestSingleInstanceLock()) { app.quit() process.exit(0) } + process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true' +export const ROOT_PATH = { + // /dist + dist: join(__dirname, '../..'), + // /dist or /public + public: join(__dirname, app.isPackaged ? '../..' : '../../../public'), +} + let win: BrowserWindow | null = null // Here, you can also use other preload -const splash = join(__dirname, '../preload/splash.js') +const preload = join(__dirname, '../preload/index.js') // 🚧 Use ['ENV_NAME'] avoid vite:define plugin const url = `http://${process.env['VITE_DEV_SERVER_HOST']}:${process.env['VITE_DEV_SERVER_PORT']}` +const indexHtml = join(ROOT_PATH.dist, 'index.html') async function createWindow() { win = new BrowserWindow({ title: 'Main window', + icon: join(ROOT_PATH.public, 'favicon.ico'), webPreferences: { - preload: splash, + preload, nodeIntegration: true, contextIsolation: false, }, }) if (app.isPackaged) { - win.loadFile(join(__dirname, '../../index.html')) + win.loadFile(indexHtml) } else { win.loadURL(url) // win.webContents.openDevTools() } - // Test active push message to Renderer-process + // Test actively push message to the Electron-Renderer win.webContents.on('did-finish-load', () => { win?.webContents.send('main-process-message', new Date().toLocaleString()) }) @@ -74,17 +84,15 @@ app.on('activate', () => { }) // new window example arg: new windows url -ipcMain.handle("open-win", (event, arg) => { +ipcMain.handle('open-win', (event, arg) => { const childWindow = new BrowserWindow({ webPreferences: { - preload: splash, + preload, }, }) if (app.isPackaged) { - childWindow.loadFile(join(__dirname, `../renderer/index.html`), { - hash: `${arg}`, - }) + childWindow.loadFile(indexHtml, { hash: arg }) } else { childWindow.loadURL(`${url}/#${arg}`) // childWindow.webContents.openDevTools({ mode: "undocked", activate: true }) From e0b35bc4e83f08ebf6ef7699f7a969dc0abf3db9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8D=89=E9=9E=8B=E6=B2=A1=E5=8F=B7?= <308487730@qq.com> Date: Wed, 29 Jun 2022 08:36:21 +0800 Subject: [PATCH 3/6] remove `global.d.ts` --- src/global.d.ts | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 src/global.d.ts diff --git a/src/global.d.ts b/src/global.d.ts deleted file mode 100644 index 66f1d08..0000000 --- a/src/global.d.ts +++ /dev/null @@ -1,9 +0,0 @@ - - -export { } - -declare global { - interface Window { - removeLoading: () => void - } -} From ef7022696b3c1b703118c053c57c76864a70c430 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8D=89=E9=9E=8B=E6=B2=A1=E5=8F=B7?= <308487730@qq.com> Date: Wed, 29 Jun 2022 08:36:32 +0800 Subject: [PATCH 4/6] chore: remove comments --- src/samples/node-api.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/samples/node-api.ts b/src/samples/node-api.ts index 8668059..db31e5f 100644 --- a/src/samples/node-api.ts +++ b/src/samples/node-api.ts @@ -2,7 +2,6 @@ import { lstat } from 'fs/promises' import { cwd } from 'process' import { ipcRenderer } from 'electron' -// Usage of ipcRenderer.on ipcRenderer.on('main-process-message', (_event, ...args) => { console.log('[Receive Main-process message]:', ...args) }) From 67cf1e7f318fff092fd6812e5727479ef5734104 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8D=89=E9=9E=8B=E6=B2=A1=E5=8F=B7?= <308487730@qq.com> Date: Wed, 29 Jun 2022 08:36:58 +0800 Subject: [PATCH 5/6] chore: use `postMessage` instead of `window.removeLoading` --- src/main.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main.ts b/src/main.ts index 5b91e1f..730f3ac 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,7 +1,9 @@ import { createApp } from 'vue' import App from './App.vue' -import './samples/node-api' +// import './samples/node-api' createApp(App) .mount('#app') - .$nextTick(window.removeLoading) + .$nextTick(() => { + postMessage({ payload: 'removeLoading' }, '*') + }) From 0827c0cdeb71149f751b237c80feed955d2f7b57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8D=89=E9=9E=8B=E6=B2=A1=E5=8F=B7?= <308487730@qq.com> Date: Wed, 29 Jun 2022 16:06:21 +0800 Subject: [PATCH 6/6] docs: update --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e40cf2a..f36480e 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ ## Features 📦 Out of the box -🎯 Based on [vue-ts](https://github.com/vitejs/vite/tree/main/packages/create-vite/template-vue-ts) template, less invasive +🎯 Based on the official [vue-ts](https://github.com/vitejs/vite/tree/main/packages/create-vite/template-vue-ts) template, less invasive 🌱 Extensible, really simple directory structure 💪 Support using Node.js API in Electron-Renderer 🔩 Support C/C++ native addons