Skip to content

Commit 9bd69b1

Browse files
committed
Merge branch 'main' of github.com:caoxiemeihao/electron-vue-vite into main
2 parents 95a83b3 + 7c3b892 commit 9bd69b1

9 files changed

+58
-70
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ dist-ssr
2323

2424
release
2525
.vscode/.debug.env
26+
dist-electron

.vscode/launch.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"--remote-debugging-port=9229",
3333
"."
3434
],
35-
"envFile": "${workspaceFolder}/.vscode/.debug.env"
35+
"envFile": "${workspaceFolder}/.vscode/.debug.env",
36+
"console": "integratedTerminal"
3637
},
3738
{
3839
"name": "Debug Renderer Process",

.vscode/tasks.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"owner": "typescript",
1313
"fileLocation": "relative",
1414
"pattern": {
15+
// TODO: correct "regexp"
1516
"regexp": "^([a-zA-Z]\\:\/?([\\w\\-]\/?)+\\.\\w+):(\\d+):(\\d+): (ERROR|WARNING)\\: (.*)$",
1617
"file": 1,
1718
"line": 3,
@@ -21,8 +22,7 @@
2122
},
2223
"background": {
2324
"activeOnStart": true,
24-
"beginsPattern": "^.*building for development.*$",
25-
"endsPattern": "built in [0-9]*ms.*$",
25+
"endsPattern": "^.*[startup] Electron App.*$",
2626
}
2727
}
2828
}

electron-builder.json5

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"output": "release/${version}"
99
},
1010
"files": [
11+
"dist-electron",
1112
"dist"
1213
],
1314
"mac": {

electron/electron-env.d.ts

+3-16
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
1-
/// <reference types="vite-plugin-electron/electron-env" />
1+
/// <reference types="vite-electron-plugin/electron-env" />
22

33
declare namespace NodeJS {
44
interface ProcessEnv {
5-
/**
6-
* The built directory structure
7-
*
8-
* ```tree
9-
* ├─┬ dist
10-
* │ ├─┬ electron
11-
* │ │ ├─┬ main
12-
* │ │ │ └── index.js
13-
* │ │ └─┬ preload
14-
* │ │ └── index.js
15-
* │ ├── index.html
16-
* │ ├── ...other-static-files-from-public
17-
* │
18-
* ```
19-
*/
5+
VSCODE_DEBUG?: 'true'
6+
DIST_ELECTRON: string
207
DIST: string
218
/** /dist/ or /public/ */
229
PUBLIC: string

electron/main/index.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// The built directory structure
22
//
3+
// ├─┬ dist-electron
4+
// │ ├─┬ main
5+
// │ │ └── index.js > Electron-Main
6+
// │ └─┬ preload
7+
// │ └── index.js > Preload-Scripts
38
// ├─┬ dist
4-
// │ ├─┬ electron
5-
// │ │ ├─┬ main
6-
// │ │ │ └── index.js
7-
// │ │ └─┬ preload
8-
// │ │ └── index.js
9-
// │ ├── index.html
10-
// │ ├── ...other-static-files-from-public
11-
// │
12-
process.env.DIST = join(__dirname, '../..')
13-
process.env.PUBLIC = app.isPackaged ? process.env.DIST : join(process.env.DIST, '../public')
9+
// │ └── index.html > Electron-Renderer
10+
//
11+
process.env.DIST_ELECTRON = join(__dirname, '..')
12+
process.env.DIST = join(process.env.DIST_ELECTRON, '../dist')
13+
process.env.PUBLIC = app.isPackaged ? process.env.DIST : join(process.env.DIST_ELECTRON, '../public')
1414

1515
import { app, BrowserWindow, shell, ipcMain } from 'electron'
1616
import { release } from 'os'
@@ -35,7 +35,7 @@ if (!app.requestSingleInstanceLock()) {
3535
let win: BrowserWindow | null = null
3636
// Here, you can also use other preload
3737
const preload = join(__dirname, '../preload/index.js')
38-
const url = process.env.VITE_DEV_SERVER_URL as string
38+
const url = process.env.VITE_DEV_SERVER_URL
3939
const indexHtml = join(process.env.DIST, 'index.html')
4040

4141
async function createWindow() {

package.json

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "electron-vue-vite",
3-
"version": "2.0.0",
4-
"main": "dist/electron/main/index.js",
3+
"version": "2.1.0",
4+
"main": "dist-electron/main/index.js",
55
"description": "Really simple Electron + Vue + Vite boilerplate.",
66
"author": "草鞋没号 <[email protected]>",
77
"license": "MIT",
@@ -15,18 +15,16 @@
1515
},
1616
"devDependencies": {
1717
"@vitejs/plugin-vue": "^3.0.1",
18-
"electron": "^20.0.2",
18+
"electron": "^21.0.1",
1919
"electron-builder": "^23.3.3",
2020
"typescript": "^4.7.4",
2121
"vite": "^3.0.8",
22-
"vite-plugin-electron": "^0.9.3",
22+
"vite-electron-plugin": "^0.4.1",
2323
"vue": "^3.2.37",
2424
"vue-tsc": "^0.40.1"
2525
},
2626
"debug": {
2727
"env": {
28-
"VITE_DEV_SERVER_HOSTNAME": "127.0.0.1",
29-
"VITE_DEV_SERVER_PORT": 3344,
3028
"VITE_DEV_SERVER_URL": "http://127.0.0.1:3344"
3129
}
3230
},

src/main.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import { createApp } from 'vue'
22
import App from './App.vue'
3+
4+
/**
5+
* If you enables use of Node.js API in the Renderer-process
6+
* ```
7+
* npm i -D vite-plugin-electron-renderer
8+
* ```
9+
* @see - https://github.com/electron-vite/vite-plugin-electron/tree/main/packages/electron-renderer#electron-renderervite-serve
10+
*/
311
// import './samples/node-api'
412

513
createApp(App)

vite.config.ts

+26-34
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,41 @@
11
import { rmSync } from 'fs'
2-
import path from 'path'
32
import { defineConfig } from 'vite'
43
import vue from '@vitejs/plugin-vue'
5-
import electron, { onstart } from 'vite-plugin-electron'
4+
import electron from 'vite-electron-plugin'
5+
import { customStart } from 'vite-electron-plugin/plugin'
66
import pkg from './package.json'
77

8-
rmSync('dist', { recursive: true, force: true }) // v14.14.0
8+
rmSync('dist-electron', { recursive: true, force: true })
99

1010
// https://vitejs.dev/config/
1111
export default defineConfig({
1212
plugins: [
1313
vue(),
1414
electron({
15-
main: {
16-
entry: 'electron/main/index.ts',
17-
vite: {
18-
build: {
19-
// For Debug
20-
sourcemap: true,
21-
outDir: 'dist/electron/main',
22-
},
23-
// Will start Electron via VSCode Debug
24-
plugins: [process.env.VSCODE_DEBUG ? onstart() : null],
25-
},
15+
include: ['electron'],
16+
transformOptions: {
17+
sourcemap: !!process.env.VSCODE_DEBUG,
2618
},
27-
preload: {
28-
input: {
29-
// You can configure multiple preload here
30-
index: path.join(__dirname, 'electron/preload/index.ts'),
31-
},
32-
vite: {
33-
build: {
34-
// For Debug
35-
sourcemap: 'inline',
36-
outDir: 'dist/electron/preload',
37-
},
38-
},
39-
},
40-
// Enables use of Node.js API in the Renderer-process
41-
// https://github.com/electron-vite/vite-plugin-electron/tree/main/packages/electron-renderer#electron-renderervite-serve
42-
renderer: {},
19+
// Will start Electron via VSCode Debug
20+
plugins: process.env.VSCODE_DEBUG
21+
? [customStart(debounce(() => console.log(/* For `.vscode/.debug.script.mjs` */'[startup] Electron App')))]
22+
: undefined,
4323
}),
4424
],
45-
server: process.env.VSCODE_DEBUG ? {
46-
host: pkg.debug.env.VITE_DEV_SERVER_HOSTNAME,
47-
port: pkg.debug.env.VITE_DEV_SERVER_PORT,
48-
} : undefined,
25+
server: process.env.VSCODE_DEBUG ? (() => {
26+
const url = new URL(pkg.debug.env.VITE_DEV_SERVER_URL)
27+
return {
28+
host: url.hostname,
29+
port: +url.port,
30+
}
31+
})() : undefined,
32+
clearScreen: false,
4933
})
34+
35+
function debounce<Fn extends (...args: any[]) => void>(fn: Fn, delay = 299) {
36+
let t: NodeJS.Timeout
37+
return ((...args) => {
38+
clearTimeout(t)
39+
t = setTimeout(() => fn(...args), delay)
40+
}) as Fn
41+
}

0 commit comments

Comments
 (0)