Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit46500e6

Browse files
committed
feat: implement vue-router transformation
1 parentdd5ee5b commit46500e6

File tree

6 files changed

+169
-2
lines changed

6 files changed

+169
-2
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
importVueRouterfrom'vue-router';
2+
importHomefrom'../views/Home.vue';
3+
importAboutfrom'../views/About.vue';
4+
5+
constroutes=[
6+
{
7+
path:'/',
8+
name:'home',
9+
component:Home,
10+
},
11+
{
12+
path:'/about',
13+
name:'about',
14+
component:About
15+
},
16+
];
17+
18+
constrouter=newVueRouter({
19+
mode:'history',
20+
base:process.env.BASE_URL,
21+
routes,
22+
});
23+
24+
exportdefaultrouter;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import{createRouter,createWebHistory}from'vue-router';
2+
importHomefrom'../views/Home.vue';
3+
importAboutfrom'../views/About.vue';
4+
5+
constroutes=[
6+
{
7+
path:'/',
8+
name:'home',
9+
component:Home,
10+
},
11+
{
12+
path:'/about',
13+
name:'about',
14+
component:About
15+
},
16+
];
17+
18+
constrouter=createRouter({
19+
history:createWebHistory(),
20+
base:process.env.BASE_URL,
21+
routes,
22+
});
23+
24+
exportdefaultrouter;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
importVueRouterfrom'vue-router';
2+
importHomefrom'../views/Home.vue';
3+
importAboutfrom'../views/About.vue';
4+
5+
constroutes=[
6+
{
7+
path:'/',
8+
name:'home',
9+
component:Home,
10+
},
11+
{
12+
path:'/about',
13+
name:'about',
14+
component:About
15+
},
16+
];
17+
18+
constrouter=newVueRouter({
19+
base:process.env.BASE_URL,
20+
routes,
21+
});
22+
23+
exportdefaultrouter;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import{createRouter}from'vue-router';
2+
importHomefrom'../views/Home.vue';
3+
importAboutfrom'../views/About.vue';
4+
5+
constroutes=[
6+
{
7+
path:'/',
8+
name:'home',
9+
component:Home,
10+
},
11+
{
12+
path:'/about',
13+
name:'about',
14+
component:About
15+
},
16+
];
17+
18+
constrouter=createRouter({
19+
base:process.env.BASE_URL,
20+
routes,
21+
});
22+
23+
exportdefaultrouter;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
jest.autoMockOff()
2+
3+
const{ defineTest}=require('jscodeshift/dist/testUtils')
4+
5+
defineTest(__dirname,'index',null,'create-router')
6+
defineTest(__dirname,'index',null,'create-history')

‎generator/codemods/router/index.js

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,77 @@
1+
constaddImport=require('../utils/add-import')
2+
constremoveExtraneousImport=require('../utils/remove-extraneous-import')
3+
14
/**@type {import('jscodeshift').Transform} */
25
module.exports=function(fileInfo,api){
36
constj=api.jscodeshift
47
constroot=j(fileInfo.source)
8+
constcontext={ j, root}
9+
10+
// new Router() -> createRouter()
11+
constrouterImportDecls=root.find(j.ImportDeclaration,{
12+
source:{
13+
value:'vue-router'
14+
}
15+
})
16+
17+
constimportedVueRouter=routerImportDecls.find(j.ImportDefaultSpecifier)
18+
if(importedVueRouter.length){
19+
constlocalVueRouter=importedVueRouter.get(0).node.local.name
20+
21+
constnewVueRouter=root.find(j.NewExpression,{
22+
callee:{
23+
type:'Identifier',
24+
name:localVueRouter
25+
}
26+
})
27+
28+
addImport(context,{imported:'createRouter'},'vue-router')
29+
newVueRouter.replaceWith(({ node})=>{
30+
// mode: 'history' -> history: createWebHistory(), etc
31+
node.arguments[0].properties=node.arguments[0].properties.map(p=>{
32+
if(p.key.name==='mode'){
33+
constmode=p.value.value
34+
if(mode==='hash'){
35+
addImport(
36+
context,
37+
{imported:'createWebHashHistory'},
38+
'vue-router'
39+
)
40+
returnj.property(
41+
'init',
42+
j.identifier('history'),
43+
j.callExpression(j.identifier('createWebHashHistory'),[])
44+
)
45+
}elseif(mode==='history'){
46+
addImport(context,{imported:'createWebHistory'},'vue-router')
47+
returnj.property(
48+
'init',
49+
j.identifier('history'),
50+
j.callExpression(j.identifier('createWebHistory'),[])
51+
)
52+
}elseif(mode==='abstract'){
53+
addImport(
54+
context,
55+
{imported:'createMemoryHistory'},
56+
'vue-router'
57+
)
58+
returnj.property(
59+
'init',
60+
j.identifier('history'),
61+
j.callExpression(j.identifier('createMemoryHistory'),[])
62+
)
63+
}else{
64+
// TODO: warn
65+
}
66+
}
67+
68+
returnp
69+
})
570

6-
// TODO: new Router() -> createRouter()
7-
// TODO: mode: 'history' -> history: createWebHistory()
71+
returnj.callExpression(j.identifier('createRouter'),node.arguments)
72+
})
73+
removeExtraneousImport(context,localVueRouter)
74+
}
875

976
returnroot.toSource({lineTerminator:'\n'})
1077
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp