-
-
Notifications
You must be signed in to change notification settings - Fork 795
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
Support importing pyodide as ESM module #2217
Comments
Oh it seems we already built out esm format here: https://github.com/pyodide/pyodide/blob/main/src/js/rollup.config.js#L40. We just need to fix the issue importing it with ESM module in a web worker here: Line 71 in 2633091
|
I tried a simple fix in the loadScript = async (url) => {
try {
globalThis.importScripts(url);
} catch (error) {
return await import(url)
}
} It seems to work. Not sure if there's a better way to detect whether a worker is a classic type or module type though. |
Thanks for investigating @leopsidom. I would be happy to accept a PR that adjusts |
👍 @hangtwenty also ran into this when trying to migrate futurecoder to Vite. I see #2220 is merged, does that mean this issue is solved? |
I am not sure. I ran into trouble importing Pyodide from a module webworker the other day, so I am not convinced that it is working. We could leave this open until we get more feedback, or I suppose we could close it and let someone open a new issue if they run into more problems. |
Currently the ESM target |
Well the |
I just tried this url. I think the change has not been released yet. I'm getting the same old error:
while the error points to the original version of |
Yeah, the change is available in the development version: |
Cool that URL works. I tried to bundle it with Natively import with |
The changelog for 0.20.0 mentions this issue, is it fully solved now? |
@leopsidom I'm trying your worker approach in Vite but the I'm very interested in the permutations of Pyodide, Vite, Vitest, ESM, module workers. It's a challenge: top-level await, Firefox not doing module workers, etc. I have a 19-part tutorial series I'm wrapping up. Can I pick your brain on |
For development this is fine in Chrome, but Worker |
🚀 Feature
This is to propose supporting loading pyodide.js as ESM module. Majority of modern browsers support ESM modules via script: can i use module in script. Both chrome and safari support ESM modules in worker script: can
i use module in worker. Firefox is also actively working on supporting ESM modules in web workers: Implement worker modules. They resolved a blocker issue recently: Factor out common ScriptLoadRequest for use in Worker and DOM.
With ESM type module supported, we should be able to import pyodide as follows:
And similar syntax in web worker so we can create a module worker as
new Worker('webworker.js', { type: 'module' })
Motivation
The main motivation of this feature is so that it can integrate more easily with modern bundling tool like vite.js, which only supports bundling module type web worker. The lack of support on ESM module import means that pyodide.js needs to be manually handled -- manually exposed (if building an npm package from it) and copied when installing the npm package.
Pitch
Basically we would like the following syntax to work, instead of throwing an error:
And similar syntax in web worker so we can create a module worker as
new Worker('webworker.js', { type: 'module' })
One option I think is to expose current pyodide.js as
pyodide.umd.js
and creates another version aspyodide.es.js
that supports ESM module import. And replaceimportScripts
:pyodide/src/pyodide.js
Line 71 in 2633091
importScripts
throws an error when the script is imported as a module type worker.Alternatives
Use current as it is, and the downside is that we need to manually manage the bundling.
Similar issue: #684
Hope this makes sense :)
The text was updated successfully, but these errors were encountered: