@tomjs/vite-plugin-electron 1.1.1 → 1.2.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/README.md +2 -2
- package/README.zh_CN.md +2 -2
- package/dist/index.d.mts +14 -9
- package/dist/index.d.ts +14 -9
- package/dist/index.js +47 -64
- package/dist/index.mjs +45 -62
- package/env.d.ts +1 -1
- package/package.json +5 -4
package/README.md
CHANGED
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
|
|
7
7
|
> A Simple [vite](https://vitejs.dev/) plugin for [electron](https://www.electronjs.org), supports `esm` and `cjs`.
|
|
8
8
|
|
|
9
|
+
I learned [caoxiemeihao](https://github.com/caoxiemeihao)'s [vite-plugin-electron](https://github.com/electron-vite/vite-plugin-electron) and [Doubleshotjs](https://github.com/Doubleshotjs)'s [doubleshot](https://github.com/Doubleshotjs/doubleshot) These two excellent works, combined with some of my own ideas, developed this plugin. I hope that using it can simplify the development configuration and only focus on business development.
|
|
10
|
+
|
|
9
11
|
## Features
|
|
10
12
|
|
|
11
13
|
- Fast build `main` and `preload` with [tsup](https://github.com/egoist/tsup)
|
|
@@ -199,7 +201,6 @@ Based on [Options](https://paka.dev/npm/tsup) of [tsup](https://tsup.egoist.dev/
|
|
|
199
201
|
|
|
200
202
|
| Property | Type | Default | Description |
|
|
201
203
|
| --- | --- | --- | --- |
|
|
202
|
-
| name | `string` | "main" | The name of the electron main process. |
|
|
203
204
|
| entry | `string` | `-` | The main process entry file. |
|
|
204
205
|
| format | `'cjs' \| 'esm'` | `-` | The bundle format. If not specified, it will use the "type" field from package.json. |
|
|
205
206
|
| outDir | `string` | "dist-electron/main" | The output directory for the main process files |
|
|
@@ -211,7 +212,6 @@ Based on [Options](https://paka.dev/npm/tsup) of [tsup](https://tsup.egoist.dev/
|
|
|
211
212
|
|
|
212
213
|
| Property | Type | Default | Description |
|
|
213
214
|
| --- | --- | --- | --- |
|
|
214
|
-
| name | `string` | "preload" | The name of the electron preload process. |
|
|
215
215
|
| entry | `string` | `-` | The preload process entry file. |
|
|
216
216
|
| format | `'cjs' \| 'esm'` | `-` | The bundle format. If not specified, it will use the "type" field from package.json. |
|
|
217
217
|
| outDir | `string` | "dist-electron/preload" | The output directory for the preload process files |
|
package/README.zh_CN.md
CHANGED
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
|
|
7
7
|
> 一个简单的 [electron](https://www.electronjs.org/zh/) [vite](https://cn.vitejs.dev/) 插件,支持 `esm` 和 `cjs`
|
|
8
8
|
|
|
9
|
+
学习了 [caoxiemeihao](https://github.com/caoxiemeihao) 的 [vite-plugin-electron](https://github.com/electron-vite/vite-plugin-electron) 和 [Doubleshotjs](https://github.com/Doubleshotjs) 的 [doubleshot](https://github.com/Doubleshotjs/doubleshot) 这两个优秀的作品,结合自己的一些想法,开发了该插件。希望使用它能简化开发配置,只关注业务开发。
|
|
10
|
+
|
|
9
11
|
## 特性
|
|
10
12
|
|
|
11
13
|
- 使用 [tsup](https://github.com/egoist/tsup) 快速构建 `main` 和 `preload`
|
|
@@ -200,7 +202,6 @@ export default defineConfig({
|
|
|
200
202
|
|
|
201
203
|
| 参数名 | 类型 | 默认值 | 说明 |
|
|
202
204
|
| --- | --- | --- | --- |
|
|
203
|
-
| name | `string` | "main" | main 名称 |
|
|
204
205
|
| **entry** | `string` | `-` | main 入口文件 |
|
|
205
206
|
| format | `'cjs' \| 'esm'` | `-` | 打包格式。如果未指定,将使用 package.json 中的 "type" 字段 |
|
|
206
207
|
| outDir | `string` | `"dist-electron/main"` | main 输出文件夹 |
|
|
@@ -212,7 +213,6 @@ export default defineConfig({
|
|
|
212
213
|
|
|
213
214
|
| 参数名 | 类型 | 默认值 | 说明 |
|
|
214
215
|
| --- | --- | --- | --- |
|
|
215
|
-
| name | `string` | "preload" | preload 名称 |
|
|
216
216
|
| **entry** | `string` | `-` | preload 入口文件 |
|
|
217
217
|
| format | `'cjs' \| 'esm'` | `-` | 打包格式。如果未指定,将使用 package.json 中的 "type" 字段 |
|
|
218
218
|
| outDir | `string` | `"dist-electron/preload"` | preload 输出文件夹 |
|
package/dist/index.d.mts
CHANGED
|
@@ -2,9 +2,11 @@ import { Plugin } from 'vite';
|
|
|
2
2
|
import { Options } from 'tsup';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Electron main process options
|
|
5
|
+
* Electron main process options.
|
|
6
|
+
* @see https://paka.dev/npm/tsup
|
|
7
|
+
* @see https://unpkg.com/browse/tsup/dist/index.d.ts
|
|
6
8
|
*/
|
|
7
|
-
|
|
9
|
+
interface MainOptions extends Omit<Options, 'name' | 'entry' | 'format' | 'outDir' | 'watch' | 'onSuccess'> {
|
|
8
10
|
/**
|
|
9
11
|
* The name of the electron main process.
|
|
10
12
|
* @default "main"
|
|
@@ -27,11 +29,13 @@ type MainOptions = Omit<Options, 'name' | 'entry' | 'format' | 'outDir' | 'watch
|
|
|
27
29
|
* A function that will be executed after the build succeeds.
|
|
28
30
|
*/
|
|
29
31
|
onSuccess?: () => Promise<void | undefined | (() => void | Promise<void>)>;
|
|
30
|
-
}
|
|
32
|
+
}
|
|
31
33
|
/**
|
|
32
|
-
* Electron preload process options
|
|
34
|
+
* Electron preload process options.
|
|
35
|
+
* @see https://paka.dev/npm/tsup
|
|
36
|
+
* @see https://unpkg.com/browse/tsup/dist/index.d.ts
|
|
33
37
|
*/
|
|
34
|
-
|
|
38
|
+
interface PreloadOptions extends Omit<Options, 'name' | 'entry' | 'format' | 'outDir' | 'watch' | 'onSuccess'> {
|
|
35
39
|
/**
|
|
36
40
|
* The name of the electron preload process.
|
|
37
41
|
* @default "preload"
|
|
@@ -46,7 +50,7 @@ type PreloadOptions = Omit<Options, 'name' | 'entry' | 'format' | 'outDir' | 'wa
|
|
|
46
50
|
*/
|
|
47
51
|
format?: 'cjs' | 'esm';
|
|
48
52
|
/**
|
|
49
|
-
* The output directory for the preload process files. Defaults
|
|
53
|
+
* The output directory for the preload process files. Defaults is `"dist-electron/preload"`.
|
|
50
54
|
* @default "dist-electron/preload"
|
|
51
55
|
*/
|
|
52
56
|
outDir?: string;
|
|
@@ -54,13 +58,14 @@ type PreloadOptions = Omit<Options, 'name' | 'entry' | 'format' | 'outDir' | 'wa
|
|
|
54
58
|
* A function that will be executed after the build succeeds.
|
|
55
59
|
*/
|
|
56
60
|
onSuccess?: () => Promise<void | undefined | (() => void | Promise<void>)>;
|
|
57
|
-
}
|
|
61
|
+
}
|
|
58
62
|
/**
|
|
59
63
|
* vite plugin options
|
|
60
64
|
*/
|
|
61
65
|
interface PluginOptions {
|
|
62
66
|
/**
|
|
63
|
-
* Recommended switch
|
|
67
|
+
* Recommended switch. Default is true.
|
|
68
|
+
* if true, will have the following default behavior:
|
|
64
69
|
* * will change the main/preload/renderer outDir to be parallel outDir;
|
|
65
70
|
* eg. if vite build.outDir is 'dist', will change main/preload/render to 'dist/main' and 'dist/preload' and 'dist/renderer'
|
|
66
71
|
* @default true
|
|
@@ -87,4 +92,4 @@ interface PluginOptions {
|
|
|
87
92
|
|
|
88
93
|
declare function vitePluginElectron(options?: PluginOptions): Plugin;
|
|
89
94
|
|
|
90
|
-
export { vitePluginElectron as default, vitePluginElectron };
|
|
95
|
+
export { MainOptions, PluginOptions, PreloadOptions, vitePluginElectron as default, vitePluginElectron };
|
package/dist/index.d.ts
CHANGED
|
@@ -2,9 +2,11 @@ import { Plugin } from 'vite';
|
|
|
2
2
|
import { Options } from 'tsup';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Electron main process options
|
|
5
|
+
* Electron main process options.
|
|
6
|
+
* @see https://paka.dev/npm/tsup
|
|
7
|
+
* @see https://unpkg.com/browse/tsup/dist/index.d.ts
|
|
6
8
|
*/
|
|
7
|
-
|
|
9
|
+
interface MainOptions extends Omit<Options, 'name' | 'entry' | 'format' | 'outDir' | 'watch' | 'onSuccess'> {
|
|
8
10
|
/**
|
|
9
11
|
* The name of the electron main process.
|
|
10
12
|
* @default "main"
|
|
@@ -27,11 +29,13 @@ type MainOptions = Omit<Options, 'name' | 'entry' | 'format' | 'outDir' | 'watch
|
|
|
27
29
|
* A function that will be executed after the build succeeds.
|
|
28
30
|
*/
|
|
29
31
|
onSuccess?: () => Promise<void | undefined | (() => void | Promise<void>)>;
|
|
30
|
-
}
|
|
32
|
+
}
|
|
31
33
|
/**
|
|
32
|
-
* Electron preload process options
|
|
34
|
+
* Electron preload process options.
|
|
35
|
+
* @see https://paka.dev/npm/tsup
|
|
36
|
+
* @see https://unpkg.com/browse/tsup/dist/index.d.ts
|
|
33
37
|
*/
|
|
34
|
-
|
|
38
|
+
interface PreloadOptions extends Omit<Options, 'name' | 'entry' | 'format' | 'outDir' | 'watch' | 'onSuccess'> {
|
|
35
39
|
/**
|
|
36
40
|
* The name of the electron preload process.
|
|
37
41
|
* @default "preload"
|
|
@@ -46,7 +50,7 @@ type PreloadOptions = Omit<Options, 'name' | 'entry' | 'format' | 'outDir' | 'wa
|
|
|
46
50
|
*/
|
|
47
51
|
format?: 'cjs' | 'esm';
|
|
48
52
|
/**
|
|
49
|
-
* The output directory for the preload process files. Defaults
|
|
53
|
+
* The output directory for the preload process files. Defaults is `"dist-electron/preload"`.
|
|
50
54
|
* @default "dist-electron/preload"
|
|
51
55
|
*/
|
|
52
56
|
outDir?: string;
|
|
@@ -54,13 +58,14 @@ type PreloadOptions = Omit<Options, 'name' | 'entry' | 'format' | 'outDir' | 'wa
|
|
|
54
58
|
* A function that will be executed after the build succeeds.
|
|
55
59
|
*/
|
|
56
60
|
onSuccess?: () => Promise<void | undefined | (() => void | Promise<void>)>;
|
|
57
|
-
}
|
|
61
|
+
}
|
|
58
62
|
/**
|
|
59
63
|
* vite plugin options
|
|
60
64
|
*/
|
|
61
65
|
interface PluginOptions {
|
|
62
66
|
/**
|
|
63
|
-
* Recommended switch
|
|
67
|
+
* Recommended switch. Default is true.
|
|
68
|
+
* if true, will have the following default behavior:
|
|
64
69
|
* * will change the main/preload/renderer outDir to be parallel outDir;
|
|
65
70
|
* eg. if vite build.outDir is 'dist', will change main/preload/render to 'dist/main' and 'dist/preload' and 'dist/renderer'
|
|
66
71
|
* @default true
|
|
@@ -87,4 +92,4 @@ interface PluginOptions {
|
|
|
87
92
|
|
|
88
93
|
declare function vitePluginElectron(options?: PluginOptions): Plugin;
|
|
89
94
|
|
|
90
|
-
export { vitePluginElectron as default, vitePluginElectron };
|
|
95
|
+
export { MainOptions, PluginOptions, PreloadOptions, vitePluginElectron as default, vitePluginElectron };
|
package/dist/index.js
CHANGED
|
@@ -1,11 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
|
|
2
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
-
var __publicField = (obj, key, value) => {
|
|
4
|
-
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
|
-
return value;
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
// src/index.ts
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }// src/index.ts
|
|
9
2
|
var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs);
|
|
10
3
|
var _lodashclonedeep = require('lodash.clonedeep'); var _lodashclonedeep2 = _interopRequireDefault(_lodashclonedeep);
|
|
11
4
|
var _lodashmerge = require('lodash.merge'); var _lodashmerge2 = _interopRequireDefault(_lodashmerge);
|
|
@@ -17,16 +10,16 @@ var PLUGIN_NAME = "@tomjs:electron";
|
|
|
17
10
|
// src/main.ts
|
|
18
11
|
var _child_process = require('child_process');
|
|
19
12
|
var _electron = require('electron'); var _electron2 = _interopRequireDefault(_electron);
|
|
20
|
-
var
|
|
13
|
+
var _treekill = require('tree-kill'); var _treekill2 = _interopRequireDefault(_treekill);
|
|
21
14
|
var _tsup = require('tsup');
|
|
22
15
|
|
|
23
16
|
// src/logger.ts
|
|
24
17
|
var _dayjs = require('dayjs'); var _dayjs2 = _interopRequireDefault(_dayjs);
|
|
25
|
-
|
|
18
|
+
var _kolorist = require('kolorist');
|
|
26
19
|
var Logger = class {
|
|
27
20
|
constructor(tag, withTime) {
|
|
28
|
-
|
|
29
|
-
|
|
21
|
+
this.tag = PLUGIN_NAME;
|
|
22
|
+
this.withTime = true;
|
|
30
23
|
this.tag = `[${tag}]`;
|
|
31
24
|
this.withTime = _nullishCoalesce(withTime, () => ( true));
|
|
32
25
|
}
|
|
@@ -64,50 +57,47 @@ var createLogger = (tag) => {
|
|
|
64
57
|
|
|
65
58
|
// src/main.ts
|
|
66
59
|
var logger = createLogger();
|
|
67
|
-
function getBuildOptions(options
|
|
68
|
-
const env = {
|
|
69
|
-
APP_DEV_SERVER_URL: innerOpts == null ? void 0 : innerOpts.serverUrl
|
|
70
|
-
};
|
|
71
|
-
Object.keys(env).forEach((key) => {
|
|
72
|
-
if (env[key] === void 0) {
|
|
73
|
-
delete env[key];
|
|
74
|
-
}
|
|
75
|
-
});
|
|
60
|
+
function getBuildOptions(options) {
|
|
76
61
|
return ["main", "preload"].filter((s) => options[s] && options[s].entry).map((s) => {
|
|
77
62
|
options[s].__NAME__ = s;
|
|
78
63
|
return options[s];
|
|
79
64
|
}).map((cfg) => {
|
|
80
65
|
return {
|
|
81
66
|
...cfg,
|
|
82
|
-
env,
|
|
83
67
|
silent: true
|
|
84
68
|
};
|
|
85
69
|
});
|
|
86
70
|
}
|
|
87
|
-
function
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const mainFile = innerOpts.mainFile;
|
|
93
|
-
logger.info(`run main file: ${_kolorist.lightGreen.call(void 0, mainFile)}`);
|
|
94
|
-
const args = options.inspect ? ["--inspect"] : [];
|
|
95
|
-
return _child_process.spawn.call(void 0, _electron2.default, [...args, mainFile], {
|
|
71
|
+
async function startup(options) {
|
|
72
|
+
await startup.exit();
|
|
73
|
+
const args = [];
|
|
74
|
+
options.inspect && args.push("--inspect");
|
|
75
|
+
process.electronApp = _child_process.spawn.call(void 0, _electron2.default, [".", ...args], {
|
|
96
76
|
stdio: "inherit"
|
|
97
|
-
})
|
|
77
|
+
});
|
|
78
|
+
process.electronApp.once("exit", process.exit);
|
|
79
|
+
process.once("exit", () => {
|
|
80
|
+
startup.exit();
|
|
81
|
+
process.electronApp.kill();
|
|
82
|
+
});
|
|
98
83
|
}
|
|
99
|
-
async
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
84
|
+
startup.exit = async () => {
|
|
85
|
+
if (!process.electronApp) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
process.electronApp.removeAllListeners();
|
|
89
|
+
return new Promise((resolve, reject) => {
|
|
90
|
+
_treekill2.default.call(void 0, process.electronApp.pid, (err) => {
|
|
91
|
+
if (err) {
|
|
92
|
+
reject(err);
|
|
93
|
+
} else {
|
|
94
|
+
resolve(true);
|
|
95
|
+
}
|
|
96
|
+
});
|
|
109
97
|
});
|
|
110
|
-
|
|
98
|
+
};
|
|
99
|
+
async function runServe(options, server) {
|
|
100
|
+
const buildOptions = getBuildOptions(options);
|
|
111
101
|
for (let i = 0; i < buildOptions.length; i++) {
|
|
112
102
|
let isFirstBuild = true;
|
|
113
103
|
const tsOpts = buildOptions[i];
|
|
@@ -124,8 +114,8 @@ async function runServe(options, innerOpts, server) {
|
|
|
124
114
|
}
|
|
125
115
|
logger.success(`${name} rebuild succeeded!`);
|
|
126
116
|
if (name === "main") {
|
|
127
|
-
|
|
128
|
-
|
|
117
|
+
console.log("main process exit");
|
|
118
|
+
await startup(options);
|
|
129
119
|
} else {
|
|
130
120
|
server.ws.send({
|
|
131
121
|
type: "full-reload"
|
|
@@ -134,13 +124,10 @@ async function runServe(options, innerOpts, server) {
|
|
|
134
124
|
};
|
|
135
125
|
await _tsup.build.call(void 0, { onSuccess, watch: true, ...tsupOptions });
|
|
136
126
|
}
|
|
137
|
-
|
|
138
|
-
return {
|
|
139
|
-
kill: killProcess
|
|
140
|
-
};
|
|
127
|
+
await startup(options);
|
|
141
128
|
}
|
|
142
|
-
async function runBuild(options
|
|
143
|
-
const buildOptions = getBuildOptions(options
|
|
129
|
+
async function runBuild(options) {
|
|
130
|
+
const buildOptions = getBuildOptions(options);
|
|
144
131
|
for (let i = 0; i < buildOptions.length; i++) {
|
|
145
132
|
await _tsup.build.call(void 0, buildOptions[i]);
|
|
146
133
|
}
|
|
@@ -206,19 +193,17 @@ function preMergeOptions(options) {
|
|
|
206
193
|
const external = opt.external || opts.external || ["electron"];
|
|
207
194
|
opt.external = [...new Set(["electron"].concat(external))];
|
|
208
195
|
});
|
|
209
|
-
|
|
210
|
-
mainFile: pkg.main
|
|
211
|
-
};
|
|
212
|
-
return { opts, innerOpts };
|
|
196
|
+
return opts;
|
|
213
197
|
}
|
|
214
198
|
function vitePluginElectron(options) {
|
|
215
|
-
const
|
|
199
|
+
const opts = preMergeOptions(options);
|
|
216
200
|
const isDev = process.env.NODE_ENV === "development";
|
|
201
|
+
let isServer = false;
|
|
217
202
|
return {
|
|
218
203
|
name: PLUGIN_NAME,
|
|
219
204
|
config(config, env) {
|
|
220
205
|
var _a;
|
|
221
|
-
|
|
206
|
+
isServer = env.command === "serve";
|
|
222
207
|
let outDir = ((_a = config == null ? void 0 : config.build) == null ? void 0 : _a.outDir) || "dist";
|
|
223
208
|
opts.preload = opts.preload || {};
|
|
224
209
|
if (opts.recommended) {
|
|
@@ -247,25 +232,23 @@ function vitePluginElectron(options) {
|
|
|
247
232
|
return;
|
|
248
233
|
}
|
|
249
234
|
server.httpServer.on("listening", async () => {
|
|
250
|
-
var _a;
|
|
251
235
|
if (server.httpServer) {
|
|
252
236
|
const serve = server.httpServer.address();
|
|
253
237
|
const { address, port, family } = serve;
|
|
254
238
|
if (family === "IPv6") {
|
|
255
|
-
|
|
239
|
+
process.env.APP_DEV_SERVER_URL = `http://[${address}]:${port}`;
|
|
256
240
|
} else {
|
|
257
|
-
|
|
241
|
+
process.env.APP_DEV_SERVER_URL = `http://${address}:${port}`;
|
|
258
242
|
}
|
|
259
243
|
}
|
|
260
|
-
(
|
|
261
|
-
process.__tomjs_electron_serve__ = await runServe(opts, innerOpts, server);
|
|
244
|
+
await runServe(opts, server);
|
|
262
245
|
});
|
|
263
246
|
},
|
|
264
247
|
async closeBundle() {
|
|
265
|
-
if (
|
|
248
|
+
if (isServer) {
|
|
266
249
|
return;
|
|
267
250
|
}
|
|
268
|
-
await runBuild(opts
|
|
251
|
+
await runBuild(opts);
|
|
269
252
|
}
|
|
270
253
|
};
|
|
271
254
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
-
var __publicField = (obj, key, value) => {
|
|
4
|
-
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
|
-
return value;
|
|
6
|
-
};
|
|
7
|
-
|
|
8
1
|
// src/index.ts
|
|
9
2
|
import fs2 from "fs";
|
|
10
3
|
import cloneDeep from "lodash.clonedeep";
|
|
@@ -17,7 +10,7 @@ var PLUGIN_NAME = "@tomjs:electron";
|
|
|
17
10
|
// src/main.ts
|
|
18
11
|
import { spawn } from "child_process";
|
|
19
12
|
import electron from "electron";
|
|
20
|
-
import
|
|
13
|
+
import treeKill from "tree-kill";
|
|
21
14
|
import { build as tsupBuild } from "tsup";
|
|
22
15
|
|
|
23
16
|
// src/logger.ts
|
|
@@ -25,8 +18,8 @@ import dayjs from "dayjs";
|
|
|
25
18
|
import { blue, gray, green, red, yellow } from "kolorist";
|
|
26
19
|
var Logger = class {
|
|
27
20
|
constructor(tag, withTime) {
|
|
28
|
-
|
|
29
|
-
|
|
21
|
+
this.tag = PLUGIN_NAME;
|
|
22
|
+
this.withTime = true;
|
|
30
23
|
this.tag = `[${tag}]`;
|
|
31
24
|
this.withTime = withTime ?? true;
|
|
32
25
|
}
|
|
@@ -64,50 +57,47 @@ var createLogger = (tag) => {
|
|
|
64
57
|
|
|
65
58
|
// src/main.ts
|
|
66
59
|
var logger = createLogger();
|
|
67
|
-
function getBuildOptions(options
|
|
68
|
-
const env = {
|
|
69
|
-
APP_DEV_SERVER_URL: innerOpts == null ? void 0 : innerOpts.serverUrl
|
|
70
|
-
};
|
|
71
|
-
Object.keys(env).forEach((key) => {
|
|
72
|
-
if (env[key] === void 0) {
|
|
73
|
-
delete env[key];
|
|
74
|
-
}
|
|
75
|
-
});
|
|
60
|
+
function getBuildOptions(options) {
|
|
76
61
|
return ["main", "preload"].filter((s) => options[s] && options[s].entry).map((s) => {
|
|
77
62
|
options[s].__NAME__ = s;
|
|
78
63
|
return options[s];
|
|
79
64
|
}).map((cfg) => {
|
|
80
65
|
return {
|
|
81
66
|
...cfg,
|
|
82
|
-
env,
|
|
83
67
|
silent: true
|
|
84
68
|
};
|
|
85
69
|
});
|
|
86
70
|
}
|
|
87
|
-
function
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const mainFile = innerOpts.mainFile;
|
|
93
|
-
logger.info(`run main file: ${lightGreen(mainFile)}`);
|
|
94
|
-
const args = options.inspect ? ["--inspect"] : [];
|
|
95
|
-
return spawn(electron, [...args, mainFile], {
|
|
71
|
+
async function startup(options) {
|
|
72
|
+
await startup.exit();
|
|
73
|
+
const args = [];
|
|
74
|
+
options.inspect && args.push("--inspect");
|
|
75
|
+
process.electronApp = spawn(electron, [".", ...args], {
|
|
96
76
|
stdio: "inherit"
|
|
97
|
-
})
|
|
77
|
+
});
|
|
78
|
+
process.electronApp.once("exit", process.exit);
|
|
79
|
+
process.once("exit", () => {
|
|
80
|
+
startup.exit();
|
|
81
|
+
process.electronApp.kill();
|
|
82
|
+
});
|
|
98
83
|
}
|
|
99
|
-
async
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
84
|
+
startup.exit = async () => {
|
|
85
|
+
if (!process.electronApp) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
process.electronApp.removeAllListeners();
|
|
89
|
+
return new Promise((resolve, reject) => {
|
|
90
|
+
treeKill(process.electronApp.pid, (err) => {
|
|
91
|
+
if (err) {
|
|
92
|
+
reject(err);
|
|
93
|
+
} else {
|
|
94
|
+
resolve(true);
|
|
95
|
+
}
|
|
96
|
+
});
|
|
109
97
|
});
|
|
110
|
-
|
|
98
|
+
};
|
|
99
|
+
async function runServe(options, server) {
|
|
100
|
+
const buildOptions = getBuildOptions(options);
|
|
111
101
|
for (let i = 0; i < buildOptions.length; i++) {
|
|
112
102
|
let isFirstBuild = true;
|
|
113
103
|
const tsOpts = buildOptions[i];
|
|
@@ -124,8 +114,8 @@ async function runServe(options, innerOpts, server) {
|
|
|
124
114
|
}
|
|
125
115
|
logger.success(`${name} rebuild succeeded!`);
|
|
126
116
|
if (name === "main") {
|
|
127
|
-
|
|
128
|
-
|
|
117
|
+
console.log("main process exit");
|
|
118
|
+
await startup(options);
|
|
129
119
|
} else {
|
|
130
120
|
server.ws.send({
|
|
131
121
|
type: "full-reload"
|
|
@@ -134,13 +124,10 @@ async function runServe(options, innerOpts, server) {
|
|
|
134
124
|
};
|
|
135
125
|
await tsupBuild({ onSuccess, watch: true, ...tsupOptions });
|
|
136
126
|
}
|
|
137
|
-
|
|
138
|
-
return {
|
|
139
|
-
kill: killProcess
|
|
140
|
-
};
|
|
127
|
+
await startup(options);
|
|
141
128
|
}
|
|
142
|
-
async function runBuild(options
|
|
143
|
-
const buildOptions = getBuildOptions(options
|
|
129
|
+
async function runBuild(options) {
|
|
130
|
+
const buildOptions = getBuildOptions(options);
|
|
144
131
|
for (let i = 0; i < buildOptions.length; i++) {
|
|
145
132
|
await tsupBuild(buildOptions[i]);
|
|
146
133
|
}
|
|
@@ -205,19 +192,17 @@ function preMergeOptions(options) {
|
|
|
205
192
|
const external = opt.external || opts.external || ["electron"];
|
|
206
193
|
opt.external = [...new Set(["electron"].concat(external))];
|
|
207
194
|
});
|
|
208
|
-
|
|
209
|
-
mainFile: pkg.main
|
|
210
|
-
};
|
|
211
|
-
return { opts, innerOpts };
|
|
195
|
+
return opts;
|
|
212
196
|
}
|
|
213
197
|
function vitePluginElectron(options) {
|
|
214
|
-
const
|
|
198
|
+
const opts = preMergeOptions(options);
|
|
215
199
|
const isDev = process.env.NODE_ENV === "development";
|
|
200
|
+
let isServer = false;
|
|
216
201
|
return {
|
|
217
202
|
name: PLUGIN_NAME,
|
|
218
203
|
config(config, env) {
|
|
219
204
|
var _a;
|
|
220
|
-
|
|
205
|
+
isServer = env.command === "serve";
|
|
221
206
|
let outDir = ((_a = config == null ? void 0 : config.build) == null ? void 0 : _a.outDir) || "dist";
|
|
222
207
|
opts.preload = opts.preload || {};
|
|
223
208
|
if (opts.recommended) {
|
|
@@ -246,25 +231,23 @@ function vitePluginElectron(options) {
|
|
|
246
231
|
return;
|
|
247
232
|
}
|
|
248
233
|
server.httpServer.on("listening", async () => {
|
|
249
|
-
var _a;
|
|
250
234
|
if (server.httpServer) {
|
|
251
235
|
const serve = server.httpServer.address();
|
|
252
236
|
const { address, port, family } = serve;
|
|
253
237
|
if (family === "IPv6") {
|
|
254
|
-
|
|
238
|
+
process.env.APP_DEV_SERVER_URL = `http://[${address}]:${port}`;
|
|
255
239
|
} else {
|
|
256
|
-
|
|
240
|
+
process.env.APP_DEV_SERVER_URL = `http://${address}:${port}`;
|
|
257
241
|
}
|
|
258
242
|
}
|
|
259
|
-
(
|
|
260
|
-
process.__tomjs_electron_serve__ = await runServe(opts, innerOpts, server);
|
|
243
|
+
await runServe(opts, server);
|
|
261
244
|
});
|
|
262
245
|
},
|
|
263
246
|
async closeBundle() {
|
|
264
|
-
if (
|
|
247
|
+
if (isServer) {
|
|
265
248
|
return;
|
|
266
249
|
}
|
|
267
|
-
await runBuild(opts
|
|
250
|
+
await runBuild(opts);
|
|
268
251
|
}
|
|
269
252
|
};
|
|
270
253
|
}
|
package/env.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tomjs/vite-plugin-electron",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "A simple vite plugin for electron, supports esm/cjs.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"vite",
|
|
@@ -44,6 +44,7 @@
|
|
|
44
44
|
"lodash.clonedeep": "^4.5.0",
|
|
45
45
|
"lodash.merge": "^4.6.2",
|
|
46
46
|
"shelljs": "^0.8.5",
|
|
47
|
+
"tree-kill": "^1.2.2",
|
|
47
48
|
"tsup": "7.2.0"
|
|
48
49
|
},
|
|
49
50
|
"devDependencies": {
|
|
@@ -51,8 +52,8 @@
|
|
|
51
52
|
"@tomjs/commitlint": "^2.0.5",
|
|
52
53
|
"@tomjs/eslint": "^1.1.1",
|
|
53
54
|
"@tomjs/prettier": "^1.0.6",
|
|
54
|
-
"@tomjs/stylelint": "^1.1.
|
|
55
|
-
"@tomjs/tsconfig": "^1.
|
|
55
|
+
"@tomjs/stylelint": "^1.1.1",
|
|
56
|
+
"@tomjs/tsconfig": "^1.1.2",
|
|
56
57
|
"@types/lodash.clonedeep": "^4.5.9",
|
|
57
58
|
"@types/lodash.merge": "^4.6.9",
|
|
58
59
|
"@types/node": "^18.19.3",
|
|
@@ -60,7 +61,7 @@
|
|
|
60
61
|
"eslint": "^8.55.0",
|
|
61
62
|
"husky": "^8.0.3",
|
|
62
63
|
"lint-staged": "^15.2.0",
|
|
63
|
-
"np": "^9.
|
|
64
|
+
"np": "^9.2.0",
|
|
64
65
|
"npm-run-all": "^4.1.5",
|
|
65
66
|
"prettier": "^3.1.0",
|
|
66
67
|
"rimraf": "^5.0.5",
|