From 2673aef55fdc24e6d3d776f55477c11710b038d3 Mon Sep 17 00:00:00 2001 From: dnsjm Date: Wed, 23 Apr 2025 10:09:01 +0800 Subject: [PATCH 1/3] Added koa js --- examples/with-koa/components/Edit.tsx | 7 +++ examples/with-koa/package.json | 30 ++++++++++++ examples/with-koa/pages/about.tsx | 28 +++++++++++ examples/with-koa/pages/home.tsx | 34 +++++++++++++ examples/with-koa/pages/page.css | 7 +++ examples/with-koa/public/global.css | 70 +++++++++++++++++++++++++++ examples/with-koa/public/react.svg | 1 + examples/with-koa/scripts/build.ts | 41 ++++++++++++++++ examples/with-koa/scripts/develop.ts | 49 +++++++++++++++++++ examples/with-koa/scripts/start.ts | 49 +++++++++++++++++++ examples/with-koa/tsconfig.json | 13 +++++ 11 files changed, 329 insertions(+) create mode 100644 examples/with-koa/components/Edit.tsx create mode 100644 examples/with-koa/package.json create mode 100644 examples/with-koa/pages/about.tsx create mode 100644 examples/with-koa/pages/home.tsx create mode 100644 examples/with-koa/pages/page.css create mode 100644 examples/with-koa/public/global.css create mode 100644 examples/with-koa/public/react.svg create mode 100644 examples/with-koa/scripts/build.ts create mode 100644 examples/with-koa/scripts/develop.ts create mode 100644 examples/with-koa/scripts/start.ts create mode 100644 examples/with-koa/tsconfig.json diff --git a/examples/with-koa/components/Edit.tsx b/examples/with-koa/components/Edit.tsx new file mode 100644 index 0000000..69a41ea --- /dev/null +++ b/examples/with-koa/components/Edit.tsx @@ -0,0 +1,7 @@ +export default function Edit({ name }: { name: string }) { + return ( +

+ Edit pages/{name}.tsx and save to test HMR +

+ ) +} \ No newline at end of file diff --git a/examples/with-koa/package.json b/examples/with-koa/package.json new file mode 100644 index 0000000..34124f3 --- /dev/null +++ b/examples/with-koa/package.json @@ -0,0 +1,30 @@ +{ + "type": "module", + "name": "reactus-with-koa", + "version": "1.0.0", + "private": true, + "scripts": { + "build": "tsx scripts/build.ts", + "dev": "tsx scripts/develop.ts", + "start": "tsx scripts/start.ts" + }, + "dependencies": { + "koa": "^2.15.3", + "koa-static": "^5.0.0", + "react": "19.0.0", + "react-dom": "19.0.0", + "reactus": "0.2.8" + }, + "devDependencies": { + "@types/koa": "^2.15.0", + "@types/koa-static": "^4.0.4", + "@types/node": "22.9.3", + "@types/react": "19.0.10", + "@types/react-dom": "19.0.4", + "@vitejs/plugin-react": "4.3.4", + "ts-node": "10.9.2", + "tsx": "4.19.3", + "typescript": "5.7.2", + "vite": "6.1.1" + } + } \ No newline at end of file diff --git a/examples/with-koa/pages/about.tsx b/examples/with-koa/pages/about.tsx new file mode 100644 index 0000000..969e59e --- /dev/null +++ b/examples/with-koa/pages/about.tsx @@ -0,0 +1,28 @@ +import './page.css'; +import Edit from '../components/Edit'; + +export function Head({ styles = [] }: { styles?: string[] }) { + return ( + <> + About Reactus + + + + {styles.map((href, index) => ( + + ))} + + ) +} + +export default function AboutPage() { + return ( + <> +

About Reactus

+
+ + Home Page +
+ + ) +} \ No newline at end of file diff --git a/examples/with-koa/pages/home.tsx b/examples/with-koa/pages/home.tsx new file mode 100644 index 0000000..5bdd8c6 --- /dev/null +++ b/examples/with-koa/pages/home.tsx @@ -0,0 +1,34 @@ +import './page.css'; +import { useState } from 'react'; +import Edit from '../components/Edit'; + +export function Head({ styles = [] }: { styles?: string[] }) { + return ( + <> + Reactus + + + + {styles.map((href, index) => ( + + ))} + + ) +} + +export default function HomePage() { + const [count, setCount] = useState(0) + + return ( + <> +

React + Reactus

+
+ + + About Reactus +
+ + ) +} \ No newline at end of file diff --git a/examples/with-koa/pages/page.css b/examples/with-koa/pages/page.css new file mode 100644 index 0000000..30e8b0c --- /dev/null +++ b/examples/with-koa/pages/page.css @@ -0,0 +1,7 @@ +:root { display: initial; } +#root { + max-width: 1280px; + margin: 0 auto; + padding: 2rem; + text-align: center; +} \ No newline at end of file diff --git a/examples/with-koa/public/global.css b/examples/with-koa/public/global.css new file mode 100644 index 0000000..bcf1636 --- /dev/null +++ b/examples/with-koa/public/global.css @@ -0,0 +1,70 @@ +:root { + display: none; + font-family: system-ui, Avenir, Helvetica, Arial, sans-serif; + line-height: 1.5; + font-weight: 400; + + color-scheme: light dark; + color: rgba(255, 255, 255, 0.87); + background-color: #242424; + + font-synthesis: none; + text-rendering: optimizeLegibility; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; + -webkit-text-size-adjust: 100%; +} + +a { + font-weight: 500; + color: #646cff; + text-decoration: inherit; +} +a:hover { + color: #535bf2; +} + +body { + margin: 0; + display: flex; + place-items: center; + min-width: 320px; + min-height: 100vh; +} + +h1 { + font-size: 3.2em; + line-height: 1.1; +} + +button { + border-radius: 8px; + border: 1px solid transparent; + padding: 0.6em 1.2em; + font-size: 1em; + font-weight: 500; + font-family: inherit; + background-color: #1a1a1a; + cursor: pointer; + transition: border-color 0.25s; +} +button:hover { + border-color: #646cff; +} +button:focus, +button:focus-visible { + outline: 4px auto -webkit-focus-ring-color; +} + +@media (prefers-color-scheme: light) { + :root { + color: #213547; + background-color: #ffffff; + } + a:hover { + color: #747bff; + } + button { + background-color: #f9f9f9; + } +} \ No newline at end of file diff --git a/examples/with-koa/public/react.svg b/examples/with-koa/public/react.svg new file mode 100644 index 0000000..6c87de9 --- /dev/null +++ b/examples/with-koa/public/react.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/examples/with-koa/scripts/build.ts b/examples/with-koa/scripts/build.ts new file mode 100644 index 0000000..aa109d4 --- /dev/null +++ b/examples/with-koa/scripts/build.ts @@ -0,0 +1,41 @@ +//node +import path from 'node:path'; +//reactus +import { build } from 'reactus'; + +async function builder() { + const cwd = process.cwd(); + const engine = build({ + cwd, + //path where to save assets (css, images, etc) + assetPath: path.join(cwd, 'public/assets'), + //path where to save and load (live) the client scripts (js) + clientPath: path.join(cwd, 'public/client'), + //path where to save and load (live) the server script (js) + pagePath: path.join(cwd, '.build/pages') + }); + + await engine.set('@/pages/home'); + await engine.set('@/pages/about'); + + const responses = [ + ...await engine.buildAllClients(), + ...await engine.buildAllAssets(), + ...await engine.buildAllPages() + ].map(response => { + const results = response.results; + if (typeof results?.contents === 'string') { + results.contents = results.contents.substring(0, 100) + ' ...'; + } + return results; + }); + + //console.log(responses); + //fix for unused variable :) + if (responses.length) return; +} + +builder().catch(e => { + console.error(e); + process.exit(1); +}); \ No newline at end of file diff --git a/examples/with-koa/scripts/develop.ts b/examples/with-koa/scripts/develop.ts new file mode 100644 index 0000000..27cf5cc --- /dev/null +++ b/examples/with-koa/scripts/develop.ts @@ -0,0 +1,49 @@ +//node +import Koa from 'koa'; // Import Koa +//reactus +import { dev } from 'reactus'; + +async function develop() { + const cwd = process.cwd(); + const engine = dev({ + cwd, + basePath: '/', + watchIgnore: ['**/.build/**'], + clientRoute: '/client' + }); + + const app = new Koa(); // Create a Koa app instance + + // Koa Middleware + app.use(async (ctx, next) => { + // Let reactus handle its development routes (client scripts, HMR, etc.) + // Pass Koa's raw req/res objects to the engine's http handler + await engine.http(ctx.req, ctx.res); + + // If reactus handled the request and sent a response, move to the next middleware (or end) + if (ctx.res.headersSent) { + return next(); + } + + // Routing logic using ctx.path + if (ctx.path === '/') { + ctx.type = 'text/html'; // Set content type using ctx.type + ctx.body = await engine.render('@/pages/home', { title: 'Home' }); // Set response body using ctx.body + } else if (ctx.path === '/about') { + ctx.type = 'text/html'; + ctx.body = await engine.render('@/pages/about'); + } else { + // Let Koa handle the 404 if no route matches + await next(); + } + }); + + app.listen(3000, () => { // Use app.listen + console.log('Server running at http://localhost:3000/'); + }); +} + +develop().catch(e => { + console.error(e); + process.exit(1); +}); \ No newline at end of file diff --git a/examples/with-koa/scripts/start.ts b/examples/with-koa/scripts/start.ts new file mode 100644 index 0000000..78cc96e --- /dev/null +++ b/examples/with-koa/scripts/start.ts @@ -0,0 +1,49 @@ +//node +import path from 'node:path'; +import Koa from 'koa'; // Import Koa +import serveStatic from 'koa-static'; // Import koa-static +//reactus +import { serve } from 'reactus'; + +async function start() { + const cwd = process.cwd(); + const engine = serve({ + cwd, + clientRoute: '/client', + pagePath: path.join(cwd, '.build/pages'), + cssRoute: '/assets' + }); + + const app = new Koa(); // Create a Koa app instance + + // Serve static files from 'public' directory using koa-static + app.use(serveStatic(path.join(cwd, 'public'), { + maxage: 31536000 * 1000, // 1Y in milliseconds + immutable: true + })); + + // Koa Middleware for page rendering + app.use(async (ctx, next) => { + // Routing logic using ctx.path + if (ctx.path === '/') { + ctx.type = 'text/html'; // Set content type + ctx.body = await engine.render('@/pages/home'); // Set response body + } else if (ctx.path === '/about') { + ctx.type = 'text/html'; + ctx.body = await engine.render('@/pages/about'); + } else { + // If it's not a page route, and wasn't handled by static middleware, + // Koa will automatically send a 404 Not Found if ctx.body remains unset. + await next(); + } + }); + + app.listen(3000, () => { // Use app.listen + console.log('Server running at http://localhost:3000/'); + }); +} + +start().catch(e => { + console.error(e); + process.exit(1); +}); \ No newline at end of file diff --git a/examples/with-koa/tsconfig.json b/examples/with-koa/tsconfig.json new file mode 100644 index 0000000..d6be85e --- /dev/null +++ b/examples/with-koa/tsconfig.json @@ -0,0 +1,13 @@ +{ + "extends": "reactus/tsconfig/client", + "compilerOptions": { + "moduleResolution": "bundler", + "outDir": ".build", + }, + "include": [ + "scripts/**/*.ts", + "components/**/*.tsx" , + "pages/**/*.tsx" + ], + "exclude": [ "dist", "node_modules", "tests" ] +} \ No newline at end of file From 0d3e27e1ddcf238c932632dbf114fd090830eb65 Mon Sep 17 00:00:00 2001 From: dnsjm Date: Wed, 30 Apr 2025 10:00:58 +0800 Subject: [PATCH 2/3] Updated koa js --- examples/with-koa/pages/home.tsx | 2 +- examples/with-koa/public/global.css | 2 +- examples/with-koa/scripts/develop.ts | 63 ++++++++++++----------- examples/with-koa/scripts/start.ts | 75 +++++++++++++++++----------- package.json | 10 ++-- 5 files changed, 87 insertions(+), 65 deletions(-) diff --git a/examples/with-koa/pages/home.tsx b/examples/with-koa/pages/home.tsx index 5bdd8c6..4710f9d 100644 --- a/examples/with-koa/pages/home.tsx +++ b/examples/with-koa/pages/home.tsx @@ -21,7 +21,7 @@ export default function HomePage() { return ( <> -

React + Reactus

+

React + Reactus + Koa