@tomjs/vite-plugin-electron 1.4.0 → 1.6.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 CHANGED
@@ -126,7 +126,7 @@ Take using `esm` as an example, but it requires Electron>=28
126
126
 
127
127
  - `package.json`
128
128
 
129
- Electron preload must use the `mjs` suffix, otherwise an error will be reported. So `esm` also uses the `mjs` suffix for output by default.
129
+ Electron `preload process` must use the `.mjs` suffix, otherwise an error will be reported, see [official documentation](https://www.electronjs.org/zh/docs/latest/tutorial/esm). So the default output of `esm` of `preload` uses the `.mjs` suffix. For consistency, `main process` also ends with `.mjs`
130
130
 
131
131
  ```json
132
132
  {
@@ -201,8 +201,9 @@ export default defineConfig({
201
201
  | external | `string[]` | | Don't bundle these modules, but dependencies and peerDependencies in your package.json are always excluded.[See more](https://tsup.egoist.dev/#excluding-packages) |
202
202
  | main | [MainOptions](#MainOptions) | | Configuration options for the electron main process. |
203
203
  | preload | [PreloadOptions](#PreloadOptions) | | Configuration options for the electron preload process. |
204
- | debug | `boolean` | `false` | Electron debug mode, don't startup electron. You can also use `process.env.APP_ELECTRON_DEBUG`. Default is false. |
205
- | 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.APP_ELECTRON_INSPECT`. See [debugging-main-process](https://www.electronjs.org/docs/latest/tutorial/debugging-main-process) for more information. |
204
+ | debug | `boolean` | `false` | Electron debug mode, don't startup electron. You can also use `process.env.VITE_ELECTRON_DEBUG`. Default is false. |
205
+ | 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`. |
206
+ | 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. |
206
207
 
207
208
  **Notice**
208
209
 
@@ -234,7 +235,7 @@ Based on [Options](https://paka.dev/npm/tsup) of [tsup](https://tsup.egoist.dev/
234
235
  | outDir | `string` | "dist-electron/preload" | The output directory for the preload process files |
235
236
  | onSuccess | `() => Promise<void \| undefined \| (() => void \| Promise<void>)>` | `undefined` | A function that will be executed after the build succeeds. |
236
237
 
237
- # BuilderOptions
238
+ ### BuilderOptions
238
239
 
239
240
  When `recommended` and `builder.enable` are both `true`, use [electron-builder](https://www.electron.build) to package Electron applications.
240
241
 
@@ -247,7 +248,6 @@ To use this function, you need to install additional `electron-builder`
247
248
 
248
249
  | Property | Type | Default | Description |
249
250
  | --- | --- | --- | --- |
250
- | enable | `boolean` | `false` | Whether to enable the [electron-builder](https://www.electron.build). |
251
251
  | appId | `string` | `"com.electron.${name}"` | The application id. [See More](https://www.electron.build/configuration/configuration#configuration) |
252
252
  | productName | `string` | `"com.electron.${name}"` | product name.[See More](https://www.electron.build/configuration/configuration#configuration) |
253
253
  | builderConfig | [Configuration](https://www.electron.build/configuration/configuration#configurationF) | `undefined` | [electron-builder](https://www.electron.build)'s [Configuration](https://www.electron.build/configuration/configuration#configuration) |
@@ -300,6 +300,24 @@ const config = {
300
300
  | sourcemap | `true` | `false` |
301
301
  | minify | `false` | `true` |
302
302
 
303
+ ## Environment Variables
304
+
305
+ ### Vite plugin variables
306
+
307
+ | Variable | Description |
308
+ | --- | --- |
309
+ | `VITE_ELECTRON_DEBUG` | Electron main process debug, don't startup electron. When value is true or 1 to enable, false or 0 to disable.Default is undefined. |
310
+ | `VITE_ELECTRON_INSPECT` | Electron will listen for V8 inspector protocol messages on the specified port, an external debugger will need to connect on this port. When value is true, the default port is 5858. |
311
+ | `VITE_ELECTRON_BUILDER` | Enable [electron-builder](https://www.electron.build) to package. When value is true or 1 to enable, false or 0 to disable. Default is undefined. |
312
+
313
+ ### Application variables
314
+
315
+ Electron `main process` and `renderer process` use.
316
+
317
+ | Variable | Description |
318
+ | ------------------ | -------------------------- |
319
+ | APP_DEV_SERVER_URL | The url of the dev server. |
320
+
303
321
  ## Debug
304
322
 
305
323
  ### Web debugging
@@ -328,16 +346,7 @@ app.whenReady().then(() => {
328
346
  });
329
347
  ```
330
348
 
331
- ### Main thread debugging
332
-
333
- #### Turn on debugging
334
-
335
- Start code compilation through the following configuration or `ELECTRON_DEBUG=1 vite dev`
336
-
337
- - Enable by setting `APP_ELECTRON_DEBUG=1` in `.env.development` file
338
- - `vite.config.js` configures `electron({ debug: true })` to be turned on
339
-
340
- #### VSCODE
349
+ ### Main Process Debug
341
350
 
342
351
  Run `Debug Main Process` through `vscode` to debug the main thread. For debugging tools, refer to [Official Documentation](https://code.visualstudio.com/docs/editor/debugging)
343
352
 
@@ -349,6 +358,7 @@ Run `Debug Main Process` through `vscode` to debug the main thread. For debuggin
349
358
  "configurations": [
350
359
  {
351
360
  "name": "Debug Main Process",
361
+ "preLaunchTask": "npm:debug",
352
362
  "type": "node",
353
363
  "request": "launch",
354
364
  "cwd": "${workspaceFolder}",
@@ -357,8 +367,52 @@ Run `Debug Main Process` through `vscode` to debug the main thread. For debuggin
357
367
  "runtimeExecutable": "${workspaceFolder}/node_modules/.bin/electron.cmd"
358
368
  },
359
369
  "args": ["."],
370
+ "outFiles": [
371
+ "${workspaceFolder}/**/*.js",
372
+ "${workspaceFolder}/**/*.cjs",
373
+ "${workspaceFolder}/**/*.mjs",
374
+ "!**/node_modules/**"
375
+ ],
360
376
  "envFile": "${workspaceFolder}/node_modules/@tomjs/vite-plugin-electron/debug/.env"
361
377
  }
362
378
  ]
363
379
  }
364
380
  ```
381
+
382
+ `tasks.json` is configured as follows:
383
+
384
+ ```json
385
+ {
386
+ "version": "2.0.0",
387
+ "tasks": [
388
+ {
389
+ "label": "npm:debug",
390
+ "type": "npm",
391
+ "script": "debug",
392
+ "detail": "cross-env VITE_ELECTRON_DEBUG=1 vite",
393
+ "isBackground": true,
394
+ "problemMatcher": {
395
+ "owner": "typescript",
396
+ "fileLocation": "relative",
397
+ "pattern": {
398
+ "regexp": "^([a-zA-Z]\\:/?([\\w\\-]/?)+\\.\\w+):(\\d+):(\\d+): (ERROR|WARNING)\\: (.*)$",
399
+ "file": 1,
400
+ "line": 3,
401
+ "column": 4,
402
+ "code": 5,
403
+ "message": 6
404
+ },
405
+ "background": {
406
+ "activeOnStart": true,
407
+ "beginsPattern": "^.*VITE v.* ready in \\d* ms.*$",
408
+ "endsPattern": "^.*\\[@tomjs:electron\\] startup electron*$"
409
+ }
410
+ }
411
+ }
412
+ ]
413
+ }
414
+ ```
415
+
416
+ ### Preload process Debug
417
+
418
+ Use `DevTools` to debug the `preload process`.
package/README.zh_CN.md CHANGED
@@ -126,7 +126,7 @@ app.whenReady().then(createWindow);
126
126
 
127
127
  - `package.json`
128
128
 
129
- Electron preload 必须使用 `mjs` 后缀,否则报错。所以 `esm` 也默认输出使用 `mjs` 后缀。
129
+ Electron `preload process` 必须使用 `.mjs` 后缀,否则报错,查看[官方文档](https://www.electronjs.org/zh/docs/latest/tutorial/esm)。所以 `preload` 的 `esm` 默认输出使用 `mjs` 后缀。为了保持一致性,`main process` 也以 `.mjs` 结尾。
130
130
 
131
131
  ```json
132
132
  {
@@ -202,8 +202,9 @@ export default defineConfig({
202
202
  | external | `string[]` | | 不打包这些模块,但是 `dependencies` and `peerDependencies` 默认排除,[详见](https://tsup.egoist.dev/#excluding-packages) |
203
203
  | main | [MainOptions](#MainOptions) | | electron main 进程选项 |
204
204
  | preload | [PreloadOptions](#PreloadOptions) | | electron preload 进程选项 |
205
- | debug | `boolean` | `false` | Electron调试模式,不启动Electron。 您还可以使用 `process.env.APP_ELECTRON_DEBUG` |
206
- | inspect | `boolean` | `false` | Electron 将监听指定 port 上的 V8 调试协议消息, 外部调试器需要连接到此端口上。您还可以使用 `process.env.APP_ELECTRON_INSPECT`。 有关更多信息,请参阅[debugging-main-process](https://www.electronjs.org/zh/docs/latest/tutorial/debugging-main-process)。 |
205
+ | debug | `boolean` | `false` | Electron调试模式,不启动Electron。 您还可以使用 `process.env.VITE_ELECTRON_DEBUG` |
206
+ | 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` 开启它。 |
207
+ | 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)。 |
207
208
 
208
209
  `recommended` 选项用于设置默认配置和行为,几乎可以达到零配置使用,默认为 `true` 。如果你要自定义配置,请设置它为`false`。以下默认的前提条件是使用推荐的 [项目结构](#目录结构)。
209
210
 
@@ -246,7 +247,6 @@ _不适合所有人使用。_
246
247
 
247
248
  | 参数名 | 类型 | 默认值 | 说明 |
248
249
  | --- | --- | --- | --- |
249
- | enable | `boolean` | `false` | 是否启用 [electron-builder](https://www.electron.build) |
250
250
  | appId | `string` | `"com.electron.${name}"` | 应用程序 ID。[详细](https://www.electron.build/configuration/configuration#configuration) |
251
251
  | productName | `string` | `` | 应用程序名称。[详细](https://www.electron.build/configuration/configuration#configuration) |
252
252
  | builderConfig | [Configuration](https://www.electron.build/configuration/configuration#configurationF) | `undefined` | [electron-builder](https://www.electron.build) 的 [Configuration](https://www.electron.build/configuration/configuration#configuration) |
@@ -299,6 +299,24 @@ const config = {
299
299
  | sourcemap | `true` | `false` |
300
300
  | minify | `false` | `true` |
301
301
 
302
+ ## 环境变量
303
+
304
+ ### vite 插件变量
305
+
306
+ | 变量 | 描述 |
307
+ | --- | --- |
308
+ | `VITE_ELECTRON_DEBUG` | Electron主进程调试,不要启动Electron。 当值为 true 或 1 时启用,为 false 或 0 时禁用。默认值未定义。 |
309
+ | `VITE_ELECTRON_INSPECT` | Electron 将在指定端口上侦听 V8 检查器协议消息,外部调试器需要连接到该端口。 当值为 true 时,默认端口为 5858。 |
310
+ | `VITE_ELECTRON_BUILDER` | 启用 [ Electron-builder ](https://www.electron.build) 进行打包。 当值为 true 或 1 时启用,为 false 或 0 时禁用。 默认值未定义。 |
311
+
312
+ ### 应用变量
313
+
314
+ Electron `main process` 和 `renderer process` 使用。
315
+
316
+ | 变量 | 描述 |
317
+ | ------------------ | --------------------- |
318
+ | APP_DEV_SERVER_URL | Vite 开发服务器的 URL |
319
+
302
320
  ## 调试
303
321
 
304
322
  ### Web调试
@@ -327,16 +345,7 @@ app.whenReady().then(() => {
327
345
  });
328
346
  ```
329
347
 
330
- ### 主线程调试
331
-
332
- #### 开启调试
333
-
334
- 通过如下配置或者 `ELECTRON_DEBUG=1 vite dev` 启动代码编译
335
-
336
- - 通过 `.env.development` 文件设置 `APP_ELECTRON_DEBUG=1` 开启
337
- - `vite.config.js` 配置 `electron({ debug: true })` 开启
338
-
339
- #### VSCODE
348
+ ### Main Process 调试
340
349
 
341
350
  通过 `vscode` 运行 `Debug Main Process` 调试主线程,调试工具参考 [官方文档](https://code.visualstudio.com/docs/editor/debugging)
342
351
 
@@ -348,6 +357,7 @@ app.whenReady().then(() => {
348
357
  "configurations": [
349
358
  {
350
359
  "name": "Debug Main Process",
360
+ "preLaunchTask": "npm:debug",
351
361
  "type": "node",
352
362
  "request": "launch",
353
363
  "cwd": "${workspaceFolder}",
@@ -356,8 +366,52 @@ app.whenReady().then(() => {
356
366
  "runtimeExecutable": "${workspaceFolder}/node_modules/.bin/electron.cmd"
357
367
  },
358
368
  "args": ["."],
369
+ "outFiles": [
370
+ "${workspaceFolder}/**/*.js",
371
+ "${workspaceFolder}/**/*.cjs",
372
+ "${workspaceFolder}/**/*.mjs",
373
+ "!**/node_modules/**"
374
+ ],
359
375
  "envFile": "${workspaceFolder}/node_modules/@tomjs/vite-plugin-electron/debug/.env"
360
376
  }
361
377
  ]
362
378
  }
363
379
  ```
380
+
381
+ `tasks.json` 配置如下:
382
+
383
+ ```json
384
+ {
385
+ "version": "2.0.0",
386
+ "tasks": [
387
+ {
388
+ "label": "npm:debug",
389
+ "type": "npm",
390
+ "script": "debug",
391
+ "detail": "cross-env VITE_ELECTRON_DEBUG=1 vite",
392
+ "isBackground": true,
393
+ "problemMatcher": {
394
+ "owner": "typescript",
395
+ "fileLocation": "relative",
396
+ "pattern": {
397
+ "regexp": "^([a-zA-Z]\\:/?([\\w\\-]/?)+\\.\\w+):(\\d+):(\\d+): (ERROR|WARNING)\\: (.*)$",
398
+ "file": 1,
399
+ "line": 3,
400
+ "column": 4,
401
+ "code": 5,
402
+ "message": 6
403
+ },
404
+ "background": {
405
+ "activeOnStart": true,
406
+ "beginsPattern": "^.*VITE v.* ready in \\d* ms.*$",
407
+ "endsPattern": "^.*\\[@tomjs:electron\\] startup electron*$"
408
+ }
409
+ }
410
+ }
411
+ ]
412
+ }
413
+ ```
414
+
415
+ ### Preload Process 调试
416
+
417
+ 使用 `DevTools` 调试 `preload process`.
package/dist/index.d.mts CHANGED
@@ -59,7 +59,8 @@ interface PreloadOptions extends Omit<Options, 'entry' | 'format' | 'outDir' | '
59
59
  interface BuilderOptions {
60
60
  /**
61
61
  * Whether to enable the [electron-builder](https://www.electron.build), the default is false.
62
- * @default false
62
+ * @default true
63
+ * @deprecated
63
64
  */
64
65
  enable?: boolean;
65
66
  /**
@@ -109,15 +110,15 @@ interface PluginOptions {
109
110
  * * In the `build.outDir` directory configured in vite, generate a new package.json based on the configuration and package.json, excluding non-dependencies.
110
111
  * * Execute `npm install` and then package.
111
112
  */
112
- builder?: BuilderOptions;
113
+ builder?: boolean | BuilderOptions;
113
114
  /**
114
- * electron debug mode, don't startup electron. You can also use `process.env.APP_ELECTRON_DEBUG`. Default is false.
115
+ * electron debug mode, don't startup electron. You can also use `process.env.VITE_ELECTRON_DEBUG`. Default is false.
115
116
  * @default false
116
117
  */
117
118
  debug?: boolean;
118
119
  /**
119
120
  * Electron will listen for V8 inspector protocol messages on the specified port, an external debugger will need to connect on this port.
120
- * You can also use `process.env.APP_ELECTRON_INSPECT`. See [debugging-main-process](https://www.electronjs.org/docs/latest/tutorial/debugging-main-process) for more information.
121
+ * 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.
121
122
  * The default port is false.
122
123
  * @see https://www.electronjs.org/docs/latest/tutorial/debugging-main-process
123
124
  * @default false
package/dist/index.d.ts CHANGED
@@ -59,7 +59,8 @@ interface PreloadOptions extends Omit<Options, 'entry' | 'format' | 'outDir' | '
59
59
  interface BuilderOptions {
60
60
  /**
61
61
  * Whether to enable the [electron-builder](https://www.electron.build), the default is false.
62
- * @default false
62
+ * @default true
63
+ * @deprecated
63
64
  */
64
65
  enable?: boolean;
65
66
  /**
@@ -109,15 +110,15 @@ interface PluginOptions {
109
110
  * * In the `build.outDir` directory configured in vite, generate a new package.json based on the configuration and package.json, excluding non-dependencies.
110
111
  * * Execute `npm install` and then package.
111
112
  */
112
- builder?: BuilderOptions;
113
+ builder?: boolean | BuilderOptions;
113
114
  /**
114
- * electron debug mode, don't startup electron. You can also use `process.env.APP_ELECTRON_DEBUG`. Default is false.
115
+ * electron debug mode, don't startup electron. You can also use `process.env.VITE_ELECTRON_DEBUG`. Default is false.
115
116
  * @default false
116
117
  */
117
118
  debug?: boolean;
118
119
  /**
119
120
  * Electron will listen for V8 inspector protocol messages on the specified port, an external debugger will need to connect on this port.
120
- * You can also use `process.env.APP_ELECTRON_INSPECT`. See [debugging-main-process](https://www.electronjs.org/docs/latest/tutorial/debugging-main-process) for more information.
121
+ * 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.
121
122
  * The default port is false.
122
123
  * @see https://www.electronjs.org/docs/latest/tutorial/debugging-main-process
123
124
  * @default false
package/dist/index.js CHANGED
@@ -10,6 +10,7 @@ var _os = require('os'); var _os2 = _interopRequireDefault(_os);
10
10
 
11
11
 
12
12
 
13
+ var _shelljs = require('shelljs'); var _shelljs2 = _interopRequireDefault(_shelljs);
13
14
 
14
15
  // src/logger.ts
15
16
  var _dayjs = require('dayjs'); var _dayjs2 = _interopRequireDefault(_dayjs);
@@ -60,7 +61,6 @@ var createLogger = (tag) => {
60
61
  };
61
62
 
62
63
  // src/utils.ts
63
- var _child_process = require('child_process');
64
64
 
65
65
  var _module = require('module');
66
66
  function readJson(path3) {
@@ -71,9 +71,6 @@ function readJson(path3) {
71
71
  function writeJson(path3, data) {
72
72
  _fs2.default.writeFileSync(path3, JSON.stringify(data, null, 2), "utf8");
73
73
  }
74
- function exec(command, args, options) {
75
- return _child_process.spawnSync.call(void 0, command, args, Object.assign({ encoding: "utf8" }, options));
76
- }
77
74
 
78
75
  // src/builder.ts
79
76
  var logger = createLogger();
@@ -82,8 +79,8 @@ function getMirror() {
82
79
  if (mirror) {
83
80
  return mirror;
84
81
  }
85
- const res = exec(_os2.default.platform() === "win32" ? "npm.cmd" : "npm", ["config", "get", "registry"]);
86
- if (res.status === 0) {
82
+ const res = _shelljs2.default.exec("npm config get registry", { silent: true });
83
+ if (res.code === 0) {
87
84
  let registry = res.stdout;
88
85
  if (!registry) {
89
86
  return;
@@ -131,6 +128,9 @@ function getBuilderConfig(options, resolvedConfig) {
131
128
  deleteAppDataOnUninstall: false
132
129
  }
133
130
  };
131
+ if (typeof options.builder == "boolean") {
132
+ return config;
133
+ }
134
134
  const { appId, productName } = options.builder || {};
135
135
  return _lodashmerge2.default.call(void 0, config, { appId, productName }, (_a = options.builder) == null ? void 0 : _a.builderConfig);
136
136
  }
@@ -205,10 +205,13 @@ function createPkg(options, resolvedConfig) {
205
205
  return newPkg;
206
206
  }
207
207
  async function runElectronBuilder(options, resolvedConfig) {
208
+ if (typeof options.builder == "boolean" && options.builder == false) {
209
+ return;
210
+ }
208
211
  logger.info("building electron app...");
209
212
  const DIST_PATH = _path2.default.join(_process.cwd.call(void 0, ), _path2.default.dirname(resolvedConfig.build.outDir));
210
213
  logger.info(`create package.json and exec "npm install"`);
211
- exec(`cd ${DIST_PATH} && npm run build`);
214
+ _shelljs2.default.exec(`cd ${DIST_PATH} && npm i`);
212
215
  logger.info(`run electron-builder to package app`);
213
216
  const config = getBuilderConfig(options, resolvedConfig);
214
217
  const { build } = await Promise.resolve().then(() => _interopRequireWildcard(require("electron-builder")));
@@ -218,7 +221,7 @@ async function runElectronBuilder(options, resolvedConfig) {
218
221
  }
219
222
 
220
223
  // src/main.ts
221
-
224
+ var _child_process = require('child_process');
222
225
  var _electron = require('electron'); var _electron2 = _interopRequireDefault(_electron);
223
226
  var _treekill = require('tree-kill'); var _treekill2 = _interopRequireDefault(_treekill);
224
227
  var _tsup = require('tsup');
@@ -286,19 +289,19 @@ async function runServe(options, server) {
286
289
  }
287
290
  if (buildCounts[i] <= 0) {
288
291
  buildCounts[i]++;
289
- logger2.info(`${name} build succeeded`);
292
+ logger2.info(`${name} build success`);
290
293
  if (buildCounts[0] == 1 && buildCounts[1] == 1) {
291
- logger2.info("electron startup");
294
+ logger2.info("startup electron");
292
295
  await startup(options);
293
296
  }
294
297
  return;
295
298
  }
296
- logger2.success(`${name} rebuild succeeded!`);
299
+ logger2.success(`${name} rebuild success`);
297
300
  if (name === "main") {
298
- logger2.info("electron restart");
301
+ logger2.info("restart electron");
299
302
  await startup(options);
300
303
  } else {
301
- logger2.info("page reload");
304
+ logger2.info("reload page");
302
305
  server.ws.send({
303
306
  type: "full-reload"
304
307
  });
@@ -352,7 +355,8 @@ function preMergeOptions(options) {
352
355
  },
353
356
  preload: {
354
357
  ...electron2
355
- }
358
+ },
359
+ builder: false
356
360
  },
357
361
  _lodashclonedeep2.default.call(void 0, options)
358
362
  );
@@ -375,7 +379,7 @@ function preMergeOptions(options) {
375
379
  return opts;
376
380
  }
377
381
  function geNumberBooleanValue(value) {
378
- if (typeof value !== "string") {
382
+ if (typeof value !== "string" || value.trim() === "") {
379
383
  return;
380
384
  }
381
385
  if (["true", "false"].includes(value)) {
@@ -384,6 +388,18 @@ function geNumberBooleanValue(value) {
384
388
  const v = Number(value);
385
389
  return Number.isNaN(v) ? void 0 : v;
386
390
  }
391
+ function getBooleanValue(value) {
392
+ if (typeof value !== "string" || value.trim() === "") {
393
+ return;
394
+ }
395
+ if (["true", "false"].includes(value)) {
396
+ return value === "true";
397
+ }
398
+ if (["1", "0"].includes(value)) {
399
+ return value === "1";
400
+ }
401
+ return;
402
+ }
387
403
  function useElectronPlugin(options) {
388
404
  const opts = preMergeOptions(options);
389
405
  let isServer = false;
@@ -428,8 +444,9 @@ function useElectronPlugin(options) {
428
444
  };
429
445
  },
430
446
  configResolved(config) {
431
- opts.debug = config.env.APP_ELECTRON_DEBUG ? !!config.env.APP_ELECTRON_DEBUG : opts.debug;
432
- opts.inspect = config.env.APP_ELECTRON_INSPECT ? geNumberBooleanValue(config.env.APP_ELECTRON_INSPECT) : opts.inspect;
447
+ opts.debug = _nullishCoalesce(getBooleanValue(config.env.VITE_ELECTRON_DEBUG), () => ( opts.debug));
448
+ opts.inspect = _nullishCoalesce(geNumberBooleanValue(config.env.VITE_ELECTRON_INSPECT), () => ( opts.inspect));
449
+ opts.builder = _nullishCoalesce(getBooleanValue(config.env.VITE_ELECTRON_BUILDER), () => ( opts.builder));
433
450
  resolvedConfig = config;
434
451
  },
435
452
  configureServer(server) {
@@ -454,12 +471,11 @@ ${env}`);
454
471
  });
455
472
  },
456
473
  async closeBundle() {
457
- var _a;
458
474
  if (isServer) {
459
475
  return;
460
476
  }
461
477
  await runBuild(opts);
462
- if (opts.recommended && ((_a = opts.builder) == null ? void 0 : _a.enable)) {
478
+ if (opts.recommended && opts.builder) {
463
479
  await runElectronBuilder(opts, resolvedConfig);
464
480
  }
465
481
  }
package/dist/index.mjs CHANGED
@@ -10,6 +10,7 @@ import os from "os";
10
10
  import path from "path";
11
11
  import { cwd } from "process";
12
12
  import merge from "lodash.merge";
13
+ import shell from "shelljs";
13
14
 
14
15
  // src/logger.ts
15
16
  import dayjs from "dayjs";
@@ -60,7 +61,6 @@ var createLogger = (tag) => {
60
61
  };
61
62
 
62
63
  // src/utils.ts
63
- import { spawnSync } from "child_process";
64
64
  import fs from "fs";
65
65
  function readJson(path3) {
66
66
  if (fs.existsSync(path3)) {
@@ -70,9 +70,6 @@ function readJson(path3) {
70
70
  function writeJson(path3, data) {
71
71
  fs.writeFileSync(path3, JSON.stringify(data, null, 2), "utf8");
72
72
  }
73
- function exec(command, args, options) {
74
- return spawnSync(command, args, Object.assign({ encoding: "utf8" }, options));
75
- }
76
73
 
77
74
  // src/builder.ts
78
75
  var logger = createLogger();
@@ -81,8 +78,8 @@ function getMirror() {
81
78
  if (mirror) {
82
79
  return mirror;
83
80
  }
84
- const res = exec(os.platform() === "win32" ? "npm.cmd" : "npm", ["config", "get", "registry"]);
85
- if (res.status === 0) {
81
+ const res = shell.exec("npm config get registry", { silent: true });
82
+ if (res.code === 0) {
86
83
  let registry = res.stdout;
87
84
  if (!registry) {
88
85
  return;
@@ -130,6 +127,9 @@ function getBuilderConfig(options, resolvedConfig) {
130
127
  deleteAppDataOnUninstall: false
131
128
  }
132
129
  };
130
+ if (typeof options.builder == "boolean") {
131
+ return config;
132
+ }
133
133
  const { appId, productName } = options.builder || {};
134
134
  return merge(config, { appId, productName }, (_a = options.builder) == null ? void 0 : _a.builderConfig);
135
135
  }
@@ -204,10 +204,13 @@ function createPkg(options, resolvedConfig) {
204
204
  return newPkg;
205
205
  }
206
206
  async function runElectronBuilder(options, resolvedConfig) {
207
+ if (typeof options.builder == "boolean" && options.builder == false) {
208
+ return;
209
+ }
207
210
  logger.info("building electron app...");
208
211
  const DIST_PATH = path.join(cwd(), path.dirname(resolvedConfig.build.outDir));
209
212
  logger.info(`create package.json and exec "npm install"`);
210
- exec(`cd ${DIST_PATH} && npm run build`);
213
+ shell.exec(`cd ${DIST_PATH} && npm i`);
211
214
  logger.info(`run electron-builder to package app`);
212
215
  const config = getBuilderConfig(options, resolvedConfig);
213
216
  const { build } = await import("electron-builder");
@@ -285,19 +288,19 @@ async function runServe(options, server) {
285
288
  }
286
289
  if (buildCounts[i] <= 0) {
287
290
  buildCounts[i]++;
288
- logger2.info(`${name} build succeeded`);
291
+ logger2.info(`${name} build success`);
289
292
  if (buildCounts[0] == 1 && buildCounts[1] == 1) {
290
- logger2.info("electron startup");
293
+ logger2.info("startup electron");
291
294
  await startup(options);
292
295
  }
293
296
  return;
294
297
  }
295
- logger2.success(`${name} rebuild succeeded!`);
298
+ logger2.success(`${name} rebuild success`);
296
299
  if (name === "main") {
297
- logger2.info("electron restart");
300
+ logger2.info("restart electron");
298
301
  await startup(options);
299
302
  } else {
300
- logger2.info("page reload");
303
+ logger2.info("reload page");
301
304
  server.ws.send({
302
305
  type: "full-reload"
303
306
  });
@@ -351,7 +354,8 @@ function preMergeOptions(options) {
351
354
  },
352
355
  preload: {
353
356
  ...electron2
354
- }
357
+ },
358
+ builder: false
355
359
  },
356
360
  cloneDeep(options)
357
361
  );
@@ -374,7 +378,7 @@ function preMergeOptions(options) {
374
378
  return opts;
375
379
  }
376
380
  function geNumberBooleanValue(value) {
377
- if (typeof value !== "string") {
381
+ if (typeof value !== "string" || value.trim() === "") {
378
382
  return;
379
383
  }
380
384
  if (["true", "false"].includes(value)) {
@@ -383,6 +387,18 @@ function geNumberBooleanValue(value) {
383
387
  const v = Number(value);
384
388
  return Number.isNaN(v) ? void 0 : v;
385
389
  }
390
+ function getBooleanValue(value) {
391
+ if (typeof value !== "string" || value.trim() === "") {
392
+ return;
393
+ }
394
+ if (["true", "false"].includes(value)) {
395
+ return value === "true";
396
+ }
397
+ if (["1", "0"].includes(value)) {
398
+ return value === "1";
399
+ }
400
+ return;
401
+ }
386
402
  function useElectronPlugin(options) {
387
403
  const opts = preMergeOptions(options);
388
404
  let isServer = false;
@@ -427,8 +443,9 @@ function useElectronPlugin(options) {
427
443
  };
428
444
  },
429
445
  configResolved(config) {
430
- opts.debug = config.env.APP_ELECTRON_DEBUG ? !!config.env.APP_ELECTRON_DEBUG : opts.debug;
431
- opts.inspect = config.env.APP_ELECTRON_INSPECT ? geNumberBooleanValue(config.env.APP_ELECTRON_INSPECT) : opts.inspect;
446
+ opts.debug = getBooleanValue(config.env.VITE_ELECTRON_DEBUG) ?? opts.debug;
447
+ opts.inspect = geNumberBooleanValue(config.env.VITE_ELECTRON_INSPECT) ?? opts.inspect;
448
+ opts.builder = getBooleanValue(config.env.VITE_ELECTRON_BUILDER) ?? opts.builder;
432
449
  resolvedConfig = config;
433
450
  },
434
451
  configureServer(server) {
@@ -453,12 +470,11 @@ ${env}`);
453
470
  });
454
471
  },
455
472
  async closeBundle() {
456
- var _a;
457
473
  if (isServer) {
458
474
  return;
459
475
  }
460
476
  await runBuild(opts);
461
- if (opts.recommended && ((_a = opts.builder) == null ? void 0 : _a.enable)) {
477
+ if (opts.recommended && opts.builder) {
462
478
  await runElectronBuilder(opts, resolvedConfig);
463
479
  }
464
480
  }
package/env.d.ts CHANGED
@@ -13,13 +13,5 @@ declare namespace NodeJS {
13
13
  * The url of the dev server.
14
14
  */
15
15
  APP_DEV_SERVER_URL?: string;
16
- /**
17
- * Electron main process debug, don't startup electron
18
- */
19
- APP_ELECTRON_DEBUG?: string;
20
- /**
21
- * Electron will listen for V8 inspector protocol messages on the specified port, an external debugger will need to connect on this port. The default port is 5858.
22
- */
23
- APP_ELECTRON_INSPECT?: string;
24
16
  }
25
17
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tomjs/vite-plugin-electron",
3
- "version": "1.4.0",
3
+ "version": "1.6.0",
4
4
  "description": "A simple vite plugin for electron, supports esm/cjs, support esm in electron v28+",
5
5
  "keywords": [
6
6
  "vite",
@@ -40,21 +40,12 @@
40
40
  "type": "git",
41
41
  "url": "git+https://github.com/tomgao365/vite-plugin-electron.git"
42
42
  },
43
- "scripts": {
44
- "dev": "tsup --watch",
45
- "build": "tsup",
46
- "lint": "run-s lint:eslint lint:stylelint lint:prettier",
47
- "lint:eslint": "eslint \"{src,examples}/**/*.{js,cjs,ts,tsx,vue}\" *.{js,cjs,ts} --fix --cache",
48
- "lint:stylelint": "stylelint \"examples/**/*.{css,scss,less,vue,html}\" --fix --cache",
49
- "lint:prettier": "prettier --write .",
50
- "prepare": "husky install",
51
- "prepublishOnly": "npm run build && np --any-branch --no-yarn --yolo --no-publish --message \"chore: release v%s\""
52
- },
53
43
  "dependencies": {
54
44
  "dayjs": "^1.11.10",
55
45
  "kolorist": "^1.8.0",
56
46
  "lodash.clonedeep": "^4.5.0",
57
47
  "lodash.merge": "^4.6.2",
48
+ "shelljs": "^0.8.5",
58
49
  "tree-kill": "^1.2.2",
59
50
  "tsup": "7.2.0"
60
51
  },
@@ -68,12 +59,13 @@
68
59
  "@types/lodash.clonedeep": "^4.5.9",
69
60
  "@types/lodash.merge": "^4.6.9",
70
61
  "@types/node": "^18.19.3",
62
+ "@types/shelljs": "^0.8.15",
71
63
  "eslint": "^8.55.0",
72
64
  "husky": "^8.0.3",
73
65
  "lint-staged": "^15.2.0",
74
66
  "np": "^9.2.0",
75
67
  "npm-run-all": "^4.1.5",
76
- "prettier": "^3.1.0",
68
+ "prettier": "^3.1.1",
77
69
  "rimraf": "^5.0.5",
78
70
  "stylelint": "^15.11.0",
79
71
  "tsx": "^4.6.2",
@@ -88,5 +80,13 @@
88
80
  "electron-builder": {
89
81
  "optional": true
90
82
  }
83
+ },
84
+ "scripts": {
85
+ "dev": "tsup --watch",
86
+ "build": "tsup",
87
+ "lint": "run-s lint:eslint lint:stylelint lint:prettier",
88
+ "lint:eslint": "eslint \"{src,examples}/**/*.{js,cjs,ts,tsx,vue}\" *.{js,cjs,ts} --fix --cache",
89
+ "lint:stylelint": "stylelint \"examples/**/*.{css,scss,less,vue,html}\" --fix --cache",
90
+ "lint:prettier": "prettier --write ."
91
91
  }
92
- }
92
+ }