Skip to content

Commit

Permalink
Make sure lib is only one file
Browse files Browse the repository at this point in the history
relative imports wont work with anywidget
  • Loading branch information
saulshanabrook committed Sep 20, 2024
1 parent 203eb84 commit 61564e1
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 503 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "egraph-visualizer",
"version": "1.1.0",
"version": "1.1.1",
"repository": {
"type": "git",
"url": "git+https://github.com/saulshanabrook/egraph-visualizer.git"
Expand All @@ -13,6 +13,7 @@
"preview": "vite preview"
},
"dependencies": {
"@anywidget/types": "0.2.0",
"@headlessui/react": "2.1.3",
"@heroicons/react": "2.1.5",
"@xyflow/react": "12.1.1",
Expand All @@ -26,7 +27,6 @@
},
"devDependencies": {
"@eslint/js": "^9.9.0",
"@jupyter-widgets/base": "6.0.10",
"@types/node": "22.5.0",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
Expand All @@ -45,6 +45,6 @@
"files": [
"dist"
],
"main": "dist/dom.js",
"main": "dist/index.js",
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { use, useState } from "react";
import "./App.css";
import Monaco from "./Monaco";
import Visualizer from "./Visualizer";
import { Visualizer } from "./Visualizer";
import DefaultCode from "/examples/manual/homepage.json?raw";

function App() {
Expand Down
45 changes: 43 additions & 2 deletions src/Visualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import {
} from "@xyflow/react";

import "@xyflow/react/dist/style.css";
import { AnyModel } from "@anywidget/types";
import { createRoot } from "react-dom/client";
// Elk has a *huge* amount of options to configure. To see everything you can
// tweak check out:
//
Expand Down Expand Up @@ -701,7 +703,7 @@ function LayoutFlow({
);
}

function Visualizer({ egraph, height = null, resize = false }: { egraph: string; height?: string | null; resize?: boolean }) {
export function Visualizer({ egraph, height = null, resize = false }: { egraph: string; height?: string | null; resize?: boolean }) {
const [outerElem, setOuterElem] = useState<HTMLDivElement | null>(null);
const [innerElem, setInnerElem] = useState<HTMLDivElement | null>(null);

Expand Down Expand Up @@ -731,4 +733,43 @@ function Visualizer({ egraph, height = null, resize = false }: { egraph: string;
);
}

export default Visualizer;
// Put these both in one file, so its emitted as a single chunk and anywidget doesn't have to import another file

/// Render anywidget model to the given element
// Must be named `render` to work as an anywidget module
// https://anywidget.dev/en/afm/#lifecycle-methods
// eslint-disable-next-line react-refresh/only-export-components
export function render({ model, el }: { el: HTMLElement; model: AnyModel }) {
const root = createRoot(el);
function render() {
startTransition(() => {
root.render(<Visualizer egraph={model.get("egraph")} height="600px" resize />);
});
}
render();
model.on("change:egraph", render);

return () => {
model.off("change:egraph", render);
root.unmount();
};
}

/// Mount the visualizer to the given element
/// Call `render` to render a new egraph
/// Call `unmount` to unmount the visualizer
// eslint-disable-next-line react-refresh/only-export-components
export function mount(element: HTMLElement): { render: (egraph: string) => void; unmount: () => void } {
const root = createRoot(element);

function render(egraph: string) {
startTransition(() => {
root.render(<Visualizer egraph={egraph} />);
});
}

function unmount() {
root.unmount();
}
return { render, unmount };
}
24 changes: 0 additions & 24 deletions src/anywidget.tsx

This file was deleted.

23 changes: 0 additions & 23 deletions src/dom.tsx

This file was deleted.

7 changes: 4 additions & 3 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ export default defineConfig({
base: "./",
build: {
lib: {
entry: [resolve(__dirname, "src/anywidget.tsx"), resolve(__dirname, "src/dom.tsx")],
entry: [resolve(__dirname, "src/Visualizer.tsx")],
formats: ["es"],
},
rollupOptions: {
input: {
main: resolve(__dirname, "index.html"),
anywidget: resolve(__dirname, "src/anywidget.tsx"),
dom: resolve(__dirname, "src/dom.tsx"),
index: resolve(__dirname, "src/Visualizer.tsx"),
},
// allow extension of entry signatures so Visualizer.tsx can be outputed as index.js and not include any imports
preserveEntrySignatures: "allow-extension",
},
},

Expand Down
Loading

0 comments on commit 61564e1

Please sign in to comment.