electron / forge

:electron: A complete tool for building and publishing Electron applications
https://electronforge.io
MIT License
6.48k stars 514 forks source link

When is ESM support planned? #3684

Open AmbitionsXXXV opened 2 months ago

AmbitionsXXXV commented 2 months ago

Pre-flight checklist

Problem description

When I wanted to migrate to ESM according to the latest version of electron-store, I found that forge.config.ts did not support ESM. CleanShot 2024-09-04 at 10 38 42

Proposed solution

Support ESM in forge.config

Alternatives considered

  1. Wait for official support in future electron-forge versions.
  2. Use a custom build process outside of electron-forge to support ESM.
  3. Revert to CommonJS modules for the time being

Additional information

ESM support is crucial for our project as we aim to use modern JavaScript features and improve code maintainability. The ability to use 'import' statements directly would significantly simplify our development process and allow us to better align with current JavaScript best practices. We've already migrated most of our codebase to use ESM syntax, and electron-forge's lack of support is the main blocker for our complete transition

SpacingBat3 commented 1 month ago

There's already a support for ESM, although you might need to use forge.config.mts for now, since Forge relies on other software in order to load some config formats based on their extension. As of loading ESM directly in forge, which is mostly useful for extensions that Forge loads directly on its own, I've made a PR that is mostly finished and needs the upstream action in order to proceed with either merge of this or some opinions what I could still change: #3582.

SpacingBat3 commented 1 month ago

As a dev that actually made a maker for forge with the use of ESM syntax and a requirement of using ESM to load it, I've also made an example how to apply this to Forge at its current stage. Having better implementation on Forge side would still be better suited for more transparent loading of stuff, so users of Forge toolkit don't have to care about whenever Forge loads something as ESM or CJS, so supporting both feels like a way to go unless there will be some movement to decide on dropping CJS entirely at some point in JS…

augustsaintfreytag commented 1 month ago

According to the merge request, this has been incorporated into Electron main two weeks ago but I’m guessing it’s release pending?

This would be a really helpful addition as it would also solve/prevent https://github.com/deepkit/deepkit-framework/issues/615, an issue with the Deepkit type compiler getting understandably thrown off by using a different module setting in tsconfig than what is actually expected to be in the output code.

For Electron apps, this seems particularly strange because — assuming we’re all writing ESM syntax — the chain is ESM → CJS → ESM for everything running on an Electron renderer thread.

smallsung commented 4 weeks ago

As a dev that actually made a maker for forge with the use of ESM syntax and a requirement of using ESM to load it, I've also made an example how to apply this to Forge at its current stage. Having better implementation on Forge side would still be better suited for more transparent loading of stuff, so users of Forge toolkit don't have to care about whenever Forge loads something as ESM or CJS, so supporting both feels like a way to go unless there will be some movement to decide on dropping CJS entirely at some point in JS…

这个例子中,没在package.json中配置相对路径,最终使用的不是forgeConfig.mts,而是{}