kdu-router 3.4.0-beta.0 → 3.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +11 -9
  3. package/dist/composables.js +253 -0
  4. package/dist/composables.mjs +244 -0
  5. package/dist/kdu-router.common.js +2682 -2572
  6. package/dist/kdu-router.esm.browser.js +2677 -2563
  7. package/dist/kdu-router.esm.browser.min.js +5 -5
  8. package/dist/kdu-router.esm.js +2685 -2572
  9. package/dist/kdu-router.js +2682 -2573
  10. package/dist/kdu-router.min.js +5 -5
  11. package/ketur/attributes.json +38 -38
  12. package/ketur/tags.json +20 -20
  13. package/package.json +115 -111
  14. package/src/components/link.js +224 -197
  15. package/src/components/view.js +155 -149
  16. package/src/composables/globals.js +34 -0
  17. package/src/composables/guards.js +68 -0
  18. package/src/composables/index.js +3 -0
  19. package/src/composables/useLink.js +113 -0
  20. package/src/composables/utils.js +11 -0
  21. package/src/create-matcher.js +226 -200
  22. package/src/create-route-map.js +220 -205
  23. package/src/entries/cjs.js +3 -0
  24. package/src/entries/esm.js +12 -0
  25. package/src/history/abstract.js +72 -68
  26. package/src/history/base.js +379 -400
  27. package/src/history/hash.js +152 -163
  28. package/src/history/html5.js +99 -94
  29. package/src/index.js +3 -277
  30. package/src/install.js +52 -52
  31. package/src/router.js +293 -0
  32. package/src/util/async.js +18 -18
  33. package/src/util/dom.js +3 -3
  34. package/src/util/errors.js +86 -85
  35. package/src/util/location.js +69 -69
  36. package/src/util/misc.js +6 -6
  37. package/src/util/params.js +37 -37
  38. package/src/util/path.js +74 -74
  39. package/src/util/push-state.js +46 -46
  40. package/src/util/query.js +113 -96
  41. package/src/util/resolve-components.js +109 -109
  42. package/src/util/route.js +151 -132
  43. package/src/util/scroll.js +175 -165
  44. package/src/util/state-key.js +22 -22
  45. package/src/util/warn.js +14 -14
  46. package/types/composables.d.ts +53 -0
  47. package/types/index.d.ts +25 -17
  48. package/types/kdu.d.ts +22 -22
  49. package/types/router.d.ts +564 -170
@@ -1,205 +1,220 @@
1
- /* @flow */
2
-
3
- import Regexp from 'path-to-regexp'
4
- import { cleanPath } from './util/path'
5
- import { assert, warn } from './util/warn'
6
-
7
- export function createRouteMap (
8
- routes: Array<RouteConfig>,
9
- oldPathList?: Array<string>,
10
- oldPathMap?: Dictionary<RouteRecord>,
11
- oldNameMap?: Dictionary<RouteRecord>
12
- ): {
13
- pathList: Array<string>,
14
- pathMap: Dictionary<RouteRecord>,
15
- nameMap: Dictionary<RouteRecord>
16
- } {
17
- // the path list is used to control path matching priority
18
- const pathList: Array<string> = oldPathList || []
19
- // $flow-disable-line
20
- const pathMap: Dictionary<RouteRecord> = oldPathMap || Object.create(null)
21
- // $flow-disable-line
22
- const nameMap: Dictionary<RouteRecord> = oldNameMap || Object.create(null)
23
-
24
- routes.forEach(route => {
25
- addRouteRecord(pathList, pathMap, nameMap, route)
26
- })
27
-
28
- // ensure wildcard routes are always at the end
29
- for (let i = 0, l = pathList.length; i < l; i++) {
30
- if (pathList[i] === '*') {
31
- pathList.push(pathList.splice(i, 1)[0])
32
- l--
33
- i--
34
- }
35
- }
36
-
37
- if (process.env.NODE_ENV === 'development') {
38
- // warn if routes do not include leading slashes
39
- const found = pathList
40
- // check for missing leading slash
41
- .filter(path => path && path.charAt(0) !== '*' && path.charAt(0) !== '/')
42
-
43
- if (found.length > 0) {
44
- const pathNames = found.map(path => `- ${path}`).join('\n')
45
- warn(false, `Non-nested routes must include a leading slash character. Fix the following routes: \n${pathNames}`)
46
- }
47
- }
48
-
49
- return {
50
- pathList,
51
- pathMap,
52
- nameMap
53
- }
54
- }
55
-
56
- function addRouteRecord (
57
- pathList: Array<string>,
58
- pathMap: Dictionary<RouteRecord>,
59
- nameMap: Dictionary<RouteRecord>,
60
- route: RouteConfig,
61
- parent?: RouteRecord,
62
- matchAs?: string
63
- ) {
64
- const { path, name } = route
65
- if (process.env.NODE_ENV !== 'production') {
66
- assert(path != null, `"path" is required in a route configuration.`)
67
- assert(
68
- typeof route.component !== 'string',
69
- `route config "component" for path: ${String(
70
- path || name
71
- )} cannot be a ` + `string id. Use an actual component instead.`
72
- )
73
- }
74
-
75
- const pathToRegexpOptions: PathToRegexpOptions =
76
- route.pathToRegexpOptions || {}
77
- const normalizedPath = normalizePath(path, parent, pathToRegexpOptions.strict)
78
-
79
- if (typeof route.caseSensitive === 'boolean') {
80
- pathToRegexpOptions.sensitive = route.caseSensitive
81
- }
82
-
83
- const record: RouteRecord = {
84
- path: normalizedPath,
85
- regex: compileRouteRegex(normalizedPath, pathToRegexpOptions),
86
- components: route.components || { default: route.component },
87
- instances: {},
88
- name,
89
- parent,
90
- matchAs,
91
- redirect: route.redirect,
92
- beforeEnter: route.beforeEnter,
93
- meta: route.meta || {},
94
- props:
95
- route.props == null
96
- ? {}
97
- : route.components
98
- ? route.props
99
- : { default: route.props }
100
- }
101
-
102
- if (route.children) {
103
- // Warn if route is named, does not redirect and has a default child route.
104
- // If users navigate to this route by name, the default child will
105
- // not be rendered (GH Issue #629)
106
- if (process.env.NODE_ENV !== 'production') {
107
- if (
108
- route.name &&
109
- !route.redirect &&
110
- route.children.some(child => /^\/?$/.test(child.path))
111
- ) {
112
- warn(
113
- false,
114
- `Named Route '${route.name}' has a default child route. ` +
115
- `When navigating to this named route (:to="{name: '${
116
- route.name
117
- }'"), ` +
118
- `the default child route will not be rendered. Remove the name from ` +
119
- `this route and use the name of the default child route for named ` +
120
- `links instead.`
121
- )
122
- }
123
- }
124
- route.children.forEach(child => {
125
- const childMatchAs = matchAs
126
- ? cleanPath(`${matchAs}/${child.path}`)
127
- : undefined
128
- addRouteRecord(pathList, pathMap, nameMap, child, record, childMatchAs)
129
- })
130
- }
131
-
132
- if (!pathMap[record.path]) {
133
- pathList.push(record.path)
134
- pathMap[record.path] = record
135
- }
136
-
137
- if (route.alias !== undefined) {
138
- const aliases = Array.isArray(route.alias) ? route.alias : [route.alias]
139
- for (let i = 0; i < aliases.length; ++i) {
140
- const alias = aliases[i]
141
- if (process.env.NODE_ENV !== 'production' && alias === path) {
142
- warn(
143
- false,
144
- `Found an alias with the same value as the path: "${path}". You have to remove that alias. It will be ignored in development.`
145
- )
146
- // skip in dev to make it work
147
- continue
148
- }
149
-
150
- const aliasRoute = {
151
- path: alias,
152
- children: route.children
153
- }
154
- addRouteRecord(
155
- pathList,
156
- pathMap,
157
- nameMap,
158
- aliasRoute,
159
- parent,
160
- record.path || '/' // matchAs
161
- )
162
- }
163
- }
164
-
165
- if (name) {
166
- if (!nameMap[name]) {
167
- nameMap[name] = record
168
- } else if (process.env.NODE_ENV !== 'production' && !matchAs) {
169
- warn(
170
- false,
171
- `Duplicate named routes definition: ` +
172
- `{ name: "${name}", path: "${record.path}" }`
173
- )
174
- }
175
- }
176
- }
177
-
178
- function compileRouteRegex (
179
- path: string,
180
- pathToRegexpOptions: PathToRegexpOptions
181
- ): RouteRegExp {
182
- const regex = Regexp(path, [], pathToRegexpOptions)
183
- if (process.env.NODE_ENV !== 'production') {
184
- const keys: any = Object.create(null)
185
- regex.keys.forEach(key => {
186
- warn(
187
- !keys[key.name],
188
- `Duplicate param keys in route with path: "${path}"`
189
- )
190
- keys[key.name] = true
191
- })
192
- }
193
- return regex
194
- }
195
-
196
- function normalizePath (
197
- path: string,
198
- parent?: RouteRecord,
199
- strict?: boolean
200
- ): string {
201
- if (!strict) path = path.replace(/\/$/, '')
202
- if (path[0] === '/') return path
203
- if (parent == null) return path
204
- return cleanPath(`${parent.path}/${path}`)
205
- }
1
+ /* @flow */
2
+
3
+ import Regexp from 'path-to-regexp'
4
+ import { cleanPath } from './util/path'
5
+ import { assert, warn } from './util/warn'
6
+
7
+ export function createRouteMap (
8
+ routes: Array<RouteConfig>,
9
+ oldPathList?: Array<string>,
10
+ oldPathMap?: Dictionary<RouteRecord>,
11
+ oldNameMap?: Dictionary<RouteRecord>,
12
+ parentRoute?: RouteRecord
13
+ ): {
14
+ pathList: Array<string>,
15
+ pathMap: Dictionary<RouteRecord>,
16
+ nameMap: Dictionary<RouteRecord>
17
+ } {
18
+ // the path list is used to control path matching priority
19
+ const pathList: Array<string> = oldPathList || []
20
+ // $flow-disable-line
21
+ const pathMap: Dictionary<RouteRecord> = oldPathMap || Object.create(null)
22
+ // $flow-disable-line
23
+ const nameMap: Dictionary<RouteRecord> = oldNameMap || Object.create(null)
24
+
25
+ routes.forEach(route => {
26
+ addRouteRecord(pathList, pathMap, nameMap, route, parentRoute)
27
+ })
28
+
29
+ // ensure wildcard routes are always at the end
30
+ for (let i = 0, l = pathList.length; i < l; i++) {
31
+ if (pathList[i] === '*') {
32
+ pathList.push(pathList.splice(i, 1)[0])
33
+ l--
34
+ i--
35
+ }
36
+ }
37
+
38
+ if (process.env.NODE_ENV === 'development') {
39
+ // warn if routes do not include leading slashes
40
+ const found = pathList
41
+ // check for missing leading slash
42
+ .filter(path => path && path.charAt(0) !== '*' && path.charAt(0) !== '/')
43
+
44
+ if (found.length > 0) {
45
+ const pathNames = found.map(path => `- ${path}`).join('\n')
46
+ warn(false, `Non-nested routes must include a leading slash character. Fix the following routes: \n${pathNames}`)
47
+ }
48
+ }
49
+
50
+ return {
51
+ pathList,
52
+ pathMap,
53
+ nameMap
54
+ }
55
+ }
56
+
57
+ function addRouteRecord (
58
+ pathList: Array<string>,
59
+ pathMap: Dictionary<RouteRecord>,
60
+ nameMap: Dictionary<RouteRecord>,
61
+ route: RouteConfig,
62
+ parent?: RouteRecord,
63
+ matchAs?: string
64
+ ) {
65
+ const { path, name } = route
66
+ if (process.env.NODE_ENV !== 'production') {
67
+ assert(path != null, `"path" is required in a route configuration.`)
68
+ assert(
69
+ typeof route.component !== 'string',
70
+ `route config "component" for path: ${String(
71
+ path || name
72
+ )} cannot be a ` + `string id. Use an actual component instead.`
73
+ )
74
+
75
+ warn(
76
+ // eslint-disable-next-line no-control-regex
77
+ !/[^\u0000-\u007F]+/.test(path),
78
+ `Route with path "${path}" contains unencoded characters, make sure ` +
79
+ `your path is correctly encoded before passing it to the router. Use ` +
80
+ `encodeURI to encode static segments of your path.`
81
+ )
82
+ }
83
+
84
+ const pathToRegexpOptions: PathToRegexpOptions =
85
+ route.pathToRegexpOptions || {}
86
+ const normalizedPath = normalizePath(path, parent, pathToRegexpOptions.strict)
87
+
88
+ if (typeof route.caseSensitive === 'boolean') {
89
+ pathToRegexpOptions.sensitive = route.caseSensitive
90
+ }
91
+
92
+ const record: RouteRecord = {
93
+ path: normalizedPath,
94
+ regex: compileRouteRegex(normalizedPath, pathToRegexpOptions),
95
+ components: route.components || { default: route.component },
96
+ alias: route.alias
97
+ ? typeof route.alias === 'string'
98
+ ? [route.alias]
99
+ : route.alias
100
+ : [],
101
+ instances: {},
102
+ enteredCbs: {},
103
+ name,
104
+ parent,
105
+ matchAs,
106
+ redirect: route.redirect,
107
+ beforeEnter: route.beforeEnter,
108
+ meta: route.meta || {},
109
+ props:
110
+ route.props == null
111
+ ? {}
112
+ : route.components
113
+ ? route.props
114
+ : { default: route.props }
115
+ }
116
+
117
+ if (route.children) {
118
+ // Warn if route is named, does not redirect and has a default child route.
119
+ // If users navigate to this route by name, the default child will
120
+ // not be rendered (GH Issue #629)
121
+ if (process.env.NODE_ENV !== 'production') {
122
+ if (
123
+ route.name &&
124
+ !route.redirect &&
125
+ route.children.some(child => /^\/?$/.test(child.path))
126
+ ) {
127
+ warn(
128
+ false,
129
+ `Named Route '${route.name}' has a default child route. ` +
130
+ `When navigating to this named route (:to="{name: '${
131
+ route.name
132
+ }'}"), ` +
133
+ `the default child route will not be rendered. Remove the name from ` +
134
+ `this route and use the name of the default child route for named ` +
135
+ `links instead.`
136
+ )
137
+ }
138
+ }
139
+ route.children.forEach(child => {
140
+ const childMatchAs = matchAs
141
+ ? cleanPath(`${matchAs}/${child.path}`)
142
+ : undefined
143
+ addRouteRecord(pathList, pathMap, nameMap, child, record, childMatchAs)
144
+ })
145
+ }
146
+
147
+ if (!pathMap[record.path]) {
148
+ pathList.push(record.path)
149
+ pathMap[record.path] = record
150
+ }
151
+
152
+ if (route.alias !== undefined) {
153
+ const aliases = Array.isArray(route.alias) ? route.alias : [route.alias]
154
+ for (let i = 0; i < aliases.length; ++i) {
155
+ const alias = aliases[i]
156
+ if (process.env.NODE_ENV !== 'production' && alias === path) {
157
+ warn(
158
+ false,
159
+ `Found an alias with the same value as the path: "${path}". You have to remove that alias. It will be ignored in development.`
160
+ )
161
+ // skip in dev to make it work
162
+ continue
163
+ }
164
+
165
+ const aliasRoute = {
166
+ path: alias,
167
+ children: route.children
168
+ }
169
+ addRouteRecord(
170
+ pathList,
171
+ pathMap,
172
+ nameMap,
173
+ aliasRoute,
174
+ parent,
175
+ record.path || '/' // matchAs
176
+ )
177
+ }
178
+ }
179
+
180
+ if (name) {
181
+ if (!nameMap[name]) {
182
+ nameMap[name] = record
183
+ } else if (process.env.NODE_ENV !== 'production' && !matchAs) {
184
+ warn(
185
+ false,
186
+ `Duplicate named routes definition: ` +
187
+ `{ name: "${name}", path: "${record.path}" }`
188
+ )
189
+ }
190
+ }
191
+ }
192
+
193
+ function compileRouteRegex (
194
+ path: string,
195
+ pathToRegexpOptions: PathToRegexpOptions
196
+ ): RouteRegExp {
197
+ const regex = Regexp(path, [], pathToRegexpOptions)
198
+ if (process.env.NODE_ENV !== 'production') {
199
+ const keys: any = Object.create(null)
200
+ regex.keys.forEach(key => {
201
+ warn(
202
+ !keys[key.name],
203
+ `Duplicate param keys in route with path: "${path}"`
204
+ )
205
+ keys[key.name] = true
206
+ })
207
+ }
208
+ return regex
209
+ }
210
+
211
+ function normalizePath (
212
+ path: string,
213
+ parent?: RouteRecord,
214
+ strict?: boolean
215
+ ): string {
216
+ if (!strict) path = path.replace(/\/$/, '')
217
+ if (path[0] === '/') return path
218
+ if (parent == null) return path
219
+ return cleanPath(`${parent.path}/${path}`)
220
+ }
@@ -0,0 +1,3 @@
1
+ import KduRouter from '../router'
2
+
3
+ export default KduRouter
@@ -0,0 +1,12 @@
1
+ import KduRouter from '../router'
2
+
3
+ export const version = '__VERSION__'
4
+ export { isNavigationFailure, NavigationFailureType } from '../util/errors'
5
+ export { START as START_LOCATION } from '../util/route'
6
+ export { default as RouterLink } from '../components/link'
7
+ export { default as RouterView } from '../components/view'
8
+
9
+ // we can't add the other composables here because people could still be using an older version of kdu and that would
10
+ // create a compilation error trying to import from kdu
11
+
12
+ export default KduRouter
@@ -1,68 +1,72 @@
1
- /* @flow */
2
-
3
- import type Router from '../index'
4
- import { History } from './base'
5
- import { NavigationFailureType, isNavigationFailure } from '../util/errors'
6
-
7
- export class AbstractHistory extends History {
8
- index: number
9
- stack: Array<Route>
10
-
11
- constructor (router: Router, base: ?string) {
12
- super(router, base)
13
- this.stack = []
14
- this.index = -1
15
- }
16
-
17
- push (location: RawLocation, onComplete?: Function, onAbort?: Function) {
18
- this.transitionTo(
19
- location,
20
- route => {
21
- this.stack = this.stack.slice(0, this.index + 1).concat(route)
22
- this.index++
23
- onComplete && onComplete(route)
24
- },
25
- onAbort
26
- )
27
- }
28
-
29
- replace (location: RawLocation, onComplete?: Function, onAbort?: Function) {
30
- this.transitionTo(
31
- location,
32
- route => {
33
- this.stack = this.stack.slice(0, this.index).concat(route)
34
- onComplete && onComplete(route)
35
- },
36
- onAbort
37
- )
38
- }
39
-
40
- go (n: number) {
41
- const targetIndex = this.index + n
42
- if (targetIndex < 0 || targetIndex >= this.stack.length) {
43
- return
44
- }
45
- const route = this.stack[targetIndex]
46
- this.confirmTransition(
47
- route,
48
- () => {
49
- this.index = targetIndex
50
- this.updateRoute(route)
51
- },
52
- err => {
53
- if (isNavigationFailure(err, NavigationFailureType.duplicated)) {
54
- this.index = targetIndex
55
- }
56
- }
57
- )
58
- }
59
-
60
- getCurrentLocation () {
61
- const current = this.stack[this.stack.length - 1]
62
- return current ? current.fullPath : '/'
63
- }
64
-
65
- ensureURL () {
66
- // noop
67
- }
68
- }
1
+ /* @flow */
2
+
3
+ import type Router from '../index'
4
+ import { History } from './base'
5
+ import { NavigationFailureType, isNavigationFailure } from '../util/errors'
6
+
7
+ export class AbstractHistory extends History {
8
+ index: number
9
+ stack: Array<Route>
10
+
11
+ constructor (router: Router, base: ?string) {
12
+ super(router, base)
13
+ this.stack = []
14
+ this.index = -1
15
+ }
16
+
17
+ push (location: RawLocation, onComplete?: Function, onAbort?: Function) {
18
+ this.transitionTo(
19
+ location,
20
+ route => {
21
+ this.stack = this.stack.slice(0, this.index + 1).concat(route)
22
+ this.index++
23
+ onComplete && onComplete(route)
24
+ },
25
+ onAbort
26
+ )
27
+ }
28
+
29
+ replace (location: RawLocation, onComplete?: Function, onAbort?: Function) {
30
+ this.transitionTo(
31
+ location,
32
+ route => {
33
+ this.stack = this.stack.slice(0, this.index).concat(route)
34
+ onComplete && onComplete(route)
35
+ },
36
+ onAbort
37
+ )
38
+ }
39
+
40
+ go (n: number) {
41
+ const targetIndex = this.index + n
42
+ if (targetIndex < 0 || targetIndex >= this.stack.length) {
43
+ return
44
+ }
45
+ const route = this.stack[targetIndex]
46
+ this.confirmTransition(
47
+ route,
48
+ () => {
49
+ const prev = this.current
50
+ this.index = targetIndex
51
+ this.updateRoute(route)
52
+ this.router.afterHooks.forEach(hook => {
53
+ hook && hook(route, prev)
54
+ })
55
+ },
56
+ err => {
57
+ if (isNavigationFailure(err, NavigationFailureType.duplicated)) {
58
+ this.index = targetIndex
59
+ }
60
+ }
61
+ )
62
+ }
63
+
64
+ getCurrentLocation () {
65
+ const current = this.stack[this.stack.length - 1]
66
+ return current ? current.fullPath : '/'
67
+ }
68
+
69
+ ensureURL () {
70
+ // noop
71
+ }
72
+ }