electron / forge

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

"type": "module" in package.json for Vite templates #3502

Open cherewaty opened 4 months ago

cherewaty commented 4 months ago

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 with electron-forge. I initialized a new project with the create-electron-app vite-typescript template, then started layering on pieces like dev and build scripts in package.json.

Actual behavior

I got remix vite:dev working with an isolated vite.remix.config.ts, but then remix vite:build ran into SyntaxError: Cannot use import statement outside a module. Adding "type": "module" to my-app's package.json seemed to fix the problem, but then electron-forge scripts started failing.

Steps to reproduce

For vite-typescript template:

For vite template:

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.

joezappie commented 4 months ago

3468 did not fix my issue #3439 unfortunately.

jdms754 commented 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.

junagao commented 4 months ago

@jdms754, I followed your steps but got this error: Error: Unable to use specified module loaders for ".cts". maybe I'm missing some config?

bqhuyy commented 4 months ago

@jdms754 thanks a lot. That's worked!

junagao commented 4 months ago

@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".

RobertWHurst commented 3 months ago

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.

jgresham commented 2 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.

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
GitMurf commented 2 months ago

@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!

joezappie commented 2 months ago

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.

jdms754 commented 6 days ago

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