Skip to content

Commit

Permalink
chore(examples): add React and Node.js examples
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Aug 2, 2024
1 parent 92a053b commit 56322d8
Show file tree
Hide file tree
Showing 18 changed files with 840 additions and 15 deletions.
10 changes: 0 additions & 10 deletions examples/basic/package.json

This file was deleted.

2 changes: 0 additions & 2 deletions examples/basic/src/constants.ts

This file was deleted.

3 changes: 0 additions & 3 deletions examples/basic/src/index.ts

This file was deleted.

3 changes: 3 additions & 0 deletions examples/express-plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @examples/express-plugin

This example demonstrates how to use Rslib to build a simple Node.js package.
18 changes: 18 additions & 0 deletions examples/express-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "@examples/express-plugin",
"private": true,
"scripts": {
"build": "rslib build"
},
"dependencies": {
"@types/express": "^4.17.21"
},
"devDependencies": {
"@rslib/core": "workspace:*",
"express": "^4.19.2",
"typescript": "^5.5.3"
},
"peerDependencies": {
"express": "^4"
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { defineConfig } from '@rslib/core';

const shared = {
autoExtension: true,
dts: {
bundle: true,
},
};

export default defineConfig({
lib: [
{
...shared,
format: 'esm',
output: {
distPath: {
Expand All @@ -11,6 +19,7 @@ export default defineConfig({
},
},
{
...shared,
format: 'cjs',
output: {
distPath: {
Expand All @@ -24,4 +33,10 @@ export default defineConfig({
main: './src/index.ts',
},
},
output: {
externals: {
express: 'express',
},
target: 'node',
},
});
15 changes: 15 additions & 0 deletions examples/express-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { RequestHandler } from 'express';
import { logRequest } from './logger';
import { type LoggerOptions, defaultOptions } from './types';

export function expressLogger(
options: Partial<LoggerOptions> = {},
): RequestHandler {
const mergedOptions = { ...defaultOptions, ...options };
return (req, res, next) => {
logRequest(req, mergedOptions);
next();
};
}

export * from './types';
25 changes: 25 additions & 0 deletions examples/express-plugin/src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { Request } from 'express';
import type { LoggerOptions } from './types';

export function logRequest(req: Request, options: LoggerOptions): void {
const { method, url } = req;
const logMessage = `${method} ${url}`;

switch (options.logLevel) {
case 'debug':
console.debug(logMessage);
break;
case 'warn':
console.warn(logMessage);
break;
case 'error':
console.error(logMessage);
break;
default:
console.log(logMessage);
}

if (options.logBody && req.body) {
console.log('Request body:', req.body);
}
}
9 changes: 9 additions & 0 deletions examples/express-plugin/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface LoggerOptions {
logLevel: 'info' | 'debug' | 'warn' | 'error';
logBody: boolean;
}

export const defaultOptions: LoggerOptions = {
logLevel: 'info',
logBody: false,
};
6 changes: 6 additions & 0 deletions examples/express-plugin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"strict": true
},
"include": ["src/**/*"]
}
3 changes: 3 additions & 0 deletions examples/react-component/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @examples/react-component

This example demonstrates how to use Rslib to build a simple React component.
16 changes: 16 additions & 0 deletions examples/react-component/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@examples/react-component",
"private": true,
"scripts": {
"build": "rslib build"
},
"devDependencies": {
"@rsbuild/plugin-react": "1.0.1-beta.9",
"@rslib/core": "workspace:*",
"@types/react": "^18.3.3",
"react": "^18.3.1"
},
"peerDependencies": {
"react": "*"
}
}
44 changes: 44 additions & 0 deletions examples/react-component/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { pluginReact } from '@rsbuild/plugin-react';
import { defineConfig } from '@rslib/core';

const shared = {
autoExtension: true,
dts: {
bundle: true,
},
};

export default defineConfig({
lib: [
{
...shared,
format: 'esm',
output: {
distPath: {
root: './dist/esm',
},
},
},
{
...shared,
format: 'cjs',
output: {
distPath: {
root: './dist/cjs',
},
},
},
],
source: {
entry: {
main: './src/index.tsx',
},
},
output: {
externals: {
react: 'react',
'react/jsx-runtime': 'react/jsx-runtime',
},
},
plugins: [pluginReact()],
});
13 changes: 13 additions & 0 deletions examples/react-component/src/CounterButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
interface CounterButtonProps {
onClick: () => void;
label: string;
}

export const CounterButton: React.FC<CounterButtonProps> = ({
onClick,
label,
}) => (
<button type="button" onClick={onClick}>
{label}
</button>
);
14 changes: 14 additions & 0 deletions examples/react-component/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { CounterButton } from './CounterButton';
import { useCounter } from './useCounter';

export const Counter: React.FC = () => {
const { count, increment, decrement } = useCounter();

return (
<div>
<h2>Counter: {count}</h2>
<CounterButton onClick={decrement} label="-" />
<CounterButton onClick={increment} label="+" />
</div>
);
};
10 changes: 10 additions & 0 deletions examples/react-component/src/useCounter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { useState } from 'react';

export const useCounter = (initialValue = 0) => {
const [count, setCount] = useState(initialValue);

const increment = () => setCount((prev) => prev + 1);
const decrement = () => setCount((prev) => prev - 1);

return { count, increment, decrement };
};
7 changes: 7 additions & 0 deletions examples/react-component/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"jsx": "react-jsx",
"strict": true
},
"include": ["src/**/*"]
}
Loading

0 comments on commit 56322d8

Please sign in to comment.