Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(examples): add React and Node.js examples #53

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
17 changes: 17 additions & 0 deletions examples/express-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "@examples/express-plugin",
"private": true,
"scripts": {
"build": "rslib build"
},
"dependencies": {},
"devDependencies": {
"@rslib/core": "workspace:*",
"@types/express": "^4.17.21",
"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: false,
},
};

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',
},
fi3ework marked this conversation as resolved.
Show resolved Hide resolved
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: false,
},
};

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
Loading