@tomjs/vite-plugin-electron 1.2.2 → 1.3.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
@@ -181,11 +181,11 @@ export default defineConfig({
181
181
 
182
182
  | Property | Type | Default | Description |
183
183
  | --- | --- | --- | --- |
184
- | 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'. |
185
- | external | `string[]` | | List of modules that should not be bundled. |
184
+ | recommended | `boolean` | `true` | This option is intended to provide recommended default parameters and behavior. |
185
+ | 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) |
186
186
  | main | [MainOptions](#MainOptions) | | Configuration options for the electron main process. |
187
187
  | preload | [PreloadOptions](#PreloadOptions) | | Configuration options for the electron preload process. |
188
- | inspect | `boolean` | true | If set to true, electron will start with the `--inspect` flag. |
188
+ | debug | `boolean` | `false` | Electron debug mode, don't startup electron. |
189
189
 
190
190
  **Notice**
191
191
 
@@ -225,3 +225,66 @@ Based on [Options](https://paka.dev/npm/tsup) of [tsup](https://tsup.egoist.dev/
225
225
  | --------- | ------------------------ | ----------------------- |
226
226
  | sourcemap | `true` | `false` |
227
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
@@ -182,11 +182,11 @@ export default defineConfig({
182
182
 
183
183
  | 参数名 | 类型 | 默认值 | 说明 |
184
184
  | --- | --- | --- | --- |
185
- | recommended | `boolean` | `true` | 推荐开关,如果为true,将具有以下默认行为:将main/preload/renderer的outDir更改为并行的outDir;例如,如果vite build.outDir为'dist',将main/preload/render更改为'dist/main'、'dist/preload'和'dist/renderer' |
186
- | external | `string[]` | | 不打包这些模块 |
185
+ | recommended | `boolean` | `true` | 这个选项是为了提供推荐的默认参数和行为 |
186
+ | external | `string[]` | | 不打包这些模块,但是 `dependencies` and `peerDependencies` 默认排除,[详见](https://tsup.egoist.dev/#excluding-packages) |
187
187
  | main | [MainOptions](#MainOptions) | | electron main 进程选项 |
188
188
  | preload | [PreloadOptions](#PreloadOptions) | | electron preload 进程选项 |
189
- | inspect | `boolean` | `true` | electron启动时使用`--inspect`参数 |
189
+ | debug | `boolean` | `false` | electron调试模式,不启动electron |
190
190
 
191
191
  `recommended` 选项用于设置默认配置和行为,几乎可以达到零配置使用,默认为 `true` 。如果你要自定义配置,请设置它为`false`。以下默认的前提条件是使用推荐的 [项目结构](#目录结构)。
192
192
 
@@ -224,3 +224,66 @@ export default defineConfig({
224
224
  | --------- | -------------- | -------------- |
225
225
  | sourcemap | `true` | `false` |
226
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
@@ -62,7 +62,8 @@ interface PluginOptions {
62
62
  */
63
63
  recommended?: boolean;
64
64
  /**
65
- * Don't bundle these modules
65
+ * Don't bundle these modules, but dependencies and peerDependencies in your package.json are always excluded. [See more](https://tsup.egoist.dev/#excluding-packages)
66
+ * @see https://tsup.egoist.dev/#excluding-packages
66
67
  */
67
68
  external?: string[];
68
69
  /**
@@ -74,10 +75,10 @@ interface PluginOptions {
74
75
  */
75
76
  preload?: PreloadOptions;
76
77
  /**
77
- * electron start with the `--inspect`
78
- * @default true
78
+ * electron debug mode, don't startup electron. You can also use `process.env.APP_ELECTRON_DEBUG`. Default is false.
79
+ * @default false
79
80
  */
80
- inspect?: boolean;
81
+ debug?: boolean;
81
82
  }
82
83
 
83
84
  declare function vitePluginElectron(options?: PluginOptions): Plugin;
package/dist/index.d.ts CHANGED
@@ -62,7 +62,8 @@ interface PluginOptions {
62
62
  */
63
63
  recommended?: boolean;
64
64
  /**
65
- * Don't bundle these modules
65
+ * Don't bundle these modules, but dependencies and peerDependencies in your package.json are always excluded. [See more](https://tsup.egoist.dev/#excluding-packages)
66
+ * @see https://tsup.egoist.dev/#excluding-packages
66
67
  */
67
68
  external?: string[];
68
69
  /**
@@ -74,10 +75,10 @@ interface PluginOptions {
74
75
  */
75
76
  preload?: PreloadOptions;
76
77
  /**
77
- * electron start with the `--inspect`
78
- * @default true
78
+ * electron debug mode, don't startup electron. You can also use `process.env.APP_ELECTRON_DEBUG`. Default is false.
79
+ * @default false
79
80
  */
80
- inspect?: boolean;
81
+ debug?: boolean;
81
82
  }
82
83
 
83
84
  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);
@@ -170,6 +177,7 @@ function preMergeOptions(options) {
170
177
  const opts = _lodashmerge2.default.call(void 0,
171
178
  {
172
179
  recommended: true,
180
+ debug: false,
173
181
  external: ["electron"],
174
182
  main: {
175
183
  ...electron2
@@ -219,32 +227,56 @@ function vitePluginElectron(options) {
219
227
  opts.preload.outDir ||= _path2.default.join("dist-electron", "preload");
220
228
  }
221
229
  if (isDev) {
222
- opts.main.sourcemap ??= true;
223
- opts.preload.sourcemap ??= true;
230
+ opts.main.sourcemap ??= "inline";
231
+ opts.preload.sourcemap ??= "inline";
224
232
  } else {
225
233
  opts.main.minify ??= true;
226
234
  opts.preload.minify ??= true;
227
235
  }
236
+ let envPrefix = config.envPrefix;
237
+ if (!envPrefix) {
238
+ envPrefix = ["VITE_"];
239
+ } else if (typeof envPrefix === "string") {
240
+ envPrefix = [envPrefix];
241
+ }
242
+ if (!envPrefix.includes("APP_")) {
243
+ envPrefix.push("APP_");
244
+ }
228
245
  return {
246
+ envPrefix: [...new Set(envPrefix)],
229
247
  build: {
230
248
  outDir
231
249
  }
232
250
  };
233
251
  },
252
+ configResolved(config) {
253
+ opts.debug = config.env.APP_ELECTRON_DEBUG ? !!config.env.APP_ELECTRON_DEBUG : opts.debug;
254
+ },
234
255
  configureServer(server) {
235
- if (!(server == null ? void 0 : server.httpServer)) {
256
+ if (!server || !server.httpServer) {
236
257
  return;
237
258
  }
238
259
  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
- }
260
+ var _a;
261
+ const serve = (_a = server.httpServer) == null ? void 0 : _a.address();
262
+ const { address, port, family } = serve;
263
+ const hostname = family === "IPv6" ? `[${address}]` : address;
264
+ const protocol = server.config.server.https ? "https" : "http";
265
+ process.env.APP_DEV_SERVER_URL = `${protocol}://${hostname}:${port}`;
266
+ const DEBUG_PATH = _path2.default.resolve(
267
+ _process.cwd.call(void 0, ),
268
+ "node_modules",
269
+ "@tomjs",
270
+ "vite-plugin-electron",
271
+ "debug"
272
+ );
273
+ if (!_fs2.default.existsSync(DEBUG_PATH)) {
274
+ _fs.mkdirSync.call(void 0, DEBUG_PATH, { recursive: true });
247
275
  }
276
+ const env = Object.keys(process.env).filter((s) => s.startsWith("APP_") || s.startsWith("VITE_")).map((s) => `${s}=${process.env[s]}`).join("\n");
277
+ _fs.writeFileSync.call(void 0, _path2.default.join(DEBUG_PATH, ".env"), `NODE_ENV=development
278
+ ${env}`);
279
+ console.log("Server is running");
248
280
  await runServe(opts, server);
249
281
  });
250
282
  },
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);
@@ -169,6 +176,7 @@ function preMergeOptions(options) {
169
176
  const opts = merge(
170
177
  {
171
178
  recommended: true,
179
+ debug: false,
172
180
  external: ["electron"],
173
181
  main: {
174
182
  ...electron2
@@ -218,32 +226,56 @@ function vitePluginElectron(options) {
218
226
  opts.preload.outDir ||= path.join("dist-electron", "preload");
219
227
  }
220
228
  if (isDev) {
221
- opts.main.sourcemap ??= true;
222
- opts.preload.sourcemap ??= true;
229
+ opts.main.sourcemap ??= "inline";
230
+ opts.preload.sourcemap ??= "inline";
223
231
  } else {
224
232
  opts.main.minify ??= true;
225
233
  opts.preload.minify ??= true;
226
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
+ }
227
244
  return {
245
+ envPrefix: [...new Set(envPrefix)],
228
246
  build: {
229
247
  outDir
230
248
  }
231
249
  };
232
250
  },
251
+ configResolved(config) {
252
+ opts.debug = config.env.APP_ELECTRON_DEBUG ? !!config.env.APP_ELECTRON_DEBUG : opts.debug;
253
+ },
233
254
  configureServer(server) {
234
- if (!(server == null ? void 0 : server.httpServer)) {
255
+ if (!server || !server.httpServer) {
235
256
  return;
236
257
  }
237
258
  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
- }
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 = path.resolve(
266
+ cwd(),
267
+ "node_modules",
268
+ "@tomjs",
269
+ "vite-plugin-electron",
270
+ "debug"
271
+ );
272
+ if (!fs2.existsSync(DEBUG_PATH)) {
273
+ mkdirSync(DEBUG_PATH, { recursive: true });
246
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
+ writeFileSync(path.join(DEBUG_PATH, ".env"), `NODE_ENV=development
277
+ ${env}`);
278
+ console.log("Server is running");
247
279
  await runServe(opts, server);
248
280
  });
249
281
  },
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.2",
3
+ "version": "1.3.1",
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",