在电子应用程序开发过程中同步代码更改

Sync code changes in electron app while developing

本文关键字:代码 同步 过程中 应用程序开发      更新时间:2023-09-26

当代码发生类似于web浏览器同步的变化时,是否有任何工具可以实时重新加载电子应用程序?

每当我们更改电子应用程序的代码时,我将终止现有的运行进程并重新启动电子。

在这种情况下,你应该看看用于NodeJS进程管理的开发工具。我个人最喜欢的是nodemon,因为你既可以使用配置文件,也可以传递这样的东西:

nodemon --watch . --exec "electron ."

它会工作得很好。但是,这是我的意见,从列表中选择适合你的

我发现最好的(也是最简单的)工具是电子重载:

// main.js
const electron = require('electron');
const { app, BrowserWindow } = electron;
const path = require('path');
// the first argument can be: a file, directory or glob pattern
require('electron-reload')(__dirname + '/app/index.html', {
  electron: path.join(__dirname, 'node_modules', '.bin', 'electron')
});
let mainWindow;
app.on('ready', () => {
  mainWindow = new BrowserWindow({
    // ...
  });
  mainWindow.setMenu(null);
  mainWindow.loadURL(`file://${__dirname}/app/index.html`);
  process.env.NODE_ENV !== 'production' && mainWindow.openDevTools();
});

如果直接使用命令"electron ."

"nodemon": "nodemon --exec electron ."

那么它会给你一个错误

'electron' is not recognized as an internal or external command,
 operable program or batch file.

So Use it 间接

"start": "electron .",
"start:nodemon": "nodemon --watch main.js --exec npm start",

并使用

重新启动应用程序
npm run start:nodemon

有点晚了,但我希望它能帮助到大家。
有一个npm模块叫做Electromon。

npm i -g electromon [install]

用法为electron .'main.js[把main.js的名字改成app.js之类的。)