Import Aliases & Path Resolution
Configure clean, refactor-proof import paths across your codebase using standard tsconfig.json or jsconfig.json path mappings — resolved identically across code, stylesheets, and assets on every bundler and runtime.
Import Aliases in Dinou
In growing codebases, deeply nested relative paths like ../../../../components/ui/button are noisy to read and break whenever a route file is moved to another folder.
Dinou includes a unified, TypeScript-spec-compliant path resolver built directly into its core pipeline. Out of the box, @/* and ~/* resolve to src/* with zero configuration, and any custom paths or extends rules declared in your tsconfig.json (or jsconfig.json) work identically across Esbuild, Rollup, and Webpack as well as Node.js, Bun, Deno, Cloudflare Workers, Netlify, and Static Export.
Configuring tsconfig.json & jsconfig.json
Declare your compilerOptions.paths mappings in your project root tsconfig.json so TypeScript, your IDE, and Dinou share the exact same module graph:
{
"compilerOptions": {
"target": "ES2022",
"rootDirs": ["src", ".dinou/routes"],
"paths": {
"@/*": ["./src/*"]
},
"allowJs": true,
"skipLibCheck": true,
"noEmit": true,
"jsx": "react-jsx",
"strict": true,
"module": "ESNext",
"moduleResolution": "bundler"
},
"include": [
"src",
"dinou-env.d.ts",
"dinou.config.ts",
".dinou/types/**/*.d.ts"
]
}JavaScript Projects (jsconfig.json or allowJs)
.js / .jsx / .mjs) projects can either configure aliases in jsconfig.json or keep a tsconfig.json with "allowJs": true. Dinou reads tsconfig.json first and automatically falls back to jsconfig.json across all three bundlers, including JSON with comments (JSONC) and trailing commas.Granular Aliases & Specificity
Beyond @/*, you can define domain-specific aliases, exact single-file aliases (without *), or overlapping prefixes:
{
"compilerOptions": {
"paths": {
"@/*": ["./src/*"],
"@ui/*": ["./src/components/ui/*"],
"@ui/icons/*": ["./src/icons/*"],
"@components/*": ["./src/components/*"],
"@server/*": ["./src/server-functions/*"],
"@lib/*": ["./src/lib/*"],
"@styles/*": ["./src/styles/*"],
"@assets/*": ["./src/assets/*"],
"@site-config": ["./src/config/site.ts"]
}
}
}Following the TypeScript specification, Dinou automatically prioritizes exact matches first, followed by longest-prefix wildcards regardless of key order in JSON — so @ui/icons/check always resolves to src/icons/check.tsx even when @ui/* is listed above @ui/icons/*.
Config Inheritance (extends) & Multi-Target Fallbacks
Monorepos and multi-package workspaces can inherit baseUrl and paths via "extends" (including single paths, TypeScript 5.0+ arrays, node_modules presets, and TypeScript 5.5+ ${configDir} placeholders) as well as ordered fallback target arrays:
{
"extends": ["../../tsconfig.base.json", "@tsconfig/strictest/tsconfig.json"],
"compilerOptions": {
"paths": {
"@/*": ["./src/*"],
"@shared/*": [
"./src/overrides/*",
"../../packages/shared/src/*"
]
}
}
}When importing @shared/format, Dinou checks ./src/overrides/format first and cleanly falls back to ../../packages/shared/src/format if no local override exists.
Where Aliases Work (Code, CSS, & Assets)
Dinou propagates the exact same alias rules across every compilation pass — RSC, SSR, Client bundles, PostCSS, asset hashing, and even dinou.config.ts:
1. TypeScript, JavaScript, & JSON Modules
Use aliases across Server Components, Client Components ("use client"), Server Functions ("use server"), Page & Layout Functions, JSON files, and dinou.config.ts:
// src/dashboard/page.tsx
import type { PageProps } from "./dinou";
import { UserCard } from "@components/user-card";
import { fetchUserMetrics } from "@server/metrics";
import { formatDate } from "@lib/utils";
import releaseMeta from "@lib/release-meta.json";
export default async function DashboardPage(_props: PageProps) {
const metrics = await fetchUserMetrics();
return (
<UserCard
data={metrics}
version={releaseMeta.version}
formattedTime={formatDate(Date.now())}
/>
);
}2. Stylesheets, CSS Modules, @import, & url()
Path aliases work both when importing Global CSS and CSS Modules from TypeScript/JavaScript and inside CSS @import rules and url(...) asset declarations:
// Importing Global CSS and CSS Modules from TS/TSX via custom aliases
import "@styles/theme.css";
import styles from "@styles/card.module.css";
export function HighlightCard({ children }: { children: React.ReactNode }) {
return <article className={styles.surface}>{children}</article>;
}/* src/styles/theme.css */
@import "@styles/tokens.css";
@import "@styles/typography.css";
.hero-surface {
color: var(--brand-foreground);
background-image: url("@assets/images/noise-grid.png");
}PostCSS resolves @styles/tokens.css at build time and rewrites url("@assets/images/noise-grid.png") to its content-hashed /assets/noise-grid-[hash].png URL automatically.
3. Static Media & Custom Assets
All 26 built-in media, vector, audio/video, and font formats (plus any custom assetExtensions configured in dinou.config.ts) resolve cleanly through path aliases:
// src/components/hero.tsx
import heroImage from "@assets/images/hero-banner.webp";
import logoSvg from "@assets/brand/logo.svg";
export function Hero() {
return (
<div className="hero">
<img src={logoSvg} alt="Brand Logo" className="h-10" />
<img src={heroImage} alt="Hero Banner" className="rounded-xl shadow-lg" />
</div>
);
}Extension, Index, & ESM Resolution
Dinou resolves extensionless imports, directory index files, and TypeScript ESM-style .js specifiers consistently across all bundlers:
| Import Statement | Resolved Disk Path |
|---|---|
| import { Card } from "@/components/card"; | src/components/card.tsx (.ts, .jsx, .js, .mjs, .cjs, .json) |
| import { Modal } from "@/components/modal"; | src/components/modal/index.tsx (directory index) |
| import { math } from "@/lib/math.js"; | src/lib/math.ts (TypeScript ESM .js → .ts/.tsx mapping) |
| @import "@styles/tokens"; | src/styles/tokens.css (prefers stylesheet extensions in CSS) |
Contextual ./dinou vs. Path Aliases
Dinou combines your tsconfig.json aliases with its auto-generated route-contextual "./dinou" module. Each serves a distinct purpose:
Inside page.tsx, layout.tsx, or page_functions.ts, import PageProps, LayoutProps, PageFunctions, Link, and useRouter from "./dinou" so TypeScript narrows dynamic route params, sibling @slot props, and relative navigation targets for that exact directory.
Use @/* (or granular aliases like @components/*, @server/*, and @lib/*) for shared UI components, Server Functions, utilities, styles, and media assets — and import Link or useRouter from "dinou" in shared non-route components.
IDE Autocomplete & Live Dev Reload
Because Dinou reads standard tsconfig.json and jsconfig.json files directly, editors (VS Code, Cursor, WebStorm, Zed, Neovim) and dinou dev stay synchronized with zero extra plugins:
IntelliSense auto-completes aliased paths and Cmd/Ctrl+Click jumps straight to source definitions.
Editing tsconfig.json, jsconfig.json, or local extends configs during dinou dev reloads RSC, SSR, and client aliases automatically without restarting the server.
Moving a route file from src/blog/page.tsx to src/news/archive/page.tsx keeps all @/* imports valid.
Best Practices & Common Gotchas
Modern TypeScript (5.0+) resolves paths directly relative to the config file containing paths without needing baseUrl. If your project or monorepo base config explicitly sets baseUrl, Dinou honors baseUrl with identical TypeScript semantics.
Pure TypeScript/JavaScript helpers, types, JSON data, stylesheets, and static assets can be aliased from external workspace folders (such as ../shared/*). Modules carrying React Flight boundary directives ("use client" or "use server") should live under src/ (or in package.json dependencies) so Dinou's manifest scanner discovers them.
As documented in Routing, directories prefixed with _ inside src/ (such as src/docs/_drafts) are excluded from routing and bundling. Importing into an escaped _folder from an active route is blocked at build time even when accessed through a custom path alias.
While aliases make cross-layer imports effortless, keep dependency direction clean (e.g. @components/* imports from @lib/*, never the reverse) to avoid circular initialization issues.