@@ -10,9 +10,9 @@ export function createRouteMap (
10
10
oldPathMap ?: Dictionary < RouteRecord > ,
11
11
oldNameMap ?: Dictionary < RouteRecord >
12
12
) : {
13
- pathList : Array < string > ;
14
- pathMap : Dictionary < RouteRecord > ;
15
- nameMap : Dictionary < RouteRecord > ;
13
+ pathList : Array < string > ,
14
+ pathMap : Dictionary < RouteRecord > ,
15
+ nameMap : Dictionary < RouteRecord >
16
16
} {
17
17
// the path list is used to control path matching priority
18
18
const pathList : Array < string > = oldPathList || [ ]
@@ -54,17 +54,15 @@ function addRouteRecord (
54
54
assert ( path != null , `"path" is required in a route configuration.` )
55
55
assert (
56
56
typeof route . component !== 'string' ,
57
- `route config "component" for path: ${ String ( path || name ) } cannot be a ` +
58
- `string id. Use an actual component instead.`
57
+ `route config "component" for path: ${ String (
58
+ path || name
59
+ ) } cannot be a ` + `string id. Use an actual component instead.`
59
60
)
60
61
}
61
62
62
- const pathToRegexpOptions : PathToRegexpOptions = route . pathToRegexpOptions || { }
63
- const normalizedPath = normalizePath (
64
- path ,
65
- parent ,
66
- pathToRegexpOptions . strict
67
- )
63
+ const pathToRegexpOptions : PathToRegexpOptions =
64
+ route . pathToRegexpOptions || { }
65
+ const normalizedPath = normalizePath ( path , parent , pathToRegexpOptions . strict )
68
66
69
67
if ( typeof route . caseSensitive === 'boolean' ) {
70
68
pathToRegexpOptions . sensitive = route . caseSensitive
@@ -81,26 +79,33 @@ function addRouteRecord (
81
79
redirect : route . redirect ,
82
80
beforeEnter : route . beforeEnter ,
83
81
meta : route . meta || { } ,
84
- props : route . props == null
85
- ? { }
86
- : route . components
87
- ? route . props
88
- : { default : route . props }
82
+ props :
83
+ route . props == null
84
+ ? { }
85
+ : route . components
86
+ ? route . props
87
+ : { default : route . props }
89
88
}
90
89
91
90
if ( route . children ) {
92
91
// Warn if route is named, does not redirect and has a default child route.
93
92
// If users navigate to this route by name, the default child will
94
93
// not be rendered (GH Issue #629)
95
94
if ( process . env . NODE_ENV !== 'production' ) {
96
- if ( route . name && ! route . redirect && route . children . some ( child => / ^ \/ ? $ / . test ( child . path ) ) ) {
95
+ if (
96
+ route . name &&
97
+ ! route . redirect &&
98
+ route . children . some ( child => / ^ \/ ? $ / . test ( child . path ) )
99
+ ) {
97
100
warn (
98
101
false ,
99
102
`Named Route '${ route . name } ' has a default child route. ` +
100
- `When navigating to this named route (:to="{name: '${ route . name } '"), ` +
101
- `the default child route will not be rendered. Remove the name from ` +
102
- `this route and use the name of the default child route for named ` +
103
- `links instead.`
103
+ `When navigating to this named route (:to="{name: '${
104
+ route . name
105
+ } '"), ` +
106
+ `the default child route will not be rendered. Remove the name from ` +
107
+ `this route and use the name of the default child route for named ` +
108
+ `links instead.`
104
109
)
105
110
}
106
111
}
@@ -112,12 +117,24 @@ function addRouteRecord (
112
117
} )
113
118
}
114
119
120
+ if ( ! pathMap [ record . path ] ) {
121
+ pathList . push ( record . path )
122
+ pathMap [ record . path ] = record
123
+ }
124
+
115
125
if ( route . alias !== undefined ) {
116
- const aliases = Array . isArray ( route . alias )
117
- ? route . alias
118
- : [ route . alias ]
126
+ const aliases = Array . isArray ( route . alias ) ? route . alias : [ route . alias ]
127
+ for ( let i = 0 ; i < aliases . length ; ++ i ) {
128
+ const alias = aliases [ i ]
129
+ if ( process . env . NODE_ENV !== 'production' && alias === path ) {
130
+ warn (
131
+ false ,
132
+ `Found an alias with the same value as the path: "${ path } ". You have to remove that alias. It will be ignored in development.`
133
+ )
134
+ // skip in dev to make it work
135
+ continue
136
+ }
119
137
120
- aliases . forEach ( alias => {
121
138
const aliasRoute = {
122
139
path : alias ,
123
140
children : route . children
@@ -130,12 +147,7 @@ function addRouteRecord (
130
147
parent ,
131
148
record . path || '/' // matchAs
132
149
)
133
- } )
134
- }
135
-
136
- if ( ! pathMap [ record . path ] ) {
137
- pathList . push ( record . path )
138
- pathMap [ record . path ] = record
150
+ }
139
151
}
140
152
141
153
if ( name ) {
@@ -145,25 +157,35 @@ function addRouteRecord (
145
157
warn (
146
158
false ,
147
159
`Duplicate named routes definition: ` +
148
- `{ name: "${ name } ", path: "${ record . path } " }`
160
+ `{ name: "${ name } ", path: "${ record . path } " }`
149
161
)
150
162
}
151
163
}
152
164
}
153
165
154
- function compileRouteRegex ( path : string , pathToRegexpOptions : PathToRegexpOptions ) : RouteRegExp {
166
+ function compileRouteRegex (
167
+ path : string ,
168
+ pathToRegexpOptions : PathToRegexpOptions
169
+ ) : RouteRegExp {
155
170
const regex = Regexp ( path , [ ] , pathToRegexpOptions )
156
171
if ( process . env . NODE_ENV !== 'production' ) {
157
172
const keys : any = Object . create ( null )
158
173
regex . keys . forEach ( key => {
159
- warn ( ! keys [ key . name ] , `Duplicate param keys in route with path: "${ path } "` )
174
+ warn (
175
+ ! keys [ key . name ] ,
176
+ `Duplicate param keys in route with path: "${ path } "`
177
+ )
160
178
keys [ key . name ] = true
161
179
} )
162
180
}
163
181
return regex
164
182
}
165
183
166
- function normalizePath ( path : string , parent ? : RouteRecord , strict ? : boolean ) : string {
184
+ function normalizePath (
185
+ path : string ,
186
+ parent ? : RouteRecord ,
187
+ strict ? : boolean
188
+ ) : string {
167
189
if ( ! strict ) path = path . replace ( / \/ $ / , '' )
168
190
if ( path [ 0 ] === '/' ) return path
169
191
if ( parent == null ) return path
0 commit comments