准备工作

  1. // 设置electron源 不然安装时可能会网络错误
  2. npm config set ELECTRON_MIRROR http://npm.taobao.org/mirrors/electron/
  3. npm install --save-dev electron
  4. // 用来打包
  5. npm i -D @electron-forge/cli
  6. // 自动修改package.json
  7. npx electron-forge import
  8. // 重启设置
  9. npm i electron-reload -D
  1. "name": "ng-dsp",
  2. "version": "1.0.0",
  3. // 需要增加main.js
  4. "main": "main.js",
  5. "scripts": {
  6. "start:electron": "npx electron . --serve",
  7. "package": "electron-forge package",
  8. "make": "electron-forge make",
  9. "start:ng": "ng s --host 0.0.0.0",
  10. "build": "ng build",
  11. "build:prod": "ng build --prod",
  12. "watch": "ng build --watch --configuration development",
  13. "test": "jest",
  14. "test:watch": "jest --watch",
  15. "test:cov": "jest --coverage"
  16. },
  17. "config": {
  18. "forge": {
  19. "packagerConfig": {
  20. "appVersion": "1.0.0",
  21. "dir": "dist/",
  22. "name": "ng-dsp",
  23. "overwrite": true,
  24. "ignore": [
  25. "node_modules"
  26. ],
  27. "win32metadata": {
  28. "ProductName": "dsp转换器",
  29. "CompanyName": "长城",
  30. "FileDescription": ""
  31. }
  32. },
  33. "makers": [
  34. {
  35. "name": "@electron-forge/maker-squirrel",
  36. "config": {
  37. "name": "ng_dsp"
  38. }
  39. },
  40. {
  41. "name": "@electron-forge/maker-zip",
  42. "platforms": [
  43. "darwin"
  44. ]
  45. },
  46. {
  47. "name": "@electron-forge/maker-deb",
  48. "config": {}
  49. },
  50. {
  51. "name": "@electron-forge/maker-rpm",
  52. "config": {}
  53. }
  54. ]
  55. }
  56. }

根路径下创建main.js

  1. // electron的启动文件
  2. let path = require("path");
  3. const { app, BrowserWindow, ipcMain, screen } = require("electron");
  4. let win = null;
  5. let url = require("url");
  6. let args = process.argv.slice(1);
  7. let size = { width: 0, height: 0 };
  8. // 用于控制是否显示devTool的环境工具
  9. let serve = args.some(function (val) {
  10. return val === "--serve";
  11. });
  12. function createWindow() {
  13. let electronScreen = screen;
  14. size = electronScreen.getPrimaryDisplay().workAreaSize;
  15. // Create the browser window.
  16. win = new BrowserWindow({
  17. width: Math.floor(size.width * 0.8),
  18. height: Math.floor(size.height * 0.8),
  19. center: true,
  20. resizable: true,
  21. webPreferences: {
  22. devTools: true,
  23. nodeIntegration: true,
  24. allowRunningInsecureContent: serve ? true : false,
  25. contextIsolation: false,
  26. enableRemoteModule: true, // true if you want to run 2e2 test with Spectron or use remote module in renderer context (ie. Angular)
  27. },
  28. // fullscreen: false,
  29. frame: false,
  30. });
  31. // 如果是测试 此时映射load localhost 否则load对应的打包文件
  32. if (serve) {
  33. win.webContents.openDevTools();
  34. require("electron-reload")(__dirname, {
  35. electron: require(__dirname + "/node_modules/electron"),
  36. });
  37. win.loadURL("http://localhost:4200");
  38. } else {
  39. win.loadURL(
  40. url.format({
  41. pathname: path.join(__dirname, "dist/index.html"),
  42. protocol: "file:",
  43. slashes: true,
  44. })
  45. );
  46. }
  47. // Emitted when the window is closed.
  48. win.on("closed", function () {
  49. // Dereference the window object, usually you would store window
  50. // in an array if your app supports multi windows, this is the time
  51. // when you should delete the corresponding element.
  52. win = null;
  53. });
  54. return win;
  55. }
  56. function closeWindow() {
  57. if (win) {
  58. // win.close();
  59. win = null;
  60. // 弹窗
  61. app.exit();
  62. }
  63. }
  64. // 窗口展开
  65. function expand() {
  66. win.setSize(size.width, size.height);
  67. win.center();
  68. }
  69. function fold() {
  70. win.setSize(Math.floor(size.width * 0.8), Math.floor(size.height * 0.8));
  71. win.center();
  72. }
  73. // 最小化
  74. function minimize() {
  75. win.minimize();
  76. }
  77. try {
  78. // This method will be called when Electron has finished
  79. // initialization and is ready to create browser windows.
  80. // Some APIs can only be used after this event occurs.
  81. // Added 400 ms to fix the black background issue while using transparent window. More detais at https://github.com/electron/electron/issues/15947
  82. app.on("ready", function () {
  83. return setTimeout(createWindow, 400);
  84. });
  85. // Quit when all windows are closed.
  86. app.on("window-all-closed", function () {
  87. // On OS X it is common for applications and their menu bar
  88. // to stay active until the user quits explicitly with Cmd + Q
  89. if (process.platform !== "darwin") {
  90. app.quit();
  91. }
  92. });
  93. app.on("activate", function () {
  94. // On OS X it's common to re-create a window in the app when the
  95. // dock icon is clicked and there are no other windows open.
  96. if (win === null) {
  97. createWindow();
  98. }
  99. });
  100. ipcMain.on("closeWindow", (event, arg) => {
  101. closeWindow();
  102. });
  103. ipcMain.on("expand", (event, arg) => {
  104. if (arg) {
  105. fold();
  106. } else {
  107. expand();
  108. }
  109. });
  110. ipcMain.on("minimize", (event, arg) => {
  111. minimize();
  112. });
  113. } catch (e) {
  114. // Catch Error
  115. // throw e;
  116. }
  117. //# sourceMappingURL=main.js.map
  1. # Electron build
  2. out/*

开发

electron和程序通信

main.js中的console会打印在控制台,代码中的console会打印在DevTool

  1. import { Component } from '@angular/core';
  2. import { IpcRenderer } from 'electron';
  3. @Component({
  4. selector: 'app-header',
  5. templateUrl: './header.component.html',
  6. styleUrls: ['./header.component.less'],
  7. })
  8. export class AppHeaderComponent {
  9. private ipc!: IpcRenderer;
  10. // 是否最大化
  11. isExpand = false;
  12. constructor() {
  13. if ((window as any).require) {
  14. try {
  15. this.ipc = (window as any).require('electron').ipcRenderer;
  16. } catch (e) {
  17. throw e;
  18. }
  19. } else {
  20. console.warn('App not running inside Electron!');
  21. }
  22. }
  23. expand() {
  24. this.ipc.send('expand', this.isExpand);
  25. this.isExpand = !this.isExpand;
  26. }
  27. minimize() {
  28. this.ipc.send('minimize');
  29. }
  30. close() {
  31. this.ipc.send('closeWindow');
  32. }
  33. }

打包

  1. 修改angular打包输出位置

image.png

  1. 修改index.html

image.png

  1. 打包
    1. npm run build
    2. npm run package
    替换electron打包后的out/xxx/resources/app
    image.png
    之后再打包时,只用替换dist即可。