@tanstack/router-core 0.0.1-beta.19 → 0.0.1-beta.191

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 (82) hide show
  1. package/LICENSE +21 -0
  2. package/build/cjs/defer.js +39 -0
  3. package/build/cjs/defer.js.map +1 -0
  4. package/build/cjs/fileRoute.js +29 -0
  5. package/build/cjs/fileRoute.js.map +1 -0
  6. package/build/cjs/history.js +228 -0
  7. package/build/cjs/history.js.map +1 -0
  8. package/build/cjs/index.js +86 -0
  9. package/build/cjs/{packages/router-core/src/index.js.map → index.js.map} +1 -1
  10. package/build/cjs/{packages/router-core/src/path.js → path.js} +45 -56
  11. package/build/cjs/path.js.map +1 -0
  12. package/build/cjs/{packages/router-core/src/qss.js → qss.js} +10 -16
  13. package/build/cjs/qss.js.map +1 -0
  14. package/build/cjs/route.js +114 -0
  15. package/build/cjs/route.js.map +1 -0
  16. package/build/cjs/router.js +1267 -0
  17. package/build/cjs/router.js.map +1 -0
  18. package/build/cjs/scroll-restoration.js +139 -0
  19. package/build/cjs/scroll-restoration.js.map +1 -0
  20. package/build/cjs/{packages/router-core/src/searchParams.js → searchParams.js} +32 -19
  21. package/build/cjs/searchParams.js.map +1 -0
  22. package/build/cjs/{packages/router-core/src/utils.js → utils.js} +69 -64
  23. package/build/cjs/utils.js.map +1 -0
  24. package/build/esm/index.js +1746 -2121
  25. package/build/esm/index.js.map +1 -1
  26. package/build/stats-html.html +59 -49
  27. package/build/stats-react.json +197 -211
  28. package/build/types/defer.d.ts +19 -0
  29. package/build/types/fileRoute.d.ts +35 -0
  30. package/build/types/history.d.ts +36 -0
  31. package/build/types/index.d.ts +13 -609
  32. package/build/types/link.d.ts +96 -0
  33. package/build/types/path.d.ts +16 -0
  34. package/build/types/qss.d.ts +2 -0
  35. package/build/types/route.d.ts +251 -0
  36. package/build/types/routeInfo.d.ts +22 -0
  37. package/build/types/router.d.ts +260 -0
  38. package/build/types/scroll-restoration.d.ts +6 -0
  39. package/build/types/searchParams.d.ts +5 -0
  40. package/build/types/utils.d.ts +44 -0
  41. package/build/umd/index.development.js +1978 -2243
  42. package/build/umd/index.development.js.map +1 -1
  43. package/build/umd/index.production.js +13 -2
  44. package/build/umd/index.production.js.map +1 -1
  45. package/package.json +11 -7
  46. package/src/defer.ts +55 -0
  47. package/src/fileRoute.ts +161 -0
  48. package/src/history.ts +300 -0
  49. package/src/index.ts +5 -10
  50. package/src/link.ts +136 -125
  51. package/src/path.ts +37 -17
  52. package/src/qss.ts +1 -2
  53. package/src/route.ts +948 -218
  54. package/src/routeInfo.ts +45 -211
  55. package/src/router.ts +1778 -1075
  56. package/src/scroll-restoration.ts +179 -0
  57. package/src/searchParams.ts +31 -9
  58. package/src/utils.ts +84 -49
  59. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +0 -33
  60. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +0 -1
  61. package/build/cjs/node_modules/@babel/runtime/helpers/esm/extends.js +0 -33
  62. package/build/cjs/node_modules/@babel/runtime/helpers/esm/extends.js.map +0 -1
  63. package/build/cjs/node_modules/history/index.js +0 -815
  64. package/build/cjs/node_modules/history/index.js.map +0 -1
  65. package/build/cjs/node_modules/tiny-invariant/dist/esm/tiny-invariant.js +0 -30
  66. package/build/cjs/node_modules/tiny-invariant/dist/esm/tiny-invariant.js.map +0 -1
  67. package/build/cjs/packages/router-core/src/index.js +0 -58
  68. package/build/cjs/packages/router-core/src/path.js.map +0 -1
  69. package/build/cjs/packages/router-core/src/qss.js.map +0 -1
  70. package/build/cjs/packages/router-core/src/route.js +0 -147
  71. package/build/cjs/packages/router-core/src/route.js.map +0 -1
  72. package/build/cjs/packages/router-core/src/routeConfig.js +0 -69
  73. package/build/cjs/packages/router-core/src/routeConfig.js.map +0 -1
  74. package/build/cjs/packages/router-core/src/routeMatch.js +0 -220
  75. package/build/cjs/packages/router-core/src/routeMatch.js.map +0 -1
  76. package/build/cjs/packages/router-core/src/router.js +0 -870
  77. package/build/cjs/packages/router-core/src/router.js.map +0 -1
  78. package/build/cjs/packages/router-core/src/searchParams.js.map +0 -1
  79. package/build/cjs/packages/router-core/src/utils.js.map +0 -1
  80. package/src/frameworks.ts +0 -11
  81. package/src/routeConfig.ts +0 -511
  82. package/src/routeMatch.ts +0 -312
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tanstack/router-core",
3
3
  "author": "Tanner Linsley",
4
- "version": "0.0.1-beta.19",
4
+ "version": "0.0.1-beta.191",
5
5
  "license": "MIT",
6
6
  "repository": "tanstack/router",
7
7
  "homepage": "https://tanstack.com/router",
@@ -23,7 +23,7 @@
23
23
  "url": "https://github.com/sponsors/tannerlinsley"
24
24
  },
25
25
  "module": "build/esm/index.js",
26
- "main": "build/cjs/packages/router-core/src/index.js",
26
+ "main": "build/cjs/index.js",
27
27
  "browser": "build/umd/index.production.js",
28
28
  "types": "build/types/index.d.ts",
29
29
  "engines": {
@@ -40,10 +40,14 @@
40
40
  },
41
41
  "dependencies": {
42
42
  "@babel/runtime": "^7.16.7",
43
- "history": "^5.2.0",
44
- "tiny-invariant": "^1.3.1"
43
+ "tiny-invariant": "^1.3.1",
44
+ "tiny-warning": "^1.0.3",
45
+ "@tanstack/store": "^0.0.1",
46
+ "@gisatcz/cross-package-react-context": "^0.2.0"
45
47
  },
46
- "devDependencies": {
47
- "babel-plugin-transform-async-to-promises": "^0.8.18"
48
+ "scripts": {
49
+ "build": "rollup --config rollup.config.js",
50
+ "test": "vitest",
51
+ "test:dev": "vitest --watch"
48
52
  }
49
- }
53
+ }
package/src/defer.ts ADDED
@@ -0,0 +1,55 @@
1
+ export type DeferredPromiseState<T> = { uid: string } & (
2
+ | {
3
+ status: 'pending'
4
+ data?: T
5
+ error?: unknown
6
+ }
7
+ | {
8
+ status: 'success'
9
+ data: T
10
+ }
11
+ | {
12
+ status: 'error'
13
+ data?: T
14
+ error: unknown
15
+ }
16
+ )
17
+
18
+ export type DeferredPromise<T> = Promise<T> & {
19
+ __deferredState: DeferredPromiseState<T>
20
+ }
21
+
22
+ export function defer<T>(_promise: Promise<T>) {
23
+ const promise = _promise as DeferredPromise<T>
24
+
25
+ if (!promise.__deferredState) {
26
+ promise.__deferredState = {
27
+ uid: Math.random().toString(36).slice(2),
28
+ status: 'pending',
29
+ }
30
+
31
+ const state = promise.__deferredState
32
+
33
+ promise
34
+ .then((data) => {
35
+ state.status = 'success' as any
36
+ state.data = data
37
+ })
38
+ .catch((error) => {
39
+ state.status = 'error' as any
40
+ state.error = error
41
+ })
42
+ }
43
+
44
+ return promise
45
+ }
46
+
47
+ export function isDehydratedDeferred(obj: any): boolean {
48
+ return (
49
+ typeof obj === 'object' &&
50
+ obj !== null &&
51
+ !(obj instanceof Promise) &&
52
+ !obj.then &&
53
+ '__deferredState' in obj
54
+ )
55
+ }
@@ -0,0 +1,161 @@
1
+ import { ParsePathParams } from './link'
2
+ import {
3
+ AnyRoute,
4
+ ResolveFullPath,
5
+ ResolveFullSearchSchema,
6
+ MergeFromFromParent,
7
+ RouteContext,
8
+ AnyContext,
9
+ RouteOptions,
10
+ InferFullSearchSchema,
11
+ UpdatableRouteOptions,
12
+ Route,
13
+ AnyPathParams,
14
+ RootRouteId,
15
+ TrimPathLeft,
16
+ RouteConstraints,
17
+ } from './route'
18
+
19
+ export interface FileRoutesByPath {
20
+ // '/': {
21
+ // parentRoute: typeof rootRoute
22
+ // }
23
+ }
24
+
25
+ type Replace<
26
+ S extends string,
27
+ From extends string,
28
+ To extends string,
29
+ > = S extends `${infer Start}${From}${infer Rest}`
30
+ ? `${Start}${To}${Replace<Rest, From, To>}`
31
+ : S
32
+
33
+ export type TrimLeft<
34
+ T extends string,
35
+ S extends string,
36
+ > = T extends `${S}${infer U}` ? U : T
37
+
38
+ export type TrimRight<
39
+ T extends string,
40
+ S extends string,
41
+ > = T extends `${infer U}${S}` ? U : T
42
+
43
+ export type Trim<T extends string, S extends string> = TrimLeft<
44
+ TrimRight<T, S>,
45
+ S
46
+ >
47
+
48
+ export type RemoveUnderScores<T extends string> = Replace<
49
+ Replace<TrimRight<TrimLeft<T, '/_'>, '_'>, '_/', '/'>,
50
+ '/_',
51
+ '/'
52
+ >
53
+
54
+ export type ResolveFilePath<
55
+ TParentRoute extends AnyRoute,
56
+ TFilePath extends string,
57
+ > = TParentRoute['id'] extends RootRouteId
58
+ ? TrimPathLeft<TFilePath>
59
+ : Replace<
60
+ TrimPathLeft<TFilePath>,
61
+ TrimPathLeft<TParentRoute['types']['customId']>,
62
+ ''
63
+ >
64
+
65
+ export type FileRoutePath<
66
+ TParentRoute extends AnyRoute,
67
+ TFilePath extends string,
68
+ > = ResolveFilePath<TParentRoute, TFilePath> extends `_${infer _}`
69
+ ? string
70
+ : ResolveFilePath<TParentRoute, TFilePath>
71
+
72
+ export class FileRoute<
73
+ TFilePath extends keyof FileRoutesByPath,
74
+ TParentRoute extends AnyRoute = FileRoutesByPath[TFilePath]['parentRoute'],
75
+ TId extends RouteConstraints['TId'] = TFilePath,
76
+ TPath extends RouteConstraints['TPath'] = FileRoutePath<
77
+ TParentRoute,
78
+ TFilePath
79
+ >,
80
+ TFullPath extends RouteConstraints['TFullPath'] = ResolveFullPath<
81
+ TParentRoute,
82
+ RemoveUnderScores<TPath>
83
+ >,
84
+ > {
85
+ constructor(public path: TFilePath) {}
86
+
87
+ createRoute = <
88
+ TLoader = unknown,
89
+ TSearchSchema extends RouteConstraints['TSearchSchema'] = {},
90
+ TFullSearchSchema extends RouteConstraints['TFullSearchSchema'] = ResolveFullSearchSchema<
91
+ TParentRoute,
92
+ TSearchSchema
93
+ >,
94
+ TParams extends RouteConstraints['TParams'] = ParsePathParams<TPath> extends never
95
+ ? AnyPathParams
96
+ : Record<ParsePathParams<TPath>, RouteConstraints['TPath']>,
97
+ TAllParams extends RouteConstraints['TAllParams'] = MergeFromFromParent<
98
+ TParentRoute['types']['allParams'],
99
+ TParams
100
+ >,
101
+ TParentContext extends RouteConstraints['TParentContext'] = TParentRoute['types']['routeContext'],
102
+ TAllParentContext extends RouteConstraints['TId'] = TParentRoute['types']['context'],
103
+ TRouteContext extends RouteConstraints['TRouteContext'] = RouteContext,
104
+ TContext extends RouteConstraints['TAllContext'] = MergeFromFromParent<
105
+ TParentRoute['types']['context'],
106
+ TRouteContext
107
+ >,
108
+ TRouterContext extends RouteConstraints['TRouterContext'] = AnyContext,
109
+ TChildren extends RouteConstraints['TChildren'] = unknown,
110
+ TRouteTree extends RouteConstraints['TRouteTree'] = AnyRoute,
111
+ >(
112
+ options: Omit<
113
+ RouteOptions<
114
+ TParentRoute,
115
+ string,
116
+ string,
117
+ TLoader,
118
+ InferFullSearchSchema<TParentRoute>,
119
+ TSearchSchema,
120
+ TFullSearchSchema,
121
+ TParams,
122
+ TAllParams,
123
+ TParentContext,
124
+ TAllParentContext,
125
+ TRouteContext,
126
+ TContext
127
+ >,
128
+ 'getParentRoute' | 'path' | 'id'
129
+ > &
130
+ UpdatableRouteOptions<
131
+ TLoader,
132
+ TSearchSchema,
133
+ TFullSearchSchema,
134
+ TAllParams,
135
+ TRouteContext,
136
+ TContext
137
+ >,
138
+ ): Route<
139
+ TParentRoute,
140
+ TPath,
141
+ TFullPath,
142
+ TFilePath,
143
+ TId,
144
+ TLoader,
145
+ TSearchSchema,
146
+ TFullSearchSchema,
147
+ TParams,
148
+ TAllParams,
149
+ TParentContext,
150
+ TAllParentContext,
151
+ TRouteContext,
152
+ TContext,
153
+ TRouterContext,
154
+ TChildren,
155
+ TRouteTree
156
+ > => {
157
+ const route = new Route(options as any)
158
+ ;(route as any).isRoot = false
159
+ return route as any
160
+ }
161
+ }
package/src/history.ts ADDED
@@ -0,0 +1,300 @@
1
+ // While the public API was clearly inspired by the "history" npm package,
2
+ // This implementation attempts to be more lightweight by
3
+ // making assumptions about the way TanStack Router works
4
+
5
+ export interface RouterHistory {
6
+ location: HistoryLocation
7
+ subscribe: (cb: () => void) => () => void
8
+ push: (path: string, state?: any) => void
9
+ replace: (path: string, state?: any) => void
10
+ go: (index: number) => void
11
+ back: () => void
12
+ forward: () => void
13
+ createHref: (href: string) => string
14
+ block: (blockerFn: BlockerFn) => () => void
15
+ }
16
+
17
+ export interface HistoryLocation extends ParsedPath {
18
+ state: HistoryState
19
+ }
20
+
21
+ export interface ParsedPath {
22
+ href: string
23
+ pathname: string
24
+ search: string
25
+ hash: string
26
+ }
27
+
28
+ export interface HistoryState {
29
+ key: string
30
+ __tempLocation?: HistoryLocation
31
+ __tempKey?: string
32
+ }
33
+
34
+ type BlockerFn = (retry: () => void, cancel: () => void) => void
35
+
36
+ const pushStateEvent = 'pushstate'
37
+ const popStateEvent = 'popstate'
38
+ const beforeUnloadEvent = 'beforeunload'
39
+
40
+ const beforeUnloadListener = (event: Event) => {
41
+ event.preventDefault()
42
+ // @ts-ignore
43
+ return (event.returnValue = '')
44
+ }
45
+
46
+ const stopBlocking = () => {
47
+ removeEventListener(beforeUnloadEvent, beforeUnloadListener, {
48
+ capture: true,
49
+ })
50
+ }
51
+
52
+ function createHistory(opts: {
53
+ getLocation: () => HistoryLocation
54
+ subscriber: false | ((onUpdate: () => void) => () => void)
55
+ pushState: (path: string, state: any) => void
56
+ replaceState: (path: string, state: any) => void
57
+ go: (n: number) => void
58
+ back: () => void
59
+ forward: () => void
60
+ createHref: (path: string) => string
61
+ }): RouterHistory {
62
+ let location = opts.getLocation()
63
+ let unsub = () => {}
64
+ let subscribers = new Set<() => void>()
65
+ let blockers: BlockerFn[] = []
66
+ let queue: (() => void)[] = []
67
+
68
+ const tryFlush = () => {
69
+ if (blockers.length) {
70
+ blockers[0]?.(tryFlush, () => {
71
+ blockers = []
72
+ stopBlocking()
73
+ })
74
+ return
75
+ }
76
+
77
+ while (queue.length) {
78
+ queue.shift()?.()
79
+ }
80
+
81
+ if (!opts.subscriber) {
82
+ onUpdate()
83
+ }
84
+ }
85
+
86
+ const queueTask = (task: () => void) => {
87
+ queue.push(task)
88
+ tryFlush()
89
+ }
90
+
91
+ const onUpdate = () => {
92
+ location = opts.getLocation()
93
+ subscribers.forEach((subscriber) => subscriber())
94
+ }
95
+
96
+ return {
97
+ get location() {
98
+ return location
99
+ },
100
+ subscribe: (cb: () => void) => {
101
+ if (subscribers.size === 0) {
102
+ unsub =
103
+ typeof opts.subscriber === 'function'
104
+ ? opts.subscriber(onUpdate)
105
+ : () => {}
106
+ }
107
+ subscribers.add(cb)
108
+
109
+ return () => {
110
+ subscribers.delete(cb)
111
+ if (subscribers.size === 0) {
112
+ unsub()
113
+ }
114
+ }
115
+ },
116
+ push: (path: string, state: any) => {
117
+ assignKey(state)
118
+ queueTask(() => {
119
+ opts.pushState(path, state)
120
+ })
121
+ },
122
+ replace: (path: string, state: any) => {
123
+ assignKey(state)
124
+ queueTask(() => {
125
+ opts.replaceState(path, state)
126
+ })
127
+ },
128
+ go: (index) => {
129
+ queueTask(() => {
130
+ opts.go(index)
131
+ })
132
+ },
133
+ back: () => {
134
+ queueTask(() => {
135
+ opts.back()
136
+ })
137
+ },
138
+ forward: () => {
139
+ queueTask(() => {
140
+ opts.forward()
141
+ })
142
+ },
143
+ createHref: (str) => opts.createHref(str),
144
+ block: (cb) => {
145
+ blockers.push(cb)
146
+
147
+ if (blockers.length === 1) {
148
+ addEventListener(beforeUnloadEvent, beforeUnloadListener, {
149
+ capture: true,
150
+ })
151
+ }
152
+
153
+ return () => {
154
+ blockers = blockers.filter((b) => b !== cb)
155
+
156
+ if (!blockers.length) {
157
+ stopBlocking()
158
+ }
159
+ }
160
+ },
161
+ }
162
+ }
163
+
164
+ function assignKey(state: HistoryState) {
165
+ state.key = createRandomKey()
166
+ // if (state.__actualLocation) {
167
+ // state.__actualLocation.state = {
168
+ // ...state.__actualLocation.state,
169
+ // key,
170
+ // }
171
+ // }
172
+ }
173
+
174
+ export function createBrowserHistory(opts?: {
175
+ getHref?: () => string
176
+ createHref?: (path: string) => string
177
+ }): RouterHistory {
178
+ const getHref =
179
+ opts?.getHref ??
180
+ (() =>
181
+ `${window.location.pathname}${window.location.search}${window.location.hash}`)
182
+
183
+ const createHref = opts?.createHref ?? ((path) => path)
184
+
185
+ const getLocation = () => parseLocation(getHref(), window.history.state)
186
+
187
+ return createHistory({
188
+ getLocation,
189
+ subscriber: (onUpdate) => {
190
+ window.addEventListener(pushStateEvent, onUpdate)
191
+ window.addEventListener(popStateEvent, onUpdate)
192
+
193
+ var pushState = window.history.pushState
194
+ window.history.pushState = function () {
195
+ let res = pushState.apply(history, arguments as any)
196
+ onUpdate()
197
+ return res
198
+ }
199
+ var replaceState = window.history.replaceState
200
+ window.history.replaceState = function () {
201
+ let res = replaceState.apply(history, arguments as any)
202
+ onUpdate()
203
+ return res
204
+ }
205
+
206
+ return () => {
207
+ window.history.pushState = pushState
208
+ window.history.replaceState = replaceState
209
+ window.removeEventListener(pushStateEvent, onUpdate)
210
+ window.removeEventListener(popStateEvent, onUpdate)
211
+ }
212
+ },
213
+ pushState: (path, state) => {
214
+ window.history.pushState(state, '', createHref(path))
215
+ },
216
+ replaceState: (path, state) => {
217
+ window.history.replaceState(state, '', createHref(path))
218
+ },
219
+ back: () => window.history.back(),
220
+ forward: () => window.history.forward(),
221
+ go: (n) => window.history.go(n),
222
+ createHref: (path) => createHref(path),
223
+ })
224
+ }
225
+
226
+ export function createHashHistory(): RouterHistory {
227
+ return createBrowserHistory({
228
+ getHref: () => window.location.hash.substring(1),
229
+ createHref: (path) => `#${path}`,
230
+ })
231
+ }
232
+
233
+ export function createMemoryHistory(
234
+ opts: {
235
+ initialEntries: string[]
236
+ initialIndex?: number
237
+ } = {
238
+ initialEntries: ['/'],
239
+ },
240
+ ): RouterHistory {
241
+ const entries = opts.initialEntries
242
+ let index = opts.initialIndex ?? entries.length - 1
243
+ let currentState = {
244
+ key: createRandomKey(),
245
+ } as HistoryState
246
+
247
+ const getLocation = () => parseLocation(entries[index]!, currentState)
248
+
249
+ return createHistory({
250
+ getLocation,
251
+ subscriber: false,
252
+ pushState: (path, state) => {
253
+ currentState = state
254
+ entries.push(path)
255
+ index++
256
+ },
257
+ replaceState: (path, state) => {
258
+ currentState = state
259
+ entries[index] = path
260
+ },
261
+ back: () => {
262
+ index--
263
+ },
264
+ forward: () => {
265
+ index = Math.min(index + 1, entries.length - 1)
266
+ },
267
+ go: (n) => window.history.go(n),
268
+ createHref: (path) => path,
269
+ })
270
+ }
271
+
272
+ function parseLocation(href: string, state: HistoryState): HistoryLocation {
273
+ let hashIndex = href.indexOf('#')
274
+ let searchIndex = href.indexOf('?')
275
+
276
+ return {
277
+ href,
278
+ pathname: href.substring(
279
+ 0,
280
+ hashIndex > 0
281
+ ? searchIndex > 0
282
+ ? Math.min(hashIndex, searchIndex)
283
+ : hashIndex
284
+ : searchIndex > 0
285
+ ? searchIndex
286
+ : href.length,
287
+ ),
288
+ hash: hashIndex > -1 ? href.substring(hashIndex) : '',
289
+ search:
290
+ searchIndex > -1
291
+ ? href.slice(searchIndex, hashIndex === -1 ? undefined : hashIndex)
292
+ : '',
293
+ state: state || {},
294
+ }
295
+ }
296
+
297
+ // Thanks co-pilot!
298
+ function createRandomKey() {
299
+ return (Math.random() + 1).toString(36).substring(7)
300
+ }
package/src/index.ts CHANGED
@@ -1,19 +1,14 @@
1
- export {
2
- createHashHistory,
3
- createBrowserHistory,
4
- createMemoryHistory,
5
- } from 'history'
6
-
7
1
  export { default as invariant } from 'tiny-invariant'
8
-
9
- export * from './frameworks'
2
+ export { default as warning } from 'tiny-warning'
3
+ export * from './history'
10
4
  export * from './link'
11
5
  export * from './path'
12
6
  export * from './qss'
13
7
  export * from './route'
14
- export * from './routeConfig'
8
+ export * from './fileRoute'
15
9
  export * from './routeInfo'
16
- export * from './routeMatch'
17
10
  export * from './router'
18
11
  export * from './searchParams'
19
12
  export * from './utils'
13
+ export * from './scroll-restoration'
14
+ export * from './defer'