kdu-router 3.5.4 → 3.6.1

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 -11
  3. package/dist/composables.js +253 -0
  4. package/dist/composables.mjs +244 -0
  5. package/dist/kdu-router.common.js +2668 -2665
  6. package/dist/kdu-router.esm.browser.js +2677 -2671
  7. package/dist/kdu-router.esm.browser.min.js +5 -5
  8. package/dist/kdu-router.esm.js +2672 -2666
  9. package/dist/kdu-router.js +2675 -2672
  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 -90
  14. package/src/components/link.js +224 -224
  15. package/src/components/view.js +155 -155
  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 -226
  22. package/src/create-route-map.js +220 -220
  23. package/src/entries/cjs.js +3 -0
  24. package/src/entries/esm.js +12 -0
  25. package/src/history/abstract.js +72 -72
  26. package/src/history/base.js +379 -379
  27. package/src/history/hash.js +152 -152
  28. package/src/history/html5.js +99 -99
  29. package/src/index.js +3 -293
  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 -86
  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 -113
  41. package/src/util/resolve-components.js +109 -109
  42. package/src/util/route.js +151 -151
  43. package/src/util/scroll.js +175 -175
  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 -21
  48. package/types/kdu.d.ts +22 -22
  49. package/types/router.d.ts +564 -211
@@ -1,220 +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
- 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
- }
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,72 +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
- 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
- }
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
+ }