Skip to content

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 ​

  1. LeggibilitΓ : @/lib/auth/auth Γ¨ piΓΉ chiaro di ../../../lib/auth/auth
  2. ManutenibilitΓ : Non devi aggiornare i path quando sposti i file
  3. Refactoring: PiΓΉ facile spostare file senza rompere gli import
  4. Autocomplete: Migliore supporto IDE con path assoluti
  5. 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

πŸ“š Riferimenti ​

Documentazione Elerama Frontend