|
1 | | -# rsbuild-plugin-workspace-dev |
| 1 | +# rsbuild-plugin-workspace-dev |
| 2 | + |
| 3 | +Start monorepo sub-projects in topological order. |
| 4 | + |
| 5 | +`rsbuild-plugin-workspace-dev` is designed for monorepo development. It computes the dependency graph starting from the current project and starts sub-projects in topological order. |
| 6 | + |
| 7 | +<p> |
| 8 | + <a href="https://npmjs.com/package/rsbuild-plugin-workspace-dev"> |
| 9 | + <img src="https://img.shields.io/npm/v/rsbuild-plugin-workspace-dev?style=flat-square&colorA=564341&colorB=EDED91" alt="npm version" /> |
| 10 | + </a> |
| 11 | + <img src="https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="license" /> |
| 12 | +</p> |
| 13 | + |
| 14 | +English | [简体中文](./README.zh-CN.md) |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +Install: |
| 19 | + |
| 20 | +```bash |
| 21 | +pnpm add rsbuild-plugin-workspace-dev -D |
| 22 | +``` |
| 23 | + |
| 24 | +Register the plugin in `rsbuild.config.ts`: |
| 25 | + |
| 26 | +```ts |
| 27 | +// rsbuild.config.ts |
| 28 | +import { pluginWorkspaceDev } from "rsbuild-plugin-workspace-dev"; |
| 29 | + |
| 30 | +export default { |
| 31 | + plugins: [pluginWorkspaceDev()], |
| 32 | +}; |
| 33 | +``` |
| 34 | + |
| 35 | +## Use Cases |
| 36 | + |
| 37 | +In a monorepo, one project may depend on multiple sub-projects, and those sub-projects can also depend on each other. |
| 38 | + |
| 39 | +For example, the monorepo contains an app and several lib packages: |
| 40 | + |
| 41 | +```ts |
| 42 | +monorepo |
| 43 | +├── app |
| 44 | +└── lib1 |
| 45 | +└── lib2 |
| 46 | +└── lib3 |
| 47 | +``` |
| 48 | + |
| 49 | +Here, app is built with Rsbuild, and lib is built with Rslib. The app depends on lib1 and lib2: |
| 50 | + |
| 51 | +```json |
| 52 | +{ |
| 53 | + "name": "app", |
| 54 | + "dependencies": { |
| 55 | + "lib1": "workspace:*", |
| 56 | + "lib2": "workspace:*" |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +`lib2` depends on `lib3`: |
| 62 | + |
| 63 | +```json |
| 64 | +{ |
| 65 | + "name": "lib2", |
| 66 | + "dependencies": { |
| 67 | + "lib3": "workspace:*" |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +When you run `pnpm dev` under app, sub-projects start in topological order: first lib1 and lib3, then lib2, and finally app. Starting a lib refers to running its dev command, for example: |
| 73 | + |
| 74 | +```json |
| 75 | +{ |
| 76 | + "scripts": { |
| 77 | + "dev": "rslib build --watch" |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +Whether a sub-project has finished starting is determined by matching sub-project logs. By default, logs from Rslib and tsup sub-projects are recognized. You can also provide a custom match function to determine when a sub-project is ready. |
| 83 | + |
| 84 | +## Options |
| 85 | + |
| 86 | +### projectConfig |
| 87 | +Configure how sub-projects are started and define custom log matching logic. |
| 88 | + |
| 89 | +- Type: |
| 90 | +``` |
| 91 | +interface ProjectConfig { |
| 92 | + /** |
| 93 | + * Custom sub-project start command. Default is `dev` (runs `npm run dev`). |
| 94 | + */ |
| 95 | + command?: string; |
| 96 | + /** |
| 97 | + * Custom logic to detect when a sub-project has started. |
| 98 | + * By default, logs from `Rsbuild` and `tsup` are supported. |
| 99 | + */ |
| 100 | + match?: (stdout: string) => boolean; |
| 101 | + /** |
| 102 | + * Whether to skip starting the current sub-project. Default is `false`. |
| 103 | + * Useful for sub-projects that do not need to be started. |
| 104 | + */ |
| 105 | + skip?: boolean; |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +### ignoreSelf |
| 110 | + |
| 111 | +- Type: `boolean` |
| 112 | +- Default: `true` |
| 113 | + |
| 114 | +Whether to ignore starting the current project. The default is `true`. In most cases, you start the current project manually, so the plugin does not interfere. |
| 115 | + |
| 116 | +Consider a scenario where docs and lib are in the same project, and docs needs to debug the output of lib. In this case, you want to run `pnpm doc` for the docs, while lib should run `pnpm dev`. After configuring this option in your Rspress config, starting `pnpm doc` will automatically run `pnpm dev` to start the lib sub-project. |
| 117 | + |
| 118 | +``` |
| 119 | +├── docs |
| 120 | +│ └── index.mdx |
| 121 | +├── package.json |
| 122 | +├── src |
| 123 | +│ └── Button.tsx |
| 124 | +├── rslib.config.ts |
| 125 | +├── rspress.config.ts |
| 126 | +``` |
| 127 | + |
| 128 | +``` |
| 129 | +"scripts": { |
| 130 | + "dev": "rslib build --watch", |
| 131 | + "doc": "rspress dev" |
| 132 | +}, |
| 133 | +``` |
| 134 | + |
| 135 | +### cwd |
| 136 | + |
| 137 | +- Type: `string` |
| 138 | +- Default: `process.cwd()` |
| 139 | + |
| 140 | +Set the current working directory. The default is the current project directory; usually no configuration is needed. |
| 141 | + |
| 142 | +### workspaceFileDir |
| 143 | + |
| 144 | +- Type: `string` |
| 145 | +- Default: `process.cwd()` |
| 146 | + |
| 147 | +Set the directory where the workspace file resides. The default is the current project directory; usually no configuration is needed. |
| 148 | + |
| 149 | +## Frequently Asked Questions |
| 150 | + |
| 151 | +### Project startup stuck |
| 152 | +Stuck may be due to slow sub-project builds, etc. The lack of log output is because, by default, sub-project logs are output all at once after startup (to avoid interleaving sub-project logs). You can enable debug mode by adding an environment variable, which will allow sub-project logs to be output in real time. |
| 153 | + |
| 154 | +``` |
| 155 | +DEBUG=rsbuild pnpm dev |
| 156 | +``` |
| 157 | + |
| 158 | +### Some projects don't need to start |
| 159 | + |
| 160 | +If some sub-projects don't need to start, simply configure `skip: true` for the specified project in `rsbuild.config.ts`. |
| 161 | + |
| 162 | +```ts |
| 163 | +// rsbuild.config.ts |
| 164 | +import { pluginWorkspaceDev } from "rsbuild-plugin-workspace-dev"; |
| 165 | + |
| 166 | +export default { |
| 167 | + plugins: [ |
| 168 | + pluginWorkspaceDev({ |
| 169 | + projectConfig: { |
| 170 | + lib1: { |
| 171 | + skip: true, |
| 172 | + }, |
| 173 | + }, |
| 174 | + }), |
| 175 | + ], |
| 176 | +}; |
| 177 | +``` |
| 178 | + |
| 179 | +## License |
| 180 | + |
| 181 | +[MIT](./LICENSE). |
0 commit comments