简体   繁体   English

如何将Electron文件从“需要”转换为“导入”以用于TypeScript? Angular2 +

[英]How to convert Electron files from 'require' to 'import' for use with TypeScript? Angular2+

I have followed the official Electron tutorials along with a few others online and created the files associated within my directories. 我在网上跟随了其他一些其他的官方Electron教程,并创建了与目录相关的文件。 However, the files shown in the tutorials are JavaScript rather than TypeScript as I am using this within Angular. 但是,教程中显示的文件是JavaScript而不是TypeScript,因为我在Angular中正在使用它。

Is there any way in which the files I have created can be converted from using require to actually import ing them? 是否可以使用require将我创建的文件转换成实际import

Electron getting started guide 电子入门指南

I have seen in the past doing things like import * as component from './...' , but I'm unsure as to importing multiple components from the same source such as: 过去我曾看到过import * as component from './...' 。/ import * as component from './...' ,但是我不确定是否要从同一来源导入多个组件,例如:

const {app, BrowserWindow} = require('electron'); const {app,BrowserWindow} = require('electron');

electronStart.ts (this is called main.js in the tutorial, but there is already a file of this name generated by Angular-CLI) electronStart.ts(这被称为main.js在本教程中,但已经有一个由角CLI生成此名称的文件)

const {app, BrowserWindow} = require('electron');
const path = require('path');

setupEvents.ts setupEvents.ts

const electron = require('electron');
const ChildProcess = require('child_process');
 module.exports = {
    ...
 }

createInstaller.ts createInstaller.ts

const createWindowsInstaller = require('electron-winstaller').createWindowsInstaller;
const {app, BrowserWindow} = require('electron');

is equivalent to 相当于

import { app, BrowserWindow } from 'electron';

whereas

const electron = require('electron');

is equivalent to 相当于

import * as electron from 'electron';

and

const createWindowsInstaller = require('electron-winstaller').createWindowsInstaller;

should be possible to rewrite as 应该可以重写为

import { createWindowsInstaller } from 'electron-winstaller';

It's confirmed it behaves like this in Angular 6.1 at least, with TypeScript 2.9. 可以肯定的是,至少在Type 2.9中,它在Angular 6.1中的行为是这样的。 With previous TS version there can be some minor issues because they recently improved interop. 在以前的TS版本中,可能会出现一些小问题,因为它们最近改进了互操作性。

Anyway you'll need to setup another build pipeline to compile TS to JS before loading it with Electron. 无论如何,您需要先设置另一个构建管道,以将TS编译为JS,然后再将其加载到Electron中。

More of an IDE issue I think. 我认为更多的是IDE问题。

Make a global search and replace with regexes : 进行全局搜索并用正则表达式替换:

 const regex = /const ([a-zA-Z0-9]*) = require\\(('.*')\\)/ const str = `const lodash = require('lodash');` console.log(str.replace(regex, `import * as $1 from $2`)); 

(This one is just an example, you should make other ones, I'm just giving you the way to go) (这只是一个示例,您应该制作其他示例,我只是给您提供方法)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM