@thatkawaiisam/electron-vite 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +105 -0
- package/bin/electron-bytecode.cjs +32 -0
- package/bin/electron-vite.js +30 -0
- package/dist/chunks/lib-6iOrRH4E.js +38 -0
- package/dist/chunks/lib-B1EDBNjL.js +110 -0
- package/dist/chunks/lib-CKGCTpsN.js +25 -0
- package/dist/chunks/lib-D8_qgfjp.js +1814 -0
- package/dist/cli.js +119 -0
- package/dist/index.d.ts +175 -0
- package/dist/index.js +108 -0
- package/node.d.ts +112 -0
- package/package.json +110 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022, Alex Wei
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://alex8088.github.io/assets/electron-vite.svg" width="150px" height="150px">
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<div align="center">
|
|
6
|
+
<h1>electron-vite</h1>
|
|
7
|
+
</div>
|
|
8
|
+
<p align="center">Next generation Electron build tooling based on Vite</p>
|
|
9
|
+
|
|
10
|
+
<p align="center">
|
|
11
|
+
<img src="https://img.shields.io/npm/v/electron-vite?color=6988e6&label=version">
|
|
12
|
+
<img src="https://img.shields.io/github/license/alex8088/electron-vite?color=blue" alt="license" />
|
|
13
|
+
</p>
|
|
14
|
+
|
|
15
|
+
<p align="center">
|
|
16
|
+
<a href="https://electron-vite.org">Documentation</a> |
|
|
17
|
+
<a href="https://electron-vite.org/guide">Getting Started</a> |
|
|
18
|
+
<a href="https://github.com/alex8088/quick-start/tree/master/packages/create-electron">create-electron</a>
|
|
19
|
+
</p>
|
|
20
|
+
|
|
21
|
+
<p align="center">
|
|
22
|
+
<a href="https://cn.electron-vite.org">中文文档</a>
|
|
23
|
+
</p>
|
|
24
|
+
|
|
25
|
+
<br />
|
|
26
|
+
<br />
|
|
27
|
+
|
|
28
|
+
## Features
|
|
29
|
+
|
|
30
|
+
- ⚡️ [Vite](https://vitejs.dev) powered and use the same way.
|
|
31
|
+
- 🛠 Pre-configure with sensible defaults optimized for Electron.
|
|
32
|
+
- 💡 Optimize asset handling for Electron main process.
|
|
33
|
+
- 🚀 Fast HMR & hot reloading.
|
|
34
|
+
- 🔥 Isolated build for multi-entry application development.
|
|
35
|
+
- ✨ Simplify multi-threading development.
|
|
36
|
+
- 🔒 Compile code to v8 bytecode to protect source code.
|
|
37
|
+
- 🔌 Easy to debug in IDEs such as VSCode or WebStorm.
|
|
38
|
+
- 📦 Out-of-the-box support for TypeScript, Vue, React, Svelte, SolidJS and more.
|
|
39
|
+
|
|
40
|
+
## Usage
|
|
41
|
+
|
|
42
|
+
### Install
|
|
43
|
+
|
|
44
|
+
```sh
|
|
45
|
+
npm i electron-vite -D
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Development & Build
|
|
49
|
+
|
|
50
|
+
In a project where `electron-vite` is installed, you can use `electron-vite` binary directly with `npx electron-vite` or add the npm scripts to your `package.json` file like this:
|
|
51
|
+
|
|
52
|
+
```json
|
|
53
|
+
{
|
|
54
|
+
"scripts": {
|
|
55
|
+
"start": "electron-vite preview",
|
|
56
|
+
"dev": "electron-vite dev",
|
|
57
|
+
"prebuild": "electron-vite build"
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Configuration
|
|
63
|
+
|
|
64
|
+
When running `electron-vite` from the command line, electron-vite will automatically try to resolve a config file named `electron.vite.config.js` inside project root. The most basic config file looks like this:
|
|
65
|
+
|
|
66
|
+
```js
|
|
67
|
+
// electron.vite.config.js
|
|
68
|
+
export default {
|
|
69
|
+
main: {
|
|
70
|
+
// vite config options
|
|
71
|
+
},
|
|
72
|
+
preload: {
|
|
73
|
+
// vite config options
|
|
74
|
+
},
|
|
75
|
+
renderer: {
|
|
76
|
+
// vite config options
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Getting Started
|
|
82
|
+
|
|
83
|
+
Clone the [electron-vite-boilerplate](https://github.com/alex8088/electron-vite-boilerplate) or use the [create-electron](https://github.com/alex8088/quick-start/tree/master/packages/create-electron) tool to scaffold your project.
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
npm create @quick-start/electron@latest
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Currently supported template presets include:
|
|
90
|
+
|
|
91
|
+
| JavaScript | TypeScript |
|
|
92
|
+
| :--------------------------------------------------------------------------------------------------------: | :--------------------------------------------------------------------------------------------------------------: |
|
|
93
|
+
| [vanilla](https://github.com/alex8088/quick-start/tree/master/packages/create-electron/playground/vanilla) | [vanilla-ts](https://github.com/alex8088/quick-start/tree/master/packages/create-electron/playground/vanilla-ts) |
|
|
94
|
+
| [vue](https://github.com/alex8088/quick-start/tree/master/packages/create-electron/playground/vue) | [vue-ts](https://github.com/alex8088/quick-start/tree/master/packages/create-electron/playground/vue-ts) |
|
|
95
|
+
| [react](https://github.com/alex8088/quick-start/tree/master/packages/create-electron/playground/react) | [react-ts](https://github.com/alex8088/quick-start/tree/master/packages/create-electron/playground/react-ts) |
|
|
96
|
+
| [svelte](https://github.com/alex8088/quick-start/tree/master/packages/create-electron/playground/svelte) | [svelte-ts](https://github.com/alex8088/quick-start/tree/master/packages/create-electron/playground/svelte-ts) |
|
|
97
|
+
| [solid](https://github.com/alex8088/quick-start/tree/master/packages/create-electron/playground/solid) | [solid-ts](https://github.com/alex8088/quick-start/tree/master/packages/create-electron/playground/solid-ts) |
|
|
98
|
+
|
|
99
|
+
## Contribution
|
|
100
|
+
|
|
101
|
+
See [Contributing Guide](CONTRIBUTING.md).
|
|
102
|
+
|
|
103
|
+
## License
|
|
104
|
+
|
|
105
|
+
[MIT](./LICENSE) © alex.wei
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const vm = require('vm')
|
|
2
|
+
const v8 = require('v8')
|
|
3
|
+
const wrap = require('module').wrap
|
|
4
|
+
|
|
5
|
+
v8.setFlagsFromString('--no-lazy')
|
|
6
|
+
v8.setFlagsFromString('--no-flush-bytecode')
|
|
7
|
+
|
|
8
|
+
let code = ''
|
|
9
|
+
|
|
10
|
+
process.stdin.setEncoding('utf-8')
|
|
11
|
+
|
|
12
|
+
process.stdin.on('readable', () => {
|
|
13
|
+
const data = process.stdin.read()
|
|
14
|
+
if (data !== null) {
|
|
15
|
+
code += data
|
|
16
|
+
}
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
process.stdin.on('end', () => {
|
|
20
|
+
try {
|
|
21
|
+
if (typeof code !== 'string') {
|
|
22
|
+
throw new Error(`javascript code must be string. ${typeof code} was given.`)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const script = new vm.Script(wrap(code), { produceCachedData: true })
|
|
26
|
+
const bytecodeBuffer = script.createCachedData()
|
|
27
|
+
|
|
28
|
+
process.stdout.write(bytecodeBuffer)
|
|
29
|
+
} catch (error) {
|
|
30
|
+
console.error(error)
|
|
31
|
+
}
|
|
32
|
+
})
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const debugIndex = process.argv.findIndex(arg => /^(?:-d|--debug)$/.test(arg))
|
|
4
|
+
const filterIndex = process.argv.findIndex(arg => /^(?:-f|--filter)$/.test(arg))
|
|
5
|
+
|
|
6
|
+
if (debugIndex > 0) {
|
|
7
|
+
let value = process.argv[debugIndex + 1]
|
|
8
|
+
if (!value || value.startsWith('-')) {
|
|
9
|
+
value = 'vite:*'
|
|
10
|
+
} else {
|
|
11
|
+
value = value
|
|
12
|
+
.split(',')
|
|
13
|
+
.map(v => `vite:${v}`)
|
|
14
|
+
.join(',')
|
|
15
|
+
}
|
|
16
|
+
process.env.DEBUG = `${process.env.DEBUG ? process.env.DEBUG + ',' : ''}${value}`
|
|
17
|
+
|
|
18
|
+
if (filterIndex > 0) {
|
|
19
|
+
const filter = process.argv[filterIndex + 1]
|
|
20
|
+
if (filter && !filter.startsWith('-')) {
|
|
21
|
+
process.env.VITE_DEBUG_FILTER = filter
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function run() {
|
|
27
|
+
import('../dist/cli.js')
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
run()
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { build as build$1 } from 'vite';
|
|
2
|
+
import { r as resolveConfig } from './lib-D8_qgfjp.js';
|
|
3
|
+
import 'node:path';
|
|
4
|
+
import 'node:fs';
|
|
5
|
+
import 'node:url';
|
|
6
|
+
import 'node:module';
|
|
7
|
+
import 'picocolors';
|
|
8
|
+
import 'esbuild';
|
|
9
|
+
import 'node:child_process';
|
|
10
|
+
import 'node:crypto';
|
|
11
|
+
import 'node:fs/promises';
|
|
12
|
+
import 'magic-string';
|
|
13
|
+
import '@babel/core';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Bundles the electron app for production.
|
|
17
|
+
*/
|
|
18
|
+
async function build(inlineConfig = {}) {
|
|
19
|
+
process.env.NODE_ENV_ELECTRON_VITE = 'production';
|
|
20
|
+
const config = await resolveConfig(inlineConfig, 'build', 'production');
|
|
21
|
+
if (!config.config) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
// Build targets in order: main -> preload -> renderer
|
|
25
|
+
const buildTargets = ['main', 'preload', 'renderer'];
|
|
26
|
+
for (const target of buildTargets) {
|
|
27
|
+
const viteConfig = config.config[target];
|
|
28
|
+
if (viteConfig) {
|
|
29
|
+
// Disable watch mode in production builds
|
|
30
|
+
if (viteConfig.build?.watch) {
|
|
31
|
+
viteConfig.build.watch = null;
|
|
32
|
+
}
|
|
33
|
+
await build$1(viteConfig);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export { build };
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { createLogger, createServer as createServer$1, mergeConfig, build } from 'vite';
|
|
2
|
+
import colors from 'picocolors';
|
|
3
|
+
import { r as resolveConfig, c as resolveHostname, s as startElectron } from './lib-D8_qgfjp.js';
|
|
4
|
+
import 'node:path';
|
|
5
|
+
import 'node:fs';
|
|
6
|
+
import 'node:url';
|
|
7
|
+
import 'node:module';
|
|
8
|
+
import 'esbuild';
|
|
9
|
+
import 'node:child_process';
|
|
10
|
+
import 'node:crypto';
|
|
11
|
+
import 'node:fs/promises';
|
|
12
|
+
import 'magic-string';
|
|
13
|
+
import '@babel/core';
|
|
14
|
+
|
|
15
|
+
async function createServer(inlineConfig = {}, options) {
|
|
16
|
+
process.env.NODE_ENV_ELECTRON_VITE = 'development';
|
|
17
|
+
const config = await resolveConfig(inlineConfig, 'serve', 'development');
|
|
18
|
+
if (config.config) {
|
|
19
|
+
const logger = createLogger(inlineConfig.logLevel);
|
|
20
|
+
let server;
|
|
21
|
+
let ps;
|
|
22
|
+
const errorHook = (e) => {
|
|
23
|
+
logger.error(`${colors.bgRed(colors.white(' ERROR '))} ${colors.red(e.message)}`);
|
|
24
|
+
};
|
|
25
|
+
const mainViteConfig = config.config?.main;
|
|
26
|
+
if (mainViteConfig && !options.rendererOnly) {
|
|
27
|
+
const watchHook = () => {
|
|
28
|
+
logger.info(colors.green(`\nelectron main process rebuilt successfully`));
|
|
29
|
+
if (ps) {
|
|
30
|
+
ps.removeAllListeners();
|
|
31
|
+
ps.kill();
|
|
32
|
+
ps = startElectron(inlineConfig.root);
|
|
33
|
+
logger.info(colors.green(`\nrestarting electron app...\n`));
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
await doBuild(mainViteConfig, watchHook, errorHook);
|
|
37
|
+
logger.info(colors.green(`\nelectron main process built successfully`));
|
|
38
|
+
}
|
|
39
|
+
const preloadViteConfig = config.config?.preload;
|
|
40
|
+
if (preloadViteConfig && !options.rendererOnly) {
|
|
41
|
+
logger.info(colors.gray(`\n-----\n`));
|
|
42
|
+
const watchHook = () => {
|
|
43
|
+
logger.info(colors.green(`\nelectron preload scripts rebuilt successfully`));
|
|
44
|
+
if (server) {
|
|
45
|
+
logger.info(colors.cyan(`\nreloading electron renderer...\n`));
|
|
46
|
+
server.ws.send({ type: 'full-reload' });
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
await doBuild(preloadViteConfig, watchHook, errorHook);
|
|
50
|
+
logger.info(colors.green(`\nelectron preload scripts built successfully`));
|
|
51
|
+
}
|
|
52
|
+
if (options.rendererOnly) {
|
|
53
|
+
logger.warn(`\n${colors.yellow(colors.bold('(!)'))} ${colors.yellow('skipped building main process and preload scripts (using previous build)')}`);
|
|
54
|
+
}
|
|
55
|
+
const rendererViteConfig = config.config?.renderer;
|
|
56
|
+
if (rendererViteConfig) {
|
|
57
|
+
logger.info(colors.gray(`\n-----\n`));
|
|
58
|
+
server = await createServer$1(rendererViteConfig);
|
|
59
|
+
if (!server.httpServer) {
|
|
60
|
+
throw new Error('HTTP server not available');
|
|
61
|
+
}
|
|
62
|
+
await server.listen();
|
|
63
|
+
const conf = server.config.server;
|
|
64
|
+
const protocol = conf.https ? 'https:' : 'http:';
|
|
65
|
+
const host = resolveHostname(conf.host);
|
|
66
|
+
const port = conf.port;
|
|
67
|
+
process.env.ELECTRON_RENDERER_URL = `${protocol}//${host}:${port}`;
|
|
68
|
+
const slogger = server.config.logger;
|
|
69
|
+
slogger.info(colors.green(`dev server running for the electron renderer process at:\n`), {
|
|
70
|
+
clear: !slogger.hasWarned && !options.rendererOnly
|
|
71
|
+
});
|
|
72
|
+
server.printUrls();
|
|
73
|
+
}
|
|
74
|
+
ps = startElectron(inlineConfig.root);
|
|
75
|
+
logger.info(colors.green(`\nstarting electron app...\n`));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
async function doBuild(config, watchHook, errorHook) {
|
|
79
|
+
return new Promise(resolve => {
|
|
80
|
+
if (config.build?.watch) {
|
|
81
|
+
let firstBundle = true;
|
|
82
|
+
const closeBundle = () => {
|
|
83
|
+
if (firstBundle) {
|
|
84
|
+
firstBundle = false;
|
|
85
|
+
resolve();
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
watchHook();
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
config = mergeConfig(config, {
|
|
92
|
+
plugins: [
|
|
93
|
+
{
|
|
94
|
+
name: 'vite:electron-watcher',
|
|
95
|
+
closeBundle
|
|
96
|
+
}
|
|
97
|
+
]
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
build(config)
|
|
101
|
+
.then(() => {
|
|
102
|
+
if (!config.build?.watch) {
|
|
103
|
+
resolve();
|
|
104
|
+
}
|
|
105
|
+
})
|
|
106
|
+
.catch(e => errorHook(e));
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export { createServer };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import colors from 'picocolors';
|
|
2
|
+
import { createLogger } from 'vite';
|
|
3
|
+
import { s as startElectron } from './lib-D8_qgfjp.js';
|
|
4
|
+
import { build } from './lib-6iOrRH4E.js';
|
|
5
|
+
import 'node:path';
|
|
6
|
+
import 'node:fs';
|
|
7
|
+
import 'node:url';
|
|
8
|
+
import 'node:module';
|
|
9
|
+
import 'esbuild';
|
|
10
|
+
import 'node:child_process';
|
|
11
|
+
import 'node:crypto';
|
|
12
|
+
import 'node:fs/promises';
|
|
13
|
+
import 'magic-string';
|
|
14
|
+
import '@babel/core';
|
|
15
|
+
|
|
16
|
+
async function preview(inlineConfig = {}, options) {
|
|
17
|
+
if (!options.skipBuild) {
|
|
18
|
+
await build(inlineConfig);
|
|
19
|
+
}
|
|
20
|
+
const logger = createLogger(inlineConfig.logLevel);
|
|
21
|
+
startElectron(inlineConfig.root);
|
|
22
|
+
logger.info(colors.green(`\nstarting electron app...\n`));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { preview };
|