Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add more react modules to the import map #50

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading