Skip to content

Commit

Permalink
fix: add more react modules to the import map
Browse files Browse the repository at this point in the history
My guess is this has changed in esm.sh, but we needed
react/jsx-runtime for mui to work
and the others seem to be needed for a package like
threejs-fiber.
  • Loading branch information
maartenbreddels committed Dec 19, 2023
1 parent a081714 commit 471e08b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
50 changes: 46 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,14 @@
"@babel/standalone": "^7.21.3",
"@jupyter-widgets/base": "^1.1.10 || ^2 || ^3 || ^4 || ^5 || ^6",
"@types/react": "^18.0.29",
"@types/react-reconciler": "^0.28.8",
"babel-plugin-import-map": "^1.0.0",
"es-module-shims": "^1.7.0",
"esbuild": "^0.17.14",
"raw-loader": "^4.0.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-reconciler": "^0.29.0",
"sucrase": "^3.30.0"
},
"devDependencies": {
Expand Down
25 changes: 18 additions & 7 deletions src/widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import {

import * as React from 'react';
import { useEffect, useState } from 'react';
import * as ReactJsxRuntime from 'react/jsx-runtime';
import * as ReactReconcilerContants from "react-reconciler/constants";
import * as ReactReconciler from "react-reconciler";
import * as ReactDOM from 'react-dom';
// @ts-ignore
import * as ReactDOMClient from 'react-dom/client';
Expand Down Expand Up @@ -41,7 +44,7 @@ async function ensureImportShimLoaded() {
}

function autoExternalReactResolve(id: string, parentUrl: string, resolve: (arg0: any, arg1: any) => any) {
const shipsWith = (id == "react") || (id == "react-dom");
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:");
Expand All @@ -66,7 +69,14 @@ let react16ESMUrls : any = null;
function ensureReactSetup(version: number) {
if(version == 18) {
if(react18ESMUrls == null) {
react18ESMUrls = {urlReact: expose(React), urlReactDom: expose(ReactDOM)};
react18ESMUrls = {
"react": expose(React),
"react-dom": expose(ReactDOM),
"react/jsx-runtime": expose(ReactJsxRuntime),
"react-dom/client": expose(ReactDOMClient),
"react-reconciler": expose(ReactReconciler),
"react-reconciler/constants": expose(ReactReconcilerContants),
};
}
return react18ESMUrls;
} else if(version == 16) {
Expand Down Expand Up @@ -202,18 +212,19 @@ export class ReactView extends DOMWidgetView {
setScope(compiledCode);
return;
}
const {urlReact, urlReactDom} = ensureReactSetup(this.model.get("react_version"));
const reactImportMap = ensureReactSetup(this.model.get("react_version"));
await ensureImportShimLoaded();
let finalCode = compiledCode;
// @ts-ignore
const importMapWidget = this.model.get("_import_map");
const importMap = {
"imports": {
"react": urlReact,
"react-dom": urlReactDom,
...importMapWidget["imports"]
...reactImportMap,
...importMapWidget["imports"],
},
"scopes": importMapWidget["scopes"]
"scopes": {
...importMapWidget["scopes"]
}
};
// @ts-ignore
importShim.addImportMap(importMap);
Expand Down

0 comments on commit 471e08b

Please sign in to comment.