|
| 1 | +--- |
| 2 | +title: Adopting the Vite Plugin |
| 3 | +--- |
| 4 | + |
| 5 | +# Adopting the Vite Plugin |
| 6 | + |
| 7 | +## Bundler Plugins |
| 8 | + |
| 9 | +The rest of this document is optional. It describes how to incrementally take advantage of the new features in v7 including |
| 10 | + |
| 11 | +- route modules |
| 12 | +- automatic code-splitting |
| 13 | +- static pre-rendering |
| 14 | +- server rendering |
| 15 | +- React Server Components |
| 16 | + |
| 17 | +We encourage you to take these steps to set your app up for future React features with React Server Components and Server Actions. |
| 18 | + |
| 19 | +### Vite |
| 20 | + |
| 21 | +First install the React Router vite plugin: |
| 22 | + |
| 23 | +```shellscript nonumber |
| 24 | +npm install -D @react-router/vite |
| 25 | +``` |
| 26 | + |
| 27 | +Then swap out the React plugin for React Router. The `react` key accepts the same options as the React plugin. |
| 28 | + |
| 29 | +```diff filename=vite.config.ts |
| 30 | +-import react from '@vitejs/plugin-react' |
| 31 | ++import { plugin as app } from "@react-router/vite"; |
| 32 | +import { defineConfig } from "vite"; |
| 33 | + |
| 34 | + |
| 35 | +export default defineConfig({ |
| 36 | + plugins: [ |
| 37 | +- react(reactOptions) |
| 38 | ++ app({ react: reactOptions }) |
| 39 | + ], |
| 40 | +}); |
| 41 | +``` |
| 42 | + |
| 43 | +### Webpack |
| 44 | + |
| 45 | +<docs-error>TODO: update this when we know exactly what it looks like</docs-error> |
| 46 | + |
| 47 | +If you are using Webpack, you will need to update your Webpack config to use the React Router plugin. |
| 48 | + |
| 49 | +```shellscript nonumber |
| 50 | +npm install -D @react-router/webpack |
| 51 | +``` |
| 52 | + |
| 53 | +```ts filename=webpack.config.js |
| 54 | +import { ReactRouterPlugin } from "@react-router/webpack"; |
| 55 | +export default { |
| 56 | + plugins: [new ReactRouterPlugin()], |
| 57 | +}; |
| 58 | +``` |
| 59 | + |
| 60 | +### Create React App |
| 61 | + |
| 62 | +<docs-error>TODO: update this when we know exactly what it looks like</docs-error> |
| 63 | + |
| 64 | +- Eject and add the Webpack plugin |
| 65 | +- Switch to Vite: https://www.robinwieruch.de/vite-create-react-app/ |
| 66 | + |
| 67 | +## Entry Points |
| 68 | + |
| 69 | +After configuring the bundler, you'll need to shuffle around your app's entry points. |
| 70 | + |
| 71 | +<docs-error>TODO: add more details here</docs-error> |
| 72 | + |
| 73 | +- Move `index.html` template to `root.tsx` |
| 74 | +- Move entry/root component to `root.tsx` and `entry.client.tsx` |
| 75 | + |
| 76 | +## Enable SSR and Pre-rendering |
| 77 | + |
| 78 | +If you want to enable server rendering and static pre-rendering, you can do so with the `ssr` and `prerender` options in the bundler plugin. |
| 79 | + |
| 80 | +```ts filename=vite.config.ts |
| 81 | +import { plugin as app } from "@react-router/vite"; |
| 82 | +import { defineConfig } from "vite"; |
| 83 | + |
| 84 | +export default defineConfig({ |
| 85 | + plugins: [ |
| 86 | + app({ |
| 87 | + ssr: true, |
| 88 | + async prerender() { |
| 89 | + return ["/", "/about", "/contact"]; |
| 90 | + }, |
| 91 | + }), |
| 92 | + ], |
| 93 | +}); |
| 94 | +``` |
| 95 | + |
| 96 | +See [Deploying][deploying] for more information on deploying a server. |
| 97 | + |
| 98 | +## Route Modules |
| 99 | + |
| 100 | +You can incrementally migrate your routes to route modules. |
| 101 | + |
| 102 | +First create a `routes.ts` file that exports your routes. |
| 103 | + |
| 104 | +```tsx filename=app/routes.ts |
| 105 | + |
| 106 | +``` |
| 107 | + |
| 108 | +[deploying]: ../start/deploying |
0 commit comments