8000 use a dynamic import for loading pyodide. This greatly simplifies the… · pyscript/pyscript@3ae4b3c · GitHub
[go: up one dir, main page]

Skip to content

Commit 3ae4b3c

Browse files
authored
use a dynamic import for loading pyodide. This greatly simplifies the logic around interpreter loading and handling of UserError (#1306)
1 parent c8f9f16 commit 3ae4b3c

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

pyscriptjs/src/main.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -207,20 +207,19 @@ export class PyScriptApp {
207207

208208
this.logStatus(`Downloading ${interpreter_cfg.name}...`);
209209

210-
// download pyodide by using a <script> tag. Once it's ready, the
211-
// "load" event will be fired and the execution logic will continue.
212-
// Note that the load event is fired asynchronously and thus any
213-
// exception which is throw inside the event handler is *NOT* caught
214-
// by the try/catch inside main(): that's why we need to .catch() it
215-
// explicitly and call _handleUserErrorMaybe also there.
216-
const script = document.createElement('script'); // create a script DOM node
217-
script.src = await this.interpreter._remote.src;
218-
script.addEventListener('load', () => {
219-
this.afterInterpreterLoad(this.interpreter).catch(async error => {
220-
await this._handleUserErrorMaybe(error);
221-
});
222-
});
223-
document.head.appendChild(script);
210+
/* Dynamically download and import pyodide: the import() puts a
211+
loadPyodide() function into globalThis, which is later called by
212+
RemoteInterpreter.
213+
214+
This is suboptimal: ideally, we would like to import() a module
215+
which exports loadPyodide(), but this plays badly with workers
216+
because at the moment of writing (2023-03-24) Firefox does not
217+
support ES modules in workers:
218+
https://caniuse.com/mdn-api_worker_worker_ecmascript_modules
219+
*/
220+
const interpreterURL = await this.interpreter._remote.src;
221+
await import(interpreterURL);
222+
await this.afterInterpreterLoad(this.interpreter);
224223
}
225224

226225
// lifecycle (5)

0 commit comments

Comments
 (0)
0