Electron Windows Installer 使用教程
1. 项目的目录结构及介绍
Electron Windows Installer 项目的目录结构如下:
/
├── .github/
│ └── workflows/
├── scripts/
├── src/
│ ├── main/
│ └── renderer/
├── .gitignore
├── .npmrc
├── package.json
├── README.md
└── tsconfig.json
目录介绍
- .github/workflows/: 包含 GitHub Actions 的工作流配置文件。
- scripts/: 包含项目使用的脚本文件。
- src/: 项目的源代码目录,包含
main
和renderer
两个子目录。- main/: 主进程的源代码。
- renderer/: 渲染进程的源代码。
- .gitignore: Git 忽略文件配置。
- .npmrc: npm 配置文件。
- package.json: 项目的 npm 配置文件,包含依赖、脚本等信息。
- README.md: 项目说明文档。
- tsconfig.json: TypeScript 配置文件。
2. 项目的启动文件介绍
项目的启动文件位于 src/main/
目录下,通常是 index.ts
或 main.ts
。这个文件负责启动 Electron 应用的主进程。
// src/main/index.ts
import { app, BrowserWindow } from 'electron';
function createWindow() {
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
},
});
mainWindow.loadFile('index.html');
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
3. 项目的配置文件介绍
package.json
package.json
文件包含了项目的元数据和依赖信息。以下是一些关键字段:
{
"name": "electron-windows-installer",
"version": "1.0.0",
"main": "src/main/index.ts",
"scripts": {
"start": "electron .",
"build": "tsc",
"package": "electron-builder"
},
"dependencies": {
"electron": "^13.1.7"
},
"devDependencies": {
"typescript": "^4.3.5",
"electron-builder": "^22.11.7"
}
}
tsconfig.json
tsconfig.json
文件是 TypeScript 的配置文件,用于配置 TypeScript 编译选项。
{
"compilerOptions": {
"target": "ES2017",
"module": "commonjs",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true
},
"include": ["src/**/*"]
}
通过以上配置,可以确保项目能够正确编译和运行。