electron "MAIN" : 要求你拥有 js 文件并从中调用函数
electron "MAIN" : requiring you own js file and call function from it
我无法理解这里与电子相关的一些事情。我一直在寻找神奇的答案,但找不到任何东西。
我的目标很简单。我不希望我的主 electron.js 文件长 5000 行而没有任何类型的组织,因此我试图将代码拆分为多个有意义的 js 文件。
我的想法是在我的 electron.js 中使用 import { someFunction1, someFunction2 } from './scripts/someScript'
,然后创建带有箭头函数的文件并导出它们。
然后我就可以在主文件中调用函数了。但是,这似乎是不可能的。我读过 electronjs 不支持 ES6 语法。我读过关于 Babel 的文章(但从我读到的内容来看,它意味着一堆额外的配置,我不想花几天时间尝试将它添加到一堆已经被 electron + React 搞得一团糟的配置中(没有样板在这里)。我没有找到这个组合的任何细节。
问题是。这在 2021 年可行吗?我错过了什么吗?你们会推荐什么?
文件看起来像这样:
import { someNodeModuleFunction } from 'someNodeModule';
const someFunction1 = () => {
return 1;
};
const someFunction2 = () => {
return 2;
};
export { someFunction1, someFunction2 }
编辑
这是我遇到问题的实际代码。我仍然得到
if the file is .js: "Must use import to load ES Module"
如果文件是 .mjs:“不能在模块外使用 import 语句”
这个脚本只是简单地使用 fs 创建一个目录:
DataManagement.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 }
像electron.js中那样调用它:
const { CreateDataDirectory } = require('./scripts/DataManagement.mjs');
[...]
CreateDataDirectory()
不确定代码的划分有多么困难。 :)
您可能需要使用 Node.js 模块语法(require
和 module.exports
)或 Babel(代码转换器)。
例如:
import { someNodeModuleFunction } from 'someNodeModule';
const someFunction1 = () => {
return 1;
};
const someFunction2 = () => {
return 2;
};
module.exports = { someFunction1, someFunction2 }
使用您的模块:
const { someFunction1, someFunction2 } = require ('./FILE.js');
// ...
您可以使用 module.exports
:
otherModule.js
const someFunction1 = () => {
return 1;
};
const someFunction2 = () => {
return 2;
};
module.exports = {
someFunction1,
someFunction2
};
main.js
const { someFunction1, someFunction2 } = require('otherModule.js');
我无法理解这里与电子相关的一些事情。我一直在寻找神奇的答案,但找不到任何东西。
我的目标很简单。我不希望我的主 electron.js 文件长 5000 行而没有任何类型的组织,因此我试图将代码拆分为多个有意义的 js 文件。
我的想法是在我的 electron.js 中使用 import { someFunction1, someFunction2 } from './scripts/someScript'
,然后创建带有箭头函数的文件并导出它们。
然后我就可以在主文件中调用函数了。但是,这似乎是不可能的。我读过 electronjs 不支持 ES6 语法。我读过关于 Babel 的文章(但从我读到的内容来看,它意味着一堆额外的配置,我不想花几天时间尝试将它添加到一堆已经被 electron + React 搞得一团糟的配置中(没有样板在这里)。我没有找到这个组合的任何细节。
问题是。这在 2021 年可行吗?我错过了什么吗?你们会推荐什么?
文件看起来像这样:
import { someNodeModuleFunction } from 'someNodeModule';
const someFunction1 = () => {
return 1;
};
const someFunction2 = () => {
return 2;
};
export { someFunction1, someFunction2 }
编辑
这是我遇到问题的实际代码。我仍然得到
if the file is .js: "Must use import to load ES Module" 如果文件是 .mjs:“不能在模块外使用 import 语句”
这个脚本只是简单地使用 fs 创建一个目录:
DataManagement.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 }
像electron.js中那样调用它:
const { CreateDataDirectory } = require('./scripts/DataManagement.mjs');
[...]
CreateDataDirectory()
不确定代码的划分有多么困难。 :)
您可能需要使用 Node.js 模块语法(require
和 module.exports
)或 Babel(代码转换器)。
例如:
import { someNodeModuleFunction } from 'someNodeModule';
const someFunction1 = () => {
return 1;
};
const someFunction2 = () => {
return 2;
};
module.exports = { someFunction1, someFunction2 }
使用您的模块:
const { someFunction1, someFunction2 } = require ('./FILE.js');
// ...
您可以使用 module.exports
:
otherModule.js
const someFunction1 = () => {
return 1;
};
const someFunction2 = () => {
return 2;
};
module.exports = {
someFunction1,
someFunction2
};
main.js
const { someFunction1, someFunction2 } = require('otherModule.js');