Import Paths - Convenzione @/ β
π Convenzione β
Il progetto utilizza path alias con @/ per importare moduli dalla cartella app/ invece di path relativi.
β Configurazione β
TypeScript (tsconfig.json) β
json
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./app/*"]
}
}
}Vite (vite.config.ts) β
typescript
import tsconfigPaths from "vite-tsconfig-paths";
export default defineConfig({
plugins: [
tsconfigPaths() // Legge automaticamente i paths da tsconfig.json
]
});Vitest (vitest.config.ts) β
typescript
import tsconfigPaths from "vite-tsconfig-paths";
import path from "path";
export default defineConfig({
plugins: [tsconfigPaths()],
resolve: {
alias: {
"@": path.resolve(__dirname, "./app"),
},
},
});π― Esempi β
β Prima (path relativi) β
typescript
// Da app/routes/auth/login.tsx
import { authService } from "../../lib/auth/auth";
import { createGuestLoader } from "../../lib/auth/loaders";
import { useAuthStore } from "../../store/auth/auth.store";
// Da app/components/auth/AuthenticatedLayout.tsx
import { useCartStore } from "../../store/cart/cart.store";
import { useMessagesStore } from "../../store/messages/messages.store";
import { Cart } from "../cart/Cart";
// Da tests/auth/api.test.ts
import { login } from "../../app/api/auth/auth.api";
import { useAuthStore } from "../../app/store/auth/auth.store";β
Dopo (path alias @/) β
typescript
// Da app/routes/auth/login.tsx
import { authService } from "@/lib/auth/auth";
import { createGuestLoader } from "@/lib/auth/loaders";
import { useAuthStore } from "@/store/auth/auth.store";
// Da app/components/auth/AuthenticatedLayout.tsx
import { useCartStore } from "@/store/cart/cart.store";
import { useMessagesStore } from "@/store/messages/messages.store";
import { Cart } from "@/components/cart/Cart";
// Da tests/auth/api.test.ts
import { login } from "@/api/auth/auth.api";
import { useAuthStore } from "@/store/auth/auth.store";π¦ Struttura Import β
@/ β app/
βββ api/ β app/api/
β βββ auth/ β app/api/auth/
β βββ warehouse/ β app/api/warehouse/
βββ components/ β app/components/
β βββ auth/ β app/components/auth/
β βββ cart/ β app/components/cart/
βββ hooks/ β app/hooks/
β βββ api/ β app/hooks/api/
β βββ iframe/ β app/hooks/iframe/
β βββ sync/ β app/hooks/sync/
β βββ ui/ β app/hooks/ui/
β βββ utils/ β app/hooks/utils/
βββ lib/ β app/lib/
β βββ auth/ β app/lib/auth/
βββ routes/ β app/routes/
β βββ auth/ β app/routes/auth/
βββ schemas/ β app/schemas/
β βββ auth/ β app/schemas/auth/
β βββ warehouse/ β app/schemas/warehouse/
βββ store/ β app/store/
βββ auth/ β app/store/auth/
βββ cart/ β app/store/cart/
βββ messages/ β app/store/messages/π¨ Casi Speciali β
Import da stessa cartella β
Usa path relativi per file nella stessa directory:
typescript
// β
OK - stessa cartella
import { AuthService } from "./auth";
import type { Route } from "./+types/login";Import di tipi generati da React Router β
Usa sempre path relativi per i tipi generati:
typescript
// β
OK - file generato da React Router
import type { Route } from "./+types/login";Import in test β
Usa @/ anche nei test:
typescript
// tests/auth/api.test.ts
import { login } from "@/api/auth/auth.api";
import { useAuthStore } from "@/store/auth/auth.store";β¨ Vantaggi β
- LeggibilitΓ :
@/lib/auth/authè più chiaro di../../../lib/auth/auth - Manutenibilità : Non devi aggiornare i path quando sposti i file
- Refactoring: PiΓΉ facile spostare file senza rompere gli import
- Autocomplete: Migliore supporto IDE con path assoluti
- Consistenza: Stesso pattern in tutto il progetto
π« Errori Comuni β
β Non usare path relativi lunghi β
typescript
import { authService } from "../../../lib/auth/auth";β
Usa sempre @/ β
typescript
import { authService } from "@/lib/auth/auth";β Non mescolare convenzioni β
typescript
// β Cattivo - mix di stili
import { authService } from "@/lib/auth/auth";
import { useAuthStore } from "../../store/auth/auth.store";β Sii consistente β
typescript
// β
Buono - un solo stile
import { authService } from "@/lib/auth/auth";
import { useAuthStore } from "@/store/auth/auth.store";π§ Migrazione β
Se hai file con path relativi, puoi convertirli facilmente:
bash
# Cerca file con import relativi
grep -r "from \"\.\./\.\./app/" .
# O usa l'IDE per refactoring automatico