Open cherewaty opened 4 months ago
I just went through this. Change forge.config.ts
to forge.config.cts
. In vite.main.config.ts
, change formats
from ['cjs']
to ['es']
. You will also need to polyfill or get rid of __dirname
and require()
in main.ts
. You. can use https://www.npmjs.com/package/vite-plugin-require to polyfill require. I just got rid of __dirname
. Here is my (super professional) sandbox. I was also testing support for TypeScript 5 decorators, which do work.
@jdms754, I followed your steps but got this error: Error: Unable to use specified module loaders for ".cts".
maybe I'm missing some config?
@jdms754 thanks a lot. That's worked!
@jdms754 I have started fresh and it worked now. thank you! ❤️
the difference I had was that in the tsconfig.json
file I had "module": "es2022"
instead of "module": "nodenext"
.
What about when one wants to use moduleResolution bundler? moduleResolution NodeNext isn't technically correct given that forge uses vite/webpack to build the project.
I just went through this. Change
forge.config.ts
toforge.config.cts
. Invite.main.config.ts
, changeformats
from['cjs']
to['es']
. You will also need to polyfill or get rid of__dirname
andrequire()
inmain.ts
. You. can use https://www.npmjs.com/package/vite-plugin-require to polyfill require. I just got rid of__dirname
. Here is my (super professional) sandbox. I was also testing support for TypeScript 5 decorators, which do work.
Thanks!
And for those who are looking for a quick replacement of __dirname
, this seems to be working for me
import * as url from 'node:url';
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
// use __dirname now
@joezappie any luck on figuring this out? I was also confused by why your gh issue xxx was closed when it does not appear ESM has been addressed with the vite-typescript template as in v7.4 I still see commonjs in tsconfig and the build > lib > format as 'cjs' in the vite config. Thanks in advance if there is any help you can provide me based on your own troubleshooting and research!
I have not had any luck in getting esm to work in main yet. I ended up just using electron-vite for my current project instead. Hopefully I can switch back to forge in the near future once this is supported.
What about when one wants to use moduleResolution bundler? moduleResolution NodeNext isn't technically correct given that forge uses vite/webpack to build the project.
Here is a commit showing changes to use bundler module resolution with Vite + TypeScript template. I had to make forge.config
a .js
file - that is the only way I have been able to get it to work so far.
https://github.com/jdms754/forge-vite-bundler/commit/00f631019a24388f2cb300c6e5d17fe9fdef14bf
Pre-flight checklist
Electron Forge version
7.3.0
Electron version
29.0.1
Operating system
macOS 14.3.1
Last known working Electron Forge version
No response
Expected behavior
I wanted to try out the newly stable
@remix-run/dev
Vite plugin in combination withelectron-forge
. I initialized a new project with thecreate-electron-app
vite-typescript
template, then started layering on pieces likedev
andbuild
scripts inpackage.json
.Actual behavior
I got
remix vite:dev
working with an isolatedvite.remix.config.ts
, but thenremix vite:build
ran intoSyntaxError: Cannot use import statement outside a module
. Adding"type": "module"
tomy-app
'spackage.json
seemed to fix the problem, but thenelectron-forge
scripts started failing.Steps to reproduce
For
vite-typescript
template:npm init electron-app@7.3.0 my-app -- --template=vite-typescript
"type": "module"
topackage.json
.npm start
For
vite
template:npm init electron-app@7.3.0 my-app -- --template=vite
"type": "module"
topackage.json
.npm start
Additional information
Seems related to https://github.com/electron/forge/issues/3439, and does not appear to be solved with https://github.com/electron/forge/pull/3468
This is likely causing friction still with SvelteKit as well.