@tomjs/vite-plugin-electron 1.2.1 → 1.3.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
@@ -6,7 +6,7 @@
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.
9
+ Many thanks to [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 libraries inspired me. I hope to use it to simplify development configuration and focus only on business development.
10
10
 
11
11
  ## Features
12
12
 
@@ -15,25 +15,18 @@ I learned [caoxiemeihao](https://github.com/caoxiemeihao)'s [vite-plugin-electro
15
15
  - Support `main`'s `Hot Restart`
16
16
  - Support `preload`'s `Hot Reload`
17
17
  - Support `esm` and `cjs`, you can use `esm` in [electron v28+](https://www.electronjs.org/blog/electron-28-0)
18
- - Support `vue` and `react` and [other frameworks](https://vitejs.dev/guide/#trying-vite-online)
18
+ - Support `vue` and `react` and other [frameworks](https://vitejs.dev/guide/#trying-vite-online) supported by `vite`
19
19
 
20
20
  ## Install
21
21
 
22
- With `pnpm`
23
-
24
22
  ```bash
23
+ # pnpm
25
24
  pnpm add @tomjs/vite-plugin-electron -D
26
- ```
27
25
 
28
- With `yarn`
29
-
30
- ```bash
26
+ # yarn
31
27
  yarn add @tomjs/vite-plugin-electron -D
32
- ```
33
-
34
- With `npm`
35
28
 
36
- ```bash
29
+ # npm
37
30
  npm i @tomjs/vite-plugin-electron --save-dev
38
31
  ```
39
32
 
@@ -188,7 +181,7 @@ export default defineConfig({
188
181
 
189
182
  | Property | Type | Default | Description |
190
183
  | --- | --- | --- | --- |
191
- | recommended | `boolean` | `true` | If set to true, it will change the main/preload/renderer outDir to be parallel outDir. For example, if vite build.outDir is 'dist', it will change main/preload/render to 'dist/main', 'dist/preload', and 'dist/renderer'. |
184
+ | recommended | `boolean` | `true` | This option is intended to provide recommended default parameters and behavior. |
192
185
  | external | `string[]` | | List of modules that should not be bundled. |
193
186
  | main | [MainOptions](#MainOptions) | | Configuration options for the electron main process. |
194
187
  | preload | [PreloadOptions](#PreloadOptions) | | Configuration options for the electron preload process. |
@@ -232,3 +225,66 @@ Based on [Options](https://paka.dev/npm/tsup) of [tsup](https://tsup.egoist.dev/
232
225
  | --------- | ------------------------ | ----------------------- |
233
226
  | sourcemap | `true` | `false` |
234
227
  | minify | `false` | `true` |
228
+
229
+ ## Debug
230
+
231
+ ### Web debugging
232
+
233
+ Use [@tomjs/electron-devtools-installer](https://npmjs.com/package/@tomjs/electron-devtools-installer) to install the `Chrome Devtools` plugins and use it like web development
234
+
235
+ ```ts
236
+ import { app } from 'electron';
237
+
238
+ app.whenReady().then(() => {
239
+ const { installExtension, REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS } = await import(
240
+ '@tomjs/electron-devtools-installer'
241
+ );
242
+
243
+ installExtension([REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS])
244
+ .then(exts => {
245
+ console.log(
246
+ 'Added Extension: ',
247
+ exts.map(s => s.name),
248
+ );
249
+ })
250
+ .catch(err => {
251
+ console.log('Failed to install extensions');
252
+ console.error(err);
253
+ });
254
+ });
255
+ ```
256
+
257
+ ### Main thread debugging
258
+
259
+ #### Turn on debugging
260
+
261
+ Start code compilation through the following configuration or `ELECTRON_DEBUG=1 vite dev`
262
+
263
+ - Enable by setting `APP_ELECTRON_DEBUG=1` in `.env` file
264
+ - `vite.config.js` configures `electron({ debug: true })` to be turned on
265
+
266
+ #### VSCODE
267
+
268
+ 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)
269
+
270
+ `launch.json` is configured as follows:
271
+
272
+ ```json
273
+ {
274
+ "version": "0.2.0",
275
+ "configurations": [
276
+ {
277
+ "name": "Debug Main Process",
278
+ "type": "node",
279
+ "request": "launch",
280
+ "cwd": "${workspaceRoot}",
281
+ "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron",
282
+ "windows": {
283
+ "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron.cmd"
284
+ },
285
+ "program": "${workspaceRoot}/dist/main/index.js",
286
+ "envFile": "${workspaceRoot}/node_modules/@tomjs/vite-plugin-electron/debug/.env"
287
+ }
288
+ ]
289
+ }
290
+ ```
package/README.zh_CN.md CHANGED
@@ -6,7 +6,7 @@
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) 这两个优秀的作品,结合自己的一些想法,开发了该插件。希望使用它能简化开发配置,只关注业务开发。
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
10
 
11
11
  ## 特性
12
12
 
@@ -15,25 +15,18 @@
15
15
  - 支持 `main` 的 `热重启`
16
16
  - 支持 `preload` 的 `热重载`
17
17
  - 支持 `esm` 和 `cjs` ,你可以在 [electron v28+](https://www.electronjs.org/zh/blog/electron-28-0) 中使用 `esm`
18
- - 支持 `vue` 和 `react` 以及 [其他框架](https://cn.vitejs.dev/guide/#trying-vite-online)
18
+ - 支持 `vue` 和 `react` 等其他 `vite` 支持的[框架](https://cn.vitejs.dev/guide/#trying-vite-online)
19
19
 
20
20
  ## 安装
21
21
 
22
- 使用 `pnpm`
23
-
24
22
  ```bash
23
+ # pnpm
25
24
  pnpm add @tomjs/vite-plugin-electron -D
26
- ```
27
25
 
28
- 使用 `yarn`
29
-
30
- ```bash
26
+ # yarn
31
27
  yarn add @tomjs/vite-plugin-electron -D
32
- ```
33
-
34
- 使用 `npm`
35
28
 
36
- ```bash
29
+ # npm
37
30
  npm i @tomjs/vite-plugin-electron --save-dev
38
31
  ```
39
32
 
@@ -189,7 +182,7 @@ export default defineConfig({
189
182
 
190
183
  | 参数名 | 类型 | 默认值 | 说明 |
191
184
  | --- | --- | --- | --- |
192
- | recommended | `boolean` | `true` | 推荐开关,如果为true,将具有以下默认行为:将main/preload/renderer的outDir更改为并行的outDir;例如,如果vite build.outDir为'dist',将main/preload/render更改为'dist/main'、'dist/preload'和'dist/renderer' |
185
+ | recommended | `boolean` | `true` | 这个选项是为了提供推荐的默认参数和行为 |
193
186
  | external | `string[]` | | 不打包这些模块 |
194
187
  | main | [MainOptions](#MainOptions) | | electron main 进程选项 |
195
188
  | preload | [PreloadOptions](#PreloadOptions) | | electron preload 进程选项 |
@@ -231,3 +224,66 @@ export default defineConfig({
231
224
  | --------- | -------------- | -------------- |
232
225
  | sourcemap | `true` | `false` |
233
226
  | minify | `false` | `true` |
227
+
228
+ ## 调试
229
+
230
+ ### Web调试
231
+
232
+ 使用 [@tomjs/electron-devtools-installer](https://npmjs.com/package/@tomjs/electron-devtools-installer) 安装 `Chrome Devtools` 插件后像 Web 开发一样使用
233
+
234
+ ```ts
235
+ import { app } from 'electron';
236
+
237
+ app.whenReady().then(() => {
238
+ const { installExtension, REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS } = await import(
239
+ '@tomjs/electron-devtools-installer'
240
+ );
241
+
242
+ installExtension([REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS])
243
+ .then(exts => {
244
+ console.log(
245
+ 'Added Extension: ',
246
+ exts.map(s => s.name),
247
+ );
248
+ })
249
+ .catch(err => {
250
+ console.log('Failed to install extensions');
251
+ console.error(err);
252
+ });
253
+ });
254
+ ```
255
+
256
+ ### 主线程调试
257
+
258
+ #### 开启调试
259
+
260
+ 通过如下配置或者 `ELECTRON_DEBUG=1 vite dev` 启动代码编译
261
+
262
+ - 通过 `.env` 文件设置 `APP_ELECTRON_DEBUG=1` 开启
263
+ - `vite.config.js` 配置 `electron({ debug: true })` 开启
264
+
265
+ #### VSCODE
266
+
267
+ 通过 `vscode` 运行 `Debug Main Process` 调试主线程,调试工具参考 [官方文档](https://code.visualstudio.com/docs/editor/debugging)
268
+
269
+ `launch.json` 配置如下:
270
+
271
+ ```json
272
+ {
273
+ "version": "0.2.0",
274
+ "configurations": [
275
+ {
276
+ "name": "Debug Main Process",
277
+ "type": "node",
278
+ "request": "launch",
279
+ "cwd": "${workspaceRoot}",
280
+ "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron",
281
+ "windows": {
282
+ "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron.cmd"
283
+ },
284
+ "program": "${workspaceRoot}/dist/main/index.js",
285
+ "envFile": "${workspaceRoot}/node_modules/@tomjs/vite-plugin-electron/debug/.env"
286
+ }
287
+ ]
288
+ }
289
+ ```
package/dist/index.d.mts CHANGED
@@ -2,7 +2,7 @@ import { Plugin } from 'vite';
2
2
  import { Options } from 'tsup';
3
3
 
4
4
  /**
5
- * Electron main process options.
5
+ * Electron main process options. See [tsup](https://tsup.egoist.dev/) and [API Doc](https://paka.dev/npm/tsup) for more information.
6
6
  * @see https://paka.dev/npm/tsup
7
7
  * @see https://unpkg.com/browse/tsup/dist/index.d.ts
8
8
  */
@@ -26,7 +26,7 @@ interface MainOptions extends Omit<Options, 'entry' | 'format' | 'outDir' | 'wat
26
26
  onSuccess?: () => Promise<void | undefined | (() => void | Promise<void>)>;
27
27
  }
28
28
  /**
29
- * Electron preload process options.
29
+ * Electron preload process options. See [tsup](https://tsup.egoist.dev/) and [API Doc](https://paka.dev/npm/tsup) for more information.
30
30
  * @see https://paka.dev/npm/tsup
31
31
  * @see https://unpkg.com/browse/tsup/dist/index.d.ts
32
32
  */
@@ -74,10 +74,10 @@ interface PluginOptions {
74
74
  */
75
75
  preload?: PreloadOptions;
76
76
  /**
77
- * electron start with the `--inspect`
78
- * @default true
77
+ * electron debug mode, don't startup electron. You can also use `process.env.APP_ELECTRON_DEBUG`. Default is false.
78
+ * @default false
79
79
  */
80
- inspect?: boolean;
80
+ debug?: boolean;
81
81
  }
82
82
 
83
83
  declare function vitePluginElectron(options?: PluginOptions): Plugin;
package/dist/index.d.ts CHANGED
@@ -2,7 +2,7 @@ import { Plugin } from 'vite';
2
2
  import { Options } from 'tsup';
3
3
 
4
4
  /**
5
- * Electron main process options.
5
+ * Electron main process options. See [tsup](https://tsup.egoist.dev/) and [API Doc](https://paka.dev/npm/tsup) for more information.
6
6
  * @see https://paka.dev/npm/tsup
7
7
  * @see https://unpkg.com/browse/tsup/dist/index.d.ts
8
8
  */
@@ -26,7 +26,7 @@ interface MainOptions extends Omit<Options, 'entry' | 'format' | 'outDir' | 'wat
26
26
  onSuccess?: () => Promise<void | undefined | (() => void | Promise<void>)>;
27
27
  }
28
28
  /**
29
- * Electron preload process options.
29
+ * Electron preload process options. See [tsup](https://tsup.egoist.dev/) and [API Doc](https://paka.dev/npm/tsup) for more information.
30
30
  * @see https://paka.dev/npm/tsup
31
31
  * @see https://unpkg.com/browse/tsup/dist/index.d.ts
32
32
  */
@@ -74,10 +74,10 @@ interface PluginOptions {
74
74
  */
75
75
  preload?: PreloadOptions;
76
76
  /**
77
- * electron start with the `--inspect`
78
- * @default true
77
+ * electron debug mode, don't startup electron. You can also use `process.env.APP_ELECTRON_DEBUG`. Default is false.
78
+ * @default false
79
79
  */
80
- inspect?: boolean;
80
+ debug?: boolean;
81
81
  }
82
82
 
83
83
  declare function vitePluginElectron(options?: PluginOptions): Plugin;
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
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
2
2
  var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs);
3
+ var _process = require('process');
3
4
  var _lodashclonedeep = require('lodash.clonedeep'); var _lodashclonedeep2 = _interopRequireDefault(_lodashclonedeep);
4
5
  var _lodashmerge = require('lodash.merge'); var _lodashmerge2 = _interopRequireDefault(_lodashmerge);
5
6
  var _path = require('path'); var _path2 = _interopRequireDefault(_path);
@@ -69,10 +70,11 @@ function getBuildOptions(options) {
69
70
  });
70
71
  }
71
72
  async function startup(options) {
73
+ if (options.debug) {
74
+ return;
75
+ }
72
76
  await startup.exit();
73
- const args = [];
74
- options.inspect && args.push("--inspect");
75
- process.electronApp = _child_process.spawn.call(void 0, _electron2.default, [".", ...args], {
77
+ process.electronApp = _child_process.spawn.call(void 0, _electron2.default, ["."], {
76
78
  stdio: "inherit"
77
79
  });
78
80
  process.electronApp.once("exit", process.exit);
@@ -97,9 +99,10 @@ startup.exit = async () => {
97
99
  });
98
100
  };
99
101
  async function runServe(options, server) {
102
+ options.debug && logger.warn(`debug mode`);
100
103
  const buildOptions = getBuildOptions(options);
104
+ const buildCounts = [0, buildOptions.length > 1 ? 0 : 1];
101
105
  for (let i = 0; i < buildOptions.length; i++) {
102
- let isFirstBuild = true;
103
106
  const tsOpts = buildOptions[i];
104
107
  const { __NAME__: name, onSuccess: _onSuccess, watch, ...tsupOptions } = tsOpts;
105
108
  logger.info(`${name} build`);
@@ -107,16 +110,21 @@ async function runServe(options, server) {
107
110
  if (typeof _onSuccess === "function") {
108
111
  await _onSuccess();
109
112
  }
110
- if (isFirstBuild) {
113
+ if (buildCounts[i] <= 0) {
114
+ buildCounts[i]++;
111
115
  logger.info(`${name} build succeeded`);
112
- isFirstBuild = false;
116
+ if (buildCounts[0] == 1 && buildCounts[1] == 1) {
117
+ logger.info("electron startup");
118
+ await startup(options);
119
+ }
113
120
  return;
114
121
  }
115
122
  logger.success(`${name} rebuild succeeded!`);
116
123
  if (name === "main") {
117
- console.log("main process exit");
124
+ logger.info("electron restart");
118
125
  await startup(options);
119
126
  } else {
127
+ logger.info("page reload");
120
128
  server.ws.send({
121
129
  type: "full-reload"
122
130
  });
@@ -124,7 +132,6 @@ async function runServe(options, server) {
124
132
  };
125
133
  await _tsup.build.call(void 0, { onSuccess, watch: true, ...tsupOptions });
126
134
  }
127
- await startup(options);
128
135
  }
129
136
  async function runBuild(options) {
130
137
  const buildOptions = getBuildOptions(options);
@@ -219,32 +226,56 @@ function vitePluginElectron(options) {
219
226
  opts.preload.outDir ||= _path2.default.join("dist-electron", "preload");
220
227
  }
221
228
  if (isDev) {
222
- opts.main.sourcemap ??= true;
223
- opts.preload.sourcemap ??= true;
229
+ opts.main.sourcemap ??= "inline";
230
+ opts.preload.sourcemap ??= "inline";
224
231
  } else {
225
232
  opts.main.minify ??= true;
226
233
  opts.preload.minify ??= true;
227
234
  }
235
+ let envPrefix = config.envPrefix;
236
+ if (!envPrefix) {
237
+ envPrefix = ["VITE_"];
238
+ } else if (typeof envPrefix === "string") {
239
+ envPrefix = [envPrefix];
240
+ }
241
+ if (!envPrefix.includes("APP_")) {
242
+ envPrefix.push("APP_");
243
+ }
228
244
  return {
245
+ envPrefix: [...new Set(envPrefix)],
229
246
  build: {
230
247
  outDir
231
248
  }
232
249
  };
233
250
  },
251
+ configResolved(config) {
252
+ opts.debug = config.env.APP_ELECTRON_DEBUG ? !!config.env.APP_ELECTRON_DEBUG : opts.debug;
253
+ },
234
254
  configureServer(server) {
235
- if (!(server == null ? void 0 : server.httpServer)) {
255
+ if (!server || !server.httpServer) {
236
256
  return;
237
257
  }
238
258
  server.httpServer.on("listening", async () => {
239
- if (server.httpServer) {
240
- const serve = server.httpServer.address();
241
- const { address, port, family } = serve;
242
- if (family === "IPv6") {
243
- process.env.APP_DEV_SERVER_URL = `http://[${address}]:${port}`;
244
- } else {
245
- process.env.APP_DEV_SERVER_URL = `http://${address}:${port}`;
246
- }
259
+ var _a;
260
+ const serve = (_a = server.httpServer) == null ? void 0 : _a.address();
261
+ const { address, port, family } = serve;
262
+ const hostname = family === "IPv6" ? `[${address}]` : address;
263
+ const protocol = server.config.server.https ? "https" : "http";
264
+ process.env.APP_DEV_SERVER_URL = `${protocol}://${hostname}:${port}`;
265
+ const DEBUG_PATH = _path2.default.resolve(
266
+ _process.cwd.call(void 0, ),
267
+ "node_modules",
268
+ "@tomjs",
269
+ "vite-plugin-electron",
270
+ "debug"
271
+ );
272
+ if (!_fs2.default.existsSync(DEBUG_PATH)) {
273
+ _fs.mkdirSync.call(void 0, DEBUG_PATH, { recursive: true });
247
274
  }
275
+ const env = Object.keys(process.env).filter((s) => s.startsWith("APP_") || s.startsWith("VITE_")).map((s) => `${s}=${process.env[s]}`).join("\n");
276
+ _fs.writeFileSync.call(void 0, _path2.default.join(DEBUG_PATH, ".env"), `NODE_ENV=development
277
+ ${env}`);
278
+ console.log("Server is running");
248
279
  await runServe(opts, server);
249
280
  });
250
281
  },
package/dist/index.mjs CHANGED
@@ -1,5 +1,6 @@
1
1
  // src/index.ts
2
- import fs2 from "fs";
2
+ import fs2, { mkdirSync, writeFileSync } from "fs";
3
+ import { cwd } from "process";
3
4
  import cloneDeep from "lodash.clonedeep";
4
5
  import merge from "lodash.merge";
5
6
  import path from "path";
@@ -69,10 +70,11 @@ function getBuildOptions(options) {
69
70
  });
70
71
  }
71
72
  async function startup(options) {
73
+ if (options.debug) {
74
+ return;
75
+ }
72
76
  await startup.exit();
73
- const args = [];
74
- options.inspect && args.push("--inspect");
75
- process.electronApp = spawn(electron, [".", ...args], {
77
+ process.electronApp = spawn(electron, ["."], {
76
78
  stdio: "inherit"
77
79
  });
78
80
  process.electronApp.once("exit", process.exit);
@@ -97,9 +99,10 @@ startup.exit = async () => {
97
99
  });
98
100
  };
99
101
  async function runServe(options, server) {
102
+ options.debug && logger.warn(`debug mode`);
100
103
  const buildOptions = getBuildOptions(options);
104
+ const buildCounts = [0, buildOptions.length > 1 ? 0 : 1];
101
105
  for (let i = 0; i < buildOptions.length; i++) {
102
- let isFirstBuild = true;
103
106
  const tsOpts = buildOptions[i];
104
107
  const { __NAME__: name, onSuccess: _onSuccess, watch, ...tsupOptions } = tsOpts;
105
108
  logger.info(`${name} build`);
@@ -107,16 +110,21 @@ async function runServe(options, server) {
107
110
  if (typeof _onSuccess === "function") {
108
111
  await _onSuccess();
109
112
  }
110
- if (isFirstBuild) {
113
+ if (buildCounts[i] <= 0) {
114
+ buildCounts[i]++;
111
115
  logger.info(`${name} build succeeded`);
112
- isFirstBuild = false;
116
+ if (buildCounts[0] == 1 && buildCounts[1] == 1) {
117
+ logger.info("electron startup");
118
+ await startup(options);
119
+ }
113
120
  return;
114
121
  }
115
122
  logger.success(`${name} rebuild succeeded!`);
116
123
  if (name === "main") {
117
- console.log("main process exit");
124
+ logger.info("electron restart");
118
125
  await startup(options);
119
126
  } else {
127
+ logger.info("page reload");
120
128
  server.ws.send({
121
129
  type: "full-reload"
122
130
  });
@@ -124,7 +132,6 @@ async function runServe(options, server) {
124
132
  };
125
133
  await tsupBuild({ onSuccess, watch: true, ...tsupOptions });
126
134
  }
127
- await startup(options);
128
135
  }
129
136
  async function runBuild(options) {
130
137
  const buildOptions = getBuildOptions(options);
@@ -218,32 +225,56 @@ function vitePluginElectron(options) {
218
225
  opts.preload.outDir ||= path.join("dist-electron", "preload");
219
226
  }
220
227
  if (isDev) {
221
- opts.main.sourcemap ??= true;
222
- opts.preload.sourcemap ??= true;
228
+ opts.main.sourcemap ??= "inline";
229
+ opts.preload.sourcemap ??= "inline";
223
230
  } else {
224
231
  opts.main.minify ??= true;
225
232
  opts.preload.minify ??= true;
226
233
  }
234
+ let envPrefix = config.envPrefix;
235
+ if (!envPrefix) {
236
+ envPrefix = ["VITE_"];
237
+ } else if (typeof envPrefix === "string") {
238
+ envPrefix = [envPrefix];
239
+ }
240
+ if (!envPrefix.includes("APP_")) {
241
+ envPrefix.push("APP_");
242
+ }
227
243
  return {
244
+ envPrefix: [...new Set(envPrefix)],
228
245
  build: {
229
246
  outDir
230
247
  }
231
248
  };
232
249
  },
250
+ configResolved(config) {
251
+ opts.debug = config.env.APP_ELECTRON_DEBUG ? !!config.env.APP_ELECTRON_DEBUG : opts.debug;
252
+ },
233
253
  configureServer(server) {
234
- if (!(server == null ? void 0 : server.httpServer)) {
254
+ if (!server || !server.httpServer) {
235
255
  return;
236
256
  }
237
257
  server.httpServer.on("listening", async () => {
238
- if (server.httpServer) {
239
- const serve = server.httpServer.address();
240
- const { address, port, family } = serve;
241
- if (family === "IPv6") {
242
- process.env.APP_DEV_SERVER_URL = `http://[${address}]:${port}`;
243
- } else {
244
- process.env.APP_DEV_SERVER_URL = `http://${address}:${port}`;
245
- }
258
+ var _a;
259
+ const serve = (_a = server.httpServer) == null ? void 0 : _a.address();
260
+ const { address, port, family } = serve;
261
+ const hostname = family === "IPv6" ? `[${address}]` : address;
262
+ const protocol = server.config.server.https ? "https" : "http";
263
+ process.env.APP_DEV_SERVER_URL = `${protocol}://${hostname}:${port}`;
264
+ const DEBUG_PATH = path.resolve(
265
+ cwd(),
266
+ "node_modules",
267
+ "@tomjs",
268
+ "vite-plugin-electron",
269
+ "debug"
270
+ );
271
+ if (!fs2.existsSync(DEBUG_PATH)) {
272
+ mkdirSync(DEBUG_PATH, { recursive: true });
246
273
  }
274
+ const env = Object.keys(process.env).filter((s) => s.startsWith("APP_") || s.startsWith("VITE_")).map((s) => `${s}=${process.env[s]}`).join("\n");
275
+ writeFileSync(path.join(DEBUG_PATH, ".env"), `NODE_ENV=development
276
+ ${env}`);
277
+ console.log("Server is running");
247
278
  await runServe(opts, server);
248
279
  });
249
280
  },
package/env.d.ts CHANGED
@@ -13,5 +13,9 @@ 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;
16
20
  }
17
21
  }
package/package.json CHANGED
@@ -1,12 +1,14 @@
1
1
  {
2
2
  "name": "@tomjs/vite-plugin-electron",
3
- "version": "1.2.1",
3
+ "version": "1.3.0",
4
4
  "description": "A simple vite plugin for electron, supports esm/cjs.",
5
5
  "keywords": [
6
6
  "vite",
7
7
  "plugin",
8
8
  "electron",
9
- "tsup"
9
+ "tsup",
10
+ "esm",
11
+ "cjs"
10
12
  ],
11
13
  "author": {
12
14
  "name": "Tom Gao",