From 7c06429791730c7cfcae373d51cb447815134ed9 Mon Sep 17 00:00:00 2001 From: Pablito Carvajal Date: Mon, 20 Oct 2025 19:30:09 -0300 Subject: [PATCH] feat: example of JSX with datastar --- examples/jsx-file-system-router/.gitignore | 34 ++ examples/jsx-file-system-router/Readme.md | 421 ++++++++++++++++++ examples/jsx-file-system-router/app/client.ts | 2 + .../app/components/TypewriterExample.tsx | 35 ++ .../app/components/WebComponentExample.tsx | 36 ++ .../jsx-file-system-router/app/global.d.ts | 8 + .../app/routes/_404.tsx | 9 + .../app/routes/_error.tsx | 12 + .../app/routes/_middleware.ts | 8 + .../app/routes/_renderer.tsx | 23 + .../app/routes/api/typewriter.ts | 25 ++ .../app/routes/index.tsx | 32 ++ examples/jsx-file-system-router/app/server.ts | 8 + examples/jsx-file-system-router/app/style.css | 1 + .../app/web-components/ReverseComponent.ts | 27 ++ examples/jsx-file-system-router/package.json | 23 + .../public/.assetsignore | 2 + .../jsx-file-system-router/public/favicon.ico | Bin 0 -> 15406 bytes examples/jsx-file-system-router/tsconfig.json | 28 ++ .../jsx-file-system-router/vite.config.ts | 22 + 20 files changed, 756 insertions(+) create mode 100644 examples/jsx-file-system-router/.gitignore create mode 100644 examples/jsx-file-system-router/Readme.md create mode 100644 examples/jsx-file-system-router/app/client.ts create mode 100644 examples/jsx-file-system-router/app/components/TypewriterExample.tsx create mode 100644 examples/jsx-file-system-router/app/components/WebComponentExample.tsx create mode 100644 examples/jsx-file-system-router/app/global.d.ts create mode 100644 examples/jsx-file-system-router/app/routes/_404.tsx create mode 100644 examples/jsx-file-system-router/app/routes/_error.tsx create mode 100644 examples/jsx-file-system-router/app/routes/_middleware.ts create mode 100644 examples/jsx-file-system-router/app/routes/_renderer.tsx create mode 100644 examples/jsx-file-system-router/app/routes/api/typewriter.ts create mode 100644 examples/jsx-file-system-router/app/routes/index.tsx create mode 100644 examples/jsx-file-system-router/app/server.ts create mode 100644 examples/jsx-file-system-router/app/style.css create mode 100644 examples/jsx-file-system-router/app/web-components/ReverseComponent.ts create mode 100644 examples/jsx-file-system-router/package.json create mode 100644 examples/jsx-file-system-router/public/.assetsignore create mode 100644 examples/jsx-file-system-router/public/favicon.ico create mode 100644 examples/jsx-file-system-router/tsconfig.json create mode 100644 examples/jsx-file-system-router/vite.config.ts diff --git a/examples/jsx-file-system-router/.gitignore b/examples/jsx-file-system-router/.gitignore new file mode 100644 index 0000000..f86f576 --- /dev/null +++ b/examples/jsx-file-system-router/.gitignore @@ -0,0 +1,34 @@ +# prod +dist/ + +# dev +.hono/ +.wrangler/ +.yarn/ +!.yarn/releases +.vscode/* +!.vscode/launch.json +!.vscode/*.code-snippets +.idea/workspace.xml +.idea/usage.statistics.xml +.idea/shelf + +# deps +node_modules/ + +# env +.env +.env.production +.dev.vars + +# logs +logs/ +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* +lerna-debug.log* + +# misc +.DS_Store diff --git a/examples/jsx-file-system-router/Readme.md b/examples/jsx-file-system-router/Readme.md new file mode 100644 index 0000000..1abc817 --- /dev/null +++ b/examples/jsx-file-system-router/Readme.md @@ -0,0 +1,421 @@ +# Datastar + HonoX Example + +A working example of [Datastar](https://data-star.dev/) (reactive hypermedia) integrated with [HonoX](https://github.com/honojs/honox) (Hono meta-framework with file-based routing, a la NextJS) + +## Quick Start + +```bash +npm install + +# Development +npm run dev +# Visit http://localhost:5173 + +# Production (build + start in one command) +npm run prod +# Visit http://localhost:3000 + +# Or run separately: +npm run build # Build first +npm start # Then start server + +# Note: 'npm start' uses 'node -C dist index.js' +# The -C flag changes working directory to dist/ before running +``` + +## Step-by-Step Setup + +### 1. Create HonoX Project + +```bash +npm create hono@latest +# Choose: x-basic (use arrow keys to select) +``` + +The **x-basic** template includes: +- ✅ **Tailwind CSS** pre-configured (no extra setup needed!) +- ✅ **Cloudflare Workers** adapter (can be changed to Node.js/Bun) +- ✅ **HonoX** with file-based routing + +> **Note**: If you need to configure Tailwind CSS manually or want to learn more, see the [HonoX Tailwind CSS documentation](https://github.com/honojs/honox?tab=readme-ov-file#tailwind-css). + +If you want to use **Node.js** or **Bun** instead of Cloudflare, follow the next steps. + +### 2. Remove Cloudflare Dependencies (Optional) + +If you're **not deploying to Cloudflare**, remove these packages: + +```bash +# Using npm +npm remove @cloudflare/workers-types wrangler + +``` + +Then remove these scripts from `package.json`: +```json +{ + "scripts": { + "preview": "wrangler dev", // ❌ Remove this + "deploy": "bun run build && wrangler deploy" // ❌ Remove this + } +} +``` + +Delete the Cloudflare configuration file: +```bash +rm wrangler.jsonc +``` + +And remove the Cloudflare types from `tsconfig.json`: +```json +{ + "types": [ + "vite/client", + "@cloudflare/workers-types/2023-07-01" // ❌ Remove this line + ] +} +``` +### 3. Configure Vite Adapter + +Edit `vite.config.ts` to use **Node.js** or **Bun** adapter: + +**For Node.js (default recommendation):** +```ts +// vite.config.ts +import build from '@hono/vite-build/node-server' // Node.js build +import adapter from '@hono/vite-dev-server/node' // Node.js adapter +//import build from '@hono/vite-build/bun' // Change to Bun +//import adapter from '@hono/vite-dev-server/bun' // Change to Bun +import tailwindcss from '@tailwindcss/vite' +import honox from 'honox/vite' +import { defineConfig } from 'vite' + +export default defineConfig({ + plugins: [ + honox({ + devServer: { adapter }, + client: { input: ['/app/client.ts', '/app/style.css'] } + }), + tailwindcss(), + build() + ] +}) +``` + +### 4. Add Datastar CDN + +Edit `app/routes/_renderer.tsx`: + +```tsx +import { jsxRenderer } from 'hono/jsx-renderer' +import { Link, Script } from 'honox/server' + +export default jsxRenderer(({ children }) => { + return ( + + + + + + + + {/* Add Datastar CDN */} + + + + +