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

[add] Glob Router generator for multiple pages mode #363

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
12 changes: 12 additions & 0 deletions preview-ui/.parcelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "@parcel/config-default",
"resolvers": [
"@parcel/resolver-glob",
"..."
],
"transformers": {
"url:*": [
"@parcel/transformer-raw"
]
}
}
4 changes: 2 additions & 2 deletions preview-ui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/logo.png" />
<link rel="icon" href="public/logo.png" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Dewhale UI preview</title>
</head>

<body>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
<script type="module" src="src/main.jsx"></script>
</body>

</html>
18 changes: 12 additions & 6 deletions preview-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"clean": "rm -rf .parcel-cache/ dist/",
"dev": "npm run clean && parcel index.html",
"build": "npm run clean && parcel build index.html --public-url .",
"lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
"preview": "serve dist/"
},
"dependencies": {
"@nivo/bar": "^0.84.0",
Expand Down Expand Up @@ -53,22 +54,27 @@
"react-day-picker": "^8.9.1",
"react-dom": "^18.2.0",
"react-resizable-panels": "^1.0.5",
"react-router-dom": "^6.25.1",
"shiki": "^0.14.7",
"tailwind-merge": "^2.1.0",
"tailwindcss-animate": "^1.0.7",
"vaul": "^0.8.0"
},
"devDependencies": {
"@parcel/config-default": "~2.12.0",
"@parcel/resolver-glob": "~2.12.0",
"@parcel/transformer-raw": "~2.12.0",
"@types/react": "^18.2.43",
"@types/react-dom": "^18.2.17",
"@vitejs/plugin-react": "^4.2.1",
"autoprefixer": "^10.4.16",
"eslint": "^8.55.0",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.5",
"parcel": "~2.12.0",
"postcss": "^8.4.32",
"tailwindcss": "^3.4.0",
"vite": "^5.0.8"
"process": "^0.11.10",
"serve": "^14.2.3",
"tailwindcss": "^3.4.0"
}
}
5,657 changes: 3,537 additions & 2,120 deletions preview-ui/pnpm-lock.yaml

Large diffs are not rendered by default.

51 changes: 37 additions & 14 deletions preview-ui/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,54 @@
import { useEffect, useState } from "react";
import { CopyToClipboard } from "react-copy-to-clipboard";
import { Button } from "@/components/ui/button";
import { Button } from "@/components/ui/button.jsx";
import ErrorBoundary from "./ErrorBoundary.jsx";
import Preview from "./Preview.jsx";
import previewStr from "./Preview.jsx?raw";
// @ts-ignore
import previewURL from "url:./Preview.jsx";
import { getHighlighter, setCDN } from "shiki";
import { createHashRouter, RouterProvider } from "react-router-dom";

import { generateRoutes } from "./lib/utils.js";
// @ts-ignore
import * as pages from "./pages/**/*.jsx";

const router = createHashRouter(
[...generateRoutes(pages)].map(({ path, component: Tag }) => ({
path,
element: <Tag />,
}))
);

export default function Home() {
const [showCanvas, setShowCanvas] = useState(true);
const [copied, setCopied] = useState(false);
const [sourceCode, setSourceCode] = useState('')
const [code, setCode] = useState("");

function setCodeHighlighter() {
async function setCodeHighlighter() {
setCDN("https://cdn.jsdelivr.net/npm/shiki");
getHighlighter({ theme: "one-dark-pro", langs: ["jsx"] })
.then((h) => {
const html = h.codeToHtml(previewStr, { lang: "jsx" });
setCode(html);
})
.catch((error) => {
setCode(error);

try {
const { codeToHtml } = await getHighlighter({
theme: "one-dark-pro",
langs: ["jsx"],
});
const response = await fetch(previewURL);
const previewStr = await response.text();

setSourceCode(previewStr)

const html = codeToHtml(previewStr, { lang: "jsx" });

setCode(html);
} catch (error) {
setCode(error);
}
}

useEffect(() => {
setCodeHighlighter();
}, []);

return (
<main className="vx-dev-wrapper relative">
<div className="flex justify-end p-1">
Expand All @@ -36,13 +59,13 @@ export default function Home() {
{showCanvas ? (
<div className="rounded-lg border border-black/5 bg-white shadow-[0px_1px_2px_0px_rgba(0,_0,_0,_0.04)] mx-1 p-1">
<ErrorBoundary>
<Preview />
<RouterProvider router={router} />
</ErrorBoundary>
</div>
) : (
<pre className="bg-gray-100 mx-1 p-1 rounded overflow-auto relative text-xs">
<CopyToClipboard
text={previewStr}
text={sourceCode}
onCopy={() => {
setCopied(true);
setTimeout(() => setCopied(false), 2000);
Expand All @@ -52,7 +75,7 @@ export default function Home() {
{copied ? "Copied!" : "Copy Code"}
</Button>
</CopyToClipboard>
<div dangerouslySetInnerHTML={{ __html: code }}></div>
<div dangerouslySetInnerHTML={{ __html: code }} />
</pre>
)}
</main>
Expand Down
20 changes: 17 additions & 3 deletions preview-ui/src/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import { clsx } from "clsx"
import { twMerge } from "tailwind-merge"
import { clsx } from "clsx";
import { twMerge } from "tailwind-merge";

export function cn(...inputs) {
return twMerge(clsx(inputs))
return twMerge(clsx(inputs));
}

export function* generateRoutes(globModules, path = "") {
for (const key in globModules) {
const value = globModules[key];

if (key === "default" && typeof value === "function")
yield {
path: path.replace(/\/index$/i, "/"),
component: value,
};
else if (value != null && typeof value === "object")
yield* generateRoutes(value, `${path}/${key}`);
}
}
5 changes: 5 additions & 0 deletions preview-ui/src/pages/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default () => (
<div>
<h1>Index page</h1>
</div>
);
5 changes: 5 additions & 0 deletions preview-ui/src/pages/user/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default () => (
<div>
<h1>User page</h1>
</div>
);
5 changes: 4 additions & 1 deletion preview-ui/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"compilerOptions": {
"baseUrl": ".",
"checkJs": true,
"module": "Node16",
"moduleResolution": "Node16",
"jsx": "react-jsx",
"paths": {
"@/*": ["./src/*"]
}
Expand Down
12 changes: 0 additions & 12 deletions preview-ui/vite.config.js

This file was deleted.