-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
docs(solid-router): webpack example and installation guide #5833
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| --- | ||
| title: Installation with Webpack | ||
| --- | ||
|
|
||
| [//]: # 'BundlerConfiguration' | ||
|
|
||
| To use file-based routing with **Webpack**, you'll need to install the `@tanstack/router-plugin` package. | ||
|
|
||
| ```sh | ||
| npm install -D @tanstack/router-plugin | ||
| ``` | ||
|
|
||
| Once installed, you'll need to add the plugin to your configuration. | ||
|
|
||
| ```tsx | ||
| // webpack.config.ts | ||
| import { tanstackRouter } from '@tanstack/router-plugin/webpack' | ||
|
|
||
| export default { | ||
| plugins: [ | ||
| tanstackRouter({ | ||
| target: 'solid', | ||
| autoCodeSplitting: true, | ||
| }), | ||
| ], | ||
| } | ||
| ``` | ||
|
|
||
| And in the .babelrc (SWC doesn't support solid-js, see [here](https://www.answeroverflow.com/m/1135200483116593182)), add these presets: | ||
|
|
||
| ```tsx | ||
| // .babelrc | ||
|
|
||
| { | ||
| "presets": ["babel-preset-solid", "@babel/preset-typescript"] | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| Or, for a full webpack.config.js, you can clone our [Quickstart Webpack example](https://github.com/TanStack/router/tree/main/examples/solid/quickstart-webpack-file-based) and get started. | ||
|
|
||
| Now that you've added the plugin to your Webpack configuration, you're all set to start using file-based routing with TanStack Router. | ||
|
|
||
| [//]: # 'BundlerConfiguration' | ||
|
|
||
| ## Ignoring the generated route tree file | ||
|
|
||
| If your project is configured to use a linter and/or formatter, you may want to ignore the generated route tree file. This file is managed by TanStack Router and therefore shouldn't be changed by your linter or formatter. | ||
|
|
||
| Here are some resources to help you ignore the generated route tree file: | ||
|
|
||
| - Prettier - [https://prettier.io/docs/en/ignore.html#ignoring-files-prettierignore](https://prettier.io/docs/en/ignore.html#ignoring-files-prettierignore) | ||
| - ESLint - [https://eslint.org/docs/latest/use/configure/ignore#ignoring-files](https://eslint.org/docs/latest/use/configure/ignore#ignoring-files) | ||
| - Biome - [https://biomejs.dev/reference/configuration/#filesignore](https://biomejs.dev/reference/configuration/#filesignore) | ||
|
|
||
| > [!WARNING] | ||
| > If you are using VSCode, you may experience the route tree file unexpectedly open (with errors) after renaming a route. | ||
|
|
||
| You can prevent that from the VSCode settings by marking the file as readonly. Our recommendation is to also exclude it from search results and file watcher with the following settings: | ||
|
|
||
| ```json | ||
| { | ||
| "files.readonlyInclude": { | ||
| "**/routeTree.gen.ts": true | ||
| }, | ||
| "files.watcherExclude": { | ||
| "**/routeTree.gen.ts": true | ||
| }, | ||
| "search.exclude": { | ||
| "**/routeTree.gen.ts": true | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| You can use those settings either at a user level or only for a single workspace by creating the file `.vscode/settings.json` at the root of your project. | ||
|
|
||
| ## Configuration | ||
|
|
||
| When using the TanStack Router Plugin with Webpack for File-based routing, it comes with some sane defaults that should work for most projects: | ||
|
|
||
| ```json | ||
| { | ||
| "routesDirectory": "./src/routes", | ||
| "generatedRouteTree": "./src/routeTree.gen.ts", | ||
| "routeFileIgnorePrefix": "-", | ||
| "quoteStyle": "single" | ||
| } | ||
| ``` | ||
|
|
||
| If these defaults work for your project, you don't need to configure anything at all! However, if you need to customize the configuration, you can do so by editing the configuration object passed into the `tanstackRouter` function. | ||
|
|
||
| You can find all the available configuration options in the [File-based Routing API Reference](../../../../api/file-based-routing.md). |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "presets": ["babel-preset-solid", "@babel/preset-typescript"] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # Local | ||
| .DS_Store | ||
| *.local | ||
| *.log* | ||
|
|
||
| # Dist | ||
| node_modules | ||
| dist/ | ||
|
|
||
| # IDE | ||
| .idea |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "files.watcherExclude": { | ||
| "**/routeTree.gen.ts": true | ||
| }, | ||
| "search.exclude": { | ||
| "**/routeTree.gen.ts": true | ||
| }, | ||
| "files.readonlyInclude": { | ||
| "**/routeTree.gen.ts": true | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # Example | ||
|
|
||
| To run this example: | ||
|
|
||
| - `npm install` or `yarn` | ||
| - `npm start` or `yarn start` |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,28 @@ | ||||||||||
| { | ||||||||||
| "name": "tanstack-router-solid-example-quickstart-webpack-file-based", | ||||||||||
| "private": true, | ||||||||||
| "type": "module", | ||||||||||
| "scripts": { | ||||||||||
| "dev": "webpack serve --port 3001 --no-open", | ||||||||||
| "build": "webpack build && tsc --noEmit" | ||||||||||
| }, | ||||||||||
| "dependencies": { | ||||||||||
| "@tanstack/solid-router": "^1.135.2", | ||||||||||
| "@tanstack/solid-router-devtools": "^1.135.2", | ||||||||||
| "solid-js": "^1.9.10" | ||||||||||
| }, | ||||||||||
| "devDependencies": { | ||||||||||
| "@babel/core": "^7.28.5", | ||||||||||
| "@babel/preset-typescript": "^7.27.1", | ||||||||||
| "@swc/core": "^1.10.15", | ||||||||||
| "@tanstack/router-plugin": "^1.135.2", | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use workspace protocol for @tanstack/router-plugin. The Apply this diff: "@swc/core": "^1.10.15",
- "@tanstack/router-plugin": "^1.135.2",
+ "@tanstack/router-plugin": "workspace:*",
"babel-loader": "^10.0.0",Based on learnings 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||
| "babel-loader": "^10.0.0", | ||||||||||
| "babel-preset-solid": "^1.9.10", | ||||||||||
| "html-webpack-plugin": "^5.6.3", | ||||||||||
| "swc-loader": "^0.2.6", | ||||||||||
| "typescript": "^5.7.2", | ||||||||||
| "webpack": "^5.97.1", | ||||||||||
| "webpack-cli": "^5.1.4", | ||||||||||
| "webpack-dev-server": "^5.2.0" | ||||||||||
| } | ||||||||||
| } | ||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>TanStack router</title> | ||
| <script src="https://unpkg.com/@tailwindcss/browser@4"></script> | ||
| </head> | ||
| <body> | ||
| <div id="root"></div> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { RouterProvider, createRouter } from '@tanstack/solid-router' | ||
|
|
||
| import { routeTree } from './routeTree.gen' | ||
|
|
||
| // Set up a Router instance | ||
| const router = createRouter({ | ||
| routeTree, | ||
| defaultPreload: 'intent', | ||
| scrollRestoration: true, | ||
| }) | ||
|
|
||
| // Register things for typesafety | ||
| declare module '@tanstack/solid-router' { | ||
| interface Register { | ||
| router: typeof router | ||
| } | ||
| } | ||
| const App = () => { | ||
| return <RouterProvider router={router} /> | ||
| } | ||
|
|
||
| export default App |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { render } from 'solid-js/web' | ||
| import App from './app' | ||
|
|
||
| const rootElement = document.getElementById('root')! | ||
|
|
||
| if (!rootElement.innerHTML) { | ||
| render(() => <App />, rootElement) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* eslint-disable */ | ||
|
|
||
| // @ts-nocheck | ||
|
|
||
| // noinspection JSUnusedGlobalSymbols | ||
|
|
||
| // This file was automatically generated by TanStack Router. | ||
| // You should NOT make any changes in this file as it will be overwritten. | ||
| // Additionally, you should also exclude this file from your linter and/or formatter to prevent it from being checked or modified. | ||
|
|
||
| import { Route as rootRouteImport } from './routes/__root' | ||
| import { Route as AboutRouteImport } from './routes/about' | ||
| import { Route as IndexRouteImport } from './routes/index' | ||
|
|
||
| const AboutRoute = AboutRouteImport.update({ | ||
| id: '/about', | ||
| path: '/about', | ||
| getParentRoute: () => rootRouteImport, | ||
| } as any) | ||
| const IndexRoute = IndexRouteImport.update({ | ||
| id: '/', | ||
| path: '/', | ||
| getParentRoute: () => rootRouteImport, | ||
| } as any) | ||
|
|
||
| export interface FileRoutesByFullPath { | ||
| '/': typeof IndexRoute | ||
| '/about': typeof AboutRoute | ||
| } | ||
| export interface FileRoutesByTo { | ||
| '/': typeof IndexRoute | ||
| '/about': typeof AboutRoute | ||
| } | ||
| export interface FileRoutesById { | ||
| __root__: typeof rootRouteImport | ||
| '/': typeof IndexRoute | ||
| '/about': typeof AboutRoute | ||
| } | ||
| export interface FileRouteTypes { | ||
| fileRoutesByFullPath: FileRoutesByFullPath | ||
| fullPaths: '/' | '/about' | ||
| fileRoutesByTo: FileRoutesByTo | ||
| to: '/' | '/about' | ||
| id: '__root__' | '/' | '/about' | ||
| fileRoutesById: FileRoutesById | ||
| } | ||
| export interface RootRouteChildren { | ||
| IndexRoute: typeof IndexRoute | ||
| AboutRoute: typeof AboutRoute | ||
| } | ||
|
|
||
| declare module '@tanstack/solid-router' { | ||
| interface FileRoutesByPath { | ||
| '/about': { | ||
| id: '/about' | ||
| path: '/about' | ||
| fullPath: '/about' | ||
| preLoaderRoute: typeof AboutRouteImport | ||
| parentRoute: typeof rootRouteImport | ||
| } | ||
| '/': { | ||
| id: '/' | ||
| path: '/' | ||
| fullPath: '/' | ||
| preLoaderRoute: typeof IndexRouteImport | ||
| parentRoute: typeof rootRouteImport | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const rootRouteChildren: RootRouteChildren = { | ||
| IndexRoute: IndexRoute, | ||
| AboutRoute: AboutRoute, | ||
| } | ||
| export const routeTree = rootRouteImport | ||
| ._addFileChildren(rootRouteChildren) | ||
| ._addFileTypes<FileRouteTypes>() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { Link, Outlet, createRootRoute } from '@tanstack/solid-router' | ||
| import { TanStackRouterDevtools } from '@tanstack/solid-router-devtools' | ||
|
|
||
| export const Route = createRootRoute({ | ||
| component: RootComponent, | ||
| }) | ||
|
|
||
| function RootComponent() { | ||
| return ( | ||
| <> | ||
| <div class="p-2 flex gap-2 text-lg"> | ||
| <Link | ||
| to="/" | ||
| activeProps={{ | ||
| class: 'font-bold', | ||
| }} | ||
| activeOptions={{ exact: true }} | ||
| > | ||
| Home | ||
| </Link>{' '} | ||
| <Link | ||
| to="/about" | ||
| activeProps={{ | ||
| class: 'font-bold', | ||
| }} | ||
| > | ||
| About | ||
| </Link> | ||
| </div> | ||
| <hr /> | ||
| <Outlet /> | ||
| <TanStackRouterDevtools position="bottom-right" /> | ||
| </> | ||
| ) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { createFileRoute } from '@tanstack/solid-router' | ||
|
|
||
| export const Route = createFileRoute('/about')({ | ||
| component: AboutComponent, | ||
| }) | ||
|
|
||
| function AboutComponent() { | ||
| return ( | ||
| <div class="p-2"> | ||
| <h3>About</h3> | ||
| </div> | ||
| ) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { createFileRoute } from '@tanstack/solid-router' | ||
|
|
||
| export const Route = createFileRoute('/')({ | ||
| component: HomeComponent, | ||
| }) | ||
|
|
||
| function HomeComponent() { | ||
| return ( | ||
| <div class="p-2"> | ||
| <h3>Welcome Home!</h3> | ||
| </div> | ||
| ) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "target": "ES2020", | ||
| "lib": ["DOM", "DOM.Iterable", "ES2022"], | ||
| "module": "ESNext", | ||
| "jsx": "preserve", | ||
| "jsxImportSource": "solid-js", | ||
| "strict": true, | ||
| "skipLibCheck": true, | ||
| "isolatedModules": true, | ||
| "resolveJsonModule": true, | ||
| "moduleResolution": "bundler", | ||
| "useDefineForClassFields": true | ||
| }, | ||
| "include": ["src"] | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use workspace protocol for internal TanStack dependencies.
Internal TanStack packages should use the
workspace:*protocol instead of specific version numbers to ensure proper monorepo dependency resolution.Apply this diff to fix the dependencies:
"dependencies": { - "@tanstack/solid-router": "^1.135.2", - "@tanstack/solid-router-devtools": "^1.135.2", + "@tanstack/solid-router": "workspace:*", + "@tanstack/solid-router-devtools": "workspace:*", "solid-js": "^1.9.10" },Based on learnings
📝 Committable suggestion
🤖 Prompt for AI Agents