-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(examples): add React and Node.js examples
- Loading branch information
Showing
18 changed files
with
840 additions
and
15 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"compilerOptions": { | ||
"strict": true | ||
}, | ||
"include": ["src/**/*"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": "*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"compilerOptions": { | ||
"jsx": "react-jsx", | ||
"strict": true | ||
}, | ||
"include": ["src/**/*"] | ||
} |
Oops, something went wrong.