Home > Web Front-end > JS Tutorial > How Webpack builds Electron apps

How Webpack builds Electron apps

php中世界最好的语言
Release: 2018-04-13 15:07:56
Original
2374 people have browsed it

This time I will show you how Webpack builds Electron applications. What are the precautions for Webpack to build Electron applications. The following is a practical case, let's take a look.

Electron allows you to use Web development technology to develop cross-platform desktop applications, led by Github and open source. The familiar Atom and VSCode Editor are developed using Electron.

Electron is a combination of Node.js and the Chromium browser. It uses the web page displayed by the Chromium browser as the application GUI, through Node.js to interact with the operating system. When you are in Electron When you operate a window in an application, you are actually operating a web page. When your operation needs to be completed through the operating system, the web page will interact with the operating system through Node.js.

The advantages of developing desktop applications in this way are:

  1. To lower the development threshold, you only need to master web development technology and Node.js. A large number of web development technologies and ready-made libraries can be reused in Electron;

  2. Since both Chromium browser and Node.js are Cross-platform, Electron can write a code to run on different operating systems.

When running an Electron application, it starts by launching a main process. The main process is started by executing an entry JavaScript file through Node.js. The content of this entry file main.js is as follows:

const { app, BrowserWindow } = require('electron')
// 保持一个对于 window 对象的全局引用,如果你不这样做,
// 当 JavaScript 对象被垃圾回收, window 会被自动地关闭
let win
// 打开主窗口
function createWindow() {
 // 创建浏览器窗口
 win = new BrowserWindow({ width: 800, height: 600 })
 // 加载应用的 index.html
 const indexPageURL = `file://${dirname}/dist/index.html`;
 win.loadURL(indexPageURL);
 // 当 window 被关闭,这个事件会被触发
 win.on('closed', () => {
  // 取消引用 window 对象
  win = null
 })
}
// Electron 会在创建浏览器窗口时调用这个函数。
app.on('ready', createWindow)
// 当全部窗口关闭时退出
app.on('window-all-closed', () => {
 // 在 macOS 上,除非用户用 Cmd + Q 确定地退出
 // 否则绝大部分应用会保持激活
 if (process.platform !== 'darwin') {
  app.quit()
 }
})
Copy after login

After the main process is started, it will always run in the background. The window you see and operate is not the main process, but the window sub-process newly started by the main process.

The application has a series of life cycle events from startup to exit. Use the electron.app.on() function to monitor life cycle events and react at specific moments. For example, use BrowserWindow to display the main window of the application in the app.on('ready') event.

The window that is started is actually a web page, and it will load the web page address passed in loadURL when it is started. Each window is a separate web page process, and communication between windows requires the use of the main process to pass messages.

Generally speaking, developing Electron applications is very similar to developing Web applications. The difference is that Electron's running environment has built-in browser and Node.js APIs. When developing web pages, in addition to using the APIs provided by the browser, you can also use Node. API provided by js.

Connect to Webpack

Next, make a simple Electron application, which requires a main window to be displayed after the application is started. There is a button in the main window. After clicking this button, a new window will be displayed, and React will be used to develop the web page.

Since each window in the Electron application corresponds to a web page, two web pages need to be developed, namely the index.html of the main window and the newly opened window. login.html. In other words, the project consists of 2 single-page applications. This is very similar to the project in 3-10 Managing Multiple Single-page Applications. Let us transform it into a Electron applications.

The places that need to be changed are as follows:

Create a new entry file main.js for the main process in the project root directory. The content is consistent with the above mentioned;

The code of the main window web page is as follows:

import React, { Component } from 'react';
import { render } from 'react-dom';
import { remote } from 'electron';
import path from 'path';
import './index.css';
class App extends Component {
 // 在按钮被点击时
 handleBtnClick() {
  // 新窗口对应的页面的 URI 地址
  const modalPath = path.join('file://', remote.app.getAppPath(), 'dist/login.html');
  // 新窗口的大小
  let win = new remote.BrowserWindow({ width: 400, height: 320 })
  win.on('close', function () {
   // 窗口被关闭时清空资源
   win = null
  })
  // 加载网页
  win.loadURL(modalPath)
  // 显示窗口
  win.show()
 }
 render() {
  return (
   <p>
    <h1>Page Index</h1>
    <button onClick={this.handleBtnClick}>Open Page Login</button>
   </p>
  )
 }
}
render(<App/>, window.document.getElementById('app'));
Copy after login

The most critical part is to use the API provided by the electron library to open a new window in the button click event and load the address of the web page file.

The code in the page part has been modified. Next, the construction code will be modified. Here you need to do the following to build:

Construct two web pages that can be run in the browser, corresponding to the interfaces of the two windows;

  1. Because the JavaScript code of the web page may call the Node.js native module or electron module, that is, the output code depends on these modules. However, since these modules have built-in support, the built code cannot package these modules.

  2. It is very simple to complete the above requirements because Webpack has built-in support for Electron. Just add a line of code to the Webpack configuration file, as follows:

target: 'electron-renderer',
Copy after login

这句配置曾在2-7其它配置项-Target中提到,意思是指让 Webpack 构建出用于 Electron 渲染进程用的 JavaScript 代码,也就是这2个窗口需要的网页代码。

以上修改都完成后重新执行 Webpack 构建,对应的网页需要的代码都输出到了项目根目录下的 dist 目录里。

为了以 Electron 应用的形式运行,还需要安装新依赖:

# 安装 Electron 执行环境到项目中
npm i -D electron
Copy after login

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

Angular实现可添加删除与计算总金额效果插件

Bootstrap模态框多次弹出提交BUG

The above is the detailed content of How Webpack builds Electron apps. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template