Skip to content

Commit 32a5704

Browse files
committed
efactor: better docs 🌱
1 parent 5eaf234 commit 32a5704

20 files changed

+562
-324
lines changed

.vitepress/config.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -47,30 +47,30 @@ export default defineConfig({
4747
{ text: 'Why Electron⚡️Vite', link: '/guide/why-electron-vite' },
4848
{ text: 'Getting Started', link: '/guide/getting-started' },
4949
{ text: 'Features', link: '/guide/features' },
50-
{ text: 'Plugins', link: '/guide/plugins' },
51-
{ text: 'Examples', link: '/guide/examples' },
50+
{ text: 'Core Plugins', link: '/guide/core-plugins' },
51+
{ text: 'C/C++ Addons', link: '/guide/cpp-addons' },
52+
{ text: 'Dependency Pre-Bundling', link: '/guide/dependency-pre-bundling' },
53+
{ text: 'Preload Code Not Split', link: '/guide/preload-not-split' },
54+
{ text: 'Not Bundle', link: '/guide/not-bundle' },
55+
{ text: 'Env Variables', link: '/guide/env-variables' },
56+
{ text: 'Static Resource', link: '/guide/static-resource' },
5257
{ text: 'Templates', link: '/guide/templates' },
58+
{ text: 'Examples 🚀', link: '/guide/examples' },
5359
],
5460
},
55-
{
61+
/* {
5662
text: 'Build',
5763
collapsed: false,
5864
items: [
59-
{ text: 'Electron Forge', link: '/build/electron-forge' },
6065
{ text: 'Electron Builder', link: '/build/electron-builder' },
6166
],
62-
},
67+
}, */
6368
{
6469
text: 'FAQ',
6570
collapsed: false,
6671
items: [
67-
{ text: 'C/C++ Addons', link: '/faq/cpp-addons' },
68-
{ text: 'Pre-Bundling', link: '/faq/pre-bundling' },
69-
{ text: 'dependencies', link: '/faq/dependencies' },
70-
{ text: 'Env Variables', link: '/faq/env-variables' },
71-
{ text: 'Static Resource', link: '/faq/static-resource' },
72-
{ text: 'Preload Code Not Split', link: '/faq/preload-not-split' },
73-
{ text: 'Improve Build Performance', link: '/faq/build-performance' },
72+
{ text: 'Electron Forge', link: '/faq/electron-forge' },
73+
{ text: 'dependencies vs devDependencies', link: '/faq/dependencies' },
7474
],
7575
},
7676
],

.vitepress/theme/styles/vars.css

+5
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,8 @@
138138
height: 240px;
139139
}
140140
}
141+
142+
a[href="/faq/dependencies.html"] .text {
143+
font-size: 12px;
144+
white-space: nowrap;
145+
}

build/electron-forge.md

-5
This file was deleted.

faq/build-performance.md

-1
This file was deleted.

faq/cpp-addons.md

-53
This file was deleted.

faq/dependencies.md

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1-
# dependencies vs devDependencies
1+
# [dependencies vs devDependencies](https://github.com/electron-vite/vite-plugin-electron-renderer#dependencies-vs-devdependencies)
22

3-
TODO:
3+
::: warning
4+
This documentation is limited to building App using [electron-builder](https://www.electron.build/).
5+
:::
6+
7+
If a third-party npm package can be built normally by [Vite](https://vitejs.dev/), we recommend placing it in `devDependencies`, which will reduce the size of the packaged App.
8+
In some extreme cases, such as when using `C/C++` addons, if we cannot build it correctly, we need to put it in `dependencies` to ensure that the App can work properly after being built.
9+
10+
<details>
11+
<summary>中文</summary>
12+
<p>此文档仅限于使用 <code>electron-builder</code> 构建应用</p>
13+
<p>如果一个第三方 npm 包可以被 <a target="_blank" href="https://vitejs.dev/">Vite</a> 正常构建,我们推荐将它放到 <code>devDependencies</code> 中,这会减小应用打包后的体积。</p>
14+
<p>在一些极端的场景中,比如使用 <code>C/C++</code> 扩展时,如果我们没办法正确的构建它,需要将它放到 <code>dependencies</code> 中以确保应用构建后能够正常工作。</p>
15+
</details>

faq/electron-forge.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Electron Forge
2+
3+
[Electron Forge](https://www.electronforge.io/) is recommended, because it is the official Electron build tool maintained by [Electron](https://www.electronjs.org/).
4+
5+
See the 👉 [Electron Forge Vite ⚡️](https://www.electronforge.io/templates/vite) docs.
6+
7+
::: warning electron-builder
8+
At present, the templates provided by [Electron⚡️Vite](https://github.com/electron-vite) are packaged using [electron-builder](https://www.electron.build/). **But it is not Electron's official build tool**.
9+
:::
10+
11+
<details>
12+
<summary>中文</summary>
13+
<p>目前,<a target="_blank" href="https://github.com/electron-vite">Electron⚡️Vite</a> 提供的均模板使用的 <a target="_blank" href="https://www.electron.build/">electron-builder</a> 打包,但它并非 Electron 官方的构建工具。</p>
14+
<p>推荐使用 <a target="_blank" href="https://www.electronforge.io/">Electron Forge</a>,因为它是 <a target="_blank" href="https://www.electronjs.org/">Electron</a> 维护的官方 Electron 构建工具。</p>
15+
<p>点击查看 <a target="_blank" href="https://www.electronforge.io/templates/vite">👉 Electron Forge Vite ⚡️</a>文档。</p>
16+
</details>

faq/pre-bundling.md

-3
This file was deleted.

faq/preload-not-split.md

-1
This file was deleted.

guide/core-plugins.md

+242
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
# Core Plugins
2+
3+
## vite-plugin-electron
4+
5+
[vite-plugin-electron](https://github.com/electron-vite/vite-plugin-electron) makes the integration of Electron With Vite very easy! Its main purpose is to build the **Main process** and **Preload scripts**, and to start and [hot restart](/guide/features.html#hot-restart) the Electron App when the **Main process** and **Preload scripts** are built.
6+
7+
It also provides a full [JavaScript API](https://github.com/electron-vite/vite-plugin-electron#javascript-api) to make it easy to use anywhere.
8+
9+
<details>
10+
<summary>中文</summary>
11+
<p><a target="_blank" href="https://github.com/electron-vite">vite-plugin-electron</a> 使得 Electron 与 Vite 的集成变得十分简单!它的主要作用是构建主进程与预加载脚本,并且当主进程与预加载脚本构建完成时启动、<a href="/guide/features.html#hot-restart">热重启</a> Electron App。</p>
12+
<p>除此之外它还提供全量的 <a target="_blank" href="https://github.com/electron-vite/vite-plugin-electron#javascript-api">JavaScript API</a> 可以很方便的在任何地方使用它。</p>
13+
</details>
14+
15+
### Basic Usage
16+
17+
```ts
18+
// vite.config.ts
19+
import electron from 'vite-plugin-electron'
20+
21+
export default {
22+
plugins: [
23+
electron({
24+
// Main process entry file of the Electron App.
25+
entry: 'electron/main.ts',
26+
}),
27+
],
28+
}
29+
```
30+
31+
### With Preload Scripts
32+
33+
[vite-plugin-electron](https://github.com/electron-vite/vite-plugin-electron) supports passing an Array.
34+
35+
```ts
36+
// vite.config.ts
37+
import electron from 'vite-plugin-electron'
38+
39+
export default {
40+
plugins: [
41+
electron([
42+
{
43+
entry: 'electron/main.ts',
44+
},
45+
{
46+
entry: 'electron/preload.ts',
47+
onstart({ reload }) {
48+
// Notify the Renderer process to reload the page when the Preload scripts build is complete,
49+
// instead of restarting the entire Electron App.
50+
reload()
51+
},
52+
},
53+
]),
54+
],
55+
}
56+
```
57+
58+
### Code Split
59+
60+
[vite-plugin-electron](https://github.com/electron-vite/vite-plugin-electron) allows for very flexible code splitting, it can be built using passing Array, or using [Vite's built-in multi-entry](https://vitejs.dev/config/build-options.html#build-lib) build.
61+
62+
<details>
63+
<summary>中文</summary>
64+
<p><code>vite-plugin-electron</code> 可以进行十分灵活的代码拆分,它可以使用传递数组的形式构建,或者使用 <a target="_blank" href="https://vitejs.dev/config/build-options.html#build-lib">Vite 内置的多入口</a> 构建。</p>
65+
</details>
66+
67+
```ts
68+
// vite.config.ts
69+
import electron from 'vite-plugin-electron'
70+
71+
// Use array
72+
export default {
73+
plugins: [
74+
electron([
75+
{
76+
// Main-Process entry file of the Electron App.
77+
entry: 'electron/main.ts',
78+
},
79+
{
80+
entry: 'electron/main-chunk.ts',
81+
},
82+
]),
83+
],
84+
}
85+
86+
// Use Vite multi-entry
87+
export default {
88+
plugins: [
89+
electron({
90+
entry: {
91+
// Main-Process entry file of the Electron App.
92+
main: 'electron/main.ts'
93+
'main-chunk': 'electron/main-chunk.ts',
94+
},
95+
}),
96+
],
97+
}
98+
```
99+
100+
### Custom Start
101+
102+
This is useful if you want to do something before or after **launching** or **restarting** the Electron App.
103+
104+
<details>
105+
<summary>中文</summary>
106+
<p>如果你想在启动或重启 Electron App 之前或之后做些什么,它会很有用。</p>
107+
</details>
108+
109+
```ts
110+
// vite.config.ts
111+
import electron from 'vite-plugin-electron'
112+
113+
export default {
114+
plugins: [
115+
electron({
116+
entry: 'electron/main/index.ts',
117+
onstart({ startup }) {
118+
// Do something...
119+
startup()
120+
},
121+
}),
122+
],
123+
}
124+
```
125+
126+
### Custom Build
127+
128+
[vite-plugin-electron](https://github.com/electron-vite/vite-plugin-electron) supports the full amount of [Vite's InlineConfig](https://vitejs.dev/guide/api-javascript.html#inlineconfig).
129+
130+
<details>
131+
<summary>中文</summary>
132+
<p><code>vite-plugin-electron</code> 支持全量的 <a target="_blank" href="https://vitejs.dev/guide/api-javascript.html#inlineconfig">Vite 配置</a>。</p>
133+
</details>
134+
135+
```ts
136+
// vite.config.ts
137+
import electron from 'vite-plugin-electron/simple'
138+
139+
export default {
140+
plugins: [
141+
electron({
142+
entry: 'electron/main.ts',
143+
// 👉 https://vitejs.dev/guide/api-javascript.html#inlineconfig
144+
vite: {/* Vite config ⚡️ */},
145+
}),
146+
],
147+
}
148+
```
149+
150+
### [JavaScript API](https://github.com/electron-vite/vite-plugin-electron#javascript-api)
151+
152+
- [nuxt-electron](https://github.com/caoxiemeihao/nuxt-electron) based on [vite-plugin-electron](https://github.com/electron-vite/vite-plugin-electron)
153+
154+
## vite-plugin-electron/simple
155+
156+
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. :)
157+
158+
```ts
159+
// vite.config.ts
160+
import electron from 'vite-plugin-electron/simple'
161+
162+
export default {
163+
plugins: [
164+
// Just like v0.9.x
165+
electron({
166+
main: {
167+
entry: 'electron/main.ts',
168+
},
169+
// Optional: input must be use absolute path
170+
preload: {
171+
input: __dirname + '/electron/preload.ts',
172+
},
173+
// Optional: Use Node.js API in the Renderer process
174+
renderer: {},
175+
}),
176+
],
177+
}
178+
```
179+
180+
## vite-plugin-electron-renderer
181+
182+
[vite-plugin-electron-renderer](https://github.com/electron-vite/vite-plugin-electron-renderer) makes it possible to support building Electron **Renderer process** by modifying some of the necessary Vite default configs.
183+
184+
In addition it allows some npm packages developed specifically for Node.js, especially C/C++ native modules, to be built properly by Vite to be available to the **Renderer process**.
185+
186+
::: tip Note
187+
Please make sure you enabled `nodeIntegration: true` in the **Main process**.
188+
:::
189+
190+
<details>
191+
<summary>中文</summary>
192+
<p><a target="_blank" href="https://github.com/electron-vite/vite-plugin-electron-renderer">vite-plugin-electron-renderer</a> 通过修改一些必要的 Vite 的默认配置使其能够支持构建 Electron 渲染进程。</p>
193+
<p>此外它使得一些专门为 Node.js 开发的 npm 包尤其是 C/C++ 本地模块,可以被 Vite 正确构建以提供给渲染进程使用。</p>
194+
</details>
195+
196+
### Basic Usage
197+
198+
```ts
199+
// vite.config.ts
200+
import electron from 'vite-plugin-electron'
201+
import renderer from 'vite-plugin-electron-renderer'
202+
203+
export default {
204+
plugins: [
205+
electron({
206+
entry: 'electron/main.ts',
207+
}),
208+
// Use Node.js and Electron in Renderer process
209+
renderer(),
210+
],
211+
}
212+
```
213+
214+
### Node.js And Electron API
215+
216+
```ts
217+
// In a Renderer process file
218+
import { readFileSync } from 'node:fs'
219+
import { ipcRenderer } from 'electron'
220+
221+
// Node.js
222+
const content = readFileSync('foo.txt', 'utf8')
223+
224+
// Electron
225+
ipcRenderer.send('foo', 'arg1')
226+
```
227+
228+
### Load Third-party modules
229+
230+
In most cases a Node.js npm-pkg written in pure JavaScript can be used directly in the Renderer process. If it is a `C/C++` addon or `ESModule` format package, it needs to be [pre-bundle](https://vitejs.dev/guide/dep-pre-bundling.html) before it can work.
231+
232+
See the [👉 Dependency Pre-Bundling](/guide/dependency-pre-bundling.html) section for details.
233+
234+
<details>
235+
<summary>中文</summary>
236+
<p>多数情况下一个纯 JavaScript 编写的 Node.js npm 包是可以直接在渲染进程中使用的。如果它是 <code>C/C++</code> 扩展,或者 <code>ESModule</code> 格式包,那么需要将它<a target="_blank" href="https://vitejs.dev/guide/dep-pre-bundling.html">预构建</a>后才可以工作。</p>
237+
<p>详情请看 <a href="/guide/dependency-pre-bundling.html">👉 Dependency Pre-Bundling</a> 部分。</p>
238+
</details>
239+
240+
241+
242+

0 commit comments

Comments
 (0)