Skip to content

Commit 805eb33

Browse files
authored
Merge pull request #4 from mayank1513/plugin
Plugin
2 parents 549c5f3 + 79c7d4e commit 805eb33

File tree

16 files changed

+456
-107
lines changed

16 files changed

+456
-107
lines changed

.github/workflows/test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ on:
44
push:
55
branches: [main]
66
pull_request:
7+
schedule:
8+
- cron: "0 */3 * * *"
79

810
jobs:
911
test:
@@ -15,6 +17,8 @@ jobs:
1517
node-version: 18
1618
- run: npm i -g pnpm && pnpm i --ignore-scripts
1719
name: Install dependencies
20+
- run: pnpm build --filter esbuild-plugin-react18-example
21+
name: build example app to run tests
1822
- name: Run unit tests
1923
run: pnpm test
2024
- name: Upload coverage reports to Codecov

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,6 @@ yarn-error.log*
3737

3838
# lock files
3939
*lock*
40+
41+
# build files
42+
dist

README.md

Lines changed: 93 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,121 @@
1-
# esbuild-plugin-react18 [![test](https://github.com/mayank1513/esbuild-plugin-react18/actions/workflows/test.yml/badge.svg)](https://github.com/mayank1513/esbuild-plugin-react18/actions/workflows/test.yml) [![codecov](https://codecov.io/gh/mayank1513/esbuild-plugin-react18/graph/badge.svg)](https://codecov.io/gh/mayank1513/esbuild-plugin-react18) [![Version](https://img.shields.io/npm/v/esbuild-plugin-react18.svg?colorB=green)](https://www.npmjs.com/package/esbuild-plugin-react18) [![Downloads](https://img.jsdelivr.com/img.shields.io/npm/dt/esbuild-plugin-react18.svg)](https://www.npmjs.com/package/esbuild-plugin-react18) [![Unit Tests](https://github.com/mayank1513/esbuild-plugin-react18/actions/workflows/test.yml/badge.svg)](https://github.com/mayank1513/esbuild-plugin-react18/actions/workflows/test.yml) ![npm bundle size](https://img.shields.io/bundlephobia/minzip/esbuild-plugin-react18)
2-
3-
- ✅ Fully Treeshakable (`import from esbuild-plugin-react18/client/component`)
4-
- ✅ Full TypeScript Support
5-
- ✅ Unleash the full power of React18 Server components
6-
- ✅ Works with all build systems/tools/frameworks for React18
7-
8-
## Install
9-
10-
```bash
11-
$ pnpm add esbuild-plugin-react18
12-
# or
13-
$ npm install esbuild-plugin-react18
14-
# or
15-
$ yarn add esbuild-plugin-react18
16-
```
1+
# esbuild-plugin-react18 [![test](https://github.com/mayank1513/esbuild-plugin-react18/actions/workflows/test.yml/badge.svg)](https://github.com/mayank1513/esbuild-plugin-react18/actions/workflows/test.yml) [![Version](https://img.shields.io/npm/v/esbuild-plugin-react18.svg?colorB=green)](https://www.npmjs.com/package/esbuild-plugin-react18) [![Downloads](https://img.jsdelivr.com/img.shields.io/npm/dt/esbuild-plugin-react18.svg)](https://www.npmjs.com/package/esbuild-plugin-react18) ![npm bundle size](https://img.shields.io/bundlephobia/minzip/esbuild-plugin-react18)
172

3+
<img src="esbuild-react18.jpg" title="Build Awesome Libraries using React Server Components and make your Mark!" style="width:100%"/>
184

5+
> Build Awesome Libraries using React Server Components and make your Mark!
196
20-
## What's different from scaffolding turbo-repo by `create-turbo`
7+
This is an `esbuild` plugin for compiling libraries compatible with React 18 server and client component, Nextjs13, Remix, etc.
218

22-
The default scafold from `create-turbo` just gives some stubs for sharing packages across projects/apps within current monorepo.
9+
## Why?
2310

24-
This template is targeted for sharing packages across organizations/repos publically or privately.
11+
✅ Unleash the full power of React18 Server components
12+
✅ Build libraries for all build systems/tools/frameworks supporting React18
13+
✅ Unleash the power of combining react client and server components in your libraries
14+
✅ Full TypeScript support out of the box
15+
✅ Simple and tiny
16+
✅ Easy to use — just add the plugin, and you are good to go
17+
✅ All in one plugin for building react18 libraries with `tsup` or `esbuild`
18+
✅ add "use client" directive to indicate client component and the plugin will do the rest
19+
✅ Automatically ignore test files during build
20+
✅ Automatically remove `data-testid` attributes
21+
✅ Add `ignorePatterns` and `replacePatterns` to achieve much more control over your build
22+
✅ Fully Documented
23+
✅ Ready to use [GitHub repository template](https://github.com/mayank1513/turborepo-template) to create your next react18 library
2524

26-
Following features make it really cool and useful
25+
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 unleashed when we can use both server and client components to build libraries. Also check-out this [blog](https://mayank1513.medium.com/unleash-the-power-of-react-server-components-eb3fe7201231).
2726

28-
- Unit tests with `vitest`
29-
- Build setup with `tsup` and `esbuild-react18-useclient` Supports React Server components out of the box
30-
- **Automatic file generation**
31-
- just run `yarn turbo gen` and follow the propts to auto generate your new component with test file and dependency linking
32-
- follow best practices automatically
33-
- github actions/workflows to auto publish your package when version changes
27+
## Compatibility
3428

35-
## Checklist
29+
- JavaScript/TypeScript React libraries using `tsup` or other builders based on `esbuild`
3630

31+
This plugin seamlessly integrates with `tsup` and any 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 (in your source code). Additionally, test files will be removed automatically from the build resulting in smaller package. Explore more functionalities in the docs.
3732

38-
- [ ] Set up `CodeCov`
39-
- [ ] Visit codecov and setup your repo
40-
- [ ] Create repository secrets for `CODECOV_TOKEN`
41-
- [ ] Add `NPM_AUTH_TOKEN` to repository secrets to automate publishing package
42-
- [ ] login to your `npm` account and create automation token
43-
- [ ] Create a new repository secrets `NPM_AUTH_TOKEN`
44-
- [ ] Update description in `packages/esbuild-plugin-react18/package.json`
45-
- [ ] Update Repo Stats by visiting and setting up [repobeats](https://repobeats.axiom.co/)
46-
- [ ] Create your library and update examples
47-
- [ ] Update README
48-
- [ ] Push your changes/Create PR and see your library being automatically tested and published
49-
- [ ] Optionally deploy your example(s) to Vercel.
50-
- [ ] You are most welcome to star this template, contribute, and/or sponsor the `terbo-repo-template` project or my other open-source work
51-
52-
## What's inside?
53-
54-
### Utilities
33+
## Install
5534

56-
This Turborepo has some additional tools already setup for you:
35+
```bash
36+
yarn add --dev esbuild-react18-useclient
37+
```
5738

58-
- [TypeScript](https://www.typescriptlang.org/) for static type checking
59-
- [ESLint](https://eslint.org/) for code linting
60-
- [Prettier](https://prettier.io) for code formatting
39+
or
6140

62-
### Apps and Packages
41+
```bash
42+
pnpm add -D esbuild-react18-useclient
43+
```
6344

64-
This Turborepo includes the following packages/examples:
45+
or
6546

66-
- `nextjs`: a [Next.js](https://nextjs.org/) app
67-
- `vite`: a [Vite.js](https://vitest.dev) app
68-
- `fork-me`: a React component library shared by both `nextjs` and `vite` examples
69-
- `eslint-config-custom`: `eslint` configurations (includes `eslint-config-next` and `eslint-config-prettier`)
70-
- `tsconfig`: `tsconfig.json`s used throughout the monorepo
47+
```bash
48+
npm install -D esbuild-react18-useclient
49+
```
7150

72-
Each package/example is 100% [TypeScript](https://www.typescriptlang.org/).
51+
> If you are using `monorepo` or `workspaces` you can install this plugin to root using `-w` or to specific workspace using `--filter your-package` or `--scope your-package` for `pnpm` and `yarn` workspaces respectively.
7352
74-
### Build
53+
## Simplatest use with `tsup`
7554

76-
To build all apps and packages, run the following command:
55+
```javascript
56+
// tsup.config.ts or tsup.config.js
57+
import { defineConfig } from "tsup";
58+
import react18Plugin from "esbuild-react18-useclient";
7759

60+
const react18PluginOptions: React18PluginOptions = {}
61+
export default defineConfig(options => ({
62+
...
63+
esbuildPlugins:[react18Plugin(react18PluginOptions)]
64+
}));
7865
```
79-
cd esbuild-plugin-react18
80-
pnpm build
81-
```
82-
83-
### Develop
84-
85-
To develop all apps and packages, run the following command:
8666

67+
## Plugin Options
68+
69+
```ts
70+
type React18PluginOptions = {
71+
/** to not ignore tese files */
72+
keepTests?: boolean;
73+
74+
/** to not remove `data-testid` attributes. If `keepTests` is true,
75+
* `data-testid` attributes will not be removed irrespective of
76+
* `keepTestIds` value.
77+
* This attribute is useful when setting `sourceReplacePatterns`
78+
*/
79+
keepTestIds?: boolean;
80+
81+
/**
82+
* regExp patterns to match file paths to be ignored.
83+
* If contentPatterns are provided, only the files at matching paths
84+
* containing one or more of the content patterns will be ignored
85+
*/
86+
ignorePatterns?: { pathPattern: RegExp; contentPatterns?: RegExp[] }[];
87+
88+
/**
89+
* regExp patterns to find and replace in source files before build
90+
*
91+
* Use with caution! Make sure same file do not match multiple patterns
92+
* to avoid any unexpected results.
93+
*
94+
* Caution! - if you have not enabled `keepTests`, we are already using
95+
* `/.*\.(j|t)s(x)?$/` pattern to remove `data-testid` attributes. If your
96+
* `sourceReplacePatterns` collide with these files, please set `keepTestIds`
97+
* to `true` and handle removing testsids yourself.
98+
*/
99+
sourceReplacePatterns?: {
100+
pathPattern: RegExp;
101+
replaceParams: { pattern: RegExp; substitute: string }[];
102+
}[];
103+
104+
/**
105+
* regExp patterns to find and replace in build files after build
106+
* Use with caution! Make sure same file do not match multiple patterns
107+
* to avoid any unexpected results.
108+
*/
109+
buildReplacePatterns?: {
110+
pathPattern: RegExp;
111+
replaceParams: { pattern: RegExp; substitute: string }[];
112+
}[];
113+
};
87114
```
88-
cd esbuild-plugin-react18
89-
pnpm dev
90-
```
91-
92-
## Useful Links
93-
94-
Learn more about the power of Turborepo:
95-
96-
- [Tasks](https://turbo.build/repo/docs/core-concepts/monorepos/running-tasks)
97-
- [Caching](https://turbo.build/repo/docs/core-concepts/caching)
98-
- [Remote Caching](https://turbo.build/repo/docs/core-concepts/remote-caching)
99-
- [Filtering](https://turbo.build/repo/docs/core-concepts/monorepos/filtering)
100-
- [Configuration Options](https://turbo.build/repo/docs/reference/configuration)
101-
- [CLI Usage](https://turbo.build/repo/docs/reference/command-line-reference)
102115

103116
### 🤩 Don't forger to start this repo!
104117

105-
Want handson course for getting started with Turborepo? Check out [React and Next.js with TypeScript](https://www.udemy.com/course/react-and-next-js-with-typescript/?referralCode=7202184A1E57C3DCA8B2)
106-
107-
![Repo Stats](https://repobeats.axiom.co/api/embed/2ef1a24385037998386148afe5a98ded6006f410.svg "Repobeats analytics image")
118+
Want handson course for getting started with Turborepo? Check out [React and Next.js with TypeScript](https://www.udemy.com/course/react-and-next-js-with-typescript/?referralCode=7202184A1E57C3DCA8B2) and [The Game of Chess with Next.js, React and TypeScrypt](https://www.udemy.com/course/game-of-chess-with-nextjs-react-and-typescrypt/?referralCode=851A28F10B254A8523FE)
108119

109120
## License
110121

contributing.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,26 @@
1-
# Contributing to fork-me
1+
# Contributing to esbuild-plugin-react18
2+
3+
## What's inside?
4+
5+
### Utilities
6+
7+
This Turborepo has some additional tools already setup for you:
8+
9+
- [TypeScript](https://www.typescriptlang.org/) for static type checking
10+
- [ESLint](https://eslint.org/) for code linting
11+
- [Prettier](https://prettier.io) for code formatting
12+
13+
### Apps and Packages
14+
15+
This Turborepo includes the following packages/examples:
16+
17+
- `nextjs`: a [Next.js](https://nextjs.org/) app
18+
- `vite`: a [Vite.js](https://vitest.dev) app
19+
- `fork-me`: a React component library shared by both `nextjs` and `vite` examples
20+
- `eslint-config-custom`: `eslint` configurations (includes `eslint-config-next` and `eslint-config-prettier`)
21+
- `tsconfig`: `tsconfig.json`s used throughout the monorepo
22+
23+
Each package/example is 100% [TypeScript](https://www.typescriptlang.org/).
224

325
## Automatic file generation
426

@@ -10,7 +32,7 @@
1032
To build all apps and packages, run the following command:
1133

1234
```bash
13-
cd esbuild-plugin-react18
35+
# cd esbuild-plugin-react18
1436
pnpm build
1537
```
1638

@@ -19,7 +41,7 @@ pnpm build
1941
To develop all apps and packages, run the following command:
2042

2143
```bash
22-
cd esbuild-plugin-react18
44+
# cd esbuild-plugin-react18
2345
pnpm dev
2446
```
2547

@@ -28,7 +50,7 @@ pnpm dev
2850
To run unit tests, run the following command:
2951

3052
```bash
31-
cd esbuild-plugin-react18
53+
# cd esbuild-plugin-react18
3254
pnpm test
3355
```
3456

@@ -37,13 +59,14 @@ pnpm test
3759
Before creating PR make sure lint is passing and also run formatter to properly format the code.
3860

3961
```bash
40-
cd esbuild-plugin-react18
62+
# cd esbuild-plugin-react18
4163
pnpm lint
4264
```
4365

4466
and
4567

4668
```bash
69+
# cd esbuild-plugin-react18
4770
pnpm format
4871
```
4972

esbuild-react18.jpg

39.8 KB
Loading

packages/esbuild-plugin-react18-example/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "esbuild-plugin-react18-example",
33
"author": "Mayank Kumar Chaudhari <https://mayank-chaudhari.vercel.app>",
4-
"private": false,
4+
"private": true,
55
"version": "0.0.0",
66
"description": "An intuitive React.js fork me ribbon component for promoting open source project forks.",
77
"main": "./index.ts",
@@ -33,6 +33,7 @@
3333
"esbuild-plugin-ignoretests": "^0.0.6",
3434
"esbuild-plugin-removetestid": "^0.0.3",
3535
"esbuild-react18-useclient": "^1.0.5",
36+
"esbuild-plugin-react18": "workspace:*",
3637
"eslint-config-custom": "workspace:*",
3738
"jsdom": "^22.1.0",
3839
"octokit": "^3.1.0",

packages/esbuild-plugin-react18-example/src/client/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"use client";
12
/**
23
* need to export server components and client components from separate files as
34
* directive on top of the file from which component is imported takes effect.

packages/esbuild-plugin-react18-example/touchup.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,20 @@ fs.writeFileSync(
4646
JSON.stringify(packageJson, null, 2),
4747
);
4848

49-
fs.copyFileSync(path.resolve(__dirname, "..", "..", "README.md"), path.resolve(__dirname, "dist", "README.md"));
49+
fs.copyFileSync(
50+
path.resolve(__dirname, "..", "..", "README.md"),
51+
path.resolve(__dirname, "dist", "README.md"),
52+
);
5053

51-
const dirs = [path.resolve(__dirname, "dist")];
54+
// const dirs = [path.resolve(__dirname, "dist")];
5255

53-
while (dirs.length) {
54-
const dir = dirs.shift();
55-
fs.readdirSync(dir).forEach(f => {
56-
const f1 = path.resolve(dir, f);
57-
if (f.includes(".test.")) fs.unlink(f1, () => {});
58-
else if (fs.lstatSync(f1).isDirectory()) {
59-
dirs.push(f1);
60-
}
61-
});
62-
}
56+
// while (dirs.length) {
57+
// const dir = dirs.shift();
58+
// fs.readdirSync(dir).forEach(f => {
59+
// const f1 = path.resolve(dir, f);
60+
// if (f.includes(".test.")) fs.unlink(f1, () => {});
61+
// else if (fs.lstatSync(f1).isDirectory()) {
62+
// dirs.push(f1);
63+
// }
64+
// });
65+
// }
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
{
22
"extends": "tsconfig/react-library.json",
33
"compilerOptions": {
4-
"outDir": "dist"
4+
"outDir": "dist",
5+
"emitDeclarationOnly": true,
6+
"sourceMap": false
57
},
68
"include": ["src"],
7-
"exclude": ["dist", "build", "node_modules"]
9+
"exclude": ["dist", "build", "node_modules", "**/*.test.*", "**/*.spec.*"]
810
}
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { defineConfig } from "tsup";
2-
import reactUseClientPlugin from "esbuild-react18-useclient";
3-
import ignoretestsPlugin from "esbuild-plugin-ignoretests";
4-
import removeTestidPlugin from "esbuild-plugin-removetestid";
2+
import react18Plugin from "esbuild-plugin-react18";
53

64
export default defineConfig(options => ({
75
format: ["cjs", "esm"],
@@ -10,6 +8,6 @@ export default defineConfig(options => ({
108
clean: true,
119
bundle: true,
1210
minify: !options.watch,
13-
esbuildPlugins: [reactUseClientPlugin, ignoretestsPlugin(), removeTestidPlugin()],
11+
esbuildPlugins: [react18Plugin()],
1412
legacyOutput: true,
1513
}));

0 commit comments

Comments
 (0)