@tomjs/vite-plugin-electron 2.3.0 → 2.4.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 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`, it is 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,9 +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 Logger from "@tomjs/logger";
9
+ import { createLogger } from "vite";
10
10
  import cp, { spawn } from "node:child_process";
11
11
  import electron from "electron";
12
+ import colors from "picocolors";
12
13
  import { build } from "tsdown";
13
14
 
14
15
  //#region src/constants.ts
@@ -16,12 +17,28 @@ const PLUGIN_NAME = "tomjs:electron";
16
17
 
17
18
  //#endregion
18
19
  //#region src/logger.ts
19
- function createLogger(tag) {
20
- return new Logger({
21
- prefix: tag || `[${PLUGIN_NAME}]`,
22
- time: true
20
+ function createLogger$1(logLevel) {
21
+ const logger$1 = createLogger(logLevel, {
22
+ prefix: `[${PLUGIN_NAME}]`,
23
+ allowClearScreen: true
23
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;
24
40
  }
41
+ const logger = createLogger$1();
25
42
 
26
43
  //#endregion
27
44
  //#region src/utils.ts
@@ -92,7 +109,6 @@ function killTree(tree) {
92
109
 
93
110
  //#endregion
94
111
  //#region src/builder.ts
95
- const logger$1 = createLogger();
96
112
  function getMirror() {
97
113
  let mirror = process.env.ELECTRON_MIRROR;
98
114
  if (mirror) return mirror;
@@ -196,15 +212,15 @@ function createPkg(options, resolvedConfig) {
196
212
  }
197
213
  async function runElectronBuilder(options, resolvedConfig) {
198
214
  if (typeof options.builder == "boolean" && options.builder === false) return;
199
- logger$1.info("building electron app...");
215
+ logger.info("building electron app...");
200
216
  const DIST_PATH = path.join(cwd(), path.dirname(resolvedConfig.build.outDir));
201
217
  createPkg(options, resolvedConfig);
202
- logger$1.info(`create package.json and exec "npm install"`);
218
+ logger.info(`create package.json and exec "npm install"`);
203
219
  execaSync(`npm install --emit=dev`, {
204
220
  cwd: DIST_PATH,
205
221
  shell: true
206
222
  });
207
- logger$1.info(`run electron-builder to package app`);
223
+ logger.info(`run electron-builder to package app`);
208
224
  const config = getBuilderConfig(options, resolvedConfig);
209
225
  const { build: build$1 } = await import("electron-builder");
210
226
  await build$1({ config });
@@ -212,7 +228,6 @@ async function runElectronBuilder(options, resolvedConfig) {
212
228
 
213
229
  //#endregion
214
230
  //#region src/main.ts
215
- const logger = createLogger();
216
231
  function getBuildOptions(options) {
217
232
  return ["main", "preload"].filter((s) => options[s] && options[s].entry).map((s) => {
218
233
  options[s].__NAME__ = s;
@@ -228,7 +243,6 @@ function getBuildOptions(options) {
228
243
  * startup electron app
229
244
  */
230
245
  async function startup(options) {
231
- console.log("startup electron debug mode:", options.debug);
232
246
  if (options.debug) return;
233
247
  await startup.exit();
234
248
  const args = [".", "--no-sandbox"];
@@ -264,8 +278,9 @@ async function runServe(options, server) {
264
278
  const buildOptions = getBuildOptions(options);
265
279
  const buildCounts = [0, buildOptions.length > 1 ? 0 : 1];
266
280
  for (let i = 0; i < buildOptions.length; i++) {
267
- const { __NAME__: name, ignoreWatch, onSuccess: _onSuccess, watchFiles, ...tsupOptions } = buildOptions[i];
268
- logger.info(`${name} build`);
281
+ const tsOpts = buildOptions[i];
282
+ const { __NAME__: name, ignoreWatch, onSuccess: _onSuccess, watchFiles, ...tsdownOptions } = tsOpts;
283
+ logger.info(`${colors.dim(name)} build start`);
269
284
  const onSuccess = async (config, signal) => {
270
285
  if (_onSuccess) {
271
286
  if (typeof _onSuccess === "string") await execa(_onSuccess);
@@ -273,14 +288,14 @@ async function runServe(options, server) {
273
288
  }
274
289
  if (buildCounts[i] <= 0) {
275
290
  buildCounts[i]++;
276
- logger.info(`${name} build success`);
291
+ logger.info(`${colors.dim(name)} build success`);
277
292
  if (buildCounts[0] === 1 && buildCounts[1] === 1) {
278
293
  logger.info("startup electron");
279
294
  await startup(options);
280
295
  }
281
296
  return;
282
297
  }
283
- logger.success(`${name} rebuild success`);
298
+ logger.info(`${colors.dim(name)} rebuild success`);
284
299
  if (name === "main") {
285
300
  logger.info("restart electron");
286
301
  await startup(options);
@@ -291,7 +306,7 @@ async function runServe(options, server) {
291
306
  };
292
307
  await build({
293
308
  onSuccess,
294
- ...tsupOptions,
309
+ ...tsdownOptions,
295
310
  watch: watchFiles ?? (options.recommended ? [`electron/${name}`] : true),
296
311
  ignoreWatch: (Array.isArray(ignoreWatch) ? ignoreWatch : []).concat([
297
312
  ".history",
@@ -299,7 +314,8 @@ async function runServe(options, server) {
299
314
  ".tmp",
300
315
  ".cache",
301
316
  "dist"
302
- ])
317
+ ]),
318
+ logLevel: tsOpts.logLevel ?? "silent"
303
319
  });
304
320
  }
305
321
  }
@@ -410,6 +426,17 @@ function useElectronPlugin(options) {
410
426
  await runServe(opts, server);
411
427
  });
412
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
+ },
413
440
  async closeBundle() {
414
441
  if (isServer) return;
415
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.3.0",
4
+ "version": "2.4.1",
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,23 +49,23 @@
49
49
  }
50
50
  },
51
51
  "dependencies": {
52
- "@tomjs/logger": "^2.0.0",
53
52
  "execa": "^9.6.1",
54
53
  "lodash.clonedeep": "^4.5.0",
55
54
  "lodash.merge": "^4.6.2",
56
- "tsdown": "~0.18.1"
55
+ "picocolors": "^1.1.1",
56
+ "tsdown": "~0.19.0"
57
57
  },
58
58
  "devDependencies": {
59
- "@commitlint/cli": "^20.2.0",
59
+ "@antfu/eslint-config": "^6.7.3",
60
+ "@commitlint/cli": "^20.3.1",
60
61
  "@tomjs/commitlint": "^5.0.0",
61
- "@tomjs/eslint": "^6.4.0",
62
- "@tomjs/stylelint": "^7.0.0",
63
- "@tomjs/tsconfig": "^3.0.1",
62
+ "@tomjs/eslint": "^6.5.0",
63
+ "@tomjs/stylelint": "^7.1.1",
64
+ "@tomjs/tsconfig": "^3.2.0",
64
65
  "@types/lodash.clonedeep": "^4.5.9",
65
66
  "@types/lodash.merge": "^4.6.9",
66
- "@types/node": "^20.19.27",
67
+ "@types/node": "^20.19.28",
67
68
  "electron": "^39.2.7",
68
- "electron-builder": "^26.0.12",
69
69
  "eslint": "^9.39.2",
70
70
  "lint-staged": "^16.2.7",
71
71
  "npm-run-all": "^4.1.5",