首页 > 解决方案 > 如何将 ES 用户模块(用于渲染器进程)导入 Electron 主进程

问题描述

https://github.com/Jazzepi/bunny-tracker

我正在为非营利性的 Ohio House Rabbit Rescue(他们很棒)开发一个小型本地化数据库,并且我正在努力部署 Electron 应用程序的版本。

当我将消息从渲染器传递到主进程时,我经常会传递整个 Typescript 对象。我想在主流程中使用这些 Typescript 定义,而不是手动复制它们。您可以在此处查看这些导入。从导入开始的任何内容都是./src/从 ES“用户”模块中提取的。

https://github.com/Jazzepi/bunny-tracker/blob/master/main.ts

import { app, BrowserWindow, ipcMain, Menu, MenuItem, MenuItemConstructorOptions, screen, shell } from 'electron';
import * as path from 'path';
import * as url from 'url';
import * as moment from 'moment';
import Bunny from './src/app/entities/Bunny';
import * as sqlite from 'sqlite';
import { Database } from 'sqlite';
import * as log from 'electron-log';
import SQL from 'sql-template-strings';
import IPC_EVENT from './src/app/ipcEvents';
import GENDER from './src/app/entities/Gender';
import RESCUE_TYPE from './src/app/entities/RescueType';

运行生产就绪应用程序映像时遇到的错误是这样的。我在 Linux 中使用 app-image 和在 Mac 中使用 dmg 获取此图像。

ohrr@ohrr:~/repos/bunny-tracker$ release/bunny-tracker-2.0.0-x86_64.AppImage 
A JavaScript error occurred in the main process
Uncaught Exception:
Error: Cannot find module './src/app/ipcEvents'
Require stack:
- /tmp/.mount_bunny-45gwlx/resources/app.asar/main.js
- 
    at Module._resolveFilename (internal/modules/cjs/loader.js:659:15)
    at Function.Module._resolveFilename (/tmp/.mount_bunny-45gwlx/resources/electron.asar/common/reset-search-paths.js:43:12)
    at Function.Module._load (internal/modules/cjs/loader.js:577:27)
    at Module.require (internal/modules/cjs/loader.js:715:19)
    at require (internal/modules/cjs/helpers.js:14:16)
    at Object.<anonymous> (/tmp/.mount_bunny-45gwlx/resources/app.asar/main.js:50:19)
    at Module._compile (internal/modules/cjs/loader.js:808:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:820:10)
    at Module.load (internal/modules/cjs/loader.js:677:32)

在开发模式下一切正常,但在生产部署中找不到模块。

我在下面的链接中阅读了自定义协议,但它们似乎更多是关于让您从自定义 URL 模式中获取值,并且我希望import不要弄乱 HTML。

Electron ES6 模块导入 https://github.com/electron/electron/issues/12011 https://gist.github.com/smotaal/f1e6dbb5c0420bfd585874bd29f11c43

任何有关答案的帮助,甚至只是将我引导到正确的方向都会很棒!如果我无法弄清楚这一点,我的解决方案是将用户导入的模块符号链接到主处理器实际可以从中导入的空间。

标签: typescriptecmascript-6electronecma

解决方案


electron-builder.json有一个“!src /”条目告诉它忽略该文件夹。我确信这使我的可执行文件更大,但我不在乎打包一些额外的文本文件。

删除已"!src/",修复的生产运行时问题!呜呜。

    "files": [
        "**/*",
        "!**/*.ts",
        "!*.code-workspace",
        "!LICENSE.md",
        "!package.json",
        "!package-lock.json",
        "!src/",
        "!e2e/",
        "!hooks/",
        "!angular.json",
        "!_config.yml",
        "!karma.conf.js",
        "!tsconfig.json",
        "!tslint.json"
    ],

推荐阅读