Skip to content

Commit 82b6dbc

Browse files
authored
fix(router): pass the base option as an argument (#15)
* fix(router): pass the base option as an argument Close #14 * test: remove redundant cases
1 parent 90b4f26 commit 82b6dbc

File tree

5 files changed

+45
-13
lines changed

5 files changed

+45
-13
lines changed

generator/codemods/router/__testfixtures__/create-history.input.js

+5
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,9 @@ const router = new VueRouter({
2020
routes
2121
});
2222

23+
new VueRouter({
24+
mode: 'history',
25+
routes
26+
});
27+
2328
export default router;

generator/codemods/router/__testfixtures__/create-history.output.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ const routes = [
1515
];
1616

1717
const router = createRouter({
18+
history: createWebHistory(process.env.BASE_URL),
19+
routes
20+
});
21+
22+
createRouter({
1823
history: createWebHistory(),
19-
base: process.env.BASE_URL,
2024
routes
2125
});
2226

generator/codemods/router/__testfixtures__/create-router.input.js

+4
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@ const router = new VueRouter({
1919
routes
2020
});
2121

22+
new VueRouter({
23+
routes
24+
});
25+
2226
export default router;

generator/codemods/router/__testfixtures__/create-router.output.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createRouter } from 'vue-router';
1+
import { createRouter, createWebHashHistory } from 'vue-router';
22
import Home from '../views/Home.vue';
33

44
const routes = [
@@ -15,7 +15,12 @@ const routes = [
1515
];
1616

1717
const router = createRouter({
18-
base: process.env.BASE_URL,
18+
history: createWebHashHistory(process.env.BASE_URL),
19+
routes
20+
});
21+
22+
createRouter({
23+
history: createWebHashHistory(),
1924
routes
2025
});
2126

generator/codemods/router/index.js

+24-10
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,47 @@ module.exports = function(fileInfo, api) {
2828
addImport(context, { imported: 'createRouter' }, 'vue-router')
2929
newVueRouter.replaceWith(({ node }) => {
3030
// mode: 'history' -> history: createWebHistory(), etc
31+
let historyMode = 'createWebHashHistory'
32+
let baseValue
3133
node.arguments[0].properties = node.arguments[0].properties.map(p => {
3234
if (p.key.name === 'mode') {
3335
const mode = p.value.value
34-
let initializer
3536
if (mode === 'hash') {
36-
initializer = 'createWebHashHistory'
37+
historyMode = 'createWebHashHistory'
3738
} else if (mode === 'history') {
38-
initializer = 'createWebHistory'
39+
historyMode = 'createWebHistory'
3940
} else if (mode === 'abstract') {
40-
initializer = 'createMemoryHistory'
41+
historyMode = 'createMemoryHistory'
4142
} else {
4243
console.error(
4344
`mode must be one of 'hash', 'history', or 'abstract'`
4445
)
4546
return p
4647
}
47-
48-
addImport(context, { imported: initializer }, 'vue-router')
49-
return j.objectProperty(
50-
j.identifier('history'),
51-
j.callExpression(j.identifier(initializer), [])
52-
)
48+
return
49+
} else if (p.key.name === 'base') {
50+
baseValue = p.value
51+
return
5352
}
5453

5554
return p
5655
})
5756

57+
// add the default mode with a hash history
58+
addImport(context, { imported: historyMode }, 'vue-router')
59+
node.arguments[0].properties = node.arguments[0].properties.filter(
60+
p => !!p
61+
)
62+
node.arguments[0].properties.unshift(
63+
j.objectProperty(
64+
j.identifier('history'),
65+
j.callExpression(
66+
j.identifier(historyMode),
67+
baseValue ? [baseValue] : []
68+
)
69+
)
70+
)
71+
5872
return j.callExpression(j.identifier('createRouter'), node.arguments)
5973
})
6074
removeExtraneousImport(context, localVueRouter)

0 commit comments

Comments
 (0)