@tomjs/vite-plugin-electron 1.2.0 → 1.2.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
@@ -39,9 +39,11 @@ npm i @tomjs/vite-plugin-electron --save-dev
39
39
 
40
40
  ## Usage
41
41
 
42
- ### Project structure
42
+ ### Recommended Agreement
43
43
 
44
- - Recommended `electron` front-end code directory structure
44
+ #### Directory Structure
45
+
46
+ - Recommend `electron` and page `src` code directory structure
45
47
 
46
48
  ```
47
49
  |--electron
@@ -54,7 +56,7 @@ npm i @tomjs/vite-plugin-electron --save-dev
54
56
  | |--main.ts
55
57
  ```
56
58
 
57
- - Use the default dist output directory of the plugin
59
+ - Zero configuration, default dist output directory
58
60
 
59
61
  ```
60
62
  |--dist
@@ -68,6 +70,10 @@ npm i @tomjs/vite-plugin-electron --save-dev
68
70
  | | |--index.html
69
71
  ```
70
72
 
73
+ #### Default configuration and behavior
74
+
75
+ See [PluginOptions](#pluginoptions) and `recommended` parameter descriptions in detail
76
+
71
77
  ### electron
72
78
 
73
79
  `electron/main/index.ts`
@@ -107,10 +113,12 @@ app.whenReady().then(createWindow);
107
113
 
108
114
  ### vue
109
115
 
110
- Support `ES modules`
116
+ Take using `esm` as an example, but it requires Electron>=28
111
117
 
112
118
  - `package.json`
113
119
 
120
+ Electron preload must use the `mjs` suffix, otherwise an error will be reported. So `esm` also uses the `mjs` suffix for output by default.
121
+
114
122
  ```json
115
123
  {
116
124
  "type": "module",
@@ -128,14 +136,17 @@ import vue from '@vitejs/plugin-vue';
128
136
  export default defineConfig({
129
137
  plugins: [
130
138
  vue(),
131
- electron({
132
- main: {
133
- entry: 'electron/main/index.ts',
134
- },
135
- preload: {
136
- entry: 'electron/preload/index.ts',
137
- },
138
- }),
139
+ // If you use the agreed directory structure, no configuration is required
140
+ electron(),
141
+ // If the directory structure is customized, the value must be assigned according to the actual situation
142
+ // electron({
143
+ // main: {
144
+ // entry: 'electron/main/index.ts',
145
+ // },
146
+ // preload: {
147
+ // entry: 'electron/preload/index.ts',
148
+ // },
149
+ // }),
139
150
  // renderer(),
140
151
  ],
141
152
  });
@@ -143,7 +154,7 @@ export default defineConfig({
143
154
 
144
155
  ### react
145
156
 
146
- Support `CommonJS`
157
+ Take using `cjs` as an example
147
158
 
148
159
  - `package.json`
149
160
 
@@ -158,23 +169,11 @@ Support `CommonJS`
158
169
 
159
170
  ```ts
160
171
  import { defineConfig } from 'vite';
161
- // import renderer from 'vite-plugin-electron-renderer'; // Enable nodeIntegration
162
172
  import electron from '@tomjs/vite-plugin-electron';
163
173
  import react from '@vitejs/plugin-react-swc';
164
174
 
165
175
  export default defineConfig({
166
- plugins: [
167
- react(),
168
- electron({
169
- main: {
170
- entry: 'electron/main/index.ts',
171
- },
172
- preload: {
173
- entry: 'electron/preload/index.ts',
174
- },
175
- }),
176
- // renderer(),
177
- ],
176
+ plugins: [react(), electron()],
178
177
  });
179
178
  ```
180
179
 
@@ -195,6 +194,14 @@ export default defineConfig({
195
194
  | preload | [PreloadOptions](#PreloadOptions) | | Configuration options for the electron preload process. |
196
195
  | inspect | `boolean` | true | If set to true, electron will start with the `--inspect` flag. |
197
196
 
197
+ **Notice**
198
+
199
+ The `recommended` option is used to set the default configuration and behavior, which can be used with almost zero configuration. The default is `true`. If you want to customize the configuration, set it to `false`. The following default prerequisites are to use the recommended [project structure](#directory-structure).
200
+
201
+ - Check whether `electron/main/index.ts` and `electron/main/index.ts` exist, and if so, assign values to `main.entry` and `preload.entry` respectively. If it does not exist, `main.entry` must be actively assigned, and an error will be reported.
202
+ - The output directory is based on the `build.outDir` parameter of `vite`, and outputs `electron/main`, `electron/preload` and `src` to `dist/main`, `dist/preload` and `dist/renderer` respectively.
203
+ - Other behaviors to be implemented
204
+
198
205
  ### MainOptions
199
206
 
200
207
  Based on [Options](https://paka.dev/npm/tsup) of [tsup](https://tsup.egoist.dev/), some default values are added for ease of use.
package/README.zh_CN.md CHANGED
@@ -39,9 +39,11 @@ npm i @tomjs/vite-plugin-electron --save-dev
39
39
 
40
40
  ## 使用说明
41
41
 
42
- ### 项目结构
42
+ ### 推荐约定
43
43
 
44
- - 推荐 `electron` 的 前端代码目录结构
44
+ #### 目录结构
45
+
46
+ - 推荐 `electron` 和 页面 `src` 代码目录结构
45
47
 
46
48
  ```
47
49
  |--electron
@@ -54,7 +56,7 @@ npm i @tomjs/vite-plugin-electron --save-dev
54
56
  | |--main.ts
55
57
  ```
56
58
 
57
- - 使用插件默认 dist 输出目录
59
+ - 零配置,默认 dist 输出目录
58
60
 
59
61
  ```
60
62
  |--dist
@@ -68,6 +70,10 @@ npm i @tomjs/vite-plugin-electron --save-dev
68
70
  | | |--index.html
69
71
  ```
70
72
 
73
+ #### 默认配置和行为
74
+
75
+ 详细查看 [PluginOptions](#pluginoptions) 和 `recommended` 参数说明
76
+
71
77
  ### electron
72
78
 
73
79
  `electron/main/index.ts`
@@ -107,10 +113,12 @@ app.whenReady().then(createWindow);
107
113
 
108
114
  ### vue
109
115
 
110
- 支持 `ES modules`
116
+ 以使用 `esm` 为例,不过要求 `Electron>=28`
111
117
 
112
118
  - `package.json`
113
119
 
120
+ Electron preload 必须使用 `mjs` 后缀,否则报错。所以 `esm` 也默认输出使用 `mjs` 后缀。
121
+
114
122
  ```json
115
123
  {
116
124
  "type": "module",
@@ -129,14 +137,17 @@ import vue from '@vitejs/plugin-vue';
129
137
  export default defineConfig({
130
138
  plugins: [
131
139
  vue(),
132
- electron({
133
- main: {
134
- entry: 'electron/main/index.ts',
135
- },
136
- preload: {
137
- entry: 'electron/preload/index.ts',
138
- },
139
- }),
140
+ // 如使用约定的目录结构,则不需要配置
141
+ electron(),
142
+ // 如果自定义了目录结构,则必须根据实际情况赋值
143
+ // electron({
144
+ // main: {
145
+ // entry: 'electron/main/index.ts',
146
+ // },
147
+ // preload: {
148
+ // entry: 'electron/preload/index.ts',
149
+ // },
150
+ // }),
140
151
  // renderer(),
141
152
  ],
142
153
  });
@@ -144,7 +155,7 @@ export default defineConfig({
144
155
 
145
156
  ### react
146
157
 
147
- 支持 `CommonJS`
158
+ 以使用 `cjs` 为例
148
159
 
149
160
  - `package.json`
150
161
 
@@ -159,23 +170,11 @@ export default defineConfig({
159
170
 
160
171
  ```ts
161
172
  import { defineConfig } from 'vite';
162
- // import renderer from 'vite-plugin-electron-renderer'; // 启用 nodeIntegration
163
173
  import electron from '@tomjs/vite-plugin-electron';
164
174
  import react from '@vitejs/plugin-react-swc';
165
175
 
166
176
  export default defineConfig({
167
- plugins: [
168
- react(),
169
- electron({
170
- main: {
171
- entry: 'electron/main/index.ts',
172
- },
173
- preload: {
174
- entry: 'electron/preload/index.ts',
175
- },
176
- }),
177
- // renderer(),
178
- ],
177
+ plugins: [react(), electron()],
179
178
  });
180
179
  ```
181
180
 
@@ -192,10 +191,16 @@ export default defineConfig({
192
191
  | --- | --- | --- | --- |
193
192
  | recommended | `boolean` | `true` | 推荐开关,如果为true,将具有以下默认行为:将main/preload/renderer的outDir更改为并行的outDir;例如,如果vite build.outDir为'dist',将main/preload/render更改为'dist/main'、'dist/preload'和'dist/renderer' |
194
193
  | external | `string[]` | | 不打包这些模块 |
195
- | **main** | [MainOptions](#MainOptions) | | electron main 进程选项 |
194
+ | main | [MainOptions](#MainOptions) | | electron main 进程选项 |
196
195
  | preload | [PreloadOptions](#PreloadOptions) | | electron preload 进程选项 |
197
196
  | inspect | `boolean` | `true` | electron启动时使用`--inspect`参数 |
198
197
 
198
+ `recommended` 选项用于设置默认配置和行为,几乎可以达到零配置使用,默认为 `true` 。如果你要自定义配置,请设置它为`false`。以下默认的前提条件是使用推荐的 [项目结构](#目录结构)。
199
+
200
+ - 检查是否存在 `electron/main/index.ts` 和 `electron/main/index.ts`,如果有则分别给 `main.entry` 和 `preload.entry` 赋值。如果不存在,`main.entry` 必须主动赋值,负责会报错
201
+ - 输出目录根据 `vite` 的 `build.outDir` 参数, 将 `electron/main`、`electron/preload`、`src` 分别输出到 `dist/main`、`dist/preload`、`dist/renderer`
202
+ - 其他待实现的行为
203
+
199
204
  ### MainOptions
200
205
 
201
206
  继承自 [tsup](https://tsup.egoist.dev/) 的 [Options](https://paka.dev/npm/tsup),添加了一些默认值,方便使用。
package/dist/index.d.mts CHANGED
@@ -6,16 +6,11 @@ import { Options } from 'tsup';
6
6
  * @see https://paka.dev/npm/tsup
7
7
  * @see https://unpkg.com/browse/tsup/dist/index.d.ts
8
8
  */
9
- interface MainOptions extends Omit<Options, 'name' | 'entry' | 'format' | 'outDir' | 'watch' | 'onSuccess'> {
10
- /**
11
- * The name of the electron main process.
12
- * @default "main"
13
- */
14
- name?: string;
9
+ interface MainOptions extends Omit<Options, 'entry' | 'format' | 'outDir' | 'watch' | 'onSuccess'> {
15
10
  /**
16
11
  * The main process entry file.
17
12
  */
18
- entry: string;
13
+ entry?: string;
19
14
  /**
20
15
  * The bundle format. If not specified, it will use the "type" field from package.json.
21
16
  */
@@ -35,12 +30,7 @@ interface MainOptions extends Omit<Options, 'name' | 'entry' | 'format' | 'outDi
35
30
  * @see https://paka.dev/npm/tsup
36
31
  * @see https://unpkg.com/browse/tsup/dist/index.d.ts
37
32
  */
38
- interface PreloadOptions extends Omit<Options, 'name' | 'entry' | 'format' | 'outDir' | 'watch' | 'onSuccess'> {
39
- /**
40
- * The name of the electron preload process.
41
- * @default "preload"
42
- */
43
- name?: string;
33
+ interface PreloadOptions extends Omit<Options, 'entry' | 'format' | 'outDir' | 'watch' | 'onSuccess'> {
44
34
  /**
45
35
  * The preload process entry file
46
36
  */
@@ -78,7 +68,7 @@ interface PluginOptions {
78
68
  /**
79
69
  * electron main process options
80
70
  */
81
- main: MainOptions;
71
+ main?: MainOptions;
82
72
  /**
83
73
  * electron preload process options
84
74
  */
package/dist/index.d.ts CHANGED
@@ -6,16 +6,11 @@ import { Options } from 'tsup';
6
6
  * @see https://paka.dev/npm/tsup
7
7
  * @see https://unpkg.com/browse/tsup/dist/index.d.ts
8
8
  */
9
- interface MainOptions extends Omit<Options, 'name' | 'entry' | 'format' | 'outDir' | 'watch' | 'onSuccess'> {
10
- /**
11
- * The name of the electron main process.
12
- * @default "main"
13
- */
14
- name?: string;
9
+ interface MainOptions extends Omit<Options, 'entry' | 'format' | 'outDir' | 'watch' | 'onSuccess'> {
15
10
  /**
16
11
  * The main process entry file.
17
12
  */
18
- entry: string;
13
+ entry?: string;
19
14
  /**
20
15
  * The bundle format. If not specified, it will use the "type" field from package.json.
21
16
  */
@@ -35,12 +30,7 @@ interface MainOptions extends Omit<Options, 'name' | 'entry' | 'format' | 'outDi
35
30
  * @see https://paka.dev/npm/tsup
36
31
  * @see https://unpkg.com/browse/tsup/dist/index.d.ts
37
32
  */
38
- interface PreloadOptions extends Omit<Options, 'name' | 'entry' | 'format' | 'outDir' | 'watch' | 'onSuccess'> {
39
- /**
40
- * The name of the electron preload process.
41
- * @default "preload"
42
- */
43
- name?: string;
33
+ interface PreloadOptions extends Omit<Options, 'entry' | 'format' | 'outDir' | 'watch' | 'onSuccess'> {
44
34
  /**
45
35
  * The preload process entry file
46
36
  */
@@ -78,7 +68,7 @@ interface PluginOptions {
78
68
  /**
79
69
  * electron main process options
80
70
  */
81
- main: MainOptions;
71
+ main?: MainOptions;
82
72
  /**
83
73
  * electron preload process options
84
74
  */
package/dist/index.js CHANGED
@@ -172,11 +172,9 @@ function preMergeOptions(options) {
172
172
  recommended: true,
173
173
  external: ["electron"],
174
174
  main: {
175
- name: "main",
176
175
  ...electron2
177
176
  },
178
177
  preload: {
179
- name: "payload",
180
178
  ...electron2
181
179
  }
182
180
  },
@@ -187,7 +185,12 @@ function preMergeOptions(options) {
187
185
  const fmt = opt.format;
188
186
  opt.format = ["cjs", "esm"].includes(fmt) ? [fmt] : [format];
189
187
  const entry = opt.entry;
190
- if (typeof entry === "string") {
188
+ if (entry == void 0) {
189
+ const filePath = `electron/${prop}/index.ts`;
190
+ if (_fs2.default.existsSync(_path2.default.join(process.cwd(), filePath))) {
191
+ opt.entry = [filePath];
192
+ }
193
+ } else if (typeof entry === "string") {
191
194
  opt.entry = [entry];
192
195
  }
193
196
  const external = opt.external || opts.external || ["electron"];
@@ -205,21 +208,22 @@ function vitePluginElectron(options) {
205
208
  var _a;
206
209
  isServer = env.command === "serve";
207
210
  let outDir = ((_a = config == null ? void 0 : config.build) == null ? void 0 : _a.outDir) || "dist";
208
- opts.preload = opts.preload || {};
211
+ opts.main ||= {};
212
+ opts.preload ||= {};
209
213
  if (opts.recommended) {
210
214
  opts.main.outDir = _path2.default.join(outDir, "main");
211
215
  opts.preload.outDir = _path2.default.join(outDir, "preload");
212
216
  outDir = _path2.default.join(outDir, "renderer");
213
217
  } else {
214
- opts.main.outDir = opts.main.outDir || _path2.default.join("dist-electron", "main");
215
- opts.preload.outDir = opts.preload.outDir || _path2.default.join("dist-electron", "preload");
218
+ opts.main.outDir ||= _path2.default.join("dist-electron", "main");
219
+ opts.preload.outDir ||= _path2.default.join("dist-electron", "preload");
216
220
  }
217
221
  if (isDev) {
218
- opts.main.sourcemap = _nullishCoalesce(opts.main.sourcemap, () => ( true));
219
- opts.preload.sourcemap = _nullishCoalesce(opts.main.sourcemap, () => ( true));
222
+ opts.main.sourcemap ??= true;
223
+ opts.preload.sourcemap ??= true;
220
224
  } else {
221
- opts.main.minify = _nullishCoalesce(opts.main.minify, () => ( true));
222
- opts.preload.minify = _nullishCoalesce(opts.preload.minify, () => ( true));
225
+ opts.main.minify ??= true;
226
+ opts.preload.minify ??= true;
223
227
  }
224
228
  return {
225
229
  build: {
package/dist/index.mjs CHANGED
@@ -171,11 +171,9 @@ function preMergeOptions(options) {
171
171
  recommended: true,
172
172
  external: ["electron"],
173
173
  main: {
174
- name: "main",
175
174
  ...electron2
176
175
  },
177
176
  preload: {
178
- name: "payload",
179
177
  ...electron2
180
178
  }
181
179
  },
@@ -186,7 +184,12 @@ function preMergeOptions(options) {
186
184
  const fmt = opt.format;
187
185
  opt.format = ["cjs", "esm"].includes(fmt) ? [fmt] : [format];
188
186
  const entry = opt.entry;
189
- if (typeof entry === "string") {
187
+ if (entry == void 0) {
188
+ const filePath = `electron/${prop}/index.ts`;
189
+ if (fs2.existsSync(path.join(process.cwd(), filePath))) {
190
+ opt.entry = [filePath];
191
+ }
192
+ } else if (typeof entry === "string") {
190
193
  opt.entry = [entry];
191
194
  }
192
195
  const external = opt.external || opts.external || ["electron"];
@@ -204,21 +207,22 @@ function vitePluginElectron(options) {
204
207
  var _a;
205
208
  isServer = env.command === "serve";
206
209
  let outDir = ((_a = config == null ? void 0 : config.build) == null ? void 0 : _a.outDir) || "dist";
207
- opts.preload = opts.preload || {};
210
+ opts.main ||= {};
211
+ opts.preload ||= {};
208
212
  if (opts.recommended) {
209
213
  opts.main.outDir = path.join(outDir, "main");
210
214
  opts.preload.outDir = path.join(outDir, "preload");
211
215
  outDir = path.join(outDir, "renderer");
212
216
  } else {
213
- opts.main.outDir = opts.main.outDir || path.join("dist-electron", "main");
214
- opts.preload.outDir = opts.preload.outDir || path.join("dist-electron", "preload");
217
+ opts.main.outDir ||= path.join("dist-electron", "main");
218
+ opts.preload.outDir ||= path.join("dist-electron", "preload");
215
219
  }
216
220
  if (isDev) {
217
- opts.main.sourcemap = opts.main.sourcemap ?? true;
218
- opts.preload.sourcemap = opts.main.sourcemap ?? true;
221
+ opts.main.sourcemap ??= true;
222
+ opts.preload.sourcemap ??= true;
219
223
  } else {
220
- opts.main.minify = opts.main.minify ?? true;
221
- opts.preload.minify = opts.preload.minify ?? true;
224
+ opts.main.minify ??= true;
225
+ opts.preload.minify ??= true;
222
226
  }
223
227
  return {
224
228
  build: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tomjs/vite-plugin-electron",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "A simple vite plugin for electron, supports esm/cjs.",
5
5
  "keywords": [
6
6
  "vite",