Skip to content

Commit

Permalink
feat: electron@28, supports "type": "module"
Browse files Browse the repository at this point in the history
  • Loading branch information
caoxiemeihao committed Dec 30, 2023
1 parent 7ec78d0 commit 0d71c4b
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 22 deletions.
15 changes: 10 additions & 5 deletions electron/main/index.ts
Original file line number Diff line number Diff line change
@@ -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
//
Expand Down Expand Up @@ -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')

Expand All @@ -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,
},
})
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": "草鞋没号 <[email protected]>",
Expand All @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions src/demos/ipc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ipcRenderer } from 'electron'

ipcRenderer.on('main-process-message', (_event, ...args) => {
console.log('[Receive Main-process message]:', ...args)
})
5 changes: 0 additions & 5 deletions src/samples/node-api.ts → src/demos/node.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
8 changes: 6 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
9 changes: 0 additions & 9 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down Expand Up @@ -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(),
],
},
},
{
Expand All @@ -64,9 +58,6 @@ export default defineConfig(({ command }) => {
external: Object.keys('dependencies' in pkg ? pkg.dependencies : {}),
},
},
plugins: [
isServe && notBundle(),
],
},
}
]),
Expand Down

0 comments on commit 0d71c4b

Please sign in to comment.