Skip to content

Commit de6a4ec

Browse files
authored
Merge pull request #24 from electron-vite/v0.4.0
V0.4.0
2 parents e4b1099 + ac8d5f7 commit de6a4ec

File tree

19 files changed

+186
-122
lines changed

19 files changed

+186
-122
lines changed

CHANGELOG.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,43 @@
1+
## 0.4.0 (2023-09-06)
2+
3+
- 38af79d chore: test v0.4.0
4+
- f5dafdf chore: update template
5+
- 7114cac chore: site use `https://electron-vite.github.io`
6+
- 394686c refactor: use `vite-plugin-electron` simple API
7+
8+
#### Main Changed
9+
10+
**0.4.0** use the simple API of `vite-plugin-electron`
11+
12+
```ts
13+
import electron from 'vite-plugin-electron/simple'
14+
15+
electron({
16+
main: {
17+
entry: 'electron/main.ts',
18+
},
19+
preload: {
20+
input: __dirname + '/electron/preload.ts',
21+
},
22+
renderer: {},
23+
})
24+
```
25+
26+
**0.3.0**
27+
28+
```ts
29+
import electron from 'vite-plugin-electron'
30+
31+
electron([
32+
{
33+
entry: 'electron/main.ts',
34+
},
35+
{
36+
entry: 'electron/preload.ts',
37+
},
38+
])
39+
```
40+
141
## 0.3.0 (2023-05-27)
242

343
42dc950 refactor: use Vite instead unbuild

__tests__/cli.spec.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { join } from 'node:path'
1+
import fs from 'node:fs'
2+
import path from 'node:path'
23
import type { ExecaSyncReturnValue, SyncOptions } from 'execa'
34
import { execaCommandSync } from 'execa'
4-
import fs from 'fs-extra'
55
import { afterEach, beforeAll, expect, test } from 'vitest'
66

7-
const CLI_PATH = join(__dirname, '..')
7+
const CLI_PATH = path.join(__dirname, '..')
88

99
const projectName = 'electron-vite-app'
10-
const genPath = join(__dirname, projectName)
10+
const generatePath = path.join(__dirname, projectName)
1111

1212
const run = (
1313
args: string[],
@@ -18,24 +18,24 @@ const run = (
1818

1919
const createNonEmptyDir = () => {
2020
// Create the temporary directory
21-
fs.mkdirpSync(genPath)
21+
fs.mkdirSync(generatePath, { recursive: true })
2222

2323
// Create a package.json file
24-
const pkgJson = join(genPath, 'package.json')
24+
const pkgJson = path.join(generatePath, 'package.json')
2525
fs.writeFileSync(pkgJson, '{ "foo": "bar" }')
2626
}
2727

28-
beforeAll(() => fs.remove(genPath))
29-
afterEach(() => fs.remove(genPath))
28+
beforeAll(() => fs.rmSync(generatePath, { recursive: true, force: true }))
29+
afterEach(() => fs.rmSync(generatePath, { recursive: true, force: true }))
3030

3131
test('prompts for the project name if none supplied', () => {
3232
const { stdout } = run([])
3333
expect(stdout).toContain('Project name:')
3434
})
3535

3636
test('prompts for the framework if none supplied when target dir is current directory', () => {
37-
fs.mkdirpSync(genPath)
38-
const { stdout } = run(['.'], { cwd: genPath })
37+
fs.mkdirSync(generatePath, { recursive: true })
38+
const { stdout } = run(['.'], { cwd: generatePath })
3939
expect(stdout).toContain('Project template:')
4040
})
4141

@@ -52,6 +52,6 @@ test('asks to overwrite non-empty target directory', () => {
5252

5353
test('asks to overwrite non-empty current directory', () => {
5454
createNonEmptyDir()
55-
const { stdout } = run(['.'], { cwd: genPath })
55+
const { stdout } = run(['.'], { cwd: generatePath })
5656
expect(stdout).toContain(`Current directory is not empty.`)
5757
})

electron/electron-builder.json5

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@
55
"$schema": "https://raw.githubusercontent.com/electron-userland/electron-builder/master/packages/app-builder-lib/scheme.json",
66
"appId": "YourAppID",
77
"asar": true,
8+
"productName": "YourAppName",
89
"directories": {
910
"output": "release/${version}"
1011
},
1112
"files": [
12-
"dist-electron",
13-
"dist"
13+
"dist",
14+
"dist-electron"
1415
],
1516
"mac": {
16-
"artifactName": "${productName}_${version}.${ext}",
1717
"target": [
1818
"dmg"
19-
]
19+
],
20+
"artifactName": "${productName}-Mac-${version}-Installer.${ext}"
2021
},
2122
"win": {
2223
"target": [
@@ -27,12 +28,18 @@
2728
]
2829
}
2930
],
30-
"artifactName": "${productName}_${version}.${ext}"
31+
"artifactName": "${productName}-Windows-${version}-Setup.${ext}"
3132
},
3233
"nsis": {
3334
"oneClick": false,
3435
"perMachine": false,
3536
"allowToChangeInstallationDirectory": true,
3637
"deleteAppDataOnUninstall": false
38+
},
39+
"linux": {
40+
"target": [
41+
"AppImage"
42+
],
43+
"artifactName": "${productName}-Linux-${version}.${ext}"
3744
}
3845
}

electron/electron-env.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ declare namespace NodeJS {
1717
*/
1818
DIST: string
1919
/** /dist/ or /public/ */
20-
PUBLIC: string
20+
VITE_PUBLIC: string
2121
}
2222
}
23+
24+
// Used in Renderer process, expose in `preload.ts`
25+
interface Window {
26+
ipcRenderer: import('electron').IpcRenderer
27+
}

electron/main.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import path from 'node:path'
1111
// │ │ └── preload.js
1212
// │
1313
process.env.DIST = path.join(__dirname, '../dist')
14-
process.env.PUBLIC = app.isPackaged ? process.env.DIST : path.join(process.env.DIST, '../public')
14+
process.env.VITE_PUBLIC = app.isPackaged ? process.env.DIST : path.join(process.env.DIST, '../public')
1515

1616

1717
let win: BrowserWindow | null
@@ -20,7 +20,7 @@ const VITE_DEV_SERVER_URL = process.env['VITE_DEV_SERVER_URL']
2020

2121
function createWindow() {
2222
win = new BrowserWindow({
23-
icon: path.join(process.env.PUBLIC, 'electron-vite.svg'),
23+
icon: path.join(process.env.VITE_PUBLIC, 'electron-vite.svg'),
2424
webPreferences: {
2525
preload: path.join(__dirname, 'preload.js'),
2626
},
@@ -39,9 +39,22 @@ function createWindow() {
3939
}
4040
}
4141

42+
// Quit when all windows are closed, except on macOS. There, it's common
43+
// for applications and their menu bar to stay active until the user quits
44+
// explicitly with Cmd + Q.
4245
app.on('window-all-closed', () => {
43-
win = null
44-
if (process.platform !== 'darwin') app.quit()
46+
if (process.platform !== 'darwin') {
47+
app.quit()
48+
win = null
49+
}
50+
})
51+
52+
app.on('activate', () => {
53+
// On OS X it's common to re-create a window in the app when the
54+
// dock icon is clicked and there are no other windows open.
55+
if (BrowserWindow.getAllWindows().length === 0) {
56+
createWindow()
57+
}
4558
})
4659

4760
app.whenReady().then(createWindow)

electron/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"devDependencies": {
3-
"electron": "^24.4.0",
4-
"electron-builder": "^23.6.0",
5-
"vite-plugin-electron": "^0.11.2",
3+
"electron": "^26.1.0",
4+
"electron-builder": "^24.6.4",
5+
"vite-plugin-electron": "^0.14.0",
66
"vite-plugin-electron-renderer": "^0.14.5"
77
}
88
}

electron/preload.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
1+
import { contextBridge, ipcRenderer } from 'electron'
2+
3+
// --------- Expose some API to the Renderer process ---------
4+
contextBridge.exposeInMainWorld('ipcRenderer', withPrototype(ipcRenderer))
5+
6+
// `exposeInMainWorld` can't detect attributes and methods of `prototype`, manually patching it.
7+
function withPrototype(obj: Record<string, any>) {
8+
const protos = Object.getPrototypeOf(obj)
9+
10+
for (const [key, value] of Object.entries(protos)) {
11+
if (Object.prototype.hasOwnProperty.call(obj, key)) continue
12+
13+
if (typeof value === 'function') {
14+
// Some native APIs, like `NodeJS.EventEmitter['on']`, don't work in the Renderer process. Wrapping them into a function.
15+
obj[key] = function (...args: any) {
16+
return value.call(obj, ...args)
17+
}
18+
} else {
19+
obj[key] = value
20+
}
21+
}
22+
return obj
23+
}
24+
25+
// --------- Preload scripts loading ---------
126
function domReady(condition: DocumentReadyState[] = ['complete', 'interactive']) {
227
return new Promise(resolve => {
328
if (condition.includes(document.readyState)) {

package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "create-electron-vite",
3-
"version": "0.3.0",
3+
"version": "0.4.0",
44
"type": "module",
55
"description": "Scaffolding Your Electron + Vite Project",
66
"license": "MIT",
@@ -34,24 +34,23 @@
3434
"preinstall": "npx only-allow pnpm",
3535
"watch": "vite build --watch",
3636
"build": "vite build",
37-
"prepublishOnly": "npm run build",
37+
"prepublishOnly": "npm run build && npm run test",
38+
"lint": "eslint .",
3839
"test": "vitest run"
3940
},
4041
"dependencies": {
4142
"prompts": "^2.4.2"
4243
},
4344
"devDependencies": {
44-
"@types/fs-extra": "^11.0.1",
4545
"@types/node": "^18.11.18",
4646
"@types/prompts": "^2.4.2",
4747
"execa": "^7.1.1",
48-
"fs-extra": "^11.1.1",
4948
"typescript": "^4.9.4",
5049
"vite": "^4.3.9",
5150
"vitest": "^0.29.3"
5251
},
5352
"engines": {
5453
"node": "^14.18.0 || >=16.0.0"
5554
},
56-
"packageManager": "pnpm@8.6.0"
55+
"packageManager": "pnpm@8.0.0"
5756
}

pnpm-lock.yaml

Lines changed: 0 additions & 45 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)