|
1 | | -# esbuild-plugin-react18 [](https://github.com/mayank1513/esbuild-plugin-react18/actions/workflows/test.yml) [](https://codecov.io/gh/mayank1513/esbuild-plugin-react18) [](https://www.npmjs.com/package/esbuild-plugin-react18) [](https://www.npmjs.com/package/esbuild-plugin-react18) [](https://github.com/mayank1513/esbuild-plugin-react18/actions/workflows/test.yml)  |
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 [](https://github.com/mayank1513/esbuild-plugin-react18/actions/workflows/test.yml) [](https://www.npmjs.com/package/esbuild-plugin-react18) [](https://www.npmjs.com/package/esbuild-plugin-react18)  |
17 | 2 |
|
| 3 | +<img src="esbuild-react18.jpg" title="Build Awesome Libraries using React Server Components and make your Mark!" style="width:100%"/> |
18 | 4 |
|
| 5 | +> Build Awesome Libraries using React Server Components and make your Mark! |
19 | 6 |
|
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. |
21 | 8 |
|
22 | | -The default scafold from `create-turbo` just gives some stubs for sharing packages across projects/apps within current monorepo. |
| 9 | +## Why? |
23 | 10 |
|
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 |
25 | 24 |
|
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). |
27 | 26 |
|
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 |
34 | 28 |
|
35 | | -## Checklist |
| 29 | +- JavaScript/TypeScript React libraries using `tsup` or other builders based on `esbuild` |
36 | 30 |
|
| 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. |
37 | 32 |
|
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 |
55 | 34 |
|
56 | | -This Turborepo has some additional tools already setup for you: |
| 35 | +```bash |
| 36 | +yarn add --dev esbuild-react18-useclient |
| 37 | +``` |
57 | 38 |
|
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 |
61 | 40 |
|
62 | | -### Apps and Packages |
| 41 | +```bash |
| 42 | +pnpm add -D esbuild-react18-useclient |
| 43 | +``` |
63 | 44 |
|
64 | | -This Turborepo includes the following packages/examples: |
| 45 | +or |
65 | 46 |
|
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 | +``` |
71 | 50 |
|
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. |
73 | 52 |
|
74 | | -### Build |
| 53 | +## Simplatest use with `tsup` |
75 | 54 |
|
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"; |
77 | 59 |
|
| 60 | +const react18PluginOptions: React18PluginOptions = {} |
| 61 | +export default defineConfig(options => ({ |
| 62 | + ... |
| 63 | + esbuildPlugins:[react18Plugin(react18PluginOptions)] |
| 64 | +})); |
78 | 65 | ``` |
79 | | -cd esbuild-plugin-react18 |
80 | | -pnpm build |
81 | | -``` |
82 | | - |
83 | | -### Develop |
84 | | - |
85 | | -To develop all apps and packages, run the following command: |
86 | 66 |
|
| 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 | +}; |
87 | 114 | ``` |
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) |
102 | 115 |
|
103 | 116 | ### 🤩 Don't forger to start this repo! |
104 | 117 |
|
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 | | - |
| 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) |
108 | 119 |
|
109 | 120 | ## License |
110 | 121 |
|
|
0 commit comments