typescript - 如何将 ES 用户模块(用于渲染器进程)导入 Electron 主进程

标签 typescript ecmascript-6 electron ecma

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)

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

我在下面的链接中阅读了自定义协议(protocol),但它们似乎更多的是让您从自定义 URL 模式中获取值,我想 import不乱用 HTML 的东西。

Electron ES6 module import
https://github.com/electron/electron/issues/12011
https://gist.github.com/smotaal/f1e6dbb5c0420bfd585874bd29f11c43

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

最佳答案

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"
    ],

关于typescript - 如何将 ES 用户模块(用于渲染器进程)导入 Electron 主进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58527067/

相关文章:

javascript - 在探索 Angular2 之前是否有必要先学习 TypeScript?

typescript - Angular 2 : Many errors compiling to ES5

reactjs - 编写带有 Electron react 样板的 Electron 应用构建的自动化测试用例

javascript - 如何在 Vue 应用程序中实现 Angular 风格的配置和运行阶段?

javascript - 如果用户导航到其他路线, Electron reactjs 应用程序在重新加载后显示空页面

javascript - Electron :渲染器访问主进程?

javascript - 使用 Angular 从弹出模式按钮删除时删除表中特定选定的行

Angular - 如何在服务中模拟 HttpError 响应

javascript - 如何使用类型不同于 T 的 Proxy<T> 作为参数?

javascript - 组件Vuejs2声明$data obj以在组件之间共享数据