Electron環境搭建與在VScode下進行調試

Electron——環境搭建

1. 安裝Electron

cnpm install electron --save-dev # 本地安裝Electron
npx electron -v # 檢查版本

2. 項目根目錄下新建main.js文件(程序入口)

// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
 
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow
 
function createWindow () {
    // Create the browser window.
    mainWindow = new BrowserWindow({
        width: 800,
        height: 600,
        webPreferences: {
            nodeIntegration: true
        }
    })
 
    // and load the index.html of the app.
    mainWindow.loadFile('index.html')
 
    // Open the DevTools.
    // mainWindow.webContents.openDevTools()
 
    // Emitted when the window is closed.
    mainWindow.on('closed', function () {
        // Dereference the window object, usually you would store windows
        // in an array if your app supports multi windows, this is the time
        // when you should delete the corresponding element.
        mainWindow = null
    })
}
 
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)
 
// Quit when all windows are closed.
app.on('window-all-closed', function () {
    // On macOS it is common for applications and their menu bar
    // to stay active until the user quits explicitly with Cmd + Q
    if (process.platform !== 'darwin') app.quit()
})
 
app.on('activate', function () {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (mainWindow === null) createWindow()
})
 
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.

3. 項目根目錄下新建index.html文件 (主頁)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hello World!</title>
</head>
<body>
    Hello World!
</body>
</html>

4. 初始化環境

npm init --yes  # 生成package.json文件

5. 在package.json文件中配置electron命令

"scripts": {
    "start": "electron .",
  }

6. 啓動程序

npm start

使用 VSCode 進行主進程調試

1.在 VSCode 中打開一個 Electron 項目。

$ git clone [email protected]:electron/electron-quick-start.git
$ code electron-quick-start

2.添加一個 .vscode/launch.json 文件並使用以下配置:

{
  "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" : ["."]
    }
  ]
}

3. 調試

main.js中設置一些斷點,並在 Debug 視圖 中開始調試。你應該能夠捕獲斷點信息。

這是一個預先配置的項目,你可以下載並直接在 VSCode中調試: https://github.com/octref/vscode-electron-debug/tree/master/electron-quick-start

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章