Skip to content

Commit 0044ff1

Browse files
ScriptedAlchemychenjiahan
authored andcommitted
chore: add examples
1 parent a819177 commit 0044ff1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+13307
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.idea
2+
node_modules
3+
*.tsbuildinfo
4+
dist
5+
.npmrc

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023-present Bytedance, Inc. and its affiliates.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

config/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "@rsbuild/config",
3+
"version": "1.0.1",
4+
"private": true,
5+
"devDependencies": {
6+
"@rsbuild/core": "1.1.0",
7+
"@rslib/core": "0.1.3",
8+
"@types/node": "^22.10.1",
9+
"typescript": "^5.7.2"
10+
}
11+
}

config/rslib.config.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import type { Minify, RsbuildPlugin } from '@rsbuild/core';
4+
import { type LibConfig, defineConfig } from '@rslib/core';
5+
6+
export const commonExternals: Array<string | RegExp> = [
7+
'webpack',
8+
/[\\/]compiled[\\/]/,
9+
];
10+
11+
export const nodeMinifyConfig: Minify = {
12+
js: true,
13+
css: false,
14+
jsOptions: {
15+
minimizerOptions: {
16+
// preserve variable name and disable minify for easier debugging
17+
mangle: false,
18+
minify: false,
19+
compress: false,
20+
},
21+
},
22+
};
23+
24+
// Clean tsc cache to ensure the dts files can be generated correctly
25+
export const pluginCleanTscCache: RsbuildPlugin = {
26+
name: 'plugin-clean-tsc-cache',
27+
setup(api) {
28+
api.onBeforeBuild(() => {
29+
const tsbuildinfo = path.join(
30+
api.context.rootPath,
31+
'tsconfig.tsbuildinfo',
32+
);
33+
if (fs.existsSync(tsbuildinfo)) {
34+
fs.rmSync(tsbuildinfo);
35+
}
36+
});
37+
},
38+
};
39+
40+
export const esmConfig: LibConfig = {
41+
format: 'esm',
42+
syntax: 'es2021',
43+
dts: {
44+
build: true,
45+
},
46+
plugins: [pluginCleanTscCache],
47+
output: {
48+
minify: nodeMinifyConfig,
49+
},
50+
};
51+
52+
export const cjsConfig: LibConfig = {
53+
format: 'cjs',
54+
syntax: 'es2021',
55+
output: {
56+
minify: nodeMinifyConfig,
57+
},
58+
};
59+
60+
export const dualPackage = defineConfig({
61+
lib: [esmConfig, cjsConfig],
62+
tools: {
63+
rspack: {
64+
externals: commonExternals,
65+
},
66+
},
67+
});

config/tsconfig-node16.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"module": "Node16",
5+
"moduleResolution": "Node16"
6+
}
7+
}

config/tsconfig.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"compilerOptions": {
3+
"lib": ["DOM", "ESNext"],
4+
"jsx": "preserve",
5+
"target": "ES2021",
6+
"skipLibCheck": true,
7+
"useDefineForClassFields": true,
8+
9+
/* modules */
10+
"module": "ES2020",
11+
"esModuleInterop": true,
12+
"isolatedModules": true,
13+
"moduleResolution": "Bundler",
14+
"resolveJsonModule": true,
15+
16+
/* type checking */
17+
"strict": true,
18+
"noUnusedLocals": true,
19+
"noUnusedParameters": true
20+
},
21+
"$schema": "https://json.schemastore.org/tsconfig",
22+
"display": "Base"
23+
}

examples/cloudflare/.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.DS_Store
2+
/node_modules/
3+
*.tsbuildinfo
4+
5+
# React Router
6+
/.react-router/
7+
/build/
8+
9+
# Cloudflare
10+
.mf
11+
.wrangler

examples/cloudflare/README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Welcome to React Router!
2+
3+
A modern, production-ready template for building full-stack React applications using React Router.
4+
5+
## Features
6+
7+
- 🚀 Server-side rendering
8+
- ⚡️ Hot Module Replacement (HMR)
9+
- 📦 Asset bundling and optimization
10+
- 🔄 Data loading and mutations
11+
- 🔒 TypeScript by default
12+
- 🎉 TailwindCSS for styling
13+
- 📖 [React Router docs](https://reactrouter.com/)
14+
15+
## Getting Started
16+
17+
### Installation
18+
19+
Install the dependencies:
20+
21+
```bash
22+
npm install
23+
```
24+
25+
### Development
26+
27+
Start the development server with HMR:
28+
29+
```bash
30+
npm run dev
31+
```
32+
33+
Your application will be available at `http://localhost:5173`.
34+
35+
## Building for Production
36+
37+
Create a production build:
38+
39+
```bash
40+
npm run build
41+
```
42+
43+
## Deployment
44+
45+
Deployment is done using the Wrangler CLI.
46+
47+
To build and deploy directly to production:
48+
49+
```sh
50+
npm run deploy
51+
```
52+
53+
To deploy a preview URL:
54+
55+
```sh
56+
npx wrangler versions upload
57+
```
58+
59+
You can then promote a version to production after verification or roll it out progressively.
60+
61+
```sh
62+
npx wrangler versions deploy
63+
```
64+
65+
## Styling
66+
67+
This template comes with [Tailwind CSS](https://tailwindcss.com/) already configured for a simple default starting experience. You can use whatever CSS framework you prefer.
68+
69+
---
70+
71+
Built with ❤️ using React Router.

examples/cloudflare/app/app.css

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@import 'tailwindcss';
2+
3+
@theme {
4+
--font-sans: 'Inter', ui-sans-serif, system-ui, sans-serif,
5+
'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
6+
}
7+
8+
html,
9+
body {
10+
@apply bg-white dark:bg-gray-950;
11+
12+
@media (prefers-color-scheme: dark) {
13+
color-scheme: dark;
14+
}
15+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { isbot } from 'isbot';
2+
import { renderToReadableStream } from 'react-dom/server';
3+
import type { AppLoadContext, EntryContext } from 'react-router';
4+
import { ServerRouter } from 'react-router';
5+
6+
export default async function handleRequest(
7+
request: Request,
8+
responseStatusCode: number,
9+
responseHeaders: Headers,
10+
routerContext: EntryContext,
11+
_loadContext: AppLoadContext,
12+
) {
13+
let shellRendered = false;
14+
const userAgent = request.headers.get('user-agent');
15+
16+
const body = await renderToReadableStream(
17+
<ServerRouter context={routerContext} url={request.url} />,
18+
{
19+
onError(error: unknown) {
20+
// biome-ignore lint: intentional parameter reassignment
21+
responseStatusCode = 500;
22+
// Log streaming rendering errors from inside the shell. Don't log
23+
// errors encountered during initial shell rendering since they'll
24+
// reject and get logged in handleDocumentRequest.
25+
if (shellRendered) {
26+
console.error(error);
27+
}
28+
},
29+
},
30+
);
31+
shellRendered = true;
32+
33+
// Ensure requests from bots and SPA Mode renders wait for all content to load before responding
34+
// https://react.dev/reference/react-dom/server/renderToPipeableStream#waiting-for-all-content-to-load-for-crawlers-and-static-generation
35+
if ((userAgent && isbot(userAgent)) || routerContext.isSpaMode) {
36+
await body.allReady;
37+
}
38+
39+
responseHeaders.set('Content-Type', 'text/html');
40+
return new Response(body, {
41+
headers: responseHeaders,
42+
status: responseStatusCode,
43+
});
44+
}

0 commit comments

Comments
 (0)