npm install -g electron-forge
electron-forge init simple-desktop-app-electronjs
simple-desktop-app-electronjs
. It will take some time to execute this command. After the template application is created, you can start it like this: cd simple-desktop-app-electronjs npm start
start
, was used to launch the application. New scripts in this file can be added independently.config.forge
file config.forge
can find specific settings for Electron. For example, the section make_targets
contains subsections describing the goals of building a project for Windows platforms ( win32
), Mac ( darwin
) and Linux ( linux
).package.json
you can find the following entry: "main": "src/index.js"
, which indicates that the entry point to the application is the file located at src/index.js
.package.json
, the main application script is index.js
. The process that executes this script is called the main process. This process controls the application. It is used in the formation of the application interface, which is based on the capabilities of the browser. It is also responsible for interaction with the operating system. The application interface is represented by web pages. Rendering of web pages and the execution of their code is the responsibility of the rendering process (renderer process).BrowserWindow
object. This object uses the rendering process to organize the operation of web pages.index.html
and abcd.html
. In our example, only one page will be used, represented by the index.html
file.index.js
loads the index.html
file into a new instance of BrowserWindow
. If we describe this process in simple words, it turns out that index.js
creates a new browser window and loads the page described in the index.html
file into it. This page runs in its own rendering process.index.js
file index.js
well commented. Consider its most important parts. So, the following code snippet of the createWindow()
function creates an instance of the BrowserWindow
object, loads the index.html
file into the window represented by this object, and opens the developer tools. // Создаём окно браузера. mainWindow = new BrowserWindow({ width: 800, height: 600, }); // и загружаем в него файл приложения index.html. mainWindow.loadURL(`file://${__dirname}/index.html`); // Открываем инструменты разработчика. mainWindow.webContents.openDevTools();
app
object is often found. For example, in the following snippet: // Этот метод будет вызван после того, как Electron завершит // инициализацию и будет готов к созданию окон браузера. // Некоторые API можно использовать только после возникновения этого события. app.on('ready', createWindow);
app
object is used to manage the life cycle of the application. In this case, after the initialization of Electron is completed, the function responsible for creating the application window is called.app
object is also used to perform other actions when various events occur. For example, it can be used to organize the execution of certain operations before closing an application.simple-desktop-app-electronjs
. npm install bootstrap --save
index.html
file with the following: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Temperature Converter</title> <link rel="stylesheet" type="text/css" href="../node_modules/bootstrap/dist/css/bootstrap.min.css"> </head> <body> <h1>Temperature Converter</h1> <div class="form-group col-md-3"> <label for="usr">Celcius:</label> <input type="text" class="form-control" id="celcius" onkeyup="celciusToFahrenheit()"> </div> <div class="form-group col-md-3"> <label for="pwd">Fahrenheit:</label> <input type="text" class="form-control" id="fahrenheit" onkeyup="fahrenheitToCelcius()"> </div> <script src='./renderer.js'></script> </body> </body> </html>
celcius
. When a user enters a certain value in this field, which should be a temperature in degrees Celsius, the function celciusToFahrenheit()
called.fahrenheit
, also created in this code, receives data from the user, which must be a temperature in degrees Fahrenheit, after which the function fahrenheitToCelcius()
called.celciusToFahrenheit()
function converts the temperature, expressed in degrees Celsius and entered in the celcius
field, into temperature in degrees Fahrenheit, and then outputs what it did, in the field fahrenheit
.fahrenheitToCelcius()
function performs the inverse transformation — it takes a temperature value, expressed in degrees Fahrenheit and entered in the fahrenheit
field, converts it to a value expressed in degrees Celsius, and then writes what it did, in the сelcius
field.renderer.js
file. This file must be created in the src
folder and put the following code into it: function celciusToFahrenheit(){ let celcius = document.getElementById('celcius').value; let fahrenheit = (celcius* 9/5) + 32; document.getElementById('fahrenheit').value = fahrenheit; } function fahrenheitToCelcius(){ let fahrenheit = document.getElementById('fahrenheit').value; let celcius = (fahrenheit - 32) * 5/9 document.getElementById('celcius').value = celcius; }
npm start
npm run package
out
folder, which will appear in the project folder.simple-desktop-app-electronjs-win32-x64
was created in the out
folder. In this folder, among other things, you can find the .exe
file of the application. In our case, it is called simple-desktop-app-electronjs.exe
. To launch an application, simply double-clicking on this file is enough.имя приложения - платформа - архитектура
. In our case, its structure is revealed as follows:simple-desktop-app-electronjs
.win32
.x64
.npm run package
command without parameters, by default, an executable file of the application is created for the platform used during development. npm run package -- --platform=<platform> arch=<architecture>
npm run package -- --platform=linux --arch=x64
simple-desktop-app-electronjs-linux-x64
directory with the corresponding contents will appear in the out
project folder. npm run make
out
folder you already know. Namely, the launch of this command in the above form on a Windows machine will lead to the creation of an installation file for a Windows application in the out\make\squirrel.windows\x64
folder. Like the package
command, the make
, invoked without parameters, creates an installer for the platform used during development.Source: https://habr.com/ru/post/436466/