@tomjs/vite-plugin-electron 2.2.0 → 2.4.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 +10 -9
- package/README.zh_CN.md +10 -9
- package/dist/index.d.mts +10 -1
- package/dist/index.mjs +45 -54
- package/package.json +9 -10
package/README.md
CHANGED
|
@@ -206,15 +206,16 @@ export default defineConfig({
|
|
|
206
206
|
|
|
207
207
|
### PluginOptions
|
|
208
208
|
|
|
209
|
-
| Property | Type | Default | Description
|
|
210
|
-
| ----------- | ---------------------------------------------- | ------- |
|
|
211
|
-
| recommended | `boolean` | `true` | This option is intended to provide recommended default parameters and behavior.
|
|
212
|
-
| external | `string[]` | | Don't bundle these modules, but dependencies and peerDependencies in your package.json are always excluded.[See more](https://tsdown.dev/reference/api/Interface.UserConfig#external)
|
|
213
|
-
| main | [MainOptions](#MainOptions) | | Configuration options for the electron main process.
|
|
214
|
-
| preload | [PreloadOptions](#PreloadOptions) | | Configuration options for the electron preload process.
|
|
215
|
-
| debug | `boolean` | `false` | Electron debug mode, don't startup electron. You can also use `process.env.VITE_ELECTRON_DEBUG`. Default is false.
|
|
216
|
-
| builder | `boolean` \| [BuilderOptions](#BuilderOptions) | `false` | If it is a `boolean` type, whether to enable [electron-builder](https://www.electron.build). If it is an object, it is the [configuration](https://www.electron.build/configuration/configuration) of [electron-builder](https://www.electron.build). You can also turn it on using `process.env.VITE_ELECTRON_DEBUG`.
|
|
217
|
-
| inspect | `boolean` | `false` | Electron will listen for V8 inspector protocol messages on the specified port, an external debugger will need to connect on this port. You can also use `process.env.VITE_ELECTRON_INSPECT`. See [debugging-main-process](https://www.electronjs.org/docs/latest/tutorial/debugging-main-process) for more information.
|
|
209
|
+
| Property | Type | Default | Description |
|
|
210
|
+
| ----------- | ---------------------------------------------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
211
|
+
| recommended | `boolean` | `true` | This option is intended to provide recommended default parameters and behavior. |
|
|
212
|
+
| external | `string[]` | | Don't bundle these modules, but dependencies and peerDependencies in your package.json are always excluded.[See more](https://tsdown.dev/reference/api/Interface.UserConfig#external) |
|
|
213
|
+
| main | [MainOptions](#MainOptions) | | Configuration options for the electron main process. |
|
|
214
|
+
| preload | [PreloadOptions](#PreloadOptions) | | Configuration options for the electron preload process. |
|
|
215
|
+
| debug | `boolean` | `false` | Electron debug mode, don't startup electron. You can also use `process.env.VITE_ELECTRON_DEBUG`. Default is false. |
|
|
216
|
+
| builder | `boolean` \| [BuilderOptions](#BuilderOptions) | `false` | If it is a `boolean` type, whether to enable [electron-builder](https://www.electron.build). If it is an object, it is the [configuration](https://www.electron.build/configuration/configuration) of [electron-builder](https://www.electron.build). You can also turn it on using `process.env.VITE_ELECTRON_DEBUG`. |
|
|
217
|
+
| inspect | `boolean` | `false` | Electron will listen for V8 inspector protocol messages on the specified port, an external debugger will need to connect on this port. You can also use `process.env.VITE_ELECTRON_INSPECT`. See [debugging-main-process](https://www.electronjs.org/docs/latest/tutorial/debugging-main-process) for more information. |
|
|
218
|
+
| devtools | `boolean` \| `number` | `true` | If `true`, depending on whether the `react` plugin exists, inject `<script src="http://localhost:8097"></script>` code for [react-devtools](https://github.com/facebook/react/tree/main/packages/react-devtools); depending on whether the `vue` plugin exists, inject `<script src="http://localhost:8097"></script>` for [vue-devtools](https://devtools.vuejs.org/guide/standalone) debugging; if `number`, the table represents a custom port. |
|
|
218
219
|
|
|
219
220
|
**Notice**
|
|
220
221
|
|
package/README.zh_CN.md
CHANGED
|
@@ -207,15 +207,16 @@ export default defineConfig({
|
|
|
207
207
|
|
|
208
208
|
### PluginOptions
|
|
209
209
|
|
|
210
|
-
| 参数名 | 类型 | 默认值 | 说明
|
|
211
|
-
| ----------- | --------------------------------- | ------- |
|
|
212
|
-
| recommended | `boolean` | `true` | 这个选项是为了提供推荐的默认参数和行为
|
|
213
|
-
| external | `string[]` | | 不打包这些模块,但是 `dependencies` and `peerDependencies` 默认排除,[详见](https://tsdown.dev/zh-CN/reference/api/Interface.UserConfig)
|
|
214
|
-
| main | [MainOptions](#MainOptions) | | electron main 进程选项
|
|
215
|
-
| preload | [PreloadOptions](#PreloadOptions) | | electron preload 进程选项
|
|
216
|
-
| debug | `boolean` | `false` | Electron调试模式,不启动Electron。 您还可以使用 `process.env.VITE_ELECTRON_DEBUG`
|
|
217
|
-
| builder | `boolean` | `false` | 如果是`boolean`类型,是否启用[electron-builder](https://www.electron.build)。如果是`Object`,则是[electron-builder](https://www.electron.build)的[配置](https://www.electron.build/configuration/configuration)。 您还可以使用 `process.env.VITE_ELECTRON_DEBUG` 开启它。
|
|
218
|
-
| inspect | `boolean` | `false` | Electron 将监听指定 port 上的 V8 调试协议消息, 外部调试器需要连接到此端口上。您还可以使用 `process.env.VITE_ELECTRON_INSPECT`。 有关更多信息,请参阅[debugging-main-process](https://www.electronjs.org/zh/docs/latest/tutorial/debugging-main-process)。
|
|
210
|
+
| 参数名 | 类型 | 默认值 | 说明 |
|
|
211
|
+
| ----------- | --------------------------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
212
|
+
| recommended | `boolean` | `true` | 这个选项是为了提供推荐的默认参数和行为 |
|
|
213
|
+
| external | `string[]` | | 不打包这些模块,但是 `dependencies` and `peerDependencies` 默认排除,[详见](https://tsdown.dev/zh-CN/reference/api/Interface.UserConfig) |
|
|
214
|
+
| main | [MainOptions](#MainOptions) | | electron main 进程选项 |
|
|
215
|
+
| preload | [PreloadOptions](#PreloadOptions) | | electron preload 进程选项 |
|
|
216
|
+
| debug | `boolean` | `false` | Electron调试模式,不启动Electron。 您还可以使用 `process.env.VITE_ELECTRON_DEBUG` |
|
|
217
|
+
| builder | `boolean` | `false` | 如果是`boolean`类型,是否启用[electron-builder](https://www.electron.build)。如果是`Object`,则是[electron-builder](https://www.electron.build)的[配置](https://www.electron.build/configuration/configuration)。 您还可以使用 `process.env.VITE_ELECTRON_DEBUG` 开启它。 |
|
|
218
|
+
| inspect | `boolean` | `false` | Electron 将监听指定 port 上的 V8 调试协议消息, 外部调试器需要连接到此端口上。您还可以使用 `process.env.VITE_ELECTRON_INSPECT`。 有关更多信息,请参阅[debugging-main-process](https://www.electronjs.org/zh/docs/latest/tutorial/debugging-main-process)。 |
|
|
219
|
+
| devtools | `boolean` \| `number` | `false` | 如果为 `true`,根据 `react` 插件是否存在,注入 `<script src="http://localhost:8097"></script>` 代码用于 [react-devtools](https://github.com/facebook/react/tree/main/packages/react-devtools); 根据 `vue` 插件是否存在,注入 `<script src="http://localhost:8097"></script>` 用于 [vue-devtools](https://devtools.vuejs.org/guide/standalone) 调试,如果为 `number`,则表为自定义端口。 |
|
|
219
220
|
|
|
220
221
|
`recommended` 选项用于设置默认配置和行为,几乎可以达到零配置使用,默认为 `true` 。如果你要自定义配置,请设置它为`false`。以下默认的前提条件是使用推荐的 [项目结构](#目录结构)。
|
|
221
222
|
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { Plugin } from "vite";
|
|
1
2
|
import { UserConfig } from "tsdown";
|
|
2
3
|
import { Configuration } from "electron-builder";
|
|
3
|
-
import { Plugin } from "vite";
|
|
4
4
|
|
|
5
5
|
//#region src/types.d.ts
|
|
6
6
|
|
|
@@ -124,6 +124,15 @@ interface PluginOptions {
|
|
|
124
124
|
* @default false
|
|
125
125
|
*/
|
|
126
126
|
inspect?: number | boolean;
|
|
127
|
+
/**
|
|
128
|
+
* Whether to enable devtools. Inject `<script src="http://localhost:<devtools-port>"></script>` into webview client . Default is true.
|
|
129
|
+
* - true:
|
|
130
|
+
* - react: inject `<script src="http://localhost:8097"></script>`
|
|
131
|
+
* - vue: inject `<script src="http://localhost:8098"></script>`
|
|
132
|
+
* - `number`: custom port
|
|
133
|
+
* @default false
|
|
134
|
+
*/
|
|
135
|
+
devtools?: boolean | number;
|
|
127
136
|
}
|
|
128
137
|
//#endregion
|
|
129
138
|
//#region src/index.d.ts
|
package/dist/index.mjs
CHANGED
|
@@ -6,10 +6,10 @@ import merge from "lodash.merge";
|
|
|
6
6
|
import os from "node:os";
|
|
7
7
|
import { cwd } from "node:process";
|
|
8
8
|
import { execa, execaSync } from "execa";
|
|
9
|
-
import
|
|
10
|
-
import { blue, gray, green, red, yellow } from "kolorist";
|
|
9
|
+
import { createLogger } from "vite";
|
|
11
10
|
import cp, { spawn } from "node:child_process";
|
|
12
11
|
import electron from "electron";
|
|
12
|
+
import colors from "picocolors";
|
|
13
13
|
import { build } from "tsdown";
|
|
14
14
|
|
|
15
15
|
//#region src/constants.ts
|
|
@@ -17,47 +17,28 @@ const PLUGIN_NAME = "tomjs:electron";
|
|
|
17
17
|
|
|
18
18
|
//#endregion
|
|
19
19
|
//#region src/logger.ts
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
* 调试日志 等同 debug
|
|
41
|
-
*/
|
|
42
|
-
log(msg, ...rest) {
|
|
43
|
-
this.debug(msg, ...rest);
|
|
44
|
-
}
|
|
45
|
-
info(msg, ...rest) {
|
|
46
|
-
console.log(`${this.getTime()}${blue(this.tag)}`, msg, ...rest);
|
|
47
|
-
}
|
|
48
|
-
warn(msg, ...rest) {
|
|
49
|
-
console.log(`${this.getTime()}${yellow(this.tag)}`, msg, ...rest);
|
|
50
|
-
}
|
|
51
|
-
error(msg, ...rest) {
|
|
52
|
-
console.log(`${this.getTime()}${red(this.tag)}`, msg, ...rest);
|
|
53
|
-
}
|
|
54
|
-
success(msg, ...rest) {
|
|
55
|
-
console.log(`${this.getTime()}${green(this.tag)}`, msg, ...rest);
|
|
56
|
-
}
|
|
57
|
-
};
|
|
58
|
-
function createLogger(tag) {
|
|
59
|
-
return new Logger(tag || PLUGIN_NAME, true);
|
|
20
|
+
function createLogger$1(logLevel) {
|
|
21
|
+
const logger$1 = createLogger(logLevel, {
|
|
22
|
+
prefix: `[${PLUGIN_NAME}]`,
|
|
23
|
+
allowClearScreen: true
|
|
24
|
+
});
|
|
25
|
+
[
|
|
26
|
+
"info",
|
|
27
|
+
"warn",
|
|
28
|
+
"warnOnce",
|
|
29
|
+
"error"
|
|
30
|
+
].forEach((level) => {
|
|
31
|
+
const _level = logger$1[level];
|
|
32
|
+
logger$1[level] = (msg, options) => {
|
|
33
|
+
_level(msg, Object.assign({
|
|
34
|
+
timestamp: true,
|
|
35
|
+
clear: false
|
|
36
|
+
}, options));
|
|
37
|
+
};
|
|
38
|
+
});
|
|
39
|
+
return logger$1;
|
|
60
40
|
}
|
|
41
|
+
const logger = createLogger$1();
|
|
61
42
|
|
|
62
43
|
//#endregion
|
|
63
44
|
//#region src/utils.ts
|
|
@@ -128,7 +109,6 @@ function killTree(tree) {
|
|
|
128
109
|
|
|
129
110
|
//#endregion
|
|
130
111
|
//#region src/builder.ts
|
|
131
|
-
const logger$1 = createLogger();
|
|
132
112
|
function getMirror() {
|
|
133
113
|
let mirror = process.env.ELECTRON_MIRROR;
|
|
134
114
|
if (mirror) return mirror;
|
|
@@ -232,15 +212,15 @@ function createPkg(options, resolvedConfig) {
|
|
|
232
212
|
}
|
|
233
213
|
async function runElectronBuilder(options, resolvedConfig) {
|
|
234
214
|
if (typeof options.builder == "boolean" && options.builder === false) return;
|
|
235
|
-
logger
|
|
215
|
+
logger.info("building electron app...");
|
|
236
216
|
const DIST_PATH = path.join(cwd(), path.dirname(resolvedConfig.build.outDir));
|
|
237
217
|
createPkg(options, resolvedConfig);
|
|
238
|
-
logger
|
|
218
|
+
logger.info(`create package.json and exec "npm install"`);
|
|
239
219
|
execaSync(`npm install --emit=dev`, {
|
|
240
220
|
cwd: DIST_PATH,
|
|
241
221
|
shell: true
|
|
242
222
|
});
|
|
243
|
-
logger
|
|
223
|
+
logger.info(`run electron-builder to package app`);
|
|
244
224
|
const config = getBuilderConfig(options, resolvedConfig);
|
|
245
225
|
const { build: build$1 } = await import("electron-builder");
|
|
246
226
|
await build$1({ config });
|
|
@@ -248,7 +228,6 @@ async function runElectronBuilder(options, resolvedConfig) {
|
|
|
248
228
|
|
|
249
229
|
//#endregion
|
|
250
230
|
//#region src/main.ts
|
|
251
|
-
const logger = createLogger();
|
|
252
231
|
function getBuildOptions(options) {
|
|
253
232
|
return ["main", "preload"].filter((s) => options[s] && options[s].entry).map((s) => {
|
|
254
233
|
options[s].__NAME__ = s;
|
|
@@ -264,7 +243,6 @@ function getBuildOptions(options) {
|
|
|
264
243
|
* startup electron app
|
|
265
244
|
*/
|
|
266
245
|
async function startup(options) {
|
|
267
|
-
console.log("startup electron debug mode:", options.debug);
|
|
268
246
|
if (options.debug) return;
|
|
269
247
|
await startup.exit();
|
|
270
248
|
const args = [".", "--no-sandbox"];
|
|
@@ -300,8 +278,9 @@ async function runServe(options, server) {
|
|
|
300
278
|
const buildOptions = getBuildOptions(options);
|
|
301
279
|
const buildCounts = [0, buildOptions.length > 1 ? 0 : 1];
|
|
302
280
|
for (let i = 0; i < buildOptions.length; i++) {
|
|
303
|
-
const
|
|
304
|
-
|
|
281
|
+
const tsOpts = buildOptions[i];
|
|
282
|
+
const { __NAME__: name, ignoreWatch, onSuccess: _onSuccess, watchFiles, ...tsdownOptions } = tsOpts;
|
|
283
|
+
logger.info(`${colors.dim(name)} build start`);
|
|
305
284
|
const onSuccess = async (config, signal) => {
|
|
306
285
|
if (_onSuccess) {
|
|
307
286
|
if (typeof _onSuccess === "string") await execa(_onSuccess);
|
|
@@ -309,14 +288,14 @@ async function runServe(options, server) {
|
|
|
309
288
|
}
|
|
310
289
|
if (buildCounts[i] <= 0) {
|
|
311
290
|
buildCounts[i]++;
|
|
312
|
-
logger.info(`${name} build success`);
|
|
291
|
+
logger.info(`${colors.dim(name)} build success`);
|
|
313
292
|
if (buildCounts[0] === 1 && buildCounts[1] === 1) {
|
|
314
293
|
logger.info("startup electron");
|
|
315
294
|
await startup(options);
|
|
316
295
|
}
|
|
317
296
|
return;
|
|
318
297
|
}
|
|
319
|
-
logger.
|
|
298
|
+
logger.info(`${colors.dim(name)} rebuild success`);
|
|
320
299
|
if (name === "main") {
|
|
321
300
|
logger.info("restart electron");
|
|
322
301
|
await startup(options);
|
|
@@ -327,7 +306,7 @@ async function runServe(options, server) {
|
|
|
327
306
|
};
|
|
328
307
|
await build({
|
|
329
308
|
onSuccess,
|
|
330
|
-
...
|
|
309
|
+
...tsdownOptions,
|
|
331
310
|
watch: watchFiles ?? (options.recommended ? [`electron/${name}`] : true),
|
|
332
311
|
ignoreWatch: (Array.isArray(ignoreWatch) ? ignoreWatch : []).concat([
|
|
333
312
|
".history",
|
|
@@ -335,7 +314,8 @@ async function runServe(options, server) {
|
|
|
335
314
|
".tmp",
|
|
336
315
|
".cache",
|
|
337
316
|
"dist"
|
|
338
|
-
])
|
|
317
|
+
]),
|
|
318
|
+
logLevel: tsOpts.logLevel ?? "silent"
|
|
339
319
|
});
|
|
340
320
|
}
|
|
341
321
|
}
|
|
@@ -446,6 +426,17 @@ function useElectronPlugin(options) {
|
|
|
446
426
|
await runServe(opts, server);
|
|
447
427
|
});
|
|
448
428
|
},
|
|
429
|
+
transformIndexHtml(html) {
|
|
430
|
+
const { devtools } = opts;
|
|
431
|
+
let port;
|
|
432
|
+
if (typeof devtools === "number") port = devtools;
|
|
433
|
+
else if (devtools === true) {
|
|
434
|
+
if (resolvedConfig.plugins.find((s) => ["vite:vue", "vite:vue2"].includes(s.name))) port = 8098;
|
|
435
|
+
else if (resolvedConfig.plugins.find((s) => ["vite:react-refresh", "vite:react-swc"].includes(s.name))) port = 8097;
|
|
436
|
+
}
|
|
437
|
+
if (port) html = html.replace("</head>", `<script src="http://localhost:${port}"><\/script></head>`);
|
|
438
|
+
return html;
|
|
439
|
+
},
|
|
449
440
|
async closeBundle() {
|
|
450
441
|
if (isServer) return;
|
|
451
442
|
await runBuild(opts);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tomjs/vite-plugin-electron",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.4.0",
|
|
5
5
|
"description": "A simple vite plugin for electron, supports esm/cjs, support esm in electron v28+",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Tom Gao",
|
|
@@ -49,24 +49,23 @@
|
|
|
49
49
|
}
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"dayjs": "^1.11.19",
|
|
53
52
|
"execa": "^9.6.1",
|
|
54
|
-
"kolorist": "^1.8.0",
|
|
55
53
|
"lodash.clonedeep": "^4.5.0",
|
|
56
54
|
"lodash.merge": "^4.6.2",
|
|
57
|
-
"
|
|
55
|
+
"picocolors": "^1.1.1",
|
|
56
|
+
"tsdown": "~0.19.0"
|
|
58
57
|
},
|
|
59
58
|
"devDependencies": {
|
|
60
|
-
"@
|
|
59
|
+
"@antfu/eslint-config": "^6.7.3",
|
|
60
|
+
"@commitlint/cli": "^20.3.1",
|
|
61
61
|
"@tomjs/commitlint": "^5.0.0",
|
|
62
|
-
"@tomjs/eslint": "^6.
|
|
63
|
-
"@tomjs/stylelint": "^7.
|
|
64
|
-
"@tomjs/tsconfig": "^
|
|
62
|
+
"@tomjs/eslint": "^6.5.0",
|
|
63
|
+
"@tomjs/stylelint": "^7.1.1",
|
|
64
|
+
"@tomjs/tsconfig": "^3.2.0",
|
|
65
65
|
"@types/lodash.clonedeep": "^4.5.9",
|
|
66
66
|
"@types/lodash.merge": "^4.6.9",
|
|
67
|
-
"@types/node": "^20.19.
|
|
67
|
+
"@types/node": "^20.19.28",
|
|
68
68
|
"electron": "^39.2.7",
|
|
69
|
-
"electron-builder": "^26.0.12",
|
|
70
69
|
"eslint": "^9.39.2",
|
|
71
70
|
"lint-staged": "^16.2.7",
|
|
72
71
|
"npm-run-all": "^4.1.5",
|