电子锻造 +ESM

问题描述 投票:0回答:1
当前状态应该是自V28

以来支持ESM。

forge

是从electron中获取包装的工具(repo位于电子/forge

),但该文件似乎不喜欢ESM Imports ...

forge.config.ts An unhandled rejection has occurred inside Forge: Error: Must use import to load ES Module: ~\forge.config.ts require() of ES modules is not supported. require() of ~\forge.config.ts from ~\node_modules\@electron-forge\core\dist\util\forge-config.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules. Instead change the requiring code to use import(), or remove "type": "module" from ~\package.json. 像这样配置(+tsconfig.json),

type: "module"

似乎使一切变得所有工作...但是无法启动Forge App。 另一方面,像
"module": "node16",
"moduleResolution": "node16",
"moduleDetection": "force",

IT启动了Forge应用程序,一切看起来都还不错,甚至能够导入某些ESM软件包,例如

nanoid
...但是与其他类型
electron-store
一样,这些类型无法正确识别(类似于此问题

我想问题是...
电子开发的当前状态是什么?我们在一个世界上没有什么可以完美地工作了吗?还是我在这里错过了一步?我觉得我尝试了最可能的配置...

我在github问题上做了

pure esmm。

我需要调整一些事情才能让纯ESM为我工作。

package.jsonconfiguration "module": "commonjs" "moduleResolution": "node", SET

{ "type": "module", "config": { "forge": "./forge.config.mjs" } }

使所有

"type": "module"
typescript electron es6-modules electron-forge
1个回答
0
投票
配置电子伪造用于使用

.jsconfig file

  1. forgeconfiguration

    为显式ESM支持
      .mjs
    • forge.config.js
      ESM进口和出口:
      
      
    • forge.config.mjs
    • wite配置
    auppated构建配置用于使用ESM格式:
  2. import { FusesPlugin } from '@electron-forge/plugin-fuses'; const config = { /* ... */ }; export default config;

    • main过程代码(main.js)
      
      
      用ESM等效化了Commonjs
    • // vite.main.config.mjs & vite.preload.config.mjs export default defineConfig({ build: { lib: { formats: ['es'], // ... } } });
    __dirname
  3. 而不是

    import { fileURLToPath } from 'node:url'; import { dirname, join } from 'node:path'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename);

    在需要的情况下,使用文件扩展名导入了适当的ESM
    Import语句
  4. comper ther:

    使用:

    join
      commonjs:
    • path.join
    最关键的部分是处理
    import { app } from 'electron'; import { dirname } from 'node:path';
      替换,因为这是node.js环境中commonj和eSM之间的主要区别之一。在ESM中,诸如
    • const { app } = require('electron'); const { dirname } = require('path');
      __dirname
      之类的全局变量不存在,因此您必须使用
      __dirname
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.