vue-router-invoke-webpack-plugin
Automatic generate the routes of vue-router
based on the file directory.
Install
npm
npm install vue-router-invoke-webpack-plugin -D
cnpm
cnpm install vue-router-invoke-webpack-plugin -D
yarn
yarn add vue-router-invoke-webpack-plugin -D
What is Automatic Generate Routes
Routing automatic injection refers to according to the format of the file directory to automatically generate the corresponding router.js
, every time without the need to create a module to reference manual
Usage
Webpack
- We need know whether the environment is
development
orproduction
.So you should setprocess.env.NODE_ENV
which is equal todevelopment
in the development environment and is equal toproduction
in the production environment.There are many plugins can do that. We recommend cross-env - If there are many people working together,we can't import route by the absolute address,so you should set a alias for the watching
dir
. - the generated route will be lazyload. So make sure you have add @babel/plugin-syntax-dynamic-import
const VueRouterInvokeWebpackPlugin = ;const path = // omit some other option... resolve: alias: '@': path plugins: dir: 'demos/src' alias: '@/src' ;
VueCli3
vueCli3 will be easier than webpack
vue.config.js
const VueRouterInvokeWebpackPlugin = ; moduleexports = // omit other options... { configplugins; }; // or another way.. moduleexports = // omit other options... configureWebpack: plugins: dir: 'src/views' // must set the alias for the dir option which you have set alias: '@/views' ;
Start
After configure the options you can use npm run serve
or some other scripts that you defined to activate the plugin in the development environment. When first generated or the file which in the dir
option's direction changes.router.js
will be automatic generated.
And you can use npm run build
or some other scripts that you defined to activate the plugin in the production environment. router.js
will be automatic generated.
Options
Prop | Type | Required | Default | Description |
---|---|---|---|---|
dir | String | true | '' | vue file directory |
alias | String | true | '' | the option dir 's alias |
notFound | String | false | '' | the alias address of notFound chunk |
mode | String | false | history | hash or history |
meta | String | false | meta | the yml file's name |
routerDir | String | false | ROOT | generated router.js file |
language | String | false | javascript | javascript or typescript |
ignore | Array | false | ['.dsstore'] | files or directions will not be resolved |
redirect | Array | false | [] | redirect route |
modules | Array | false | [] | the import modules |
scrollBehavior | Function | false | '' | same as scrollBehavior |
beforeEach | Function | false | '' | router.beforeEach |
beforeResolve | Function | false | '' | router.beforeResolve |
afterEach | Function | false | '' | router.afterEach |
How To Automatical Invoke
The following example depends on VueCli3. I believe that if you know how to use in VueCli3,the using of webpack is easy for you.
vue.config.js
const VueRouterInvokeWebpackPlugin = ; moduleexports = // omit other options... { configplugins; };
And import router.js
in your entry file src/main.js
The default location of router.js
is under the invoke folder in the root directory,You can change the location anywhere by setting the routerDir
option
The address of routerDir
is relative to ROOT
, Pay attention to that it is not a absolute address
And I recommoned that router.js
may put into .gitignore
or .eslintignore
. Everyone's branch can be independent because router.js
will be automatic generated
;;; ;
SingleRoute
Please pay attention to that there is a direcotry which wrapping the Index.vue
,Do not name vue
directly.It maybe not quite in the usual way
The same, do not name the directory with Index
, it may have diffrent sense on Nested Route
version 0.2.7, The plugin will throw an error when the wrong naming of the directory in production environment and will show you a danger notice in development environment
So if you see that
The rule of naming about your directory maybe wrong
If your directory just like this
src
├── views
│ ├── Login
│ │ └── Index.vue
│ └── User
│ ├── Account
│ │ └── Index.vue
│ ├── Home
│ │ └── Index.vue
│ └── Index.vue
automatical generated route will be this
import'@/views/Login/Index.vue' name: 'login' path: '/login' import'@/views/User/Index.vue' name: 'user' path: '/user' import'@/views/User/Account/Index.vue' name: 'user-account' path: '/user/account' import'@/views/User/Home/Index.vue' name: 'user-home' path: '/user/home'
HomePage
We make a special treatment for HomePage which route is /
HomePage we named Index.vue
and is a unique route
If your directory just like this
src
├── views
│ ├── Login
│ │ └── Index.vue
│ └── Index.vue
automatical generated route will be this
import'@/views/Index.vue' name: 'index' path: '/' import'@/views/Login/Index.vue' name: 'login' path: '/login'
Dynamic Route
If your directory just like this
src
├── views
│ ├── Login
│ │ └── Index.vue
│ └── User
│ ├── _Home
│ │ └── Index.vue
│ └── Index.vue
automatical generated route will be this
import'@/views/Login/Index.vue' name: 'login' path: '/login' import'@/views/User/Index.vue' name: 'user' path: '/user' import'@/views/User/_Home/Index.vue' name: 'user-home' path: '/user/:home'
Nested Route
If your directory just like this
src
├── views
│ ├── Login
│ │ └── Index.vue
│ └── User
│ ├── Chart
│ │ └── Index.vue
│ ├── Home
│ │ └── Index.vue
│ └── User.vue
automatical generated route will be this
import'@/views/Login/Index.vue' name: 'login' path: '/login' import'@/views/User/User.vue' name: 'user' path: '/user' children: import'@/views/User/Chart/Index.vue' name: 'user-chart' path: 'chart' import'@/views/User/Home/Index.vue' name: 'user-home' path: 'home'
Dymaic and Nested Route
If your directory just like this
src
├── views
│ ├── Login
│ │ └── Index.vue
│ └── User
│ ├── _Category
│ │ ├── _Category.vue
│ │ └── Infor
│ │ └── Index.vue
│ └── Index.vue
automatical generated route will be this
import'@/views/Login/Index.vue' name: 'login' path: '/login' import'@/views/User/Index.vue' name: 'user' path: '/user' import'@/views/User/_Category/_Category.vue' name: 'user-category' path: '/user/:category' children: import'@/views/User/_Category/Infor/Index.vue' name: 'user-category-infor' path: 'infor'
Correct the name
We will transform diffetent rule of naming into upperCamelCase
naming
For Example
src
├── views
│ ├── LoginPage
│ │ └── index.vue
│ └── User-home
│ ├── account
│ │ └── Index.vue
│ ├── Home-details
│ │ └── Index.vue
│ └── Index.vue
automatical generated route will be this
import'@/views/LoginPage/index.vue' name: 'loginPage' path: '/loginPage' import'@/views/User-home/Index.vue' name: 'userHome' path: '/userHome' import'@/views/User-home/Home-details/Index.vue' name: 'userHome-homeDetails' path: '/userHome/homeDetails' import'@/views/User-home/account/Index.vue' name: 'userHome-account' path: '/userHome/account'
Meta Succedaneum
The meta
option in vue-router
can resolve many questions.Just like define the title of a page or define a page is necessary to login or not.
Some of the questions just like define the page title can be resolved by vue-meta.That is a fantastic repository.
But if you really need define the plain meta
option of vue-router
.You should make a yml
file.
For example
src/views├── Single│ ├── Indexvue│ └── User│ ├── Indexvue│ └── metayml
meta.yml
meta: - name: user
automatical generated route will be this
import'@/views/Single/Index.vue' name: 'single' path: 'single' import'@/views/Single/User/Index.vue' name: 'single-user' meta: name: user path: 'single/user'
Special Options
NotFound
If your set options like this
plugins: dir: 'src/views' alias: '@/views' // muse set ignore for notFound chunk ignore: 'NotFound.vue' notFound: '@/views/NotFound.vue' ;
the directory
src
├── views
│ ├── Login
│ │ └── Index.vue
│ └── Index.vue
│ └── NotFound.vue
automatical generated route will be this
import'@/views/Index.vue' name: 'index' path: '/' import'@/views/NotFound.vue' name: 'notFound' path: '*' import'@/views/Login/Index.vue' name: 'login' path: '/login'
Ignore
If your set options like this
images
components
template.vue
will not be resolved by the plugin
And the value ignore case
plugins: dir: 'src/views' alias: '@/views' language: 'javascript' ignore: 'images' 'components' 'template.vue' ;
the directory
src
├── views
│ ├── Login
│ │ └── Index.vue
│ ├── Template.vue
│ └── User
│ ├── Components
│ ├── Images
│ └── Index.vue
automatical generated route will be this
import'@/views/Login/Index.vue' name: 'login' path: '/login' import'@/views/User/Index.vue' name: 'user' path: '/user'
Obviously The plugin ignores the files
Redirect
If your set options like this
plugins: dir: 'src/views' alias: '@/views' language: 'javascript' redirect: redirect: '/' path: '/home' redirect: '/test' path: '/demo' ;
automatical generated route will be this
path: '/home' redirect: '/' path: '/demo' redirect: '/test'
Modules
The generated router.js
has Two modules
;;
If you need some other module which would use in beforeEach
or some other place you can define it by using modules
. For example
dir: 'src/views' alias: '@/views' modules: name: 'diyName' package: 'some-packages' ;
automatical generated route will be this
// omit other options;
VueRouter Guards
we have supported VueRouter's Guards beforeEach
beforeResolve
afterEach
If your set options like this
dir: 'src/views' alias: '@/views' language: 'javascript' { ; } { ; } {};
automatical generated route will be this
// omit others ...const router = mode: 'history' routes ;router; router; router;;
ScrollBehavior
If your set options like this
dir: 'src/views' alias: '@/views' language: 'javascript' { if savedPosition return savedPosition; else return x: 0 y: 0 ; };
automatical generated route will be this
// omit others...const router = mode: 'history' routes { if savedPosition return savedPosition; else return x: 0 y: 0 ; };
Demos
The detailed usage you can git clone
our project and run npm run build:demos
or you can just watch our demos directly.The demos dont't have substantial content,the more we focus is on the generation of directory,you can get how router.js
generated in the demos.