Skip to content

Commit 8938be6

Browse files
authored
feat: auto recognize vue router version (#18)
1 parent 0dcf2c2 commit 8938be6

File tree

10 files changed

+45
-32
lines changed

10 files changed

+45
-32
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
node_modules/
22
.DS_Store
3-
/index.js
43
dist

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
> Use it with your own risk, we'll rewrite this soon.
2-
31
![](https://user-images.githubusercontent.com/19513289/59971813-06ab8000-95b6-11e9-8b6b-627af2c2f1ae.png)
42

53
> 🚦Future-Oriented vue routing system
@@ -73,8 +71,12 @@ Then in your `router.js`:
7371

7472
```js
7573
import { routes } from 'vue-auto-routes'
76-
74+
// v3
7775
export default new VueRouter({ routes })
76+
// v4
77+
export const router = createRouter({
78+
routes
79+
})
7880
```
7981

8082
## Routing Convertion
@@ -108,11 +110,14 @@ Since `v1.1.11` options for [@ream/collect-fs-routes v1.0.2](https://github.com/
108110

109111
Routes directory, e.g. `src/views`.
110112

111-
### next
113+
### ~~next~~
112114

115+
- Deprecated
113116
- Type: `boolean`
114117
- Default: `false`
115118

119+
Now we recognize v4 automatically. Do not need this anymore.
120+
116121
Vue router next [See migration](https://next.router.vuejs.org/guide/migration/#removed-star-or-catch-all-routes).
117122

118123
### ~~componentPrefix~~

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const routes = []

lib/collect-fs-routes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function renderRoutes(
6262
chunkPrefix = 'page-',
6363
componentPrefix = process.cwd(),
6464
extraRoutes = [],
65-
next = false
65+
v4
6666
} = {}
6767
) {
6868
return `
@@ -74,7 +74,7 @@ function renderRoutes(
7474
{
7575
path: ${
7676
route.path === '/404'
77-
? stringify(next ? '/:pathMatch(.*)*' : '*') // vue router next
77+
? stringify(v4 ? '/:pathMatch(.*)*' : '*') // vue router v4
7878
: stringify(route.path.replace(/\\/g, '/'))
7979
},
8080
${route.component.match(/\{(.*?)\}/g) ? 'props: true,' : ''}

lib/plugin.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
const path = require('path')
22
const { writeFile: _writeFile, watch } = require('fs')
33
const { promisify } = require('util')
4+
const VueRouterVersion = require('vue-router/package.json').version
45
const { collectRoutes, renderRoutes } = require('./collect-fs-routes')
56

7+
const v4 = VueRouterVersion.slice(0, 1) === '4'
8+
69
const writeFile = promisify(_writeFile)
710

811
const pluginName = 'vue-auto-routes'
@@ -12,8 +15,7 @@ module.exports = class VueAutoRoutes {
1215
this.opts = Object.assign(
1316
{
1417
watchMode: false,
15-
default404: true,
16-
next: false
18+
default404: true
1719
},
1820
opts
1921
)
@@ -64,7 +66,7 @@ module.exports = class VueAutoRoutes {
6466

6567
return `export const routes = ${renderRoutes(routes, {
6668
extraRoutes,
67-
next: this.opts.next
69+
v4
6870
})}
6971
`
7072
}

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
"files": [
1313
"lib",
1414
"plugin.js",
15-
"404.vue"
15+
"404.vue",
16+
"index.js"
1617
],
1718
"scripts": {
1819
"lint": "xo",
19-
"test": "npm run lint && ava --verbose",
20-
"install": "echo 'export const routes = []' > index.js"
20+
"test": "npm run lint && ava --verbose"
2121
},
2222
"dependencies": {
2323
"lodash.sortby": "^4.7.0"
@@ -30,6 +30,7 @@
3030
"husky": "^1.0.0-rc.13",
3131
"lint-staged": "^7.2.0",
3232
"prettier": "^1.17.0",
33+
"vue-router": "^4.0.0-rc.6",
3334
"xo": "^0.23.0"
3435
},
3536
"xo": {

test/index.test.js

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,27 @@ const options = {
88
match: 'vue,js'
99
}
1010

11+
const route404 = {
12+
path: '/404',
13+
component: path.relative(process.cwd(), path.join(__dirname, '../404.vue'))
14+
}
15+
1116
test('main', async t => {
1217
const routes = await collectRoutes(options)
13-
const routesString = renderRoutes(routes, {
18+
const routesString = renderRoutes([...routes, route404], {
1419
componentPrefix: ''
1520
})
1621

1722
t.snapshot(JSON.stringify(routes), 'collect routes')
1823
t.snapshot(routesString, 'render routes')
1924
})
2025

21-
test('next', async t => {
26+
test('v4', async t => {
2227
const routes = await collectRoutes(options)
23-
const routesString = renderRoutes(
24-
[
25-
...routes,
26-
{
27-
path: '/404',
28-
component: path.relative(
29-
process.cwd(),
30-
path.join(__dirname, '../404.vue')
31-
)
32-
}
33-
],
34-
{
35-
componentPrefix: '',
36-
next: true
37-
}
38-
)
28+
const routesString = renderRoutes([...routes, route404], {
29+
componentPrefix: '',
30+
v4: true
31+
})
3932

4033
t.snapshot(JSON.stringify(routes), 'collect routes')
4134
t.snapshot(routesString, 'render routes')

test/snapshots/index.test.js.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,18 @@ Generated by [AVA](https://ava.li).
4949
component: () => import(/* webpackChunkName: "page-test-views-foo-{name}-vue" */ "test/views/foo/{name}.vue"),␊
5050
5151
}␊
52+
,␊
53+
{␊
54+
path: "*",␊
55+
56+
component: () => import(/* webpackChunkName: "page-404-vue" */ "404.vue"),␊
57+
58+
}␊
5259
5360
]␊
5461
`
5562

56-
## next
63+
## v4
5764

5865
> collect routes
5966

test/snapshots/index.test.js.snap

1 Byte
Binary file not shown.

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4950,6 +4950,11 @@ vfile@^2.0.0:
49504950
unist-util-stringify-position "^1.0.0"
49514951
vfile-message "^1.0.0"
49524952

4953+
vue-router@^4.0.0-rc.6:
4954+
version "4.0.0-rc.6"
4955+
resolved "https://registry.npmjs.org/vue-router/-/vue-router-4.0.0-rc.6.tgz#8fa9e2d612c28ccf9b2bfa9421052932808ce24b"
4956+
integrity sha512-hVHC8A4/0yku1Z6+oUtX9odeHv78XwDI7putt1hd7os27P7mLabkjArN7f3TI3e/cz17MxAUz5Yp+m8ZE3sPsw==
4957+
49534958
wcwidth@^1.0.1:
49544959
version "1.0.1"
49554960
resolved "https://registry.npmjs.org/wcwidth/-/wcwidth-1.0.1.tgz#f0b0dcf915bc5ff1528afadb2c0e17b532da2fe8"

0 commit comments

Comments
 (0)