From ac7aff1b47b0b271d2afb010f3ffd199b9b6685d Mon Sep 17 00:00:00 2001 From: Sean Scally Date: Tue, 24 Jan 2023 19:09:39 -0800 Subject: Add FileSystemRouter + React example (#1894) --- examples/react-file-system-router/bun.lockb | Bin 0 -> 4963 bytes examples/react-file-system-router/index.tsx | 24 ++++++++++++++++++++++ examples/react-file-system-router/package.json | 14 +++++++++++++ examples/react-file-system-router/pages/index.tsx | 17 +++++++++++++++ examples/react-file-system-router/pages/one.tsx | 12 +++++++++++ examples/react-file-system-router/pages/two.tsx | 12 +++++++++++ examples/react-file-system-router/tsconfig.json | 20 ++++++++++++++++++ 7 files changed, 99 insertions(+) create mode 100755 examples/react-file-system-router/bun.lockb create mode 100644 examples/react-file-system-router/index.tsx create mode 100644 examples/react-file-system-router/package.json create mode 100644 examples/react-file-system-router/pages/index.tsx create mode 100644 examples/react-file-system-router/pages/one.tsx create mode 100644 examples/react-file-system-router/pages/two.tsx create mode 100644 examples/react-file-system-router/tsconfig.json (limited to 'examples') diff --git a/examples/react-file-system-router/bun.lockb b/examples/react-file-system-router/bun.lockb new file mode 100755 index 000000000..ffe6b6c6a Binary files /dev/null and b/examples/react-file-system-router/bun.lockb differ diff --git a/examples/react-file-system-router/index.tsx b/examples/react-file-system-router/index.tsx new file mode 100644 index 000000000..a784c393b --- /dev/null +++ b/examples/react-file-system-router/index.tsx @@ -0,0 +1,24 @@ +// A simple way to connect FileSystemRouter to Bun#serve +// run with `bun run index.tsx` + +import { renderToReadableStream } from 'react-dom/server' +import { FileSystemRouter } from 'bun' + +export default { + port: 3000, + async fetch(request: Request) { + const router = new FileSystemRouter({ + dir: process.cwd() + "/pages", + style: "nextjs" + }) + + const route = router.match(request) + + const { default: Root } = await import(route.filePath) + return new Response( + await renderToReadableStream( + + ) + ) + } +} \ No newline at end of file diff --git a/examples/react-file-system-router/package.json b/examples/react-file-system-router/package.json new file mode 100644 index 000000000..36e0349c0 --- /dev/null +++ b/examples/react-file-system-router/package.json @@ -0,0 +1,14 @@ +{ + "name": "react-routes", + "module": "index.tsx", + "type": "module", + "devDependencies": { + "@types/react": "^18.0.27", + "@types/react-dom": "^18.0.10", + "bun-types": "^0.4.0" + }, + "dependencies": { + "react": "^18.2.0", + "react-dom": "^18.2.0" + } +} \ No newline at end of file diff --git a/examples/react-file-system-router/pages/index.tsx b/examples/react-file-system-router/pages/index.tsx new file mode 100644 index 000000000..b2d9e735e --- /dev/null +++ b/examples/react-file-system-router/pages/index.tsx @@ -0,0 +1,17 @@ +// reachable from http://localhost:3000/ + +export default () => ( + + + index + + +

+ one +

+

+ two +

+ + +) \ No newline at end of file diff --git a/examples/react-file-system-router/pages/one.tsx b/examples/react-file-system-router/pages/one.tsx new file mode 100644 index 000000000..68c9fb4b9 --- /dev/null +++ b/examples/react-file-system-router/pages/one.tsx @@ -0,0 +1,12 @@ +// reachable from http://localhost:3000/one + +export default () => ( + + + one + + +

one

+ + +) \ No newline at end of file diff --git a/examples/react-file-system-router/pages/two.tsx b/examples/react-file-system-router/pages/two.tsx new file mode 100644 index 000000000..09be9fa5c --- /dev/null +++ b/examples/react-file-system-router/pages/two.tsx @@ -0,0 +1,12 @@ +// reachable from http://localhost:3000/two + +export default () => ( + + + two + + +

two

+ + +) \ No newline at end of file diff --git a/examples/react-file-system-router/tsconfig.json b/examples/react-file-system-router/tsconfig.json new file mode 100644 index 000000000..c458f8fe8 --- /dev/null +++ b/examples/react-file-system-router/tsconfig.json @@ -0,0 +1,20 @@ +{ + "compilerOptions": { + "lib": [ + "ESNext" + ], + "module": "esnext", + "target": "esnext", + "moduleResolution": "nodenext", + "strict": false, + "downlevelIteration": true, + "skipLibCheck": true, + "jsx": "preserve", + "allowSyntheticDefaultImports": true, + "forceConsistentCasingInFileNames": true, + "allowJs": true, + "types": [ + "bun-types" // add Bun global + ] + } +} \ No newline at end of file -- cgit v1.2.3