简体   繁体   English

electron “MAIN”:要求您拥有 js 文件并从中调用 function

[英]electron “MAIN” : requiring you own js file and call function from it

I'm can't understand some things here related to electron.我无法理解这里与 electron 相关的一些事情。 I've been searching for hours for the magic answer but couldn't find anything.我一直在寻找神奇的答案几个小时,但找不到任何东西。

My goal is simple.我的目标很简单。 I don't want my main electron.js file to be 5000 lines long without any kind of organization so I'm trying to split the code into multiple js file that make sense.我不希望我的主要 electron.js 文件长 5000 行而没有任何组织,所以我试图将代码拆分为多个有意义的 js 文件。

My idea was to use import { someFunction1, someFunction2 } from './scripts/someScript' in my electron.js and then just creating that file with the arrow function in it and export them.我的想法是在我的 electron.js 中使用 import { someFunction1, someFunction2 } from './scripts/someScript' ,然后在其中创建带有箭头 function 的文件并导出它们。

Then I could call the function as I want in the main file.然后我可以在主文件中调用 function。 However, it doesn't seem to be possible.然而,这似乎是不可能的。 I've read electronjs doesn't support ES6 syntax.我读过 electronjs 不支持 ES6 语法。 I've read about Babel (But from what I read, it implies a bunch of additional configuration and I don't want to spend days trying to add this to the bunch of configuration that are already messy with electron + React (No boiler plate here). And I didn't find anything specifics for this combo.我读过关于 Babel 的文章(但从我读到的内容来看,它意味着一堆额外的配置,我不想花几天时间尝试将它添加到已经与 electron + React 混淆的一堆配置中(没有样板这里)。我没有找到这个组合的任何细节。

Question is.问题是。 Is this doable in 2021?这在 2021 年可行吗? Am I missing anything?我错过了什么吗? What would you guys recommend?你们会推荐什么?

File would look something like this:文件看起来像这样:

import { someNodeModuleFunction } from 'someNodeModule';

const someFunction1 = () => {
    return 1;
};

const someFunction2 = () => {
    return 2;
};

export { someFunction1, someFunction2 }

EDIT编辑

Here's the actual code I have a problem with.这是我遇到问题的实际代码。 I still get我仍然得到

if the file is.js: "Must use import to load ES Module" If the file is.mjs: "Cannot use import statement outside a module" if file is.js: "必须使用 import 加载 ES 模块" If file is.mjs: "Cannot use import statement outside a module"

This script is simply using fs to create a directory:这个脚本只是使用 fs 创建一个目录:

DataManagement.mjs:数据管理.mjs:

import { existsSync, mkdir } from 'fs';

const electron = require('electron');
const app = electron.app;

const documentFolder = app.getPath('documents');

const CreateDataDirectory = () => {
   const mainPath = documentFolder + 'AppName'
   if (!existsSync(mainPath)) {
      mkdir(mainPath);
   }
};

module.exports = { CreateDataDirectory } 

Calling it like that in electron.js:在 electron.js 中这样称呼它:

const { CreateDataDirectory } = require('./scripts/DataManagement.mjs');

[...]

CreateDataDirectory()

Not sure how dividing the code can be that hard.不知道划分代码有多难。 :) :)

You may need to use Node.js module syntax ( require and module.exports ) or Babel (code transpiler).您可能需要使用 Node.js 模块语法( requiremodule.exports )或 Babel(代码转译器)。

For example:例如:

import { someNodeModuleFunction } from 'someNodeModule';

const someFunction1 = () => {
    return 1;
};

const someFunction2 = () => {
    return 2;
};

module.exports = { someFunction1, someFunction2 }

Using your module:使用您的模块:

const { someFunction1, someFunction2 } = require ('./FILE.js');

// ...

You could use module.exports :您可以使用module.exports

otherModule.js其他模块.js

const someFunction1 = () => {
    return 1;
};

const someFunction2 = () => {
    return 2;
};

module.exports = {
    someFunction1,
    someFunction2
};

main.js main.js

const { someFunction1, someFunction2 } = require('otherModule.js');

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

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