1.0.1-beta.2 • Published 4 years ago
@originjs/vite-plugin-pages v1.0.1-beta.2
vite-plugin-pages
Automate generate route config for Vue components in pages directory, and support layouts in the same time
Features
- component which name starts with 
_is dynamic page - component which name starts with 
$is nested route - component which name is 
_will catch the error page - default layout will be applied to every Vue component in pages direcotry
 - custom layout will only be applied to Vue component has set layout property in layout block
 
Install
npm install @originjs/vite-plugin-pages --save-devor
yarn add @originjs/vite-plugin-pages --devUsage
First, add @originjs/vite-plugin-pages in vite.config.js.
// vite.config.js
import pages from '@originjs/vite-plugin-pages'
export default {
    plugins: [
        pages(
            /* options */
        )
    ]
}Then import the routes generated from vite-plugin-pages, and create router instance with the routes:
import { createRouter, createWebHistory } from 'vue-router'
import routes from 'virtual:plugin-pages'
console.log(routes)
const router = createRouter({
  history: createWebHistory(),
  routes,
})
export default routernow we could write our pages and layouts in pages directory and layouts directory respectively.
Convert rules
See these examples:
- Normal file name: /user/foo.vue -> /user/foo
 - File name starts with 
_: /user/_id.vue -> /user/:id - File name is 
index.vue: /user/index.vue -> /user - File name is 
_: /_.vue -> /:pathMatch(.) 
Others
- The name of files and directories in same directory must be different.
For example, if we already have /user/, we can't add a 
user.vuefile. - Directory can not be 
index