feat(plugin-vite): automatically switch to esm mode when enabled #4016
+10
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summarize your changes:
Electron now supports ESM by settings
"type": "module"inpackage.jsonbut@electron-forge/plugin-vitewill output CommonJS code regardless, which will then fail to load (typically by trying to referencerequire).This patch reads
typefrompackage.jsonand automatically changes the main vite config output format toesmif modules are requested. Note that we parsepackage.jsontwice now (the first time to look for a forge config) but this seemed cleaner than trying to pass the parsed JSON around.I am deliberately leaving preload scripts as-is since they ignore
"type": "module"and load fine with the current settings. That being said, Electron supports ESM preload scripts, so we might want to automatically change the output format there as well. This would also require renaming the output from.jsto.mjsThere is also the sandboxing limitation, which may mean that bundling to CommonJS is the best way for preload scripts regardless, but I do not understand well enough how people use preload scripts (and in particular how common sandboxing is) to make that decision.Please let me know if I need to add docs or automated tests here—the change is pretty trivial so I did not bother.
Related: #3684