Electron Playground 项目教程
1. 项目的目录结构及介绍
Electron Playground 项目的目录结构如下:
electron-playground/
├── app/
│ ├── build/
│ ├── example/
│ │ └── terminal/
│ ├── playground/
│ └── resources/
├── env/
├── .eslintignore
├── .eslintrc.js
├── .gitignore
├── .huskyrc
├── .npmrc
├── .prettierignore
├── .prettierrc
├── .stylelintrc
├── CHANGELOG.md
├── CODE_OF_CONDUCT.md
├── README.md
├── commitlint.config.js
├── electron-builder-template.yml
├── electron-builder.yml
目录介绍
- app/: 包含项目的主要应用代码。
- build/: 存放构建后的文件。
- example/: 包含示例代码,如
terminal/
。 - playground/: 包含用于实验和学习的代码。
- resources/: 存放项目资源文件。
- env/: 存放环境配置文件。
- .eslintignore: ESLint 忽略文件配置。
- .eslintrc.js: ESLint 配置文件。
- .gitignore: Git 忽略文件配置。
- .huskyrc: Husky 配置文件。
- .npmrc: npm 配置文件。
- .prettierignore: Prettier 忽略文件配置。
- .prettierrc: Prettier 配置文件。
- .stylelintrc: Stylelint 配置文件。
- CHANGELOG.md: 项目变更日志。
- CODE_OF_CONDUCT.md: 项目行为准则。
- README.md: 项目说明文档。
- commitlint.config.js: commitlint 配置文件。
- electron-builder-template.yml: Electron 构建模板配置文件。
- electron-builder.yml: Electron 构建配置文件。
2. 项目的启动文件介绍
项目的启动文件主要是 app/playground/index.js
,该文件负责启动 Electron 应用。
// app/playground/index.js
const { app, BrowserWindow } = require('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();
}
});
启动文件介绍
- 引入 Electron 模块: 使用
require('electron')
引入 Electron 模块。 - 创建窗口: 使用
BrowserWindow
创建主窗口。 - 加载文件: 使用
loadFile
方法加载index.html
文件。 - 事件处理: 处理
window-all-closed
和activate
事件,确保应用在不同平台上的行为一致。
3. 项目的配置文件介绍
项目的配置文件主要包括以下几个:
- .eslintrc.js: ESLint 配置文件,用于代码风格检查。
- .prettierrc: Prettier 配置文件,用于代码格式化。
- .stylelintrc: Stylelint 配置文件,用于样式检查。
- electron-builder.yml: Electron 构建配置文件,用于打包应用。
配置文件介绍
.eslintrc.js
module.exports = {
root: true,
env: {
node: true,
es6: true,
},
extends: [
'eslint:recommended',
],
rules: {
// 自定义规则
},
};
.prettierrc
{
"singleQuote": true,
"trailingComma": "all",
"printWidth":