From 0d71c4bcd19dea8c314e5d862e6c1f2424e4a881 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: Sat, 30 Dec 2023 14:14:46 +0800 Subject: [PATCH] feat: electron@28, supports `"type": "module"` --- electron/main/index.ts | 15 ++++++++++----- package.json | 3 ++- src/demos/ipc.ts | 5 +++++ src/{samples/node-api.ts => demos/node.ts} | 5 ----- src/main.ts | 8 ++++++-- vite.config.ts | 9 --------- 6 files changed, 23 insertions(+), 22 deletions(-) create mode 100644 src/demos/ipc.ts rename src/{samples/node-api.ts => demos/node.ts} (52%) diff --git a/electron/main/index.ts b/electron/main/index.ts index ccee8e48..c76425f2 100644 --- a/electron/main/index.ts +++ b/electron/main/index.ts @@ -1,14 +1,18 @@ import { app, BrowserWindow, shell, ipcMain } from 'electron' import { release } from 'node:os' -import { join } from 'node:path' +import { join, dirname } from 'node:path' +import { fileURLToPath } from 'node:url' + +const __filename = fileURLToPath(import.meta.url) +const __dirname = dirname(__filename) // The built directory structure // // ├─┬ dist-electron // │ ├─┬ main -// │ │ └── index.js > Electron-Main +// │ │ └── index.mjs > Electron-Main // │ └─┬ preload -// │ └── index.js > Preload-Scripts +// │ └── index.mjs > Preload-Scripts // ├─┬ dist // │ └── index.html > Electron-Renderer // @@ -36,7 +40,7 @@ if (!app.requestSingleInstanceLock()) { let win: BrowserWindow | null = null // Here, you can also use other preload -const preload = join(__dirname, '../preload/index.js') +const preload = join(__dirname, '../preload/index.mjs') const url = process.env.VITE_DEV_SERVER_URL const indexHtml = join(process.env.DIST, 'index.html') @@ -47,9 +51,10 @@ async function createWindow() { webPreferences: { preload, // Warning: Enable nodeIntegration and disable contextIsolation is not secure in production + // nodeIntegration: true, + // Consider using contextBridge.exposeInMainWorld // Read more on https://www.electronjs.org/docs/latest/tutorial/context-isolation - nodeIntegration: true, contextIsolation: false, }, }) diff --git a/package.json b/package.json index 48c4c185..57e295e1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "electron-vue-vite", - "version": "2.2.0", + "version": "28.0.0", "main": "dist-electron/main/index.mjs", "description": "Really simple Electron + Vue + Vite boilerplate.", "author": "草鞋没号 <308487730@qq.com>", @@ -18,6 +18,7 @@ "VITE_DEV_SERVER_URL": "http://127.0.0.1:3344/" } }, + "type": "module", "scripts": { "dev": "vite", "build": "vue-tsc --noEmit && vite build && electron-builder", diff --git a/src/demos/ipc.ts b/src/demos/ipc.ts new file mode 100644 index 00000000..b94a9392 --- /dev/null +++ b/src/demos/ipc.ts @@ -0,0 +1,5 @@ +import { ipcRenderer } from 'electron' + +ipcRenderer.on('main-process-message', (_event, ...args) => { + console.log('[Receive Main-process message]:', ...args) +}) diff --git a/src/samples/node-api.ts b/src/demos/node.ts similarity index 52% rename from src/samples/node-api.ts rename to src/demos/node.ts index f7a4d4da..277e6a34 100644 --- a/src/samples/node-api.ts +++ b/src/demos/node.ts @@ -1,10 +1,5 @@ import { lstat } from 'node:fs/promises' import { cwd } from 'node:process' -import { ipcRenderer } from 'electron' - -ipcRenderer.on('main-process-message', (_event, ...args) => { - console.log('[Receive Main-process message]:', ...args) -}) lstat(cwd()).then(stats => { console.log('[fs.lstat]', stats) diff --git a/src/main.ts b/src/main.ts index e864d880..8924aee7 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,7 +1,11 @@ import { createApp } from 'vue' -import "./style.css" import App from './App.vue' -import './samples/node-api' + +import './style.css' + +// `nodeIntegration` needs to be enabled in the Main process. +// import './demos/node' +// import './demos/ipc' createApp(App) .mount('#app') diff --git a/vite.config.ts b/vite.config.ts index 4d136f01..1ab23d59 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -3,7 +3,6 @@ import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import electron from 'vite-plugin-electron' import renderer from 'vite-plugin-electron-renderer' -import { notBundle } from 'vite-plugin-electron/plugin' import pkg from './package.json' // https://vitejs.dev/config/ @@ -41,11 +40,6 @@ export default defineConfig(({ command }) => { external: Object.keys('dependencies' in pkg ? pkg.dependencies : {}), }, }, - plugins: [ - // This is just an option to improve build performance, it's non-deterministic! - // e.g. `import log from 'electron-log'` -> `const log = require('electron-log')` - isServe && notBundle(), - ], }, }, { @@ -64,9 +58,6 @@ export default defineConfig(({ command }) => { external: Object.keys('dependencies' in pkg ? pkg.dependencies : {}), }, }, - plugins: [ - isServe && notBundle(), - ], }, } ]),