├── .gitignore ├── .npmrc ├── CHANGELOG.md ├── LICENSE ├── README.md ├── README.zh-CN.md ├── electron-env.d.ts ├── logo.animate.svg ├── logo.svg ├── logo.v1.svg ├── package.json ├── plugin.d.ts ├── pnpm-lock.yaml ├── simple.d.ts ├── src ├── index.ts ├── plugin.ts ├── simple.ts └── utils.ts ├── test ├── __snapshots__ │ └── external-main.js ├── fixtures │ ├── external-foo.ts │ └── external-main.ts ├── plugin.test.ts └── utils.test.ts ├── tsconfig.json ├── vite-plugin-electron.gif └── vite.config.ts /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | 9 | # Diagnostic reports (https://nodejs.org/api/report.html) 10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 11 | 12 | # Runtime data 13 | pids 14 | *.pid 15 | *.seed 16 | *.pid.lock 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | *.lcov 24 | 25 | # nyc test coverage 26 | .nyc_output 27 | 28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 29 | .grunt 30 | 31 | # Bower dependency directory (https://bower.io/) 32 | bower_components 33 | 34 | # node-waf configuration 35 | .lock-wscript 36 | 37 | # Compiled binary addons (https://nodejs.org/api/addons.html) 38 | build/Release 39 | 40 | # Dependency directories 41 | node_modules/ 42 | jspm_packages/ 43 | 44 | # TypeScript v1 declaration files 45 | typings/ 46 | 47 | # TypeScript cache 48 | *.tsbuildinfo 49 | 50 | # Optional npm cache directory 51 | .npm 52 | 53 | # Optional eslint cache 54 | .eslintcache 55 | 56 | # Microbundle cache 57 | .rpt2_cache/ 58 | .rts2_cache_cjs/ 59 | .rts2_cache_es/ 60 | .rts2_cache_umd/ 61 | 62 | # Optional REPL history 63 | .node_repl_history 64 | 65 | # Output of 'npm pack' 66 | *.tgz 67 | 68 | # Yarn Integrity file 69 | .yarn-integrity 70 | 71 | # dotenv environment variables file 72 | .env 73 | .env.test 74 | 75 | # parcel-bundler cache (https://parceljs.org/) 76 | .cache 77 | 78 | # Next.js build output 79 | .next 80 | 81 | # Nuxt.js build / generate output 82 | .nuxt 83 | dist 84 | 85 | # Gatsby files 86 | .cache/ 87 | # Comment in the public line in if your project uses Gatsby and *not* Next.js 88 | # https://nextjs.org/blog/next-9-1#public-directory-support 89 | # public 90 | 91 | # vuepress build output 92 | .vuepress/dist 93 | 94 | # Serverless directories 95 | .serverless/ 96 | 97 | # FuseBox cache 98 | .fusebox/ 99 | 100 | # DynamoDB Local files 101 | .dynamodb/ 102 | 103 | # TernJS port file 104 | .tern-port 105 | 106 | .DS_Store 107 | 108 | package-lock.json 109 | # pnpm-lock.yaml 110 | yarn.lock 111 | 112 | /types 113 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | # For electron-builder 2 | # https://github.com/electron-userland/electron-builder/issues/6289#issuecomment-1042620422 3 | shamefully-hoist=true 4 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 0.29.0 (2024-11-16) 2 | 3 | - 638d0f3 feat: support main process control hot-reload 4 | - c58fbf9 feat: mock index.html for support use the main process only 5 | 6 | **Hot Reload** 7 | 8 | Since `v0.29.0`, when preload scripts are rebuilt, they will send an `electron-vite&type=hot-reload` event to the main process. 9 | If your App doesn't need a renderer process, this will give you **hot-reload**. 10 | 11 | ```js 12 | // electron/main.ts 13 | 14 | process.on('message', (msg) => { 15 | if (msg === 'electron-vite&type=hot-reload') { 16 | for (const win of BrowserWindow.getAllWindows()) { 17 | // Hot reload preload scripts 18 | win.webContents.reload() 19 | } 20 | } 21 | }) 22 | ``` 23 | 24 | ## 0.28.8 (2024-09-19) 25 | 26 | - 3239718 fix: better exit app #251 27 | - 927885d fix: unable to disable watch #252 28 | - f043f74 chore: cleanup 29 | - 149c4a9 Merge pull request #247 from think-flow/testfix 30 | - f9bda13 fix: notBundle test is failed 31 | 32 | ## 0.28.7 (2024-04-27) 33 | 34 | - b3cf74c chore: cleanup, code format 35 | - 957b144 Merge pull request #233 from kuoruan/feat/config 36 | - c0f2647 feat: inherit more config from base user config 37 | 38 | **Contributors:** 39 | 40 | - [#233](https://github.com/electron-vite/vite-plugin-electron/pull/233)@[kuoruan](https://github.com/kuoruan) 41 | 42 | ## 0.28.6 (2024-04-21) 43 | 44 | - be94383 fix: `ElectronOptions['startup']` definition 45 | - 96905d9 chore: compatible vite@4 resolve node first 46 | - a16fe3c fix: hot-reload compatible vite@4 47 | 48 | ## 0.28.5 (2024-04-19) 49 | 50 | - f325403 chore: update comments 51 | - 5ebd3a6 Merge pull request #229 from ThatKawaiiSam/feat/custom-electron-fork 52 | - dbc85dc feat: allow custom electron forks in `startup` call 53 | - 8b076be chore: update deprecated API 54 | 55 | **Contributors:** 56 | 57 | - [#229](https://github.com/electron-vite/vite-plugin-electron/pull/229)@[ThatKawaiiSam](https://github.com/ThatKawaiiSam) 58 | 59 | ## 0.28.4 (2024-03-17) 60 | 61 | - 4f7748c chore: restore `vite-plugin-electron-renderer` in `peerDependencies` 62 | - 412ed9b fix(#214): catch the `process.kill(pid)` error 63 | 64 | ## 0.28.3 (2024-03-13) 65 | 66 | - a300c08 fix: remove electron@28 version pre-deps 67 | 68 | ## 0.28.2 (2024-02-11) 69 | 70 | - 5b0fc87 feat: use `treeKillSync()` instead `tree-kill` 71 | 72 | ## 0.28.1 (2024-02-06) 73 | 74 | - efc5826 fix: correct `calcEntryCount()` for startup 75 | - cf0e91f feat: startup support `SpawnOptions` #194 76 | - 512a1c5 Merge pull request #204 from SanGongHappy/tree-kill-sync 77 | - edf4bf6 refactor: better `treeKillSync` implemented 78 | - 82cd32f Merge branch 'electron-vite:main' into tree-kill-sync 79 | - 6e122a7 feat: polyfill `process.env` by default 80 | - 45f28bb (github/main, main) chore: update CHANGELOG 81 | - f9b81e4 chore(simple): update comments 82 | - b5376a9 docs: add `Built format` 83 | - e334366 copy tree-kill 84 | 85 | ## 0.28.0 (2024-01-07) 86 | 87 | - 20170a5 refactor: preload built `format` use `esm` by default 88 | 89 | #### Migration to v0.28.0 90 | 91 | - **No break changes**. Just need to define `"type": "module"` in package.json for supports `esm` :) 92 | - **By the way**. Recommend using the `vite-plugin-electron/simple` API. It is simpler, and the main difference is its adaptation to `preload` scripts. 93 | - **electron@28** Preload scripts [👉 limitations](https://github.com/electron/electron/blob/v30.0.0-nightly.20240104/docs/tutorial/esm.md#preload-scripts). 94 | 95 | #### Preload scripts 96 | 97 | In most cases, use `cjs` format 98 | 99 | ```log 100 | `require()` can usable matrix 101 | 102 | @see - https://github.com/electron/electron/blob/v30.0.0-nightly.20240104/docs/tutorial/esm.md#preload-scripts 103 | ┏———————————————————————————————————┳——————————┳———————————┓ 104 | │ webPreferences: { } │ import │ require │ 105 | ┠———————————————————————————————————╂——————————╂———————————┨ 106 | │ nodeIntegration: false(undefined) │ ✘ │ ✔ │ 107 | ┠———————————————————————————————————╂——————————╂———————————┨ 108 | │ nodeIntegration: true │ ✔ │ ✔ │ 109 | ┠———————————————————————————————————╂——————————╂———————————┨ 110 | │ sandbox: true(undefined) │ ✘ │ ✔ │ 111 | ┠———————————————————————————————————╂——————————╂———————————┨ 112 | │ sandbox: false │ ✔ │ ✘ │ 113 | ┠———————————————————————————————————╂——————————╂———————————┨ 114 | │ nodeIntegration: false │ ✘ │ ✔ │ 115 | │ sandbox: true │ │ │ 116 | ┠———————————————————————————————————╂——————————╂———————————┨ 117 | │ nodeIntegration: false │ ✔ │ ✘ │ 118 | │ sandbox: false │ │ │ 119 | ┠———————————————————————————————————╂——————————╂———————————┨ 120 | │ nodeIntegration: true │ ✘ │ ✔ │ 121 | │ sandbox: true │ │ │ 122 | ┠———————————————————————————————————╂——————————╂———————————┨ 123 | │ nodeIntegration: true │ ✔ │ ✔ │ 124 | │ sandbox: false │ │ │ 125 | ┗———————————————————————————————————┸——————————┸———————————┛ 126 | - import(✘): SyntaxError: Cannot use import statement outside a module 127 | - require(✘): ReferenceError: require is not defined in ES module scope, you can use import instead 128 | ``` 129 | 130 | #### Built format 131 | 132 | This is just the default behavior, and you can modify them at any time through custom config in the `vite.config.js` 133 | 134 | ```log 135 | { "type": "module" } 136 | ┏————————————————————┳——————————┳———————————┓ 137 | │ built │ format │ suffix │ 138 | ┠————————————————————╂——————————╂———————————┨ 139 | │ main process │ esm │ .js │ 140 | ┠————————————————————╂——————————╂———————————┨ 141 | │ preload scripts │ cjs │ .mjs │ diff 142 | ┠————————————————————╂——————————╂———————————┨ 143 | │ renderer process │ - │ .js │ 144 | ┗————————————————————┸——————————┸———————————┛ 145 | 146 | { "type": "commonjs" } - default 147 | ┏————————————————————┳——————————┳———————————┓ 148 | │ built │ format │ suffix │ 149 | ┠————————————————————╂——————————╂———————————┨ 150 | │ main process │ cjs │ .js │ 151 | ┠————————————————————╂——————————╂———————————┨ 152 | │ preload scripts │ cjs │ .js │ diff 153 | ┠————————————————————╂——————————╂———————————┨ 154 | │ renderer process │ - │ .js │ 155 | ┗————————————————————┸——————————┸———————————┛ 156 | ``` 157 | 158 | ## 0.28.0-beta.3 (2024-01-05) 159 | 160 | - d7c0a91 feat: preload built `format` adapt to `esm` 161 | 162 | ## 0.28.0-beta.2 (2024-01-02) 163 | 164 | - f557a98 feat: supports `"type": "module"`, better `esm` logic 165 | 166 | #### Migration to v0.28.0-beta.2 167 | 168 | - ~~**No break changes**. Just need to define `"type": "module"` in package.json for supports `esm` :)~~ 169 | - ~~**By the way**. Recommend using the `vite-plugin-electron/simple` API. It is simpler, and the main difference is its adaptation to `preload` scripts.~~ 170 | - ~~**electron@28** Preload scripts [👉 limitations](https://github.com/electron/electron/blob/v30.0.0-nightly.20231218/docs/tutorial/esm.md#preload-scripts).~~ 171 | 172 | ## 0.28.0-beta.1 (2023-12-30) 173 | 174 | - 9ec82b5 feat: supports electron@28, build `esm` by default 175 | - 5dd3ccd feat: bump deps to vite@5, electron@28 and others 176 | - 39520cc feat: upgrade to vite@5 177 | - 3340cfc chore: prevent encounter problem 178 | 179 | > Why `v0.28.0`? This is consistent with `electron@28`, meaning it is recommended to use electron@28+, which is the first version to support `esm`. 180 | 181 | #### Migration to v0.28.0-beta.1 182 | 183 | **🚨 deprecated** 184 | 185 | - ~~Update `main` of `package.json` for `esm`~~ 186 | 187 | ```diff 188 | { 189 | - "main": "dist-electron/main.js", 190 | + "main": "dist-electron/main.mjs", 191 | } 192 | ``` 193 | 194 | - ~~If you still need to use `cjs`, you need to make some changes~~ 195 | 196 | **~~Simple API~~** 197 | 198 | ```diff 199 | import electron from 'vite-plugin-electron/simple' 200 | 201 | export default { 202 | plugins: [ 203 | electron({ 204 | main: { 205 | - entry: 'electron/main.ts', 206 | + vite: { 207 | + build: { 208 | + lib: { 209 | + entry: 'electron/main.ts', 210 | + formats: ['cjs'], 211 | + fileName: () => '[name].js', 212 | + }, 213 | + }, 214 | + }, 215 | }, 216 | }), 217 | ], 218 | } 219 | ``` 220 | 221 | **~~Flat API~~** 222 | 223 | ```diff 224 | import electron from 'vite-plugin-electron' 225 | 226 | export default { 227 | plugins: [ 228 | electron({ 229 | - entry: 'electron/main.ts', 230 | + vite: { 231 | + build: { 232 | + lib: { 233 | + entry: 'electron/main.ts', 234 | + formats: ['cjs'], 235 | + fileName: () => '[name].js', 236 | + }, 237 | + }, 238 | }, 239 | }), 240 | ], 241 | } 242 | ``` 243 | 244 | ## 0.15.5 (2023-12-12) 245 | 246 | - f57a60a fix(#182, #187): correctly `options.reload()` 247 | - f606551 Merge pull request #178 from electron-vite/v0.15.4 248 | 249 | ## 0.15.4 (2023-11-08) 250 | 251 | - 532e0b4 Merge pull request #177 from Siykt/main 252 | - 845c39b fix: add SIGKILL to tree-kill call 253 | 254 | **Contributors:** 255 | 256 | - [#177](https://github.com/electron-vite/vite-plugin-electron/pull/177)@[Siykt](https://github.com/Siykt) 257 | 258 | ## 0.15.3 (2023-11-07) 259 | 260 | - 20907ef chore: recover `v0.15.1` 261 | 262 | ## 0.15.2 (2023-11-06) 263 | 264 | - 9719203 chore: better exit `electron` logic 265 | 266 | ## 0.15.1 (2023-11-06) 267 | 268 | - 2119108 fix: correct use `tree-kill` 269 | 270 | ## 0.15.0 (2023-11-06) 271 | 272 | - 234be07 fix: `tree-kill` doesn't work 273 | - 5e6c36d feat: `calcEntryCount()` 274 | - bbd6bd1 Merge branch 'main' of github.com:electron-vite/vite-plugin-electron into main 275 | - 9a910e5 Merge pull request #166 from CommandMC/fix/wait-for-all-files 276 | - 3962718 fix: startup electron wait for all files built 277 | - 7f75ae2 Merge branch 'fix/wait-for-all-files' of https://github.com/CommandMC/vite-plugin-electron into main 278 | - 65b73c2 Merge pull request #122 from sevenc-nanashi/main 279 | - e4033c3 Merge pull request #1 from caoxiemeihao/cleanup 280 | - 8e70a39 chore: cleanup 281 | - 20dd675 refactor: migrate examples to `github.com/caoxiemeihao/electron-vite-samples` 282 | - de9259f Change: Use peer-dependency 283 | - 8247192 Merge: main -> main 284 | - ab990b9 Wait until the last file is done bundling before starting Electron 285 | - 6e5a033 Merge pull request #163 from electron-vite/v0.14.1 286 | 287 | **Fixs:** 288 | 289 | - `electron` does not exit correctly | 使用ctrl + c 停止代码,electron后台不关闭 [#168](https://github.com/electron-vite/vite-plugin-electron/issues/168), [#122](https://github.com/electron-vite/vite-plugin-electron/pull/122) 290 | - Fixed `electron` startup timing [#166](https://github.com/electron-vite/vite-plugin-electron/pull/166) 291 | 292 | **Contributors:** 293 | 294 | - [#122](https://github.com/electron-vite/vite-plugin-electron/pull/122)@[sevenc-nanashi](https://github.com/sevenc-nanashi) 295 | - [#166](https://github.com/electron-vite/vite-plugin-electron/pull/166)@[CommandMC](https://github.com/CommandMC) 296 | 297 | ## 0.14.1 (2023-09-10) 298 | 299 | - ba14f55 feat(simple): Preload scripts code not split by default 300 | 301 | ## 0.14.0 (2023-09-01) 302 | 303 | - 3cc71ca feat(simple): build Preload scripts as `cjs` by default 304 | 305 | ## 0.14.0-beta.0 (2023-08-23) 306 | 307 | - d3cf87f feat: simple API build 308 | - d8c3c27 feat: simple API 🥳 309 | 310 | See **[👉 Simple API](https://github.com/electron-vite/vite-plugin-electron/tree/v0.14.0-beta.0#simple-api)** 311 | 312 | ```js 313 | import electron from 'vite-plugin-electron/simple' 314 | 315 | electron({ 316 | main: {}, 317 | preload: {}, 318 | renderer: {}, 319 | }) 320 | ``` 321 | 322 | ## 0.13.0-beta.3 (2023-08-22) 323 | 324 | - 37e14b0 fix: remove `apply: 'serve'` 325 | - 4f196e3 fix: export bare `vite-plugin-electron/plugin` 326 | - 06c91f3 docs: v0.13.0 327 | 328 | The user decides when `notBundle` should be used. See **[👉 Not Bundle](https://github.com/electron-vite/vite-plugin-electron/tree/v0.13.0-beta.3#not-bundle)** 329 | 330 | ```js 331 | import electron from 'vite-plugin-electron' 332 | import { notBundle } from 'vite-plugin-electron/plugin' 333 | 334 | export default defineConfig(({ command }) => ({ 335 | plugins: [ 336 | electron({ 337 | entry: 'electron/main.ts', 338 | vite: { 339 | plugins: [ 340 | command === 'serve' && notBundle(/* NotBundleOptions */), 341 | ], 342 | }, 343 | }), 344 | ], 345 | })) 346 | ``` 347 | 348 | ## 0.13.0-beta.2 (2023-08-21) 349 | 350 | - 86d9a34 chore: update pnpm lock 351 | - c6aceb3 refactor: standalone `Not Bundle` plugin 352 | - b3decf4 Merge pull request #150 from haodaking/changelog 353 | - 3f77d12 Update CHANGELOG.md 354 | 355 | Users need to import the `notBundle` plugin explicitly. See **[👉 Not Bundle](https://github.com/electron-vite/vite-plugin-electron/tree/v0.13.0-beta.2#not-bundle)** 356 | 357 | ```js 358 | import electron from 'vite-plugin-electron' 359 | import { notBundle } from 'vite-plugin-electron/plugin' 360 | 361 | export default { 362 | plugins: [ 363 | electron({ 364 | entry: 'electron/main.ts', 365 | vite: { 366 | plugins: [ 367 | notBundle(/* NotBundleOptions */), 368 | ], 369 | }, 370 | }), 371 | ], 372 | } 373 | ``` 374 | 375 | ## 0.13.0-beta.1 (2023-08-06) 376 | 377 | - e5403c1 feat: check `cjs` availabe 378 | 379 | ## 0.13.0-beta.0 (2023-08-06) 380 | 381 | - 5fbcf40 test: plugin `external_node_modules` 382 | - 9803e26 feat: non-bundle during dev 🚀 383 | 384 | Let's use the `electron-log` as an examples. 385 | 386 | ```js 387 | ┏—————————————————————————————————————┓ 388 | │ import log from 'electron-log' │ 389 | ┗—————————————————————————————————————┛ 390 | ↓ 391 | Modules in `node_modules` are not bundled during development, it's fast! 392 | ↓ 393 | ┏—————————————————————————————————————┓ 394 | │ const log = require('electron-log') │ 395 | ┗—————————————————————————————————————┛ 396 | ``` 397 | 398 | ## 0.12.0 (2023-06-13) 399 | 400 | - 6822409 docs: v0.12.0 401 | - 9ec9d3b v0.12.0 402 | - 53a0dd8 feat: disable minify during dev 403 | - 3925cd1 refactor: remove `defineConfig`, `Configuration` -> `ElectronOptions`, config.ts -> utils.ts 404 | 405 | ## 0.11.2 (2023-04-15) 406 | 407 | - b033bc5 v0.11.2 408 | - 8190208 docs: typo 409 | - 111d495 refactor(🔨): better build logic 410 | - 76611a9 fix: disable `browserField` by default #136 411 | - 241534f feat: add `.npmrc` for `pnpm` 412 | - 65d4ab1 chore: bump deps 413 | - b4308e3 chore: rename 414 | - 036e52c docs: update 415 | - a222c48 examples: update quick-start 416 | - d04155c examples: add javascript-api 417 | - 9213ba0 docs: update 418 | - 9b8da83 fix: cannot find type definition #126 419 | - e864b81 examples: update multiple-window, add multiple-renderer 420 | - 0a226b5 docs: update 421 | - ff5e9dc feat: add logo.svg 422 | - 7cec6ad examples: add multiple-windows 423 | - f8528ed Merge pull request #123 from xhayper/patch-1 424 | - 1e9f9b0 feat: remove extra dependencies 425 | - 8fa6ce4 feat: update package and example 426 | - 1d7eca5 feat: add test 🌱 427 | - 9ef3e8e chore: bump deps 428 | - 56446e2 chore: cleanup 429 | 430 | ## 0.11.1 (2022-12-19) 431 | 432 | - 2bf7d0b docs: `startup()` 433 | - 401a44e refactor: cleanup 434 | 435 | ## 0.11.0 (2022-12-17) 436 | 437 | #### Break! 438 | 439 | ```ts 440 | // 0.10.0 441 | function build(config: Configuration | Configuration[]): Promise 442 | 443 | // 0.11.0 - Same as Vite's build 444 | function build(config: Configuration): Promise 445 | ``` 446 | 447 | #### Features 448 | 449 | **JavaScript API** 450 | 451 | `vite-plugin-electron`'s JavaScript APIs are fully typed, and it's recommended to use TypeScript or enable JS type checking in VS Code to leverage the intellisense and validation. 452 | 453 | - `Configuration` - type 454 | - `defineConfig` - function 455 | - `resolveViteConfig` - function, Resolve the default Vite's `InlineConfig` for build Electron-Main 456 | - `withExternalBuiltins` - function 457 | - `build` - function 458 | - `startup` - function 459 | 460 | Example: 461 | 462 | ```js 463 | build( 464 | withExternalBuiltins( // external Node.js builtin modules 465 | resolveViteConfig( // with default config 466 | { 467 | entry: 'foo.ts', 468 | vite: { 469 | mode: 'foo-mode', // for .env file 470 | plugins: [{ 471 | name: 'plugin-build-done', 472 | closeBundle() { 473 | // Startup Electron App 474 | startup() 475 | }, 476 | }], 477 | }, 478 | } 479 | ) 480 | ) 481 | ) 482 | ``` 483 | 484 | **V8 Bytecode support** 👉 [bytecode](https://github.com/electron-vite/vite-plugin-electron/tree/main/examples/bytecode) 485 | 486 | Inspired by: 487 | 488 | - [Nihiue/little-byte-demo](https://github.com/Nihiue/little-byte-demo) 489 | - [通过字节码保护Node.js源码之原理篇 - 知乎](https://zhuanlan.zhihu.com/p/359235114) 490 | 491 | #### Commit/PR 492 | 493 | - Support Vite4.x | #118, 28d38b6 494 | - Bytecode example | df170c2 495 | - JavaScript API docs | 3049169 496 | - Fix load `.env` | 758695d 497 | - Refactor `build()` | d9c3343 498 | 499 | ## 0.10.4 (2022-11-13) 500 | 501 | - e4f943f refactor: move `build.resolve` to `resolve` 502 | - 91fb525 docs(zh-CN): update | @ggdream 503 | - 41db615 Use mode from source config. | #105, #106 504 | 505 | ## 0.10.3 (2022-11-10) 506 | 507 | - 0b24909 refactor: cleanup, provides some programmable APIs 🌱 508 | - 58517d8 refactor(proj-struct): remove `vite-plugin-electron-renderer` 509 | - d2b3c29 Merge pull request #98 from skyrpex/patch-1 510 | - 1645be2 fix: ignore the browser field when bundling 511 | 512 | ## 0.10.2 (2022-10-24) 513 | 514 | By default, the `dist` folder will be automatically removed by Vite. We build Electron related files into `dist-electron` to prevent it from being removed by mistake 515 | 516 | 1. Remove Vite `3.2.0` | #90 517 | 2. ad9bb3c refactor(electron-renderer)!: remove `options.resolve()`, use 'lib-esm' for resolve Node.js modules and `electron` | vite-plugin-electron-renderer 518 | 3. b500039 feat(electron-renderer): support `optimizeDeps` for Electron-Renderer 🚀 519 | 4. f28e66b faet(outDir)!: `dist/electron` -> `dist-electron` | vite-plugin-electron 520 | 521 | ## 0.10.1 (2022-10-12) 522 | 523 | - 59a24df fix(electron-renderer): use `createRequire()` instead of `import()` 🐞 524 | - 11cd4c3 feat(electron): `onstart` provides `reload()` 525 | 526 | ## 0.10.0 (2022-10-09) 527 | 528 | #### Break! 529 | 530 | This is a redesigned version of the API(Only 3 APIs). Not compatible with previous versions! 531 | 532 | ```ts 533 | export type Configuration = { 534 | /** 535 | * Shortcut of `build.lib.entry` 536 | */ 537 | entry?: import('vite').LibraryOptions['entry'] 538 | /** 539 | * Triggered when Vite is built. 540 | * If passed this parameter will not automatically start Electron App. 541 | * You can start Electron App through the `startup` function passed through the callback function. 542 | */ 543 | onstart?: (this: import('rollup').PluginContext, startup: (args?: string[]) => Promise) => void 544 | vite?: import('vite').InlineConfig 545 | } 546 | ``` 547 | 548 | In the past few weeks, some issues have been mentioned in many issues that cannot be solved amicably. So I refactored the API to avoid design flaws. But despite this, the new version will only be easier rather than harder. 549 | 550 | For example, some common problems in the following issues. 551 | 552 | #### Multiple entry files is not support #86 553 | 554 | Thanks to Vite@3.2.0's `lib.entry` supports multiple entries, which makes the configuration of the new version very simple. **So the `vite-plugin-electron@0.10.0` requires Vite at least `v3.2.0`**. 555 | 556 | **e.g.** 557 | 558 | ```ts 559 | import electron from 'vite-plugin-electron' 560 | 561 | // In plugins option 562 | electron({ 563 | entry: [ 564 | 'electron/entry-1.ts', 565 | 'electron/entry-2.ts', 566 | ], 567 | }) 568 | 569 | // Or use configuration array 570 | electron([ 571 | { 572 | entry: [ 573 | 'electron/entry-1.ts', 574 | 'electron/entry-2.ts', 575 | ], 576 | }, 577 | { 578 | entry: 'foo/bar.ts', 579 | }, 580 | ]) 581 | ``` 582 | 583 | #### require is not defined #48, #87 584 | 585 | `vite-plugin-electron-renderer` will change `output.format` to `cjs` format by default(This is because currently Electron@21 only supports CommonJs), which will cause the built code to use `require` to import modules, if the user `nodeIntegration` is not enabled in the Electron-Main process which causes the error `require is not defined` to be thrown. 586 | 587 | `vite-plugin-electron-renderer@0.10.0` provides the `nodeIntegration` option. It is up to the user to decide whether to use Node.js(CommonJs). 588 | 589 | **e.g.** 590 | 591 | ```ts 592 | import renderer from 'vite-plugin-electron-renderer' 593 | 594 | // In plugins option 595 | renderer({ 596 | nodeIntegration: true, 597 | }) 598 | ``` 599 | 600 | #### Use `Worker` in Electron-Main or Electron-Renderer #77, #81 601 | 602 | > You can see 👉 [examples/worker](https://github.com/caoxiemeihao/vite-plugin-electron/tree/main/examples/worker) 603 | 604 | - Use Worker in Electron-Main 605 | 606 | **e.g.** This looks the same as multiple entry 607 | 608 | ```ts 609 | import electron from 'vite-plugin-electron' 610 | 611 | // In plugins option 612 | electron({ 613 | entry: [ 614 | 'electron/main.ts', 615 | 'electron/worker.ts', 616 | ], 617 | }) 618 | 619 | // In electron/main.ts 620 | new Worker(path.join(__dirname, './worker.js')) 621 | ``` 622 | 623 | - Use Worker in Electron-Renderer 624 | 625 | **e.g.** 626 | 627 | ```ts 628 | import renderer, { worker } from 'vite-plugin-electron-renderer' 629 | 630 | export default { 631 | plugins: [ 632 | renderer({ 633 | // If you need use Node.js in Electron-Renderer process 634 | nodeIntegration: true, 635 | }), 636 | ], 637 | worker: { 638 | plugins: [ 639 | worker({ 640 | // If you need use Node.js in Worker 641 | nodeIntegrationInWorker: true, 642 | }), 643 | ], 644 | }, 645 | } 646 | ``` 647 | 648 | #### TODO 649 | 650 | - [ ] There is no way to differentiate between Preload-Scripts, which will cause the entire Electron App to restart after the preload update, not the Electron-Renderer reload. 651 | 652 | #### PR 653 | 654 | https://github.com/electron-vite/vite-plugin-electron/pull/89 655 | 656 | ## 0.9.3 (2022-09-10) 657 | 658 | ~~*vite-plugin-electron*~~ 659 | 660 | *vite-plugin-electron-renderer* 661 | 662 | - 191afb8 feat: proxy `ipcRenderer` in `Worker` | #69 663 | 664 | ## 0.9.2 (2022-08-29) 665 | 666 | *vite-plugin-electron* 667 | 668 | - 715a1cd fix(electron): `VITE_DEV_SERVER_HOSTNAME` instead `VITE_DEV_SERVER_HOST` 669 | 670 | *vite-plugin-electron-renderer* 671 | 672 | ## 0.9.1 (2022-08-24) 673 | 674 | *vite-plugin-electron* 675 | 676 | - db61e49 feat(electron): support custom start 🌱 | #57, #58 677 | 678 | ~~*vite-plugin-electron-renderer*~~ 679 | 680 | ## 0.9.0 (2022-08-12) 681 | 682 | 🎉 `v0.9.0` is a stable version based on `vite@3.0.6` 683 | 684 | ~~*vite-plugin-electron*~~ 685 | 686 | *vite-plugin-electron-renderer* 687 | 688 | - ebc6a3d chore(electron-renderer): remove `renderBuiltUrl()` based on vite@3.0.6 ([vite@3.0.6-8f2065e](https://github.com/vitejs/vite/pull/9381/commits/8f2065efcb6ba664f7dce6f3c7666b29e2c56027#diff-aa53520bfd53e6c24220c44494457cc66370fd2bee513c15f9be7eb537a363e7L874)) 689 | 690 | --- 691 | 692 | 693 | ## [2022-08-11] v0.8.8 694 | 695 | sync `vite-plugin-electron-renderer` version 696 | 697 | ## [2022-08-10] v0.8.7 698 | 699 | 70a8c6e fix: https://github.com/electron-vite/electron-vite-vue/issues/229 700 | 701 | ## [2022-08-08] v0.8.6 702 | 703 | ESM -> CJS 704 | 705 | ## [2022-08-08] v0.8.5 706 | 707 | PR: #51 708 | 709 | 1. feat: add `VITE_DEV_SERVER_URL` to electron 710 | process env, so that it is easier to use 711 | 712 | 2. fix(🐞): VITE_DEV_SERVER_HOSTNAME cannot be used directly when 713 | VITE_DEV_SERVER_HOSTNAME is a ipv6 address or 714 | vite config `server.host` is true 715 | 716 | 3. fix(🐞): use vite config `mode` as default build 717 | mode to avoid build mode not equal to vite config `mode` when 718 | vite config `mode` !== 'development' which would lead to render env 719 | not equal to electron main or preload 720 | 721 | 4. fix(🐞): build electron output after render to avoid the electron 722 | output being deleted when the vite config emptyOutDir 723 | is `true` and the vite command is `build` 724 | 725 | 5. fix(🐞): use `closeBundle` to replace `writeBundle`, because in 726 | extreme cases, an error will be reported. For example, 727 | `can't find preload module` will occur as an error 728 | when `preload` update frequently 729 | 730 | ## [2022-07-31] v0.8.4 731 | 732 | - 🌱 c8b59ba Support `envFile` options [electron-vite-vue/issues/209](https://github.com/electron-vite/electron-vite-vue/issues/209) 733 | - 🌱 2d7f033 Add `ImportMeta['env']` declaration 734 | - 🌱 20d0a22 Must use `pnpm publish` instead of `npm publish` #43 735 | 736 | ## [2022-07-25] v0.8.2 737 | 738 | - 9ee720f chore(electron): remove `envFile: false` 739 | - 0016d52 fix(🐞): `normalizePath()` 740 | 741 | ## [2022-07-21] v0.8.1 742 | 743 | - 33b121a chore(deps): hoist `typescript` 744 | - d3bd37a chore(electron): change import path 745 | 746 | ## [2022-07-19] v0.8.0 747 | 748 | - 45f34d9 vite-plugin-electron@0.8.0 749 | - daa1c52 docs: `vite-plugin-electron@0.8.0` 750 | - e4e7ee0 chore: fix link 751 | - d1d4c82 chroe: bump deps 752 | - 581ef71 chore(deps): bump vite to 3.0.2 753 | - 481368a chore: remove unused def 754 | - 71d54c1 chore(🐞): update import path 755 | - 3bae8e5 refactor: `checkPkgMain.buildElectronMainPlugin()` 756 | - dba119d chore: format code 757 | - 739e659 feat: use `checkPkgMain()` 758 | - 192a8ca refactor: standlone `checkPkgMain` function 759 | - aaa9030 chore: explicitly specify external modules 760 | - 3eac722 chore: remove TODO 761 | - c03abe9 feat: add `checkPkgMain()` plugin 762 | - d8c0f8e chore: rename param 763 | - 04c94dd feat: `checkPkgMain()` 764 | - 7b9631a deps: add `rollup` 765 | - cec6db6 deps(🐞): add rollup for import `InputOption` 766 | - a4de86e monorepo: move `vite-plugin-electron` to `packages/electron` 767 | 768 | ## [2022-07-11] v0.7.5 769 | 770 | - 1ed6e19 chore: bump vite-plugin-electron-renderer to v0.5.7 771 | 772 | ## [2022-07-11] v0.7.4 773 | 774 | - c60fb17 chore: update params 775 | - 13f0c70 chore: bump vite-plugin-electron-renderer to 0.5.6 776 | 777 | ## [2022-07-10] v0.7.3 778 | 779 | - 2033256 chore: bump deps 780 | 781 | ## [2022-07-07] v0.7.2 782 | 783 | - 8fb064e fix(🐞): bump vite-plugin-electron-renderer to 0.5.3 784 | - 44006b2 chore: comments 785 | 786 | ## [2022-07-07] v0.7.1 787 | 788 | - 42acf37 docs: update 789 | - 6d878f7 refactor: use `resolveModules()` 790 | - acf1751 chore: bump deps 791 | 792 | ## [2022-07-06] v0.7.0 793 | 794 | - 661a146 docs: v0.7.0 795 | - 8ee091d feat: support restart on `vite.config.ts` changed #24 796 | - ca15795 add `electron-env.d.ts` 797 | - 76863bb electron-env.d.ts 798 | - 3fbef04 refactor: optimize code 799 | - cc3c8c0 feat: `resolveBuildConfig()` 800 | - 50c3afe feat: `resolveRuntime()` 801 | - 1184dd3 `node.js.ts` -> `config.ts` 802 | 803 | ## [2022-07-01] v0.6.2 804 | 805 | - 5779397 chore: bump vite-plugin-electron-renderer to 0.5.1 806 | - 2a2f77d docs: `Put Node.js packages in dependencies` 807 | 808 | ## [2022-06-26] v0.6.1 809 | 810 | - 5b736fa bump vite-plugin-electron-renderer to 0.5.0 811 | - f70e030 fix(🐞): ensure always load into `buildConfig()` 812 | - 73ae8f2 docs: update 813 | 814 | ## [2022-06-26] v0.6.0 815 | 816 | - 5204eca docs: v0.6.0 817 | - e885e54 feat: `withExternal()` 818 | - dee6d6a feat: `CommonConfiguration` 819 | - 55f9e11 node.js.ts 820 | - 83d0b8d remove README.zh-CN.md 821 | - 5f488b8 remove `polyfille-exports` 822 | - e118fbd remove `renderer` 823 | - 6e5761f refactor: integrate `vite-plugin-electron-renderer` 824 | - 0e696ec add `vite-plugin-electron-renderer` 825 | - 87bc5bb remove `vite-plugin-optimizer` 826 | 827 | ## [2022-06-25] v0.5.1 828 | 829 | - ccf6b29 [feat: make prefix-only core module as external module](https://github.com/electron-vite/vite-plugin-electron/pull/22) 830 | 831 | ## [2022-06-24] v0.5.0 832 | 833 | - 0c5155c chore: TODO 834 | - 892940e fix app,getName() return error name 835 | 836 | ## [2022-06-14] v0.4.9 837 | 838 | - abf460f chore: variable rename 839 | - ceb559f chore: `build.cssCodeSplit=false` 840 | 841 | ## [2022-06-08] v0.4.8 842 | 843 | - ab40088 fix(🐞): set `emptyOutDir=false` for prevent accidental deletion of files 844 | - 7baafa0 break: set `electron` `build.outDir` value is `dist/electron` by default 845 | - c13eb49 fix(🐞): assign default value `dist/electron/[process]` of `build.outDir` 846 | https://github.com/electron-vite/vite-plugin-electron/issues/10 847 | 848 | ## [2022-06-06] v0.4.7 849 | 850 | - 1346707 refactor: better assign default `build.outDir` 851 | - f489da1 chore: commnets 852 | - 6db3bf3 fix: check `output` is Array 853 | 854 | ## [2022-06-04] v0.4.6 855 | 856 | - 394cf6f feat: `config.build.emptyOutDir = false` by default 857 | - 4a67688 feat(🐞): enabled `polyfill-exports` by default 858 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 草鞋没号 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 |

4 |
5 |

vite-plugin-electron

6 |
7 |

Electron 🔗 Vite

8 |

9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |

19 |

20 | 21 | English 22 | | 23 | 简体中文 24 | 25 |

26 | 27 |
28 | 29 | In short, `vite-plugin-electron` makes developing Electron apps as easy as normal Vite projects. 30 | 31 | ## Features 32 | 33 | - [🔥 Hot Restart (Main process)](https://electron-vite.github.io/guide/features.html#hot-restart) 34 | - [🔄 Hot Reload (Preload scripts)](https://electron-vite.github.io/guide/features.html#hot-reload) 35 | - [⚡️ HMR (Renderer process)](https://electron-vite.github.io/guide/features.html#hmr) 36 | 37 | - 🌱 Fully compatible with Vite and Vite's ecosystem (Based on Vite) 38 | - 🔮 Full-featured [JavaScript API](https://github.com/electron-vite/vite-plugin-electron#javascript-api), really easy to integrate with complex projects. 39 | - 🐣 Few APIs, easy to use 40 | 41 | 42 | 43 | ## Quick Setup 44 | 45 | 1. Add the following dependency to your project 46 | 47 | ```sh 48 | npm i -D vite-plugin-electron 49 | ``` 50 | 51 | 2. Add `vite-plugin-electron` to the `plugins` section of `vite.config.ts` 52 | 53 | ```js 54 | import electron from 'vite-plugin-electron/simple' 55 | 56 | export default { 57 | plugins: [ 58 | electron({ 59 | main: { 60 | // Shortcut of `build.lib.entry` 61 | entry: 'electron/main.ts', 62 | }, 63 | preload: { 64 | // Shortcut of `build.rollupOptions.input` 65 | input: 'electron/preload.ts', 66 | }, 67 | // Optional: Use Node.js API in the Renderer process 68 | renderer: {}, 69 | }), 70 | ], 71 | } 72 | ``` 73 | 74 | 3. Create the `electron/main.ts` file and type the following code 75 | 76 | ```js 77 | import { app, BrowserWindow } from 'electron' 78 | 79 | app.whenReady().then(() => { 80 | const win = new BrowserWindow({ 81 | title: 'Main window', 82 | }) 83 | 84 | // You can use `process.env.VITE_DEV_SERVER_URL` when the vite command is called `serve` 85 | if (process.env.VITE_DEV_SERVER_URL) { 86 | win.loadURL(process.env.VITE_DEV_SERVER_URL) 87 | } else { 88 | // Load your file 89 | win.loadFile('dist/index.html'); 90 | } 91 | }) 92 | ``` 93 | 94 | 4. Add the `main` entry to `package.json` 95 | 96 | ```diff 97 | { 98 | + "main": "dist-electron/main.mjs" 99 | } 100 | ``` 101 | 102 | That's it! You can now use Electron in your Vite app ✨ 103 | 104 | ## Flat API 105 | 106 | In most cases, the `vite-plugin-electron/simple` API is recommended. If you know very well how this plugin works or you want to use `vite-plugin-electron` API as a secondary encapsulation of low-level API, then the flat API is more suitable for you. It is also simple but more flexible. :) 107 | 108 | The difference compared to the simple API is that it does not identify which entry represents `preload` and the adaptation to preload. 109 | 110 | ```js 111 | import electron from 'vite-plugin-electron' 112 | 113 | export default { 114 | plugins: [ 115 | electron({ 116 | entry: 'electron/main.ts', 117 | }), 118 | ], 119 | } 120 | ``` 121 | 122 | ## Flat API vs Simple API 123 | 124 | - Simple API is based on the Flat API 125 | - Simple API incluess some Preload scripts preset configs. 126 | - Flat API provides some more general APIs, which you can use for secondary encapsulation, such as [nuxt-electron](https://github.com/caoxiemeihao/nuxt-electron). 127 | 128 | ## Flat API (Define) 129 | 130 | `electron(options: ElectronOptions | ElectronOptions[])` 131 | 132 | ```ts 133 | export interface ElectronOptions { 134 | /** 135 | * Shortcut of `build.lib.entry` 136 | */ 137 | entry?: import('vite').LibraryOptions['entry'] 138 | vite?: import('vite').InlineConfig 139 | /** 140 | * Triggered when Vite is built every time -- `vite serve` command only. 141 | * 142 | * If this `onstart` is passed, Electron App will not start automatically. 143 | * However, you can start Electroo App via `startup` function. 144 | */ 145 | onstart?: (args: { 146 | /** 147 | * Electron App startup function. 148 | * It will mount the Electron App child-process to `process.electronApp`. 149 | * @param argv default value `['.', '--no-sandbox']` 150 | * @param options options for `child_process.spawn` 151 | * @param customElectronPkg custom electron package name (default: 'electron') 152 | */ 153 | startup: (argv?: string[], options?: import('node:child_process').SpawnOptions, customElectronPkg?: string) => Promise 154 | /** Reload Electron-Renderer */ 155 | reload: () => void 156 | }) => void | Promise 157 | } 158 | ``` 159 | 160 | ## Recommend Structure 161 | 162 | Let's use the official [template-vanilla-ts](https://github.com/vitejs/vite/tree/main/packages/create-vite/template-vanilla-ts) created based on `create vite` as an example 163 | 164 | ```diff 165 | + ├─┬ electron 166 | + │ └── main.ts 167 | ├─┬ src 168 | │ ├── main.ts 169 | │ ├── style.css 170 | │ └── vite-env.d.ts 171 | ├── .gitignore 172 | ├── favicon.svg 173 | ├── index.html 174 | ├── package.json 175 | ├── tsconfig.json 176 | + └── vite.config.ts 177 | ``` 178 | 179 | ## Built format 180 | 181 | This is just the default behavior, and you can modify them at any time through custom config in the `vite.config.js` 182 | 183 | ```log 184 | { "type": "module" } 185 | ┏————————————————————┳——————————┳———————————┓ 186 | │ built │ format │ suffix │ 187 | ┠————————————————————╂——————————╂———————————┨ 188 | │ main process │ esm │ .js │ 189 | ┠————————————————————╂——————————╂———————————┨ 190 | │ preload scripts │ cjs │ .mjs │ diff 191 | ┠————————————————————╂——————————╂———————————┨ 192 | │ renderer process │ - │ .js │ 193 | ┗————————————————————┸——————————┸———————————┛ 194 | 195 | { "type": "commonjs" } - default 196 | ┏————————————————————┳——————————┳———————————┓ 197 | │ built │ format │ suffix │ 198 | ┠————————————————————╂——————————╂———————————┨ 199 | │ main process │ cjs │ .js │ 200 | ┠————————————————————╂——————————╂———————————┨ 201 | │ preload scripts │ cjs │ .js │ diff 202 | ┠————————————————————╂——————————╂———————————┨ 203 | │ renderer process │ - │ .js │ 204 | ┗————————————————————┸——————————┸———————————┛ 205 | ``` 206 | 207 | ## Examples 208 | 209 | There are many cases here 👉 [electron-vite-samples](https://github.com/caoxiemeihao/electron-vite-samples) 210 | 211 | ## JavaScript API 212 | 213 | `vite-plugin-electron`'s JavaScript APIs are fully typed, and it's recommended to use TypeScript or enable JS type checking in VS Code to leverage the intellisense and validation. 214 | 215 | - `ElectronOptions` - type 216 | - `resolveViteConfig` - function, Resolve the default Vite's `InlineConfig` for build Electron-Main 217 | - `withExternalBuiltins` - function 218 | - `build` - function 219 | - `startup` - function 220 | 221 | **Example** 222 | 223 | ```js 224 | import { build, startup } from 'vite-plugin-electron' 225 | 226 | const isDev = process.env.NODE_ENV === 'development' 227 | const isProd = process.env.NODE_ENV === 'production' 228 | 229 | build({ 230 | entry: 'electron/main.ts', 231 | vite: { 232 | mode: process.env.NODE_ENV, 233 | build: { 234 | minify: isProd, 235 | watch: isDev ? {} : null, 236 | }, 237 | plugins: [{ 238 | name: 'plugin-start-electron', 239 | closeBundle() { 240 | if (isDev) { 241 | // Startup Electron App 242 | startup() 243 | } 244 | }, 245 | }], 246 | }, 247 | }) 248 | ``` 249 | 250 | **Hot Reload** 251 | 252 | Since `v0.29.0`, when preload scripts are rebuilt, they will send an `electron-vite&type=hot-reload` event to the main process. 253 | If your App doesn't need a renderer process, this will give you **hot-reload**. 254 | 255 | ```js 256 | // electron/main.ts 257 | 258 | process.on('message', (msg) => { 259 | if (msg === 'electron-vite&type=hot-reload') { 260 | for (const win of BrowserWindow.getAllWindows()) { 261 | // Hot reload preload scripts 262 | win.webContents.reload() 263 | } 264 | } 265 | }) 266 | ``` 267 | 268 | ## How to work 269 | 270 | It just executes the `electron .` command in the Vite build completion hook and then starts or restarts the Electron App. 271 | 272 | ## Be aware 273 | 274 | - 🚨 By default, the files in `electron` folder will be built into the `dist-electron` 275 | - 🚨 Currently, `"type": "module"` is not supported in Electron 276 | 277 | ## C/C++ Native 278 | 279 | We have two ways to use C/C++ native modules 280 | 281 | **First way** 282 | 283 | In general, Vite may not correctly build Node.js packages, especially C/C++ native modules, but Vite can load them as external packages 284 | 285 | So, put your Node.js package in `dependencies`. Unless you know how to properly build them with Vite 286 | 287 | ```js 288 | export default { 289 | plugins: [ 290 | electron({ 291 | entry: 'electron/main.ts', 292 | vite: { 293 | build: { 294 | rollupOptions: { 295 | // Here are some C/C++ modules them can't be built properly 296 | external: [ 297 | 'serialport', 298 | 'sqlite3', 299 | ], 300 | }, 301 | }, 302 | }, 303 | }), 304 | ], 305 | } 306 | ``` 307 | 308 | **Second way** 309 | 310 | Use 👉 [vite-plugin-native](https://github.com/vite-plugin/vite-plugin-native) 311 | 312 | ```js 313 | import native from 'vite-plugin-native' 314 | 315 | export default { 316 | plugins: [ 317 | electron({ 318 | entry: 'electron/main.ts', 319 | vite: { 320 | plugins: [ 321 | native(/* options */), 322 | ], 323 | }, 324 | }), 325 | ], 326 | } 327 | ``` 328 | 329 | 330 | 331 |