Created
March 6, 2023 13:29
-
-
Save Tiex84/b8cca4879ddc8b681ded4b925e584752 to your computer and use it in GitHub Desktop.
React + Vite - Auto generates routes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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