使用Angular 12将应用程序转化为Electron桌面应用程序

假设已经创建了一个Angular项目。 chù lǐ le Angular .)

参考网站

环境 –

Angular 版本

ng version

     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/


Angular CLI: 12.2.10
Node: 14.15.1
Package Manager: npm 8.1.0
OS: darwin x64

Angular: 12.2.10
... animations, cdk, cli, common, compiler, compiler-cli, core
... forms, localize, material, platform-browser
... platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1200.5
@angular-devkit/build-angular   12.2.10
@angular-devkit/core            12.0.5
@angular-devkit/schematics      12.2.10
@angular/fire                   6.1.5
@schematics/angular             12.2.10
rxjs                            6.6.7
typescript                      4.3.5

添加模块

将 @types/node 升级到新版本

    "@types/node": "^14.15.5",

在Angular12中实现electron所需的添加模块。

"dependencies" or "devDependencies": {
...
    "ngx-electron": "^2.2.0",
    "ts-loader": "^9.2.5",
    "concurrently": "^6.2.1",
    "electron": "^13.2.0",
    "electron-log": "4.4.1",
    "electron-packager": "^15.3.0",
    "webpack-cli": "^4.8.0",
    "webpack-node-externals": "^3.0.0"
}

加入 webpack.**.config.js

image.png
const path = require('path');
const { ProgressPlugin } = require('webpack');

const src = path.join(process.cwd(), 'src', 'electron');

module.exports = {
  resolve: {
    extensions: [
      '.ts',
      '.js'
    ]
  },
  entry: {
    main: path.join(src, 'main.ts')
  },
  module: {
    rules: [
      {
        test: /\.ts$/,
        loader: 'ts-loader',
        options: {
          configFile: path.join(src, 'tsconfig.json')
        }
      }
    ]
  },
  plugins: [
    new ProgressPlugin()
  ],
  // indicates that transpilation code is to be run in the main process
  target: 'electron-main'
};
const path = require('path');
const baseConfig = require('./webpack.electron.config');

module.exports = {
  ...baseConfig,
  mode: 'development',
  devtool: 'source-map',
  output: {
    path: path.join(process.cwd(), 'dist'),
    // avoid conflicts with the `main.js` file generated from the Angular CLI
    filename: 'shell.js'
  }
};
const path = require('path');
const nodeExternals = require('webpack-node-externals');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const baseConfig = require('./webpack.electron.config');

module.exports = {
  ...baseConfig,
  mode: 'production',
  output: {
    path: path.join(process.cwd(), 'dist'),
    filename: 'main.js'
  },
  plugins: [
    // the electron-packager requires a valid `package.json` file along with the Electron code
    new CopyWebpackPlugin({
      patterns: [
        {
          context: path.join(process.cwd(), 'src', 'electron'),
          from: 'package.json'
        }
      ]
    })
  ],
  // Indicate that we do not want to bundle modules from 3rd party libraries automatically, as part of ES6 imports.
  // Instead we must define them in the `dependencies` property of the `package.json` file and will be installed
  // using the `package` npm script
  externals: [nodeExternals()]
};

新增一个 npm run 脚本

  "scripts": {
...
    "build:dev": "concurrently \"ng build --delete-output-path=false --watch\" \"webpack --config webpack.electron.dev.config.js --watch\"",
    "start:dev": "electron dist/shell.js",
    "build:prod": "ng build --optimization --outputHashing=all --sourceMap=false --namedChunks=false --extract-licenses --vendorChunk=false --build-optimizer && webpack --config webpack.electron.prod.config.js",
    "start:prod": "electron dist/main.js"
  }

在测试时,请运行桌面版。

npm run build:dev
npm run start:dev

为电子启动准备 main.ts。

image.png
import { app, BrowserWindow, ipcMain, dialog } from 'electron';
import * as fs from 'fs';
import log from 'electron-log';

log.info(`${app.name} ${app.getVersion()}`);

function createWindow () {
  const mainWindow = new BrowserWindow({
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false
    }
  });
  mainWindow.maximize();
  mainWindow.webContents.openDevTools();
  mainWindow.loadFile('index.html');
}

app.whenReady().then(() => {
  createWindow();
});

// Angular -> Electron
ipcMain.on('selectPirate', async (event: Electron.IpcMainEvent, name: string) => {
    await dialog.showMessageBox({ message: 'You selected: ' + name });
    event.returnValue = true;
});
{
    "name": "angular-electron-sample",
    "description": "Angular Electron Sample",
    "version": "0.0.0",
    "author": {
      "name": "Aristeidis Bampakos"
    },
    "main": "main.js",
    "dependencies": {
      "electron-log": "4.3.1"
    }
}
{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "importHelpers": false
  },
  "include": [
    "**/*.ts"
  ]
}

考试

我试着构建一下

build:dev
Jan-30-2022 09-45-18.gif

最后会进入冻结状态,不用在意,使用 ctrl+c 进行强制结束。

请尝试启动

start:dev

在Angular应用程序中添加针对Electron的功能。

将app.module添加到一个模块中。

import { ElectronService, NgxElectronModule } from "ngx-electron";

...

@NgModule({
  imports: [

    NgxElectronModule
  ],
  providers: [

    ElectronService
  ]

在使用电子组件时添加。

import { ElectronService } from 'ngx-electron';

  constructor(
    public electronService: ElectronService
   ) {}

  // electron を使う関数
  public use_electron(): void{
    if(this.electronService.isElectronApp) {
      this.electronService.ipcRenderer.sendSync('selectPirate', 'hogehoge');
    }
  }

最后

由于在Angular 12的文档中很难找到相关信息,所以我写下了这篇内容。

广告
将在 10 秒后关闭
bannerAds