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
fix: expose the built-in electron module via the ESM loader #35930
Conversation
c1921ff
to
6490a8e
Compare
Wow, that's great! I assume this will only work using the Node.js ESM module loader and not in a Renderer with Node Integration? |
Haven't tested, but I believe that to be correct. V8 allows a single ESM loader per isolate, not per context. Which means in the renderer we have to choose between nodes ESM loader and blinks. Obviously we're going to choose blinks to ensure the web, well, works 😅 |
Release Notes Persisted
|
I have automatically backported this PR to "20-x-y", please check out #35956 |
I have automatically backported this PR to "22-x-y", please check out #35957 |
I have automatically backported this PR to "21-x-y", please check out #35958 |
What is this?
This specifically enables the following two patterns:
What isn't this?
This is not "ESM support for Electron". I classify that as supporting an ESM entrypoint for an Electron app. Currently for reasons well outlined in the requisite issue #21457 an Electron app must have a CJS entrypoint. It can then immediately farm out to an ESModule but it's important apps understand the limitations / issues with that approach, hence we do not allow an
mjs
file to be directly used as an entrypoint.How does it work?
Weirdly, this is like the 4th time I've tried this. And this time I found a relatively lightweight patch to make it work, basically we trick the ESM module resolver in node.js to consider "electron" the url
electron:electron
which then routes to the commonjs esm loader and loads theelectron
module straight out of ourModule._cache
entry we inject it into ✨As a bonus this PR comes with tests proving it works 😄
I can claim this as
semver/patch
given it was always kinda supposed to work (it's a bug that you can load a module viarequire
and not viaimport
) so I think this is a fine backport candidate.Notes: You can now import the built-in electron module via ESModule loaders, i.e.
import('electron')
andimport 'electron'
now work natively