@tomjs/vite-plugin-electron 1.0.1 → 1.1.1
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/README.md +38 -1
- package/README.zh_CN.md +38 -1
- package/dist/index.js +23 -16
- package/dist/index.mjs +23 -16
- package/env.d.ts +17 -0
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
|
|
11
11
|
- Fast build `main` and `preload` with [tsup](https://github.com/egoist/tsup)
|
|
12
12
|
- Little configuration, focus on business
|
|
13
|
+
- Support `main`'s `Hot Restart`
|
|
14
|
+
- Support `preload`'s `Hot Reload`
|
|
13
15
|
- Support `esm` and `cjs`, you can use `esm` in [electron v28+](https://www.electronjs.org/blog/electron-28-0)
|
|
14
16
|
- Support `vue` and `react` and [other frameworks](https://vitejs.dev/guide/#trying-vite-online)
|
|
15
17
|
|
|
@@ -64,7 +66,42 @@ npm i @tomjs/vite-plugin-electron --save-dev
|
|
|
64
66
|
| | |--index.html
|
|
65
67
|
```
|
|
66
68
|
|
|
67
|
-
|
|
69
|
+
### electron
|
|
70
|
+
|
|
71
|
+
`electron/main/index.ts`
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
import { dirname } from 'node:path';
|
|
75
|
+
import { fileURLToPath } from 'node:url';
|
|
76
|
+
import { app, BrowserWindow } from 'electron';
|
|
77
|
+
|
|
78
|
+
// when package.json "type": module"
|
|
79
|
+
global.__dirname = dirname(fileURLToPath(import.meta.url));
|
|
80
|
+
|
|
81
|
+
const preload = join(__dirname, '../preload/index.mjs');
|
|
82
|
+
const url = process.env.APP_DEV_SERVER_URL;
|
|
83
|
+
|
|
84
|
+
async function createWindow() {
|
|
85
|
+
win = new BrowserWindow({
|
|
86
|
+
title: 'Main window',
|
|
87
|
+
width: 800,
|
|
88
|
+
height: 700,
|
|
89
|
+
webPreferences: {
|
|
90
|
+
preload,
|
|
91
|
+
nodeIntegration: true,
|
|
92
|
+
contextIsolation: false,
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
if (isDev) {
|
|
97
|
+
win.loadURL(url);
|
|
98
|
+
} else {
|
|
99
|
+
win.loadFile(indexHtml);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
app.whenReady().then(createWindow);
|
|
104
|
+
```
|
|
68
105
|
|
|
69
106
|
### vue
|
|
70
107
|
|
package/README.zh_CN.md
CHANGED
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
|
|
11
11
|
- 使用 [tsup](https://github.com/egoist/tsup) 快速构建 `main` 和 `preload`
|
|
12
12
|
- 配置简单,专注业务
|
|
13
|
+
- 支持 `main` 的 `热重启`
|
|
14
|
+
- 支持 `preload` 的 `热重载`
|
|
13
15
|
- 支持 `esm` 和 `cjs` ,你可以在 [electron v28+](https://www.electronjs.org/zh/blog/electron-28-0) 中使用 `esm`
|
|
14
16
|
- 支持 `vue` 和 `react` 以及 [其他框架](https://cn.vitejs.dev/guide/#trying-vite-online)
|
|
15
17
|
|
|
@@ -64,7 +66,42 @@ npm i @tomjs/vite-plugin-electron --save-dev
|
|
|
64
66
|
| | |--index.html
|
|
65
67
|
```
|
|
66
68
|
|
|
67
|
-
|
|
69
|
+
### electron
|
|
70
|
+
|
|
71
|
+
`electron/main/index.ts`
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
import { dirname } from 'node:path';
|
|
75
|
+
import { fileURLToPath } from 'node:url';
|
|
76
|
+
import { app, BrowserWindow } from 'electron';
|
|
77
|
+
|
|
78
|
+
// when package.json "type": module"
|
|
79
|
+
global.__dirname = dirname(fileURLToPath(import.meta.url));
|
|
80
|
+
|
|
81
|
+
const preload = join(__dirname, '../preload/index.mjs');
|
|
82
|
+
const url = process.env.APP_DEV_SERVER_URL;
|
|
83
|
+
|
|
84
|
+
async function createWindow() {
|
|
85
|
+
win = new BrowserWindow({
|
|
86
|
+
title: 'Main window',
|
|
87
|
+
width: 800,
|
|
88
|
+
height: 700,
|
|
89
|
+
webPreferences: {
|
|
90
|
+
preload,
|
|
91
|
+
nodeIntegration: true,
|
|
92
|
+
contextIsolation: false,
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
if (isDev) {
|
|
97
|
+
win.loadURL(url);
|
|
98
|
+
} else {
|
|
99
|
+
win.loadFile(indexHtml);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
app.whenReady().then(createWindow);
|
|
104
|
+
```
|
|
68
105
|
|
|
69
106
|
### vue
|
|
70
107
|
|
package/dist/index.js
CHANGED
|
@@ -73,7 +73,10 @@ function getBuildOptions(options, innerOpts) {
|
|
|
73
73
|
delete env[key];
|
|
74
74
|
}
|
|
75
75
|
});
|
|
76
|
-
return ["main", "preload"].filter((s) => options[s] && options[s].entry).map((s) =>
|
|
76
|
+
return ["main", "preload"].filter((s) => options[s] && options[s].entry).map((s) => {
|
|
77
|
+
options[s].__NAME__ = s;
|
|
78
|
+
return options[s];
|
|
79
|
+
}).map((cfg) => {
|
|
77
80
|
return {
|
|
78
81
|
...cfg,
|
|
79
82
|
env,
|
|
@@ -93,40 +96,45 @@ function runMainProcess(options, innerOpts) {
|
|
|
93
96
|
stdio: "inherit"
|
|
94
97
|
}).on("exit", exitMainProcess);
|
|
95
98
|
}
|
|
96
|
-
async function runServe(options, innerOpts) {
|
|
97
|
-
let
|
|
99
|
+
async function runServe(options, innerOpts, server) {
|
|
100
|
+
let mainProcess;
|
|
98
101
|
const killProcess = () => {
|
|
99
|
-
if (
|
|
100
|
-
|
|
101
|
-
|
|
102
|
+
if (mainProcess) {
|
|
103
|
+
mainProcess.off("exit", exitMainProcess);
|
|
104
|
+
mainProcess.kill();
|
|
102
105
|
}
|
|
103
106
|
};
|
|
104
107
|
process.on("exit", () => {
|
|
105
108
|
killProcess();
|
|
106
109
|
});
|
|
107
|
-
logger.info(`electron server start`);
|
|
108
110
|
const buildOptions = getBuildOptions(options, innerOpts);
|
|
109
111
|
for (let i = 0; i < buildOptions.length; i++) {
|
|
110
112
|
let isFirstBuild = true;
|
|
111
113
|
const tsOpts = buildOptions[i];
|
|
112
|
-
const { onSuccess: _onSuccess, watch, ...tsupOptions } = tsOpts;
|
|
113
|
-
logger.info(
|
|
114
|
+
const { __NAME__: name, onSuccess: _onSuccess, watch, ...tsupOptions } = tsOpts;
|
|
115
|
+
logger.info(`${name} build`);
|
|
114
116
|
const onSuccess = async () => {
|
|
115
|
-
logger.info(`electron ${tsOpts.name} process build success`);
|
|
116
117
|
if (typeof _onSuccess === "function") {
|
|
117
118
|
await _onSuccess();
|
|
118
119
|
}
|
|
119
|
-
logger.success("rebuild succeeded!");
|
|
120
120
|
if (isFirstBuild) {
|
|
121
|
+
logger.info(`${name} build succeeded`);
|
|
121
122
|
isFirstBuild = false;
|
|
122
123
|
return;
|
|
123
124
|
}
|
|
124
|
-
|
|
125
|
-
|
|
125
|
+
logger.success(`${name} rebuild succeeded!`);
|
|
126
|
+
if (name === "main") {
|
|
127
|
+
killProcess();
|
|
128
|
+
mainProcess = runMainProcess(options, innerOpts);
|
|
129
|
+
} else {
|
|
130
|
+
server.ws.send({
|
|
131
|
+
type: "full-reload"
|
|
132
|
+
});
|
|
133
|
+
}
|
|
126
134
|
};
|
|
127
135
|
await _tsup.build.call(void 0, { onSuccess, watch: true, ...tsupOptions });
|
|
128
136
|
}
|
|
129
|
-
|
|
137
|
+
mainProcess = runMainProcess(options, innerOpts);
|
|
130
138
|
return {
|
|
131
139
|
kill: killProcess
|
|
132
140
|
};
|
|
@@ -224,7 +232,6 @@ function vitePluginElectron(options) {
|
|
|
224
232
|
if (isDev) {
|
|
225
233
|
opts.main.sourcemap = _nullishCoalesce(opts.main.sourcemap, () => ( true));
|
|
226
234
|
opts.preload.sourcemap = _nullishCoalesce(opts.main.sourcemap, () => ( true));
|
|
227
|
-
opts.inspect = _nullishCoalesce(opts.inspect, () => ( true));
|
|
228
235
|
} else {
|
|
229
236
|
opts.main.minify = _nullishCoalesce(opts.main.minify, () => ( true));
|
|
230
237
|
opts.preload.minify = _nullishCoalesce(opts.preload.minify, () => ( true));
|
|
@@ -251,7 +258,7 @@ function vitePluginElectron(options) {
|
|
|
251
258
|
}
|
|
252
259
|
}
|
|
253
260
|
(_a = process.__tomjs_electron_serve__) == null ? void 0 : _a.kill();
|
|
254
|
-
process.__tomjs_electron_serve__ = await runServe(opts, innerOpts);
|
|
261
|
+
process.__tomjs_electron_serve__ = await runServe(opts, innerOpts, server);
|
|
255
262
|
});
|
|
256
263
|
},
|
|
257
264
|
async closeBundle() {
|
package/dist/index.mjs
CHANGED
|
@@ -73,7 +73,10 @@ function getBuildOptions(options, innerOpts) {
|
|
|
73
73
|
delete env[key];
|
|
74
74
|
}
|
|
75
75
|
});
|
|
76
|
-
return ["main", "preload"].filter((s) => options[s] && options[s].entry).map((s) =>
|
|
76
|
+
return ["main", "preload"].filter((s) => options[s] && options[s].entry).map((s) => {
|
|
77
|
+
options[s].__NAME__ = s;
|
|
78
|
+
return options[s];
|
|
79
|
+
}).map((cfg) => {
|
|
77
80
|
return {
|
|
78
81
|
...cfg,
|
|
79
82
|
env,
|
|
@@ -93,40 +96,45 @@ function runMainProcess(options, innerOpts) {
|
|
|
93
96
|
stdio: "inherit"
|
|
94
97
|
}).on("exit", exitMainProcess);
|
|
95
98
|
}
|
|
96
|
-
async function runServe(options, innerOpts) {
|
|
97
|
-
let
|
|
99
|
+
async function runServe(options, innerOpts, server) {
|
|
100
|
+
let mainProcess;
|
|
98
101
|
const killProcess = () => {
|
|
99
|
-
if (
|
|
100
|
-
|
|
101
|
-
|
|
102
|
+
if (mainProcess) {
|
|
103
|
+
mainProcess.off("exit", exitMainProcess);
|
|
104
|
+
mainProcess.kill();
|
|
102
105
|
}
|
|
103
106
|
};
|
|
104
107
|
process.on("exit", () => {
|
|
105
108
|
killProcess();
|
|
106
109
|
});
|
|
107
|
-
logger.info(`electron server start`);
|
|
108
110
|
const buildOptions = getBuildOptions(options, innerOpts);
|
|
109
111
|
for (let i = 0; i < buildOptions.length; i++) {
|
|
110
112
|
let isFirstBuild = true;
|
|
111
113
|
const tsOpts = buildOptions[i];
|
|
112
|
-
const { onSuccess: _onSuccess, watch, ...tsupOptions } = tsOpts;
|
|
113
|
-
logger.info(
|
|
114
|
+
const { __NAME__: name, onSuccess: _onSuccess, watch, ...tsupOptions } = tsOpts;
|
|
115
|
+
logger.info(`${name} build`);
|
|
114
116
|
const onSuccess = async () => {
|
|
115
|
-
logger.info(`electron ${tsOpts.name} process build success`);
|
|
116
117
|
if (typeof _onSuccess === "function") {
|
|
117
118
|
await _onSuccess();
|
|
118
119
|
}
|
|
119
|
-
logger.success("rebuild succeeded!");
|
|
120
120
|
if (isFirstBuild) {
|
|
121
|
+
logger.info(`${name} build succeeded`);
|
|
121
122
|
isFirstBuild = false;
|
|
122
123
|
return;
|
|
123
124
|
}
|
|
124
|
-
|
|
125
|
-
|
|
125
|
+
logger.success(`${name} rebuild succeeded!`);
|
|
126
|
+
if (name === "main") {
|
|
127
|
+
killProcess();
|
|
128
|
+
mainProcess = runMainProcess(options, innerOpts);
|
|
129
|
+
} else {
|
|
130
|
+
server.ws.send({
|
|
131
|
+
type: "full-reload"
|
|
132
|
+
});
|
|
133
|
+
}
|
|
126
134
|
};
|
|
127
135
|
await tsupBuild({ onSuccess, watch: true, ...tsupOptions });
|
|
128
136
|
}
|
|
129
|
-
|
|
137
|
+
mainProcess = runMainProcess(options, innerOpts);
|
|
130
138
|
return {
|
|
131
139
|
kill: killProcess
|
|
132
140
|
};
|
|
@@ -223,7 +231,6 @@ function vitePluginElectron(options) {
|
|
|
223
231
|
if (isDev) {
|
|
224
232
|
opts.main.sourcemap = opts.main.sourcemap ?? true;
|
|
225
233
|
opts.preload.sourcemap = opts.main.sourcemap ?? true;
|
|
226
|
-
opts.inspect = opts.inspect ?? true;
|
|
227
234
|
} else {
|
|
228
235
|
opts.main.minify = opts.main.minify ?? true;
|
|
229
236
|
opts.preload.minify = opts.preload.minify ?? true;
|
|
@@ -250,7 +257,7 @@ function vitePluginElectron(options) {
|
|
|
250
257
|
}
|
|
251
258
|
}
|
|
252
259
|
(_a = process.__tomjs_electron_serve__) == null ? void 0 : _a.kill();
|
|
253
|
-
process.__tomjs_electron_serve__ = await runServe(opts, innerOpts);
|
|
260
|
+
process.__tomjs_electron_serve__ = await runServe(opts, innerOpts, server);
|
|
254
261
|
});
|
|
255
262
|
},
|
|
256
263
|
async closeBundle() {
|
package/env.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* fix code hint
|
|
3
|
+
*/
|
|
4
|
+
type UnionType<T> = T | (string & {});
|
|
5
|
+
|
|
6
|
+
declare namespace NodeJS {
|
|
7
|
+
interface ProcessEnv {
|
|
8
|
+
/**
|
|
9
|
+
* Node.js environment
|
|
10
|
+
*/
|
|
11
|
+
NODE_ENV: UnionType<'development' | 'test' | 'production'>;
|
|
12
|
+
/**
|
|
13
|
+
* The url of the dev server.
|
|
14
|
+
*/
|
|
15
|
+
readonly APP_DEV_SERVER_URL: string;
|
|
16
|
+
}
|
|
17
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tomjs/vite-plugin-electron",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "A simple vite plugin for electron, supports esm/cjs.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"vite",
|
|
@@ -24,7 +24,8 @@
|
|
|
24
24
|
}
|
|
25
25
|
},
|
|
26
26
|
"files": [
|
|
27
|
-
"dist"
|
|
27
|
+
"dist",
|
|
28
|
+
"env.d.ts"
|
|
28
29
|
],
|
|
29
30
|
"engines": {
|
|
30
31
|
"node": ">=16"
|