Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions docs/router/framework/solid/installation/with-webpack.md
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).
3 changes: 3 additions & 0 deletions examples/solid/quickstart-webpack-file-based/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["babel-preset-solid", "@babel/preset-typescript"]
}
11 changes: 11 additions & 0 deletions examples/solid/quickstart-webpack-file-based/.gitignore
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
11 changes: 11 additions & 0 deletions examples/solid/quickstart-webpack-file-based/.vscode/settings.json
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
}
}
6 changes: 6 additions & 0 deletions examples/solid/quickstart-webpack-file-based/README.md
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`
28 changes: 28 additions & 0 deletions examples/solid/quickstart-webpack-file-based/package.json
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"
Comment on lines +10 to +12
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

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

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
"@tanstack/solid-router": "^1.135.2",
"@tanstack/solid-router-devtools": "^1.135.2",
"solid-js": "^1.9.10"
"@tanstack/solid-router": "workspace:*",
"@tanstack/solid-router-devtools": "workspace:*",
"solid-js": "^1.9.10"
🤖 Prompt for AI Agents
In examples/solid/quickstart-webpack-file-based/package.json around lines 10 to
12, the TanStack internal packages are pinned to fixed versions; replace the
version strings for "@tanstack/solid-router" and
"@tanstack/solid-router-devtools" with the workspace protocol (e.g.,
"workspace:*") so the monorepo resolves internal dependencies correctly, leaving
"solid-js" as-is.

},
"devDependencies": {
"@babel/core": "^7.28.5",
"@babel/preset-typescript": "^7.27.1",
"@swc/core": "^1.10.15",
"@tanstack/router-plugin": "^1.135.2",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Use workspace protocol for @tanstack/router-plugin.

The @tanstack/router-plugin is an internal package and should use the workspace:* protocol.

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

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
"@tanstack/router-plugin": "^1.135.2",
"@swc/core": "^1.10.15",
"@tanstack/router-plugin": "workspace:*",
"babel-loader": "^10.0.0",
🤖 Prompt for AI Agents
In examples/solid/quickstart-webpack-file-based/package.json around line 18, the
dependency "@tanstack/router-plugin" uses a semantic version "^1.135.2" but it
should reference the local/internal package via the workspace protocol; update
the dependency entry to use "workspace:*" (i.e., replace the version string with
workspace:*), save package.json and run your package manager's install so the
workspace link is resolved.

"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"
}
}
12 changes: 12 additions & 0 deletions examples/solid/quickstart-webpack-file-based/public/index.html
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>
22 changes: 22 additions & 0 deletions examples/solid/quickstart-webpack-file-based/src/app.tsx
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
8 changes: 8 additions & 0 deletions examples/solid/quickstart-webpack-file-based/src/index.tsx
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)
}
77 changes: 77 additions & 0 deletions examples/solid/quickstart-webpack-file-based/src/routeTree.gen.ts
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>()
35 changes: 35 additions & 0 deletions examples/solid/quickstart-webpack-file-based/src/routes/__root.tsx
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" />
</>
)
}
13 changes: 13 additions & 0 deletions examples/solid/quickstart-webpack-file-based/src/routes/about.tsx
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>
)
}
13 changes: 13 additions & 0 deletions examples/solid/quickstart-webpack-file-based/src/routes/index.tsx
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>
)
}
16 changes: 16 additions & 0 deletions examples/solid/quickstart-webpack-file-based/tsconfig.json
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"]
}
Loading
Loading