我有一个在 Electron 中运行的 aurelia 应用程序。我的源文件是 typescript ,我有环境 typings对于 Electron 和节点。
因为我知道我正在编译用于 Electron ,所以我正在将我的 typescript 转换为 es6 并加载系统模块;这意味着我可以关闭 system.js 的转译器。我正在使用 system.js 和 jspm,因为这是 Aurelia 一直在插入的方法。
所以在我的 ts 文件中:我希望能够做到:
import {remote} from 'electron';
不幸的是,system.js 对模块 Electron 一无所知,并且在运行时失败。另一方面,TypeScript 非常高兴,因为我已经为 Electron 和节点设置了类型;我也在 VSCode 中获得了完整的智能感知。
注意:如果您尝试执行
var electron = require('electron');
在 header 中,system.js 对其进行了干扰并且无法加载。 You can place that 'require('electron')' within a class or function and it will work ,但我不觉得这个理想。问题:如何让 system.js 正确返回仅当您在 Electron 本身中运行应用程序时才可用的“Electron ”模块?
最佳答案
一个解决方案——希望有更好的方法——我想出的是填充 system.js 的 Electron 模块并将其直接链接到 require('electron')
的内容:
Electron .js
System.register([], function (exports_1, context_1) {
"use strict";
var __moduleName = context_1 && context_1.id;
var electron;
return {
setters: [],
execute: function () {
electron = require('electron');
exports_1("default", electron);
Object.keys(electron).forEach(function (key) {
exports_1(key, electron[key]);
});
}
}
});
这有效地 package 了内部 Electron 模块并允许 system.js 了解它。有用;但希望有其他人知道的更优雅/内置的方式。
您不需要对 typecypt 进行任何映射或更改,如
import {remote} from 'electron'
将尝试解决 electron.js
作为最后的手段。
关于electron - Aurelia:使用 es6 导入 Electron + typescript ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37346275/