oedotme / generouted

Generated file-based routes for Vite
https://stackblitz.com/github.com/oedotme/generouted/tree/main/explorer
MIT License
1.02k stars 47 forks source link

Can I setting a different entry point instead of './src/router.ts'? #151

Closed MJRT closed 6 months ago

MJRT commented 6 months ago

Hi, I'm trying to use it in ruby on rails framework via ruby-vite, but it's not working and show:

[Error: ENOENT: no such file or directory, open './src/router.ts'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: './src/router.ts'
}

Maybe the reason is the path is hard coded, and I didn't find any way to change it.

My project can use react-router normally, and the structure is followd, all react or typescript file in ./app/frontend: PixPin_2024-02-24_17-45-38

I would like to know how can I setting entry is ./app/frontend instead of ./src/

VriskaSerket51 commented 6 months ago

You can custom your path by edit vite.config.ts. Like this.

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import generouted from '@generouted/react-router/plugin'

export default defineConfig({
  plugins: [
    react(),
    generouted({
      source: { routes: './client/src/pages/**/[\\w[-]*.{jsx,tsx}', modals: './client/src/pages/**/[+]*.{jsx,tsx}' },
      output: './client/src/router.ts',
    }),
  ],
  resolve: { alias: { '@': '/client/src' } },
})
MJRT commented 6 months ago

hi @VriskaSerket51, thanks your information, but it still has some trouble after trying to follow this example. The scanned path is not expected. PixPin_2024-02-26_17-16-26

I think that path should like these.

export type Path =
  | `/`
  | `/engagements/:id/edit`
  | `/hello`;
oedotme commented 6 months ago

@MJRT Right now, the convention requires you to declare the routes inside of a src/pages directory even in nested directory structure.

Could you try using app/frontend/src/pages instead:

// vite.config.ts

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import generouted from '@generouted/react-router/plugin'

export default defineConfig({
  plugins: [
    react(),
    generouted({
      source: {
        routes: './app/frontend/src/pages/**/[\\w[-]*.{jsx,tsx}',
        modals: './app/frontend/src/pages/**/[+]*.{jsx,tsx}'
      },
      output: './app/frontend/src/router.ts',
    }),
  ],
  resolve: { alias: { '@': '/app/frontend/src' } },
})
oedotme commented 6 months ago

You'll likely also need to add a custom app/frontend/src/routes.tsx file for the runtime glob to pick up your routes.

You can find all steps needed for using generouted in a custom directory structure: examples/react-router-custom-path

MJRT commented 6 months ago

@oedotme I get it. Thank you.