my-electron-app
├── package.json
├── main.js
├── preload.js
└── index.html
1.mkdir my-electron-app-name && cd my-electron-app-name
2.npm init -y
3.npm i --save-dev electron
// 为了管理应用程序的生命周期事件以及创建和控制浏览器窗口,您从 electron 包导入了 app 和 BrowserWindow 模块 。
const { app, BrowserWindow } = require('electron')
// 导入 path 包,该包为操作文件路径提供了实用的功能。
const path = require('path')
// 在此之后,你定义一个方法用来创建一个带有预加载脚本的新的浏览器窗口,并加载index.html文件进入该窗口
function createWindow () {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
// 你通过调用 createWindow方法,在 electron app 第一次被初始化时创建了一个新的窗口。
win.loadFile('index.html')
}
app.whenReady().then(() => {
createWindow()
// 您添加一个新的侦听器,只有当应用程序激活后没有可见窗口时,才能创建新的浏览器窗口。 例如,在首次启动应用程序后或重启运行中的应用程序
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
})
// 您添加了一个新的侦听器,当应用程序不再有任何打开窗口时试图退出。 由于操作系统的 窗口管理行为 ,此监听器在 macOS 上是禁止操作的
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
Hello World!
Hello World!
We are using Node.js ,
Chromium ,
and Electron .
//首先定义了一个事件监听器,当web页面加载完成后通知你。
window.addEventListener('DOMContentLoaded', () => {
//接着您定义了一个功能函数用来为index.html中的所有placeholder设置文本。
const replaceText = (selector, text) => {
const element = document.getElementById(selector)
if (element) element.innerText = text
}
//接下来您遍历了您想展示版本号的组件列表。
for (const type of ['chrome', 'node', 'electron']) {
//最终,您调用 replaceText 来查找index.html中的版本占位符并将其文本值设置为process.versions的值。
replaceText(`${type}-version`, process.versions[type])
}
})
未修改前:
{
"name": "my-electron-app-name",
"version": "0.1.0",//版本
"author": "your name",//作者
"description": "My Electron app",//描述
"main": "index.js"//主脚本
}
注意:如果未设置 main 字段,Electron 将尝试加载包含在 package.json 文件目录中的 index.js 文件。
注意:author 和 description 字段对于打包来说是必要的,否则运行 npm run make 命令时会报错。
{
"name": "my-electron-app-name",
"version": "0.1.0",
"author": "your name",
"description": "My Electron app",
"main": "main.js",
"scripts": {
"start": "electron ."
}
}
npm start
npm install --save-dev @electron-forge/cli
npx electron-forge import
✔ Checking your system
✔ Initializing Git Repository
✔ Writing modified package.json file
✔ Installing dependencies
✔ Writing modified package.json file
✔ Fixing .gitignore
We have ATTEMPTED to convert your app to be in a format that electron-forge understands.
Thanks for using "electron-forge"!!!
npm run make
> [email protected] make /my-electron-app
> electron-forge make
✔ Checking your system
✔ Resolving Forge Config
We need to package your application before we can make it
✔ Preparing to Package Application for arch: x64
✔ Preparing native dependencies
✔ Packaging Application
Making for the following targets: zip
✔ Making for target: zip - On platform: darwin - For arch: x64
Electron-forge 会创建 out 文件夹,您的软件包将在那里找到:
// MacOS 示例
out/
├── out/make/zip/darwin/x64/my-electron-app-darwin-x64-1.0.0.zip
├── ...
└── out/my-electron-app-darwin-x64/my-electron-app.app/Contents/MacOS/my-electron-app
// Windows 示例
out/
├── out\make\squirrel.windows\x64
├── ...
└── out/my-electron-app-darwin-x64/
npm install --save-dev electron-reloader
try {
require('electron-reloader')(module,{});
} catch (_) {}
npm start