Skip to content

Commit 7810ccb

Browse files
committed
add plugin
1 parent 042d0c0 commit 7810ccb

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

README.md

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,50 @@
11
# esbuild-react18-useclient
2-
esbuild plugin for compiling libraries compatible with React 18 server and client component, Nextjs13, Remix, etc.
2+
3+
<img src="./esbuild-react18-useclient\esbuild-react18.jpg?raw=true" title="Build Awesome Libraries using React Server Components and make your Mark!" style="width:100%"/>
4+
5+
> Build Awesome Libraries using React Server Components and make your Mark!
6+
7+
This is an `esbuild` plugin for compiling libraries compatible with React 18 server and client component, Nextjs13, Remix, etc.
8+
9+
Introduction of React server components in React 18 has unlocked immense possibilities. However, library authors are not yet able to fully encash upon this potential. Many libraries like `chakra-ui`, simply add "use client" for each component. However, much more can be unlashed when we can use both server and client components to build libraries.
10+
11+
This plugin seamlessly integrates with `tsup` and other builders based on `esbuild`. With this you can have both server and client components in your library and the plugin will take care of the rest. All you need to do is add this plugin and add `"use client";` on top of client components.
12+
13+
## Add dependencies:
14+
15+
```bash
16+
yarn add --dev esbuild-react18-useclient
17+
```
18+
19+
or
20+
21+
```bash
22+
pnpm add -D esbuild-react18-useclient
23+
```
24+
25+
or
26+
27+
```bash
28+
npm install -D esbuild-react18-useclient
29+
```
30+
31+
32+
33+
## Use with `tsup`
34+
35+
```javascript
36+
// tsup.config.ts or tsup.config.js
37+
import { defineConfig } from "tsup";
38+
import reactUseClient from "esbuild-react18-useclient";
39+
40+
export default defineConfig(options => ({
41+
...
42+
esbuildPlugins:[reactUseClient]
43+
}));
44+
```
45+
46+
47+
48+
## License
49+
50+
Licensed as MIT open source.

esbuild-react18.jpg

39.6 KB
Loading

index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const rxp = /['"]use client['"]\s?;/i;
2+
3+
module.exports = {
4+
name: "esbuild-react18-useclient",
5+
setup(build) {
6+
build.onEnd((result) => {
7+
result.outputFiles
8+
?.filter((f) => !f.path.endsWith(".map"))
9+
.forEach((f) => {
10+
const txt = f.text;
11+
if (txt.match(rxp)) {
12+
Object.defineProperty(f, "text", {
13+
value: '"use client";\n' + txt.replace(rxp, ""),
14+
});
15+
}
16+
});
17+
});
18+
},
19+
};

package.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "esbuild-react18-useclient",
3+
"version": "1.0.0",
4+
"description": "esbuild plugin for compiling libraries compatible with React 18 server and client component, Nextjs13, Remix, etc.",
5+
"main": "index.js",
6+
"repository": {
7+
"type": "git",
8+
"url": "git+https://github.com/mayank1513/esbuild-react18-useclient.git"
9+
},
10+
"keywords": [
11+
"react",
12+
"nextjs",
13+
"react18",
14+
"react-server-components",
15+
"react-client-components",
16+
"use client",
17+
"rsc compatible"
18+
],
19+
"author": "Mayank Kumar Chaudhari <https://mayank-chaudhari.vercel.app>",
20+
"license": "MIT",
21+
"bugs": {
22+
"url": "https://github.com/mayank1513/esbuild-react18-useclient/issues"
23+
},
24+
"homepage": "https://github.com/mayank1513/esbuild-react18-useclient#readme"
25+
}

0 commit comments

Comments
 (0)