@tomjs/vite-plugin-electron 1.1.0 → 1.1.2

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
@@ -66,7 +66,42 @@ npm i @tomjs/vite-plugin-electron --save-dev
66
66
  | | |--index.html
67
67
  ```
68
68
 
69
- For example, for vue/react projects, `vite.config.ts` configuration.
69
+ ### electron
70
+
71
+ `electron/main/index.ts`
72
+
73
+ ```ts
74
+ import { dirname } from 'node:path';
75
+ import { fileURLToPath } from 'node:url';
76
+ import { app, BrowserWindow } from 'electron';
77
+
78
+ // when package.json "type": module"
79
+ global.__dirname = dirname(fileURLToPath(import.meta.url));
80
+
81
+ const preload = join(__dirname, '../preload/index.mjs');
82
+ const url = process.env.APP_DEV_SERVER_URL;
83
+
84
+ async function createWindow() {
85
+ win = new BrowserWindow({
86
+ title: 'Main window',
87
+ width: 800,
88
+ height: 700,
89
+ webPreferences: {
90
+ preload,
91
+ nodeIntegration: true,
92
+ contextIsolation: false,
93
+ },
94
+ });
95
+
96
+ if (isDev) {
97
+ win.loadURL(url);
98
+ } else {
99
+ win.loadFile(indexHtml);
100
+ }
101
+ }
102
+
103
+ app.whenReady().then(createWindow);
104
+ ```
70
105
 
71
106
  ### vue
72
107
 
@@ -164,7 +199,6 @@ Based on [Options](https://paka.dev/npm/tsup) of [tsup](https://tsup.egoist.dev/
164
199
 
165
200
  | Property | Type | Default | Description |
166
201
  | --- | --- | --- | --- |
167
- | name | `string` | "main" | The name of the electron main process. |
168
202
  | entry | `string` | `-` | The main process entry file. |
169
203
  | format | `'cjs' \| 'esm'` | `-` | The bundle format. If not specified, it will use the "type" field from package.json. |
170
204
  | outDir | `string` | "dist-electron/main" | The output directory for the main process files |
@@ -176,7 +210,6 @@ Based on [Options](https://paka.dev/npm/tsup) of [tsup](https://tsup.egoist.dev/
176
210
 
177
211
  | Property | Type | Default | Description |
178
212
  | --- | --- | --- | --- |
179
- | name | `string` | "preload" | The name of the electron preload process. |
180
213
  | entry | `string` | `-` | The preload process entry file. |
181
214
  | format | `'cjs' \| 'esm'` | `-` | The bundle format. If not specified, it will use the "type" field from package.json. |
182
215
  | outDir | `string` | "dist-electron/preload" | The output directory for the preload process files |
package/README.zh_CN.md CHANGED
@@ -66,7 +66,42 @@ npm i @tomjs/vite-plugin-electron --save-dev
66
66
  | | |--index.html
67
67
  ```
68
68
 
69
- vue/react 项目为例,`vite.config.ts` 配置。
69
+ ### electron
70
+
71
+ `electron/main/index.ts`
72
+
73
+ ```ts
74
+ import { dirname } from 'node:path';
75
+ import { fileURLToPath } from 'node:url';
76
+ import { app, BrowserWindow } from 'electron';
77
+
78
+ // when package.json "type": module"
79
+ global.__dirname = dirname(fileURLToPath(import.meta.url));
80
+
81
+ const preload = join(__dirname, '../preload/index.mjs');
82
+ const url = process.env.APP_DEV_SERVER_URL;
83
+
84
+ async function createWindow() {
85
+ win = new BrowserWindow({
86
+ title: 'Main window',
87
+ width: 800,
88
+ height: 700,
89
+ webPreferences: {
90
+ preload,
91
+ nodeIntegration: true,
92
+ contextIsolation: false,
93
+ },
94
+ });
95
+
96
+ if (isDev) {
97
+ win.loadURL(url);
98
+ } else {
99
+ win.loadFile(indexHtml);
100
+ }
101
+ }
102
+
103
+ app.whenReady().then(createWindow);
104
+ ```
70
105
 
71
106
  ### vue
72
107
 
@@ -165,7 +200,6 @@ export default defineConfig({
165
200
 
166
201
  | 参数名 | 类型 | 默认值 | 说明 |
167
202
  | --- | --- | --- | --- |
168
- | name | `string` | "main" | main 名称 |
169
203
  | **entry** | `string` | `-` | main 入口文件 |
170
204
  | format | `'cjs' \| 'esm'` | `-` | 打包格式。如果未指定,将使用 package.json 中的 "type" 字段 |
171
205
  | outDir | `string` | `"dist-electron/main"` | main 输出文件夹 |
@@ -177,7 +211,6 @@ export default defineConfig({
177
211
 
178
212
  | 参数名 | 类型 | 默认值 | 说明 |
179
213
  | --- | --- | --- | --- |
180
- | name | `string` | "preload" | preload 名称 |
181
214
  | **entry** | `string` | `-` | preload 入口文件 |
182
215
  | format | `'cjs' \| 'esm'` | `-` | 打包格式。如果未指定,将使用 package.json 中的 "type" 字段 |
183
216
  | outDir | `string` | `"dist-electron/preload"` | preload 输出文件夹 |
package/dist/index.d.mts CHANGED
@@ -4,7 +4,7 @@ import { Options } from 'tsup';
4
4
  /**
5
5
  * Electron main process options
6
6
  */
7
- type MainOptions = Omit<Options, 'name' | 'entry' | 'format' | 'outDir' | 'watch' | 'onSuccess'> & {
7
+ interface MainOptions extends Omit<Options, 'name' | 'entry' | 'format' | 'outDir' | 'watch' | 'onSuccess'> {
8
8
  /**
9
9
  * The name of the electron main process.
10
10
  * @default "main"
@@ -27,11 +27,11 @@ type MainOptions = Omit<Options, 'name' | 'entry' | 'format' | 'outDir' | 'watch
27
27
  * A function that will be executed after the build succeeds.
28
28
  */
29
29
  onSuccess?: () => Promise<void | undefined | (() => void | Promise<void>)>;
30
- };
30
+ }
31
31
  /**
32
32
  * Electron preload process options
33
33
  */
34
- type PreloadOptions = Omit<Options, 'name' | 'entry' | 'format' | 'outDir' | 'watch' | 'onSuccess'> & {
34
+ interface PreloadOptions extends Omit<Options, 'name' | 'entry' | 'format' | 'outDir' | 'watch' | 'onSuccess'> {
35
35
  /**
36
36
  * The name of the electron preload process.
37
37
  * @default "preload"
@@ -54,7 +54,7 @@ type PreloadOptions = Omit<Options, 'name' | 'entry' | 'format' | 'outDir' | 'wa
54
54
  * A function that will be executed after the build succeeds.
55
55
  */
56
56
  onSuccess?: () => Promise<void | undefined | (() => void | Promise<void>)>;
57
- };
57
+ }
58
58
  /**
59
59
  * vite plugin options
60
60
  */
@@ -84,7 +84,28 @@ interface PluginOptions {
84
84
  */
85
85
  inspect?: boolean;
86
86
  }
87
+ /**
88
+ * Only used internally
89
+ */
90
+ interface InnerOptions {
91
+ /**
92
+ * whether is vite server
93
+ */
94
+ isServer?: boolean;
95
+ /**
96
+ * vite server url, will be passed to electron
97
+ */
98
+ serverUrl?: string;
99
+ /**
100
+ * renderer outDir
101
+ */
102
+ rendererOutDir?: string;
103
+ /**
104
+ * electron main entry file
105
+ */
106
+ mainFile?: string;
107
+ }
87
108
 
88
109
  declare function vitePluginElectron(options?: PluginOptions): Plugin;
89
110
 
90
- export { vitePluginElectron as default, vitePluginElectron };
111
+ export { InnerOptions, MainOptions, PluginOptions, PreloadOptions, vitePluginElectron as default, vitePluginElectron };
package/dist/index.d.ts CHANGED
@@ -4,7 +4,7 @@ import { Options } from 'tsup';
4
4
  /**
5
5
  * Electron main process options
6
6
  */
7
- type MainOptions = Omit<Options, 'name' | 'entry' | 'format' | 'outDir' | 'watch' | 'onSuccess'> & {
7
+ interface MainOptions extends Omit<Options, 'name' | 'entry' | 'format' | 'outDir' | 'watch' | 'onSuccess'> {
8
8
  /**
9
9
  * The name of the electron main process.
10
10
  * @default "main"
@@ -27,11 +27,11 @@ type MainOptions = Omit<Options, 'name' | 'entry' | 'format' | 'outDir' | 'watch
27
27
  * A function that will be executed after the build succeeds.
28
28
  */
29
29
  onSuccess?: () => Promise<void | undefined | (() => void | Promise<void>)>;
30
- };
30
+ }
31
31
  /**
32
32
  * Electron preload process options
33
33
  */
34
- type PreloadOptions = Omit<Options, 'name' | 'entry' | 'format' | 'outDir' | 'watch' | 'onSuccess'> & {
34
+ interface PreloadOptions extends Omit<Options, 'name' | 'entry' | 'format' | 'outDir' | 'watch' | 'onSuccess'> {
35
35
  /**
36
36
  * The name of the electron preload process.
37
37
  * @default "preload"
@@ -54,7 +54,7 @@ type PreloadOptions = Omit<Options, 'name' | 'entry' | 'format' | 'outDir' | 'wa
54
54
  * A function that will be executed after the build succeeds.
55
55
  */
56
56
  onSuccess?: () => Promise<void | undefined | (() => void | Promise<void>)>;
57
- };
57
+ }
58
58
  /**
59
59
  * vite plugin options
60
60
  */
@@ -84,7 +84,28 @@ interface PluginOptions {
84
84
  */
85
85
  inspect?: boolean;
86
86
  }
87
+ /**
88
+ * Only used internally
89
+ */
90
+ interface InnerOptions {
91
+ /**
92
+ * whether is vite server
93
+ */
94
+ isServer?: boolean;
95
+ /**
96
+ * vite server url, will be passed to electron
97
+ */
98
+ serverUrl?: string;
99
+ /**
100
+ * renderer outDir
101
+ */
102
+ rendererOutDir?: string;
103
+ /**
104
+ * electron main entry file
105
+ */
106
+ mainFile?: string;
107
+ }
87
108
 
88
109
  declare function vitePluginElectron(options?: PluginOptions): Plugin;
89
110
 
90
- export { vitePluginElectron as default, vitePluginElectron };
111
+ export { InnerOptions, MainOptions, PluginOptions, PreloadOptions, vitePluginElectron as default, vitePluginElectron };
package/env.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ /**
2
+ * fix code hint
3
+ */
4
+ type UnionType<T> = T | (string & {});
5
+
6
+ declare namespace NodeJS {
7
+ interface ProcessEnv {
8
+ /**
9
+ * Node.js environment
10
+ */
11
+ NODE_ENV: UnionType<'development' | 'test' | 'production'>;
12
+ /**
13
+ * The url of the dev server.
14
+ */
15
+ readonly APP_DEV_SERVER_URL: string;
16
+ }
17
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tomjs/vite-plugin-electron",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "A simple vite plugin for electron, supports esm/cjs.",
5
5
  "keywords": [
6
6
  "vite",
@@ -24,7 +24,8 @@
24
24
  }
25
25
  },
26
26
  "files": [
27
- "dist"
27
+ "dist",
28
+ "env.d.ts"
28
29
  ],
29
30
  "engines": {
30
31
  "node": ">=16"