Skip to content

Commit d8c3c27

Browse files
committedAug 23, 2023
feat: simple API 🥳
1 parent da26c48 commit d8c3c27

File tree

4 files changed

+112
-4
lines changed

4 files changed

+112
-4
lines changed
 

‎README.md

+28-4
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828

2929
## Features
3030

31-
- 🐣 Few APIs, easy to use
32-
- 🌱 Fully compatible with Vite and Vite's ecosystem <sub><sup>(Based on Vite)</sup></sub>
33-
- [🚀 Not Bundle, It's fast <sub><sup>(Like Vite's Not Bundle)</sup></sub>](https://github.com/electron-vite/vite-plugin-electron#not-bundle)
3431
- [🔥 Hot Restart <sub><sup>(Main process)</sup></sub>](https://electron-vite.github.io/guide/features.html#hot-restart)
3532
- [🔄 Hot Reload <sub><sup>(Preload scripts)</sup></sub>](https://electron-vite.github.io/guide/features.html#hot-reload)
3633
- [⚡️ HMR <sub><sup>(Renderer process)</sup></sub>](https://electron-vite.github.io/guide/features.html#hmr)
34+
- [🚀 Not Bundle, It's fast <sub><sup>(Like Vite's Not Bundle)</sup></sub>](https://github.com/electron-vite/vite-plugin-electron#not-bundle)
35+
- 🌱 Fully compatible with Vite and Vite's ecosystem <sub><sup>(Based on Vite)</sup></sub>
36+
- 🐣 Few APIs, easy to use
3737

3838
<!-- ![vite-plugin-electron.gif](https://github.com/electron-vite/vite-plugin-electron/blob/main/vite-plugin-electron.gif?raw=true) -->
3939

@@ -89,9 +89,33 @@ app.whenReady().then(() => {
8989

9090
That's it! You can now use Electron in your Vite app ✨
9191

92+
## Simple API
93+
94+
Many times, for a developer who is new to Vite and Electron, the oversimplified and open API design is confusing to them. Maybe Simple API makes them easier to understand. :)
95+
96+
```js
97+
import electron from 'vite-plugin-electron/simple'
98+
99+
export default {
100+
plugins: [
101+
electron({
102+
main: {
103+
entry: 'electron/main.ts',
104+
},
105+
// Optional: input must be use absolute path
106+
preload: {
107+
input: __dirname + '/electron/preload.ts',
108+
},
109+
// Optional: Use Node.js API in the Renderer process
110+
renderer: {},
111+
}),
112+
],
113+
}
114+
```
115+
92116
## API <sub><sup>(Define)</sup></sub>
93117

94-
`electron(config: ElectronOptions | ElectronOptions[])`
118+
`electron(options: ElectronOptions | ElectronOptions[])`
95119

96120
```ts
97121
export interface ElectronOptions {

‎plugin.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// For import intellisense
2+
export * from './dist/plugin'

‎simple.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// For import intellisense
2+
export * from './dist/simple'
3+
import simple from './dist/simple'
4+
export default simple

‎src/simple.ts

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import {
2+
type Plugin,
3+
type UserConfig,
4+
mergeConfig,
5+
} from 'vite'
6+
import type { InputOption } from 'rollup'
7+
import electron, { type ElectronOptions } from '.'
8+
9+
export interface ElectronSimpleOptions {
10+
main: ElectronOptions
11+
preload?: Omit<ElectronOptions, 'entry'> & {
12+
/**
13+
* Shortcut of `build.rollupOptions.input`.
14+
*
15+
* Preload scripts perhaps bundle as web format, so use the `build.rollupOptions.input` instead `build.lib.entry`.
16+
*/
17+
input: InputOption
18+
}
19+
/**
20+
* Support use Node.js API in Electron-Renderer
21+
* @see https://github.com/electron-vite/vite-plugin-electron-renderer
22+
*/
23+
renderer?: import('vite-plugin-electron-renderer').RendererOptions
24+
}
25+
26+
// Vite v3.x support async plugin.
27+
export default async function electronSimple(options: ElectronSimpleOptions): Promise<Plugin[]> {
28+
const opts = [options.main]
29+
if (options.preload) {
30+
const {
31+
input,
32+
vite: viteConfig = {},
33+
...preloadOptions
34+
} = options.preload
35+
const preload: ElectronOptions = {
36+
onstart(args) {
37+
// Notify the Renderer-Process to reload the page when the Preload-Scripts build is complete,
38+
// instead of restarting the entire Electron App.
39+
args.reload()
40+
},
41+
...preloadOptions,
42+
vite: mergeConfig({
43+
build: {
44+
rollupOptions: {
45+
input,
46+
output: {
47+
// Only one file will be bundled, which is consistent with the behavior of `build.lib`
48+
manualChunks: {},
49+
// https://github.com/vitejs/vite/blob/v4.4.9/packages/vite/src/node/build.ts#L604
50+
entryFileNames: '[name].js',
51+
chunkFileNames: '[name].js',
52+
assetFileNames: '[name].[ext]',
53+
},
54+
},
55+
} as UserConfig,
56+
}, viteConfig),
57+
}
58+
opts.push(preload)
59+
}
60+
const plugins = electron(opts)
61+
62+
if (options.renderer) {
63+
try {
64+
const renderer = await import('vite-plugin-electron-renderer')
65+
plugins.push(renderer.default(options.renderer))
66+
} catch (error: any) {
67+
if (error.code === 'ERR_MODULE_NOT_FOUND') {
68+
throw new Error(
69+
`\`renderer\` option dependency "vite-plugin-electron-renderer" not found. Did you install it? Try \`npm i -D vite-plugin-electron-renderer\`.`,
70+
)
71+
}
72+
73+
throw error
74+
}
75+
}
76+
77+
return plugins
78+
}

0 commit comments

Comments
 (0)
Please sign in to comment.