Commit 12a0f23
authored
feat: Unbundle react (#653)
### Problem
When `rwsdk` is installed as a dependency, two issues arise:
1. **Dependency Resolution:** The Vite build process resolved React packages (`react`, `react-dom`, `react-server-dom-webpack`) relative to the `rwsdk` package's location, not the user's project. This blocks users from getting new features and fixes for react deps
2. **Plugin Conflicts:** The SDK's Vite plugin unconditionally included `@vitejs/plugin-react`. This blocks users from configuring vite react plugin their own way (e.g. to enable react compiler).
### Solution
This PR addresses these issues with the following changes:
1. **Prioritize User Project for React Dependencies**
* `react`, `react-dom`, and `react-server-dom-webpack` are now defined as optional `peerDependencies`. This allows users to add their own React dependencies rather than relying on the specific versions pinned in the SDK.
* These packages are also kept in the SDK's `dependencies` to ensure backward compatibility and to provide a fallback.
* We first attempt resolving these packages from the user's project root. It only falls back to the versions bundled with the SDK if they are not found in the host project.
* The resolution logic within the plugin has been centralized into a single function, `resolveReactImport`, to reduce code duplication and improve maintainability.
* The starter templates (`minimal` and `standard`) have been updated to include these React packages as direct dependencies, ensuring new projects work correctly out of the box.
2. **Conditional Vite React Plugin**
* We now check the host project's `package.json` to determine if `@vitejs/plugin-react` is already listed as a dependency.
* If the plugin is found, the SDK will not add it again, preventing configuration conflicts. This behavior is consistent with how the Cloudflare plugin is managed.
* A manual override, `includeReactPlugin`, has been added to the plugin options for cases where the default behavior needs to be changed.
## Tests done
- [x] pnpm
- [x] yarn
- [x] npm
- [x] usage of new react features (e.g. react compiler)
For each of the package manager tests done, checked:
* login in standard starter in dev and production
* grepped in `node_modules/.vite` to confirm the project's react deps were used only and not the fallback deps we have in the sdk
## Links
* Experiment project using react-compiler: https://github.com/redwoodjs/sdk-experiments/tree/main/react-compiler
* Fixes #6231 parent b0eaf64 commit 12a0f23
4 files changed
Lines changed: 183 additions & 70 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
157 | 157 | | |
158 | 158 | | |
159 | 159 | | |
160 | | - | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
161 | 175 | | |
162 | 176 | | |
163 | 177 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
10 | 12 | | |
11 | 13 | | |
12 | 14 | | |
| |||
49 | 51 | | |
50 | 52 | | |
51 | 53 | | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
62 | 68 | | |
63 | | - | |
64 | | - | |
| 69 | + | |
65 | 70 | | |
66 | | - | |
| 71 | + | |
| 72 | + | |
67 | 73 | | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
72 | 87 | | |
73 | | - | |
| 88 | + | |
74 | 89 | | |
75 | 90 | | |
76 | | - | |
77 | | - | |
| 91 | + | |
| 92 | + | |
78 | 93 | | |
79 | | - | |
| 94 | + | |
80 | 95 | | |
81 | 96 | | |
82 | 97 | | |
83 | | - | |
| 98 | + | |
84 | 99 | | |
| 100 | + | |
85 | 101 | | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
86 | 122 | | |
87 | 123 | | |
88 | 124 | | |
| |||
97 | 133 | | |
98 | 134 | | |
99 | 135 | | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
106 | 143 | | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
118 | 153 | | |
119 | | - | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
120 | 161 | | |
121 | | - | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
122 | 166 | | |
123 | 167 | | |
124 | | - | |
| 168 | + | |
125 | 169 | | |
126 | | - | |
127 | 170 | | |
| 171 | + | |
128 | 172 | | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
139 | 178 | | |
140 | | - | |
| 179 | + | |
| 180 | + | |
141 | 181 | | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
| 182 | + | |
147 | 183 | | |
148 | | - | |
149 | | - | |
150 | | - | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
151 | 210 | | |
152 | 211 | | |
153 | 212 | | |
| |||
178 | 237 | | |
179 | 238 | | |
180 | 239 | | |
181 | | - | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
182 | 244 | | |
183 | 245 | | |
184 | 246 | | |
| |||
200 | 262 | | |
201 | 263 | | |
202 | 264 | | |
203 | | - | |
| 265 | + | |
204 | 266 | | |
205 | 267 | | |
206 | 268 | | |
| |||
211 | 273 | | |
212 | 274 | | |
213 | 275 | | |
214 | | - | |
| 276 | + | |
215 | 277 | | |
216 | 278 | | |
217 | 279 | | |
| |||
242 | 304 | | |
243 | 305 | | |
244 | 306 | | |
245 | | - | |
| 307 | + | |
| 308 | + | |
246 | 309 | | |
247 | 310 | | |
248 | 311 | | |
249 | 312 | | |
250 | 313 | | |
251 | 314 | | |
252 | 315 | | |
253 | | - | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
254 | 327 | | |
255 | 328 | | |
256 | 329 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
| 36 | + | |
35 | 37 | | |
36 | 38 | | |
37 | 39 | | |
| |||
80 | 82 | | |
81 | 83 | | |
82 | 84 | | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
83 | 89 | | |
84 | 90 | | |
85 | 91 | | |
| |||
112 | 118 | | |
113 | 119 | | |
114 | 120 | | |
115 | | - | |
| 121 | + | |
116 | 122 | | |
117 | 123 | | |
118 | 124 | | |
| |||
127 | 133 | | |
128 | 134 | | |
129 | 135 | | |
130 | | - | |
| 136 | + | |
131 | 137 | | |
132 | 138 | | |
133 | 139 | | |
| |||
0 commit comments