mirror of
https://github.com/pyscript/pyscript.git
synced 2022-05-01 19:47:48 +03:00
Very rudimentary support for ESM and import maps
This commit is contained in:
@@ -45,6 +45,11 @@ function htmlDecode(input) {
|
||||
return doc.documentElement.textContent;
|
||||
}
|
||||
|
||||
// TODO: use type declaractions
|
||||
type PyodideInterface = {
|
||||
registerJsModule(name: string, module: object): void
|
||||
}
|
||||
|
||||
class Script {
|
||||
source: string;
|
||||
state: string;
|
||||
@@ -241,17 +246,50 @@ export class PyScript extends HTMLElement {
|
||||
// pkg.do_something();
|
||||
}
|
||||
|
||||
async evaluate() {
|
||||
protected async _register_esm(pyodide: PyodideInterface): Promise<void> {
|
||||
const imports: {[key: string]: unknown} = {}
|
||||
|
||||
for (const node of document.querySelectorAll("script[type='importmap']")) {
|
||||
const importmap = (() => {
|
||||
try {
|
||||
return JSON.parse(node.textContent)
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
})()
|
||||
|
||||
if (importmap?.imports == null)
|
||||
continue
|
||||
|
||||
for (const [name, url] of Object.entries(importmap.imports)) {
|
||||
if (typeof name != "string" || typeof url != "string")
|
||||
continue
|
||||
|
||||
try {
|
||||
// XXX: pyodide doesn't like Module(), failing with
|
||||
// "can't read 'name' of undefined" at import time
|
||||
imports[name] = {...await import(url)}
|
||||
} catch {
|
||||
console.error(`failed to fetch '${url}' for '${name}'`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pyodide.registerJsModule("esm", imports)
|
||||
}
|
||||
|
||||
async evaluate(): Promise<void> {
|
||||
console.log('evaluate');
|
||||
|
||||
if (this.source){
|
||||
this.loadFromFile(this.source)
|
||||
}else{
|
||||
let pyodide = await pyodideReadyPromise;
|
||||
const pyodide = await pyodideReadyPromise;
|
||||
await this._register_esm(pyodide)
|
||||
// debugger
|
||||
try {
|
||||
// @ts-ignore
|
||||
let source = htmlDecode(this.editor.state.doc.toString());
|
||||
const source = htmlDecode(this.editor.state.doc.toString());
|
||||
let output;
|
||||
if (source.includes("asyncio")){
|
||||
output = await pyodide.runPythonAsync(source);
|
||||
|
||||
Reference in New Issue
Block a user