Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

path-aliases.md

98 lines2.8 KB

1---
2id: path-aliases
3title: Path Aliases
4---
5​
6Path aliases are a useful feature of TypeScript that allows you to define a shortcut for a path that could be distant in your project's directory structure. This can help you avoid long relative imports in your code and make it easier to refactor your project's structure. This is especially useful for avoiding long relative imports in your code.
7​
8By default, TanStack Start does not include path aliases. However, you can easily add them to your project by updating your `tsconfig.json` file in the root of your project and adding the following configuration:
9​
10```json
12 "compilerOptions": {
13 "baseUrl": ".",
14 "paths": {
15 "~/*": ["./src/*"]
16 }
17 }
19```
20​
21In this example, we've defined the path alias `~/*` that maps to the `./src/*` directory. This means that you can now import files from the `src` directory using the `~` prefix.
22​
23After updating your `tsconfig.json` file, configure your build tool so it resolves the same path aliases.
24​
25<!-- ::start:tabs variant="bundler" -->
26​
27# Vite
28​
29## Vite 8
30​
31Vite 8+ has [built-in support for path aliases](https://vite.dev/config/shared-options#resolve-tsconfigpaths), which is disabled by default. To enable it, simply add the following configuration to your `vite.config.ts` file:
32​
33```ts
34// vite.config.ts
35import { defineConfig } from 'vite'
36​
37export default defineConfig({
38 resolve: {
39 // This enables built-in support for path aliases defined in tsconfig.json
40 tsconfigPaths: true,
41 },
42})
43```
44​
45## Vite 7 and earlier
46​
47For Vite 7 and earlier, install the `vite-tsconfig-paths` plugin to enable path aliases in your TanStack Start project. You can do this by running the following command:
48​
49```sh
50npm install -D vite-tsconfig-paths
51```
52​
53Now, you'll need to update your `vite.config.ts` file to include the following:
54​
55```ts
56// vite.config.ts
57import { defineConfig } from 'vite'
58import viteTsConfigPaths from 'vite-tsconfig-paths'
59​
60export default defineConfig({
61 plugins: [
62 // this is the plugin that enables path aliases
63 viteTsConfigPaths({
64 projects: ['./tsconfig.json'],
65 }),
66 ],
67})
68```
69​
70# Rsbuild
71​
72Rsbuild reads the `paths` field from `tsconfig.json` by default. No extra config is needed when your aliases live in the root `tsconfig.json`.
73​
74If you use a custom tsconfig file, point Rsbuild at it with `source.tsconfigPath`:
75​
76```ts
77// rsbuild.config.ts
78import { defineConfig } from '@rsbuild/core'
79​
80export default defineConfig({
81 source: {
82 tsconfigPath: './tsconfig.custom.json',
83 },
84})
85```
86​
87<!-- ::end:tabs -->
88​
89Once this configuration has completed, you'll now be able to import files using the path alias like so:
90​
91```ts
92// app/routes/posts/$postId/edit.tsx
93import { Input } from '~/components/ui/input'
94​
95// instead of
96​
97import { Input } from '../../../components/ui/input'
98```