Skip to content

Commit

Permalink
fix!: do not use a custom resolve function that auto imports from esm.sh
Browse files Browse the repository at this point in the history
Although initially this worked well, we do not know if we want to load
from a module, or from esm.sh. Instead, we can have the default
import map make MUI and confetti-canvas work.
  • Loading branch information
maartenbreddels committed Feb 6, 2024
1 parent 90ce5a9 commit a277d3d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 41 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,15 @@ Apart from `react`, the default we provide is:

```python
define_import_map({
"@mui/material": "https://esm.sh/@mui/[email protected]",
"@mui/material/": "https://esm.sh/@mui/[email protected]/",
"@mui/icons-material/": "https://esm.sh/@mui/icons-material/",
"canvas-confetti": "https://esm.sh/[email protected]",
"@mui/material": "https://esm.sh/@mui/[email protected]?external=react,react-dom",
"@mui/material/": "https://esm.sh/@mui/[email protected]&external=react,react-dom/",
"@mui/icons-material/": "https://esm.sh/@mui/icons-material/?external=react,react-dom",
"canvas-confetti": "https://esm.sh/[email protected]?external=react,react-dom",
})

```

_Note that it is important to add `external=react,react-dom`, otherwise [esm.sh](https://esm.sh/#using-import-maps) would import ReactJS again_.

Which means we can now write our ConfettiButton as:

```python
Expand Down
8 changes: 4 additions & 4 deletions examples/full_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -773,10 +773,10 @@
"\n",
"```python\n",
"define_import_map({\n",
" \"@mui/material\": \"https://esm.sh/@mui/[email protected]\",\n",
" \"@mui/material/\": \"https://esm.sh/@mui/[email protected]/\",\n",
" \"@mui/icons-material/\": \"https://esm.sh/@mui/icons-material/\",\n",
" \"canvas-confetti\": \"https://esm.sh/[email protected]\",\n",
" \"@mui/material\": \"https://esm.sh/@mui/[email protected]?external=react,react-dom\",\n",
" \"@mui/material/\": \"https://esm.sh/@mui/[email protected]&external=react,react-dom/\",\n",
" \"@mui/icons-material/\": \"https://esm.sh/@mui/icons-material/?external=react,react-dom\",\n",
" \"canvas-confetti\": \"https://esm.sh/[email protected]?external=react,react-dom\",\n",
"})\n",
"\n",
"```\n",
Expand Down
8 changes: 4 additions & 4 deletions ipyreact/importmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ def _update_import_map():
# still for backwards compatibility
define_import_map(
{
"@mui/material": "https://esm.sh/@mui/[email protected]",
"@mui/material/": "https://esm.sh/@mui/[email protected]/",
"@mui/icons-material/": "https://esm.sh/@mui/icons-material/",
"canvas-confetti": "https://esm.sh/[email protected]",
"@mui/material": "https://esm.sh/@mui/[email protected]?external=react,react-dom",
"@mui/material/": "https://esm.sh/@mui/[email protected]&external=react,react-dom/",
"@mui/icons-material/": "https://esm.sh/@mui/icons-material/?external=react,react-dom",
"canvas-confetti": "https://esm.sh/[email protected]?external=react,react-dom",
}
)
56 changes: 28 additions & 28 deletions src/widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,39 +97,39 @@ async function ensureImportShimLoaded() {
return await importShimLoaded;
}

function autoExternalReactResolve(
id: string,
parentUrl: string,
resolve: (arg0: any, arg1: any) => any,
) {
const shipsWith =
id == "react" ||
id == "react-dom" ||
id == "react/jsx-runtime" ||
id == "react-dom/client" ||
id == "react-reconciler" ||
id == "react-reconciler/constants";
const alreadyPatched = parentUrl.includes("?external=react,react-dom");
const parentIsEsmSh = parentUrl.startsWith("https://esm.sh/");
const isBlob = id.startsWith("blob:");
if (!shipsWith && !id.includes("://") && !parentIsEsmSh) {
id = "https://esm.sh/" + id;
}
if (!shipsWith && !alreadyPatched && !isBlob) {
id = id + "?external=react,react-dom";
}
return resolve(id, parentUrl);
}
// function autoExternalReactResolve(
// id: string,
// parentUrl: string,
// resolve: (arg0: any, arg1: any) => any,
// ) {
// const shipsWith =
// id == "react" ||
// id == "react-dom" ||
// id == "react/jsx-runtime" ||
// id == "react-dom/client" ||
// id == "react-reconciler" ||
// id == "react-reconciler/constants";
// const alreadyPatched = parentUrl.includes("?external=react,react-dom");
// const parentIsEsmSh = parentUrl.startsWith("https://esm.sh/");
// const isBlob = id.startsWith("blob:");
// if (!shipsWith && !id.includes("://") && !parentIsEsmSh) {
// id = "https://esm.sh/" + id;
// }
// if (!shipsWith && !alreadyPatched && !isBlob) {
// id = id + "?external=react,react-dom";
// }
// return resolve(id, parentUrl);
// }

// @ts-ignore
window.esmsInitOptions = {
shimMode: true,
mapOverrides: true,
resolve: (
id: string,
parentUrl: string,
resolve: (id: string, parentUrl: string) => any,
) => autoExternalReactResolve(id, parentUrl, resolve),
// resolve: (
// id: string,
// parentUrl: string,
// resolve: (id: string, parentUrl: string) => any,
// ) => autoExternalReactResolve(id, parentUrl, resolve),
};

let react18ESMUrls: any = null;
Expand Down

0 comments on commit a277d3d

Please sign in to comment.