Package Exports
- yojan
- yojan/router
Readme
๐ฃ๏ธ yojan-router โ Dynamic Route Generator for Vite Projects
yojan-router is a powerful Vite plugin that scans your source files (like src/pages) and auto-generates a route map. It supports SSR, SSG, ISR, CSR distinctions, route grouping (Next.js-style), regeneration modes, and much more โ all through a simple .yojanrc.json file.
โจ Features
- โ File-based routing
- โก Auto route regeneration (watch, interval, file-modified, or once)
- ๐งฉ Grouped dynamic routes like
(admin)/dashboard - ๐ Static/lazy/dynamic import strategies
- ๐ SSR / CSR / SSG / ISR export detection
- ๐ Custom export fields
- ๐ Customizable output directory and filename
- ๐ฃ Fine-grained logging with log levels
๐ฆ Installation
npm add -D yojanโ๏ธ Usage
Add yojan-router to your Vite config:
// vite.config.ts
import yojanRouter from 'yojan/router'
export default {
plugins: [yojanRouter()]
}๐๏ธ Example Folder Structure
src/pages/
โโโ index.tsx โ "/"
โโโ about.tsx โ "/about"
โโโ [user].tsx โ "/:user"
โโโ (admin)/home.tsx โ "/admin/home" (if grouping enabled)๐ Output Example
export const routes = [
{
path: "/",
importPath: "./src/pages/index",
type: "csr", // or ssr/ssg/isr
absolutePath: "/abs/path/to/src/pages/index.tsx"
},
...
]๐ Configuration โ .yojanrc.json
Create a .yojanrc.json at your project root:
or do
npx yojan init
{
"ssrImportType": "static",
"isrImportType": "static",
"csrImportType": "lazy",
"ssgImportType": "static",
"includeRenderingType": true,
"fileLocation": ".",
"csr": "default",
"ssr": "getServerSideProps",
"ssg": "getStaticProps",
"isr": "getISRProps",
"absoluteLocationForAll": false,
"absoluteLocation": {},
"extraField": [],
"staticImportForExtraField": {},
"groupingAllowedInDynamicRoutes": false,
"excludeFiles": [],
"includeFiles": ["src/pages"],
"router": {
"file": "generated.routes"
},
"logLevel": "warn",
"regeneration": {
"mode": "watch"
}
}๐ Configuration Field Details
Rendering Mode Detection
Defines how to detect rendering type from exports:
"ssr": "getServerSideProps",
"ssg": "getStaticProps",
"isr": "getISRProps",
"csr": "default"Import Type for Routes
Control how route components are imported:
"ssrImportType": "static", // static | lazy
"ssgImportType": "static",
"isrImportType": "static",
"csrImportType": "lazy"Extra Field Detection
Attach custom export info to routes:
"extraField": ["auth", "middleware"],
"staticImportForExtraField": {
"auth": true,
"middleware": false
}Absolute Path Handling
Control whether absolutePath is included in output:
"absoluteLocationForAll": false,
"absoluteLocation": {
"ssr": true
}Grouping in Dynamic Routes
Enables Next.js-style folder grouping:
src/pages/(admin)/dashboard.tsx โ /admin/dashboard"groupingAllowedInDynamicRoutes": trueRoute Generation Settings
"fileLocation": ".", // Output directory
"router": {
"file": "generated.routes" // Filename (.ts or .js based on projectType)
}Regeneration Modes
"regeneration": {
"mode": "watch" // watch | interval | file-modified | dev-once
}| Mode | Description |
|---|---|
watch |
Watch for file creation/deletion/changes |
interval |
Regenerate on a timer |
file-modified |
React only to file content changes |
dev-once |
Run once when dev server starts |
Logging
"logLevel": "warn" // silent | info | warn | error๐งช Dev & Build
npm install
npm buildGenerates files inside the dist/ folder.
๐งพ License
MIT
Built with ๐ by Shivpujjan Kumar