JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 6
  • Score
    100M100P100Q25557F
  • License MIT

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: and customize yojanrc json file as required 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"

fileLocation

it tells where generated routes file will be stored ,directory of that

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 โ†’ /dashboard
"groupingAllowedInDynamicRoutes": true

Route Generation Settings

 fileLocation -  it tells where generated routes file will be stored ,directory of that
"fileLocation": ".",              // Output directory
"router": {
  "file": "generated.routes"      // Filename (.ts or .js based on projectType)
}

Regeneration Modes

"regeneration": {
  "mode": "watch" // 'watch' | 'build' | 'dev-once' | 'interval' | 'file-modified'
}
Mode Description
watch Watch for file creation/deletion/changes
interval Regenerate on a timer
like

"regeneration": { "mode": "interval", "intervalMs": 3000 } | file-modified | React only to file content changes | | dev-once | Run once when dev server starts |


Logging

"logLevel": "warn" // 'verbose' | 'warn' | 'errorOnly' | 'silent'

excludeFiles: [],

includeFiles: ['src/pages'], exclude the file of regenerated routes location if it is inside the includeFilesLocation like inside pages

excludeFiles: ['**/src/pages/generated.routes.js/ts']

includeFiles: [],

it include the routes from the location given for includefiles,
here you don't need to write like /** ,it will take after that location by default ,so just pass project folder
if src/pages and if file is like  src/pages/about/index.jsx
then route would be /about
if file is like  src/pages/about/me.jsx
then route would be /about/me

๐Ÿงพ License

MIT


Built with ๐Ÿ’› by Shivpujan Kumar

Keywords: vite, vite-plugin, vite-router, dynamic routes, ssr, ssg, csr, isr, yojan, react, file-based routing