mirror of
https://github.com/pyscript/pyscript.git
synced 2022-05-01 19:47:48 +03:00
Merge pull request #25 from anaconda/mattpap/esm_import_module
Allow `import d3` instead of `from esm import d3`
This commit is contained in:
@@ -109,7 +109,7 @@ for (const d of data) {
|
|||||||
|
|
||||||
<py-script>
|
<py-script>
|
||||||
from pyodide import create_proxy, to_js
|
from pyodide import create_proxy, to_js
|
||||||
from esm import d3
|
import d3
|
||||||
|
|
||||||
fruits = [
|
fruits = [
|
||||||
dict(name="🍊", count=21),
|
dict(name="🍊", count=21),
|
||||||
|
|||||||
@@ -247,8 +247,6 @@ export class PyScript extends HTMLElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected async _register_esm(pyodide: PyodideInterface): Promise<void> {
|
protected async _register_esm(pyodide: PyodideInterface): Promise<void> {
|
||||||
const imports: {[key: string]: unknown} = {}
|
|
||||||
|
|
||||||
for (const node of document.querySelectorAll("script[type='importmap']")) {
|
for (const node of document.querySelectorAll("script[type='importmap']")) {
|
||||||
const importmap = (() => {
|
const importmap = (() => {
|
||||||
try {
|
try {
|
||||||
@@ -265,17 +263,19 @@ export class PyScript extends HTMLElement {
|
|||||||
if (typeof name != "string" || typeof url != "string")
|
if (typeof name != "string" || typeof url != "string")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
let exports: object
|
||||||
try {
|
try {
|
||||||
// XXX: pyodide doesn't like Module(), failing with
|
// XXX: pyodide doesn't like Module(), failing with
|
||||||
// "can't read 'name' of undefined" at import time
|
// "can't read 'name' of undefined" at import time
|
||||||
imports[name] = {...await import(url)}
|
exports = {...await import(url)}
|
||||||
} catch {
|
} catch {
|
||||||
console.error(`failed to fetch '${url}' for '${name}'`)
|
console.warn(`failed to fetch '${url}' for '${name}'`)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pyodide.registerJsModule(name, exports)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pyodide.registerJsModule("esm", imports)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async evaluate(): Promise<void> {
|
async evaluate(): Promise<void> {
|
||||||
|
|||||||
Reference in New Issue
Block a user