feat: electron@28, supports "type": "module"

This commit is contained in:
草鞋没号 2023-12-30 14:14:46 +08:00
parent 7ec78d0366
commit 0d71c4bcd1
6 changed files with 23 additions and 22 deletions

View File

@ -1,14 +1,18 @@
import { app, BrowserWindow, shell, ipcMain } from 'electron' import { app, BrowserWindow, shell, ipcMain } from 'electron'
import { release } from 'node:os' 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 // The built directory structure
// //
// ├─┬ dist-electron // ├─┬ dist-electron
// │ ├─┬ main // │ ├─┬ main
// │ │ └── index.js > Electron-Main // │ │ └── index.mjs > Electron-Main
// │ └─┬ preload // │ └─┬ preload
// │ └── index.js > Preload-Scripts // │ └── index.mjs > Preload-Scripts
// ├─┬ dist // ├─┬ dist
// │ └── index.html > Electron-Renderer // │ └── index.html > Electron-Renderer
// //
@ -36,7 +40,7 @@ if (!app.requestSingleInstanceLock()) {
let win: BrowserWindow | null = null let win: BrowserWindow | null = null
// Here, you can also use other preload // 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 url = process.env.VITE_DEV_SERVER_URL
const indexHtml = join(process.env.DIST, 'index.html') const indexHtml = join(process.env.DIST, 'index.html')
@ -47,9 +51,10 @@ async function createWindow() {
webPreferences: { webPreferences: {
preload, preload,
// Warning: Enable nodeIntegration and disable contextIsolation is not secure in production // Warning: Enable nodeIntegration and disable contextIsolation is not secure in production
// nodeIntegration: true,
// Consider using contextBridge.exposeInMainWorld // Consider using contextBridge.exposeInMainWorld
// Read more on https://www.electronjs.org/docs/latest/tutorial/context-isolation // Read more on https://www.electronjs.org/docs/latest/tutorial/context-isolation
nodeIntegration: true,
contextIsolation: false, contextIsolation: false,
}, },
}) })

View File

@ -1,6 +1,6 @@
{ {
"name": "electron-vue-vite", "name": "electron-vue-vite",
"version": "2.2.0", "version": "28.0.0",
"main": "dist-electron/main/index.mjs", "main": "dist-electron/main/index.mjs",
"description": "Really simple Electron + Vue + Vite boilerplate.", "description": "Really simple Electron + Vue + Vite boilerplate.",
"author": "草鞋没号 <308487730@qq.com>", "author": "草鞋没号 <308487730@qq.com>",
@ -18,6 +18,7 @@
"VITE_DEV_SERVER_URL": "http://127.0.0.1:3344/" "VITE_DEV_SERVER_URL": "http://127.0.0.1:3344/"
} }
}, },
"type": "module",
"scripts": { "scripts": {
"dev": "vite", "dev": "vite",
"build": "vue-tsc --noEmit && vite build && electron-builder", "build": "vue-tsc --noEmit && vite build && electron-builder",

5
src/demos/ipc.ts Normal file
View File

@ -0,0 +1,5 @@
import { ipcRenderer } from 'electron'
ipcRenderer.on('main-process-message', (_event, ...args) => {
console.log('[Receive Main-process message]:', ...args)
})

View File

@ -1,10 +1,5 @@
import { lstat } from 'node:fs/promises' import { lstat } from 'node:fs/promises'
import { cwd } from 'node:process' 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 => { lstat(cwd()).then(stats => {
console.log('[fs.lstat]', stats) console.log('[fs.lstat]', stats)

View File

@ -1,7 +1,11 @@
import { createApp } from 'vue' import { createApp } from 'vue'
import "./style.css"
import App from './App.vue' 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) createApp(App)
.mount('#app') .mount('#app')

View File

@ -3,7 +3,6 @@ import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue' import vue from '@vitejs/plugin-vue'
import electron from 'vite-plugin-electron' import electron from 'vite-plugin-electron'
import renderer from 'vite-plugin-electron-renderer' import renderer from 'vite-plugin-electron-renderer'
import { notBundle } from 'vite-plugin-electron/plugin'
import pkg from './package.json' import pkg from './package.json'
// https://vitejs.dev/config/ // https://vitejs.dev/config/
@ -41,11 +40,6 @@ export default defineConfig(({ command }) => {
external: Object.keys('dependencies' in pkg ? pkg.dependencies : {}), 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 : {}), external: Object.keys('dependencies' in pkg ? pkg.dependencies : {}),
}, },
}, },
plugins: [
isServe && notBundle(),
],
}, },
} }
]), ]),