Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

e2: Output ESM instead of CJS for main and preload scripts #193

Merged
merged 15 commits into from
Sep 9, 2024

Conversation

jermy-c
Copy link
Collaborator

@jermy-c jermy-c commented Sep 5, 2024

  • update better-sqlite3 to 11.2.1 since older versions don't work with electron@32
  • update electron-vite to 2.3.0 to support ESM
  • update electron to 32.0.1 to support ESM
  • use custom ESM shim plugin since the one in electron-vite doesn't seem to work nor the @rollup/esm-shim plugin, therefore causing the __dirname syntax error
  • fix syntax errors to be compatible with ESM
  • set the config to output ESM for main and preload scripts
  • in the main script set the preload to mjs because it outputs it with themjs file extension for some reason

Where should we point the esm-shim-plugin package? It is a package based on rollup/plugins#1709 (comment). Will it be moved to Mustang?

@jermy-c jermy-c self-assigned this Sep 5, 2024
e2/electron.vite.config.ts Outdated Show resolved Hide resolved
e2/electron.vite.config.ts Outdated Show resolved Hide resolved
@benbucksch
Copy link
Collaborator

benbucksch commented Sep 5, 2024

Good changes. Thanks a lot for figuring that out!
I'm glad that it's not as difficult as I thought to change compliation to ESM.

If it works well (please test carefully!), and if you can avoid the shim, then it's good to go. The remaining changes look good to me, assuming that everything works.

@jermy-c
Copy link
Collaborator Author

jermy-c commented Sep 5, 2024

You're welcome. Thanks!

  • I replaced esm-shim with @rollup/plugin-replace, now it replaces all instances of __dirname with import.meta.dirname for the output. But it requires Node v20.11.0 or later. Would that be better or do we still do the shim()? NOTE: It is only a release candidate for now and not stable feature yet. https://nodejs.org/api/esm.html#importmetadirname
  • Removed unnecessary config for ESM electron-vite config
  • It is normal for the preload script file extension to change to .mjs or .cjs when outputing ESM. https://electron-vite.org/guide/dev#enabling-esm
  • I tested the build i.e.yarn build:mac and it works.

@benbucksch
Copy link
Collaborator

benbucksch commented Sep 5, 2024 via email

@jermy-c
Copy link
Collaborator Author

jermy-c commented Sep 5, 2024

Yes, but it'll replaced by the replace plugin also. So, we don't need to replace it manually.

@benbucksch
Copy link
Collaborator

benbucksch commented Sep 5, 2024 via email

@jermy-c
Copy link
Collaborator Author

jermy-c commented Sep 6, 2024

Could you please replace it?

Ok, done.

@benbucksch
Copy link
Collaborator

benbucksch commented Sep 6, 2024 via email

@benbucksch benbucksch merged commit c426b19 into master Sep 9, 2024
@benbucksch benbucksch deleted the jeremy/esm branch September 9, 2024 03:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants