@tomjs/create-app 0.3.2 → 0.4.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 +14 -28
- package/README.zh_CN.md +14 -29
- package/dist/index.mjs +1 -1
- package/package.json +3 -3
- package/template-electron-react/README.md +19 -11
- package/template-electron-react/electron/env.d.ts +1 -0
- package/template-electron-react/electron/main/index.ts +36 -27
- package/template-electron-react/electron/preload/index.ts +1 -0
- package/template-electron-react/package.json +7 -11
- package/template-electron-react/scripts/builder.ts +1 -1
- package/template-electron-react/scripts/release.ts +1 -1
- package/template-electron-react/src/App.tsx +4 -2
- package/template-electron-react/vite.config.ts +9 -81
- package/template-electron-vue/README.md +19 -11
- package/template-electron-vue/electron/env.d.ts +1 -0
- package/template-electron-vue/electron/main/index.ts +36 -27
- package/template-electron-vue/electron/preload/index.ts +1 -0
- package/template-electron-vue/package.json +9 -13
- package/template-electron-vue/scripts/builder.ts +1 -1
- package/template-electron-vue/scripts/release.ts +1 -1
- package/template-electron-vue/src/App.vue +16 -4
- package/template-electron-vue/src/components/HelloWorld.vue +6 -0
- package/template-electron-vue/src/style.css +3 -3
- package/template-electron-vue/vite.config.ts +9 -80
- package/template-node/package.json +3 -3
- package/template-react/package.json +3 -3
- package/template-vue/package.json +5 -5
- package/template-electron-react/.vscode/.debug.env +0 -1
- package/template-electron-react/.vscode/.debug.script.mjs +0 -23
- package/template-electron-react/.vscode/extensions.json +0 -6
- package/template-electron-react/.vscode/launch.json +0 -53
- package/template-electron-react/.vscode/settings.json +0 -13
- package/template-electron-react/.vscode/tasks.json +0 -31
- package/template-electron-react/electron/electron-env.d.ts +0 -11
- package/template-electron-vue/.vscode/.debug.env +0 -1
- package/template-electron-vue/.vscode/.debug.script.mjs +0 -23
- package/template-electron-vue/.vscode/extensions.json +0 -6
- package/template-electron-vue/.vscode/launch.json +0 -53
- package/template-electron-vue/.vscode/settings.json +0 -13
- package/template-electron-vue/.vscode/tasks.json +0 -31
- package/template-electron-vue/electron/electron-env.d.ts +0 -11
package/README.md
CHANGED
|
@@ -1,51 +1,37 @@
|
|
|
1
1
|
# @tomjs/create-app
|
|
2
2
|
|
|
3
|
-
 + [tomjs](https://github.com/tomgao365/tomjs).
|
|
3
|
+
  
|
|
6
4
|
|
|
7
5
|
**English** | [中文](./README.zh_CN.md)
|
|
8
6
|
|
|
9
|
-
|
|
7
|
+
> Create a web application based on [vite](https://github.com/vitejs/vite) + [tomjs](https://github.com/tomgao365/tomjs).
|
|
10
8
|
|
|
11
|
-
|
|
9
|
+
## Creating a Project
|
|
12
10
|
|
|
13
11
|
```bash
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
- Using `yarn`
|
|
12
|
+
# pnpm
|
|
13
|
+
pnpm create @tomjs/app
|
|
18
14
|
|
|
19
|
-
|
|
15
|
+
# yarn
|
|
20
16
|
yarn create @tomjs/app
|
|
21
|
-
```
|
|
22
17
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
```bash
|
|
26
|
-
pnpm create @tomjs/app
|
|
18
|
+
# npm
|
|
19
|
+
npm create @tomjs/app@latest
|
|
27
20
|
```
|
|
28
21
|
|
|
29
22
|
Then generate the project based on the prompts.
|
|
30
23
|
|
|
31
|
-
You can also directly specify the project name, template
|
|
32
|
-
|
|
33
|
-
- Using `npm`
|
|
24
|
+
You can also directly specify the project name, template.
|
|
34
25
|
|
|
35
26
|
```bash
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
- Using `yarn`
|
|
27
|
+
# pnpm
|
|
28
|
+
pnpm create @tomjs/app my-app --template vue
|
|
40
29
|
|
|
41
|
-
|
|
30
|
+
# yarn
|
|
42
31
|
yarn create @tomjs/app my-app --template vue
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
- Using `pnpm`
|
|
46
32
|
|
|
47
|
-
|
|
48
|
-
|
|
33
|
+
# npm 7+, extra double-dash is needed:
|
|
34
|
+
npm create @tomjs/app@latest my-app -- --template vue
|
|
49
35
|
```
|
|
50
36
|
|
|
51
37
|
Parameters:
|
package/README.zh_CN.md
CHANGED
|
@@ -1,52 +1,37 @@
|
|
|
1
1
|
# @tomjs/create-app
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[English](./README.md) | **中文**
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
  
|
|
6
6
|
|
|
7
|
-
[
|
|
7
|
+
> 创建基于 [vite](https://github.com/vitejs/vite) + [tomjs](https://github.com/tomgao365/tomjs) 的 web 应用。
|
|
8
8
|
|
|
9
9
|
## 创建项目
|
|
10
10
|
|
|
11
|
-
- 使用 `npm`
|
|
12
|
-
|
|
13
11
|
```bash
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
- 使用 `yarn`
|
|
12
|
+
# pnpm
|
|
13
|
+
pnpm create @tomjs/app
|
|
18
14
|
|
|
19
|
-
|
|
15
|
+
# yarn
|
|
20
16
|
yarn create @tomjs/app
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
- 使用 `pnpm`
|
|
24
17
|
|
|
25
|
-
|
|
26
|
-
|
|
18
|
+
# npm
|
|
19
|
+
npm create @tomjs/app@latest
|
|
27
20
|
```
|
|
28
21
|
|
|
29
22
|
然后根据提示生成项目。
|
|
30
23
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
- 使用 `npm`
|
|
24
|
+
你也可以直接指定项目名、模板。
|
|
34
25
|
|
|
35
26
|
```bash
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
- 使用 `yarn`
|
|
27
|
+
# pnpm
|
|
28
|
+
pnpm create @tomjs/app my-app --template vue
|
|
41
29
|
|
|
42
|
-
|
|
30
|
+
# yarn
|
|
43
31
|
yarn create @tomjs/app my-app --template vue
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
- 使用 `pnpm`
|
|
47
32
|
|
|
48
|
-
|
|
49
|
-
|
|
33
|
+
# npm 7+,需要额外的双破折号:
|
|
34
|
+
npm create @tomjs/app@latest my-app -- --template vue
|
|
50
35
|
```
|
|
51
36
|
|
|
52
37
|
参数说明:
|
package/dist/index.mjs
CHANGED
|
@@ -4,5 +4,5 @@ import a,{renameSync as Ce}from"fs";import Oe from"os";import i from"path";impor
|
|
|
4
4
|
Scaffolding project in ${s}...`);let T=g||P?.name||n||"",G=t=>i.join(Fe(import.meta.url),"../..",`template-${t}`),we=G(T),U=T.includes("node");[we,G("config")].forEach(t=>{let o=a.readdirSync(t);for(let r of o){let l=r.startsWith("_")?r.replace("_","."):r;if(U&&!f.includes("examples")&&r.includes("stylelint"))continue;let m=i.join(s,l);z(i.join(t,r),m)}});let d=D||h(),Se=`template-${T}`,j={name:"UserName",email:"name@github.com"};function ee(){let t=d.startsWith("@")?d.split("/")[0].substring(1):Ge(j.name);return`${xe||`https://github.com/${t}`}/${d.substring(d.indexOf("/")+1)}.git`}ke(),U&&(je(),Ee(),be());let te=se(process.env.npm_config_user_agent),_=te?te.name:"npm";b.which("git")&&(b.exec(`cd ${s} && git init`),f.includes("publish")&&b.exec(`cd ${s} && git remote add origin ${ee()}`));let I=i.relative(Q,s);switch(console.log(`
|
|
5
5
|
Done. Now run:
|
|
6
6
|
`),s!==Q&&console.log(` cd ${I.includes(" ")?`"${I}"`:I}`),_){case"yarn":console.log(" yarn"),console.log(" yarn dev");break;default:console.log(` ${_} install`),console.log(` ${_} run dev`);break}function je(){["LICENSE","README.md","README.zh_CN.md"].forEach(t=>{let o=i.join(s,t);if(!f.includes("publish")&&a.existsSync(o)){a.rmSync(o),t==="README.md"&&a.writeFileSync(o,`# ${d}
|
|
7
|
-
`,{encoding:"utf-8"});return}if(!a.existsSync(o))return;let r=a.readFileSync(o,"utf-8").replace(new RegExp(Se,"g"),d).replace(new RegExp("{{user.name}}","g"),j.name).replace(new RegExp("{{user.email}}","g"),j.email);if(t.startsWith("README")){let l=d.replace("@","%40"),m=d.replace("@","%40").replace(new RegExp("/","g"),"%2F");r=r.replace("{{badges}}",[``,``,``].join(" "))}a.writeFileSync(o,r)})}function k(t,...o){["dependencies","devDependencies"].forEach(r=>{Object.keys(t[r]||{}).forEach(l=>{o.find(m=>l.includes(m))&&delete t[r][l]})})}function ke(){let t=v(i.join(s,"package.json"));t.name=d,f.includes("publish")?(b.which("git")&&(j.name=ye("user.name")||Oe.userInfo().username,j.email=ye("user.email")||"",t.author=Object.assign(t.author,j)),t.repository.url=`git+${ee()}`):(delete t.author,delete t.publishConfig,delete t.repository,delete t.scripts.prepublishOnly,delete t.devDependencies.np),f.includes("electron")||(k(t,"electron"),t.scripts["lint:eslint"]=t.scripts["lint:eslint"].replace(",electron","")),
|
|
7
|
+
`,{encoding:"utf-8"});return}if(!a.existsSync(o))return;let r=a.readFileSync(o,"utf-8").replace(new RegExp(Se,"g"),d).replace(new RegExp("{{user.name}}","g"),j.name).replace(new RegExp("{{user.email}}","g"),j.email);if(t.startsWith("README")){let l=d.replace("@","%40"),m=d.replace("@","%40").replace(new RegExp("/","g"),"%2F");r=r.replace("{{badges}}",[``,``,``].join(" "))}a.writeFileSync(o,r)})}function k(t,...o){["dependencies","devDependencies"].forEach(r=>{Object.keys(t[r]||{}).forEach(l=>{o.find(m=>l.includes(m))&&delete t[r][l]})})}function ke(){let t=v(i.join(s,"package.json"));t.name=d,f.includes("publish")?(b.which("git")&&(j.name=ye("user.name")||Oe.userInfo().username,j.email=ye("user.email")||"",t.author=Object.assign(t.author,j)),t.repository.url=`git+${ee()}`):(delete t.author,delete t.publishConfig,delete t.repository,delete t.scripts.prepublishOnly,delete t.devDependencies.np),U&&(f.includes("electron")||(k(t,"electron"),t.scripts["lint:eslint"]=t.scripts["lint:eslint"].replace(",electron","")),f.includes("vite")&&k(t,"vite")),x(i.join(s,"package.json"),t)}function W(t,o){let r=t.lastIndexOf("."),l=t.substring(0,r)+"."+o+t.substring(r),m=i.join(s,t),y=i.join(s,l);f.includes(o)?($(m),a.existsSync(y)&&Ce(y,m)):$(y)}function Ee(){if(W("tsconfig.json","test"),W("jest.config.cjs","electron"),f.includes("test"))return;a.readdirSync(s).filter(r=>r.startsWith("jest.config")||["test"].includes(r)).forEach(r=>{$(i.join(s,r))});let t=i.join(s,"package.json"),o=v(t);delete o.scripts.test,o.scripts["lint:eslint"]=o.scripts["lint:eslint"].replace(",test",""),k(o,"jest"),x(t,o)}function be(){if(W(".lintstagedrc.cjs","examples"),!f.includes("examples")){let r=v(i.join(s,"package.json"));r.scripts.lint=r.scripts.lint.replace(" lint:stylelint",""),r.scripts["lint:eslint"]=r.scripts["lint:eslint"].replace(",examples",""),delete r.scripts["lint:stylelint"],k(r,"stylelint"),x(i.join(s,"package.json"),r);return}let t=i.join(s,"examples");a.mkdirSync(t);let o=f.includes("electron");["vue","react"].forEach(r=>{let l=G(o?`electron-${r}`:r),m=i.join(t,r);if(!a.existsSync(l)){console.log(`${Z(l)} template is not exist`);return}a.cpSync(l,m,{recursive:!0});let y=v(i.join(m,"package.json"));Object.keys(y.scripts).forEach(E=>{(E.startsWith("lint")||["prepare"].includes(E))&&delete y.scripts[E]}),k(y,"eslint","prettier","stylelint","commitlint","husky","lint-staged","tsconfig","lint-staged","npm-run-all"),x(i.join(m,"package.json"),y),["_lintstagedrc.cjs"].forEach(E=>{let ne=i.join(m,E);a.existsSync(ne)&&a.rmSync(ne,{recursive:!0})})}),a.writeFileSync(i.join(s,"pnpm-workspace.yaml"),`packages:
|
|
8
8
|
- 'examples/*'`,{encoding:"utf-8"})}}q().then(async()=>Ie().catch(e=>{e.message&&console.error(e)})).catch(e=>{e.message&&console.error(e)});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tomjs/create-app",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "create tomjs web app",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"tomjs",
|
|
@@ -54,8 +54,8 @@
|
|
|
54
54
|
"@tomjs/commitlint": "^2.0.5",
|
|
55
55
|
"@tomjs/eslint": "^1.1.1",
|
|
56
56
|
"@tomjs/prettier": "^1.0.6",
|
|
57
|
-
"@tomjs/stylelint": "^1.1.
|
|
58
|
-
"@tomjs/tsconfig": "^1.0.
|
|
57
|
+
"@tomjs/stylelint": "^1.1.1",
|
|
58
|
+
"@tomjs/tsconfig": "^1.0.9",
|
|
59
59
|
"@types/lodash-es": "^4.17.12",
|
|
60
60
|
"@types/minimist": "^1.2.5",
|
|
61
61
|
"@types/node": "^18.19.3",
|
|
@@ -4,23 +4,31 @@ vite + electron + react
|
|
|
4
4
|
|
|
5
5
|
## Description
|
|
6
6
|
|
|
7
|
-
-
|
|
7
|
+
- Recommended `electron` front-end code directory structure
|
|
8
8
|
|
|
9
9
|
```
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
|--electron
|
|
11
|
+
| |--main
|
|
12
|
+
| | |--index.ts
|
|
13
|
+
| |--preload
|
|
14
|
+
| | |--index.ts
|
|
15
|
+
|--src
|
|
16
|
+
| |--App.tsx
|
|
17
|
+
| |--main.tsx
|
|
14
18
|
```
|
|
15
19
|
|
|
16
|
-
- output
|
|
20
|
+
- Use the default dist output directory of the plugin
|
|
17
21
|
|
|
18
22
|
```
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
|--dist
|
|
24
|
+
| |--main
|
|
25
|
+
| | |--index.js
|
|
26
|
+
| | |--index.js.map
|
|
27
|
+
| |--preload
|
|
28
|
+
| | |--index.js
|
|
29
|
+
| | |--index.js.map
|
|
30
|
+
| |--renderer
|
|
31
|
+
| | |--index.html
|
|
24
32
|
```
|
|
25
33
|
|
|
26
34
|
## Reference project
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/// <reference types="@tomjs/vite-plugin-electron/env" />
|
|
@@ -3,20 +3,8 @@ import { dirname, join } from 'node:path';
|
|
|
3
3
|
import { fileURLToPath } from 'node:url';
|
|
4
4
|
import { app, BrowserWindow, ipcMain, shell } from 'electron';
|
|
5
5
|
|
|
6
|
-
// The built directory structure
|
|
7
|
-
//
|
|
8
|
-
// ├─┬ dist
|
|
9
|
-
// │ ├── main.js > Electron-Main
|
|
10
|
-
// │ ├── preload.js > Preload-Scripts
|
|
11
|
-
// │ ├─┬ render > Electron-Renderer
|
|
12
|
-
// │ │ └── index.html
|
|
13
|
-
//
|
|
14
6
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
15
|
-
process.env.
|
|
16
|
-
process.env.DIST = join(process.env.DIST_ELECTRON, 'render');
|
|
17
|
-
process.env.VITE_PUBLIC = process.env.VITE_DEV_SERVER_URL
|
|
18
|
-
? join(process.env.DIST_ELECTRON, '../public')
|
|
19
|
-
: process.env.DIST;
|
|
7
|
+
const isDev = process.env.NODE_ENV === 'development';
|
|
20
8
|
|
|
21
9
|
// Disable GPU Acceleration for Windows 7
|
|
22
10
|
if (release().startsWith('6.1')) app.disableHardwareAcceleration();
|
|
@@ -32,19 +20,22 @@ if (!app.requestSingleInstanceLock()) {
|
|
|
32
20
|
// Remove electron security warnings
|
|
33
21
|
// This warning only shows in development mode
|
|
34
22
|
// Read more on https://www.electronjs.org/docs/latest/tutorial/security
|
|
35
|
-
|
|
23
|
+
process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true';
|
|
36
24
|
|
|
37
25
|
let win: BrowserWindow | null = null;
|
|
38
|
-
//
|
|
39
|
-
const preload = join(__dirname, '
|
|
40
|
-
const
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
26
|
+
// package.json "type":"module", must use mjs extension
|
|
27
|
+
const preload = join(__dirname, '../preload/index.mjs');
|
|
28
|
+
const serverUrl = process.env.APP_DEV_SERVER_URL as string;
|
|
29
|
+
const rendererDist = join(__dirname, '../renderer');
|
|
30
|
+
const indexHtml = join(rendererDist, 'index.html');
|
|
31
|
+
const publicPath = serverUrl ? join(__dirname, '../../public') : rendererDist;
|
|
32
|
+
|
|
33
|
+
function createWindow() {
|
|
44
34
|
win = new BrowserWindow({
|
|
45
35
|
title: 'Main window',
|
|
46
36
|
width: 800,
|
|
47
37
|
height: 700,
|
|
38
|
+
icon: join(publicPath, 'img/icon.png'),
|
|
48
39
|
webPreferences: {
|
|
49
40
|
preload,
|
|
50
41
|
// Warning: Enable nodeIntegration and disable contextIsolation is not secure in production
|
|
@@ -55,11 +46,10 @@ async function createWindow() {
|
|
|
55
46
|
},
|
|
56
47
|
});
|
|
57
48
|
|
|
58
|
-
if (
|
|
59
|
-
|
|
60
|
-
win.loadURL(url);
|
|
49
|
+
if (isDev) {
|
|
50
|
+
win.loadURL(serverUrl);
|
|
61
51
|
// Open devTool if the app is not packaged
|
|
62
|
-
|
|
52
|
+
win.webContents.openDevTools();
|
|
63
53
|
} else {
|
|
64
54
|
win.loadFile(indexHtml);
|
|
65
55
|
}
|
|
@@ -77,7 +67,26 @@ async function createWindow() {
|
|
|
77
67
|
// win.webContents.on('will-navigate', (event, url) => { }) #344
|
|
78
68
|
}
|
|
79
69
|
|
|
80
|
-
app.whenReady().then(
|
|
70
|
+
app.whenReady().then(async () => {
|
|
71
|
+
createWindow();
|
|
72
|
+
|
|
73
|
+
if (isDev) {
|
|
74
|
+
const { installExtension, REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS } = await import(
|
|
75
|
+
'@tomjs/electron-devtools-installer'
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
installExtension([REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS])
|
|
79
|
+
.then(exts => {
|
|
80
|
+
console.log(
|
|
81
|
+
'Added Extension: ',
|
|
82
|
+
exts.map(s => s.name),
|
|
83
|
+
);
|
|
84
|
+
})
|
|
85
|
+
.catch(() => {
|
|
86
|
+
console.log('Failed to install extensions');
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
});
|
|
81
90
|
|
|
82
91
|
app.on('window-all-closed', () => {
|
|
83
92
|
win = null;
|
|
@@ -111,8 +120,8 @@ ipcMain.handle('open-win', (_, arg) => {
|
|
|
111
120
|
},
|
|
112
121
|
});
|
|
113
122
|
|
|
114
|
-
if (
|
|
115
|
-
childWindow.loadURL(`${
|
|
123
|
+
if (serverUrl) {
|
|
124
|
+
childWindow.loadURL(`${serverUrl}#${arg}`);
|
|
116
125
|
} else {
|
|
117
126
|
childWindow.loadFile(indexHtml, { hash: arg });
|
|
118
127
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
console.log('Electron Preload Process!');
|
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "electron-app",
|
|
3
3
|
"version": "0.0.0",
|
|
4
|
+
"type": "module",
|
|
4
5
|
"description": "vite + electron + vue",
|
|
5
6
|
"engines": {
|
|
6
7
|
"node": ">=18"
|
|
7
8
|
},
|
|
8
|
-
"
|
|
9
|
-
"env": {
|
|
10
|
-
"VITE_DEV_SERVER_URL": "http://127.0.0.1:6888/"
|
|
11
|
-
}
|
|
12
|
-
},
|
|
13
|
-
"main": "dist/main.js",
|
|
9
|
+
"main": "dist/main/index.mjs",
|
|
14
10
|
"scripts": {
|
|
15
11
|
"dev": "vite",
|
|
16
12
|
"build": "npm run clean && vite build && npm run release",
|
|
@@ -29,10 +25,12 @@
|
|
|
29
25
|
"devDependencies": {
|
|
30
26
|
"@commitlint/cli": "^18.4.3",
|
|
31
27
|
"@tomjs/commitlint": "^2.0.5",
|
|
28
|
+
"@tomjs/electron-devtools-installer": "^2.0.3",
|
|
32
29
|
"@tomjs/eslint": "^1.1.1",
|
|
33
30
|
"@tomjs/prettier": "^1.0.6",
|
|
34
|
-
"@tomjs/stylelint": "^1.1.
|
|
35
|
-
"@tomjs/tsconfig": "^1.0.
|
|
31
|
+
"@tomjs/stylelint": "^1.1.1",
|
|
32
|
+
"@tomjs/tsconfig": "^1.0.9",
|
|
33
|
+
"@tomjs/vite-plugin-electron": "^1.1.2",
|
|
36
34
|
"@types/node": "^18.19.3",
|
|
37
35
|
"@types/react": "^18.2.42",
|
|
38
36
|
"@types/react-dom": "^18.2.17",
|
|
@@ -48,11 +46,9 @@
|
|
|
48
46
|
"rimraf": "^5.0.5",
|
|
49
47
|
"shelljs": "^0.8.5",
|
|
50
48
|
"stylelint": "^15.11.0",
|
|
51
|
-
"tree-kill": "^1.2.2",
|
|
52
49
|
"tsx": "^4.6.2",
|
|
53
50
|
"typescript": "~5.2.2",
|
|
54
|
-
"vite": "^5.0.
|
|
55
|
-
"vite-plugin-electron": "^0.15.4",
|
|
51
|
+
"vite": "^5.0.7",
|
|
56
52
|
"vite-plugin-electron-renderer": "^0.14.5"
|
|
57
53
|
}
|
|
58
54
|
}
|
|
@@ -11,7 +11,7 @@ export default {
|
|
|
11
11
|
output: `build/${pkg.version}`,
|
|
12
12
|
app: 'dist',
|
|
13
13
|
},
|
|
14
|
-
files: ['main.
|
|
14
|
+
files: ['main/index.mjs', 'preload/index.mjs', 'renderer'],
|
|
15
15
|
icon: 'public/img/icon.png',
|
|
16
16
|
artifactName: '${productName}-${version}-${os}-${arch}.${ext}',
|
|
17
17
|
electronLanguages: ['zh-CN', 'en-US'],
|
|
@@ -13,7 +13,7 @@ function genPkgJson() {
|
|
|
13
13
|
version: pkg.version,
|
|
14
14
|
author: os.userInfo().username,
|
|
15
15
|
description: pkg.description,
|
|
16
|
-
main: '
|
|
16
|
+
main: 'main/index.mjs',
|
|
17
17
|
dependencies: Object.entries(Object.assign({}, pkg.dependencies, pkg.devDependencies))
|
|
18
18
|
.filter(([name]) => ELECTRON_DEPENDENCIES.includes(name))
|
|
19
19
|
.reduce((object, entry) => ({ ...object, [entry[0]]: entry[1] }), {}),
|
|
@@ -9,7 +9,9 @@ import './App.css';
|
|
|
9
9
|
function App() {
|
|
10
10
|
const [count, setCount] = useState(0);
|
|
11
11
|
|
|
12
|
-
const
|
|
12
|
+
const versions = ['chrome', 'node', 'electron']
|
|
13
|
+
.map(s => `${s}: ${process.versions[s]}`)
|
|
14
|
+
.concat(['platform: ' + os.platform() + ' ' + os.arch()]);
|
|
13
15
|
|
|
14
16
|
return (
|
|
15
17
|
<>
|
|
@@ -25,8 +27,8 @@ function App() {
|
|
|
25
27
|
</a>
|
|
26
28
|
</div>
|
|
27
29
|
<h1>Vite + Electron + React</h1>
|
|
28
|
-
<div>{info}</div>
|
|
29
30
|
<div className="card">
|
|
31
|
+
<div style={{ marginBottom: '1em' }}>{versions.join(', ')}</div>
|
|
30
32
|
<button onClick={() => setCount(count => count + 1)}>count is {count}</button>
|
|
31
33
|
<p>
|
|
32
34
|
Edit <code>src/App.tsx</code> and save to test HMR
|
|
@@ -1,22 +1,12 @@
|
|
|
1
|
-
import { rmSync } from 'node:fs';
|
|
2
1
|
import path from 'node:path';
|
|
3
2
|
import { defineConfig } from 'vite';
|
|
4
|
-
import electron from 'vite-plugin-electron';
|
|
5
|
-
import { notBundle } from 'vite-plugin-electron/plugin';
|
|
6
3
|
import renderer from 'vite-plugin-electron-renderer';
|
|
4
|
+
import electron from '@tomjs/vite-plugin-electron';
|
|
7
5
|
import react from '@vitejs/plugin-react-swc';
|
|
8
6
|
import pkg from './package.json';
|
|
9
7
|
|
|
10
8
|
// https://vitejs.dev/config/
|
|
11
|
-
export default defineConfig((
|
|
12
|
-
['main.js', 'main.js.map', 'preload.js'].forEach(s => {
|
|
13
|
-
rmSync(`dist/${s}`, { recursive: true, force: true });
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
const isServe = command === 'serve';
|
|
17
|
-
const isBuild = command === 'build';
|
|
18
|
-
const sourcemap = isServe || !!process.env.VSCODE_DEBUG;
|
|
19
|
-
|
|
9
|
+
export default defineConfig(() => {
|
|
20
10
|
process.env.APP_BUILD_TIME = Date.now() + '';
|
|
21
11
|
process.env.APP_VERSION = pkg.version;
|
|
22
12
|
|
|
@@ -28,84 +18,22 @@ export default defineConfig(({ command }) => {
|
|
|
28
18
|
},
|
|
29
19
|
},
|
|
30
20
|
build: {
|
|
31
|
-
outDir: 'dist/render',
|
|
32
21
|
emptyOutDir: true,
|
|
33
22
|
},
|
|
34
23
|
plugins: [
|
|
35
24
|
react(),
|
|
36
|
-
electron(
|
|
37
|
-
{
|
|
38
|
-
|
|
39
|
-
onstart({ startup }) {
|
|
40
|
-
if (process.env.VSCODE_DEBUG) {
|
|
41
|
-
console.log(/* For `.vscode/.debug.script.mjs` */ '[startup] Electron App');
|
|
42
|
-
} else {
|
|
43
|
-
startup();
|
|
44
|
-
}
|
|
45
|
-
},
|
|
46
|
-
vite: {
|
|
47
|
-
build: {
|
|
48
|
-
sourcemap,
|
|
49
|
-
minify: isBuild,
|
|
50
|
-
outDir: 'dist',
|
|
51
|
-
emptyOutDir: false,
|
|
52
|
-
lib: {
|
|
53
|
-
entry: 'electron/main/index.ts',
|
|
54
|
-
formats: ['cjs'],
|
|
55
|
-
fileName: () => 'main.js',
|
|
56
|
-
},
|
|
57
|
-
rollupOptions: {
|
|
58
|
-
// Some third-party Node.js libraries may not be built correctly by Vite, especially `C/C++` addons,
|
|
59
|
-
// we can use `external` to exclude them to ensure they work correctly.
|
|
60
|
-
// Others need to put them in `dependencies` to ensure they are collected into `app.asar` after the app is built.
|
|
61
|
-
// Of course, this is not absolute, just this way is relatively simple. :)
|
|
62
|
-
external: Object.keys('dependencies' in pkg ? pkg.dependencies : {}),
|
|
63
|
-
},
|
|
64
|
-
},
|
|
65
|
-
plugins: [
|
|
66
|
-
// This is just an option to improve build performance, it's non-deterministic!
|
|
67
|
-
// e.g. `import log from 'electron-log'` -> `const log = require('electron-log')`
|
|
68
|
-
isServe && notBundle(),
|
|
69
|
-
],
|
|
70
|
-
},
|
|
25
|
+
electron({
|
|
26
|
+
main: {
|
|
27
|
+
entry: 'electron/main/index.ts',
|
|
71
28
|
},
|
|
72
|
-
{
|
|
73
|
-
|
|
74
|
-
// Notify the Renderer process to reload the page when the Preload scripts build is complete,
|
|
75
|
-
// instead of restarting the entire Electron App.
|
|
76
|
-
reload();
|
|
77
|
-
},
|
|
78
|
-
vite: {
|
|
79
|
-
build: {
|
|
80
|
-
sourcemap: sourcemap ? 'inline' : undefined, // #332
|
|
81
|
-
minify: isBuild,
|
|
82
|
-
outDir: 'dist',
|
|
83
|
-
emptyOutDir: false,
|
|
84
|
-
lib: {
|
|
85
|
-
entry: 'electron/preload/index.ts',
|
|
86
|
-
formats: ['cjs'],
|
|
87
|
-
fileName: () => 'preload.js',
|
|
88
|
-
},
|
|
89
|
-
rollupOptions: {
|
|
90
|
-
external: Object.keys('dependencies' in pkg ? pkg.dependencies : {}),
|
|
91
|
-
},
|
|
92
|
-
},
|
|
93
|
-
plugins: [isServe && notBundle()],
|
|
94
|
-
},
|
|
29
|
+
preload: {
|
|
30
|
+
entry: 'electron/preload/index.ts',
|
|
95
31
|
},
|
|
96
|
-
|
|
32
|
+
}),
|
|
97
33
|
// Use Node.js API in the Renderer process
|
|
98
34
|
renderer(),
|
|
99
35
|
],
|
|
100
|
-
server:
|
|
101
|
-
? (() => {
|
|
102
|
-
const url = new URL(pkg.debug.env.VITE_DEV_SERVER_URL);
|
|
103
|
-
return {
|
|
104
|
-
host: url.hostname,
|
|
105
|
-
port: +url.port,
|
|
106
|
-
};
|
|
107
|
-
})()
|
|
108
|
-
: {},
|
|
36
|
+
server: {},
|
|
109
37
|
clearScreen: false,
|
|
110
38
|
};
|
|
111
39
|
});
|
|
@@ -4,23 +4,31 @@ vite + electron + vue
|
|
|
4
4
|
|
|
5
5
|
## Description
|
|
6
6
|
|
|
7
|
-
-
|
|
7
|
+
- Recommended `electron` front-end code directory structure
|
|
8
8
|
|
|
9
9
|
```
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
|--electron
|
|
11
|
+
| |--main
|
|
12
|
+
| | |--index.ts
|
|
13
|
+
| |--preload
|
|
14
|
+
| | |--index.ts
|
|
15
|
+
|--src
|
|
16
|
+
| |--App.vue
|
|
17
|
+
| |--main.ts
|
|
14
18
|
```
|
|
15
19
|
|
|
16
|
-
- output
|
|
20
|
+
- Use the default dist output directory of the plugin
|
|
17
21
|
|
|
18
22
|
```
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
|--dist
|
|
24
|
+
| |--main
|
|
25
|
+
| | |--index.js
|
|
26
|
+
| | |--index.js.map
|
|
27
|
+
| |--preload
|
|
28
|
+
| | |--index.js
|
|
29
|
+
| | |--index.js.map
|
|
30
|
+
| |--renderer
|
|
31
|
+
| | |--index.html
|
|
24
32
|
```
|
|
25
33
|
|
|
26
34
|
## Reference project
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/// <reference types="@tomjs/vite-plugin-electron/env" />
|