Lançado · em melhoria
Guia de Electron · 1/6
Por enquanto, este capítulo está disponível apenas em inglês.
This chapter sets up a development environment and builds a first desktop app from an empty folder. Along the way you will scaffold a project with Electron Forge, the official tooling, and learn how to debug the main process from your editor.
Electron is distributed as an npm package and driven by Node.js tooling, so install a Node.js LTS release first. Note that the Node.js running inside your app is the one embedded in Electron; the Node.js on your machine is used to install packages and run build tools.
node -v
npm -vIf both commands print a version, you are ready. If you ever need to compile native modules, you may also need the C++ build tools for Visual Studio on Windows or the Xcode Command Line Tools on macOS.
Building a project manually once is the best way to see how the pieces fit together. Create a folder, generate a package.json, and install electron.
mkdir my-electron-app
cd my-electron-app
npm init -y
npm install --save-dev electronInstall electron as a dev dependency. Packaging tools bundle the Electron binary into your finished app themselves, so it does not belong among the runtime dependencies.
Next, point the main field of package.json at your entry file and add a start script. Running electron . reads the package.json in the current folder and launches the file named in main as the main process.
{
"name": "my-electron-app",
"version": "1.0.0",
"main": "main.js",
"scripts": {
"start": "electron ."
}
}Now write the main process in main.js. Once the app is ready (app.whenReady()), it creates a window and loads an HTML file.
const { app, BrowserWindow } = require('electron')
function createWindow() {
const win = new BrowserWindow({ width: 800, height: 600 })
win.loadFile('index.html')
}
app.whenReady().then(() => {
createWindow()
// macOS: recreate a window when the dock icon is clicked and none are open
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
// Quit when all windows are closed, except on macOS
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
})The index.html shown in the window is an ordinary web page. Adding a Content Security Policy from day one keeps unexpected scripts from running.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'" />
<title>Hello Electron</title>
</head>
<body>
<h1>Hello from Electron</h1>
</body>
</html>Run npm start and a desktop window opens with your page.
For real projects, start with Electron Forge. It ties together scaffolding, running in development, packaging, building installers, and publishing under a single set of commands.
npm init electron-app@latest my-app
cd my-app
npm start
# A template with TypeScript and Vite
npm init electron-app@latest my-ts-app -- --template=vite-typescriptAvailable templates include webpack, webpack-typescript, vite, and vite-typescript. To add Forge to an existing project, install @electron-forge/cli and run npx electron-forge import.
Visual Studio Code is a solid choice thanks to its TypeScript support. The renderer (your web page) is debugged with the Chromium DevTools: open them from the View menu of the default application menu, or call win.webContents.openDevTools() in code.
The main process is plain Node.js code, so use a Node.js debugger. In VS Code, add this configuration to .vscode/launch.json and press F5.
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Main Process",
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/electron",
"windows": {
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/electron.cmd"
},
"args": ["."],
"outputCapture": "std"
}
]
}From the command line you can run npx electron --inspect=5858 . and attach from chrome://inspect in Chrome. For quick API experiments without a project, try Electron Fiddle, the official playground.
main field of package.json names the main process entry file, and electron . runs it.BrowserWindow, loads HTML, and follows each platform's quit conventions.npm init electron-app@latest to get an Electron Forge setup.--inspect or the VS Code debugger.
0 comentários
Fazer login · Faça login para deixar um comentário.
Seja o primeiro a comentar.