Skip to content

Instantly share code, notes, and snippets.

@Tiex84
Created March 6, 2023 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tiex84/b8cca4879ddc8b681ded4b925e584752 to your computer and use it in GitHub Desktop.
Save Tiex84/b8cca4879ddc8b681ded4b925e584752 to your computer and use it in GitHub Desktop.
React + Vite - Auto generates routes
// Auto generates routes from files under ./pages
// https://vitejs.dev/guide/features.html#glob-import
const pages = import.meta.glob('./pages/*.jsx', { eager: true })
const routes = Object.keys(pages).map((path) => {
const name = path.match(/\.\/pages\/(.*)\.jsx$/)[1]
return {
name,
path: name === 'Home' ? '/' : `/${name.toLowerCase()}`,
component: pages[path].default,
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment