@tanstack/router-core 0.0.1-beta.13 → 0.0.1-beta.145

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 (60) hide show
  1. package/LICENSE +21 -0
  2. package/build/cjs/history.js +226 -0
  3. package/build/cjs/history.js.map +1 -0
  4. package/build/cjs/{packages/router-core/src/index.js → index.js} +33 -15
  5. package/build/cjs/{packages/router-core/src/index.js.map → index.js.map} +1 -1
  6. package/build/cjs/{packages/router-core/src/path.js → path.js} +45 -56
  7. package/build/cjs/path.js.map +1 -0
  8. package/build/cjs/{packages/router-core/src/qss.js → qss.js} +10 -16
  9. package/build/cjs/qss.js.map +1 -0
  10. package/build/cjs/route.js +147 -0
  11. package/build/cjs/route.js.map +1 -0
  12. package/build/cjs/router.js +1102 -0
  13. package/build/cjs/router.js.map +1 -0
  14. package/build/cjs/{packages/router-core/src/searchParams.js → searchParams.js} +11 -13
  15. package/build/cjs/searchParams.js.map +1 -0
  16. package/build/cjs/{packages/router-core/src/utils.js → utils.js} +54 -64
  17. package/build/cjs/utils.js.map +1 -0
  18. package/build/esm/index.js +1444 -2095
  19. package/build/esm/index.js.map +1 -1
  20. package/build/stats-html.html +59 -49
  21. package/build/stats-react.json +186 -249
  22. package/build/types/index.d.ts +559 -422
  23. package/build/umd/index.development.js +1675 -2223
  24. package/build/umd/index.development.js.map +1 -1
  25. package/build/umd/index.production.js +12 -2
  26. package/build/umd/index.production.js.map +1 -1
  27. package/package.json +11 -7
  28. package/src/history.ts +292 -0
  29. package/src/index.ts +2 -10
  30. package/src/link.ts +116 -113
  31. package/src/path.ts +37 -17
  32. package/src/qss.ts +1 -2
  33. package/src/route.ts +927 -218
  34. package/src/routeInfo.ts +121 -197
  35. package/src/router.ts +1483 -1008
  36. package/src/searchParams.ts +1 -1
  37. package/src/utils.ts +80 -49
  38. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +0 -33
  39. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +0 -1
  40. package/build/cjs/node_modules/@babel/runtime/helpers/esm/extends.js +0 -33
  41. package/build/cjs/node_modules/@babel/runtime/helpers/esm/extends.js.map +0 -1
  42. package/build/cjs/node_modules/history/index.js +0 -815
  43. package/build/cjs/node_modules/history/index.js.map +0 -1
  44. package/build/cjs/node_modules/tiny-invariant/dist/esm/tiny-invariant.js +0 -30
  45. package/build/cjs/node_modules/tiny-invariant/dist/esm/tiny-invariant.js.map +0 -1
  46. package/build/cjs/packages/router-core/src/path.js.map +0 -1
  47. package/build/cjs/packages/router-core/src/qss.js.map +0 -1
  48. package/build/cjs/packages/router-core/src/route.js +0 -147
  49. package/build/cjs/packages/router-core/src/route.js.map +0 -1
  50. package/build/cjs/packages/router-core/src/routeConfig.js +0 -69
  51. package/build/cjs/packages/router-core/src/routeConfig.js.map +0 -1
  52. package/build/cjs/packages/router-core/src/routeMatch.js +0 -226
  53. package/build/cjs/packages/router-core/src/routeMatch.js.map +0 -1
  54. package/build/cjs/packages/router-core/src/router.js +0 -823
  55. package/build/cjs/packages/router-core/src/router.js.map +0 -1
  56. package/build/cjs/packages/router-core/src/searchParams.js.map +0 -1
  57. package/build/cjs/packages/router-core/src/utils.js.map +0 -1
  58. package/src/frameworks.ts +0 -11
  59. package/src/routeConfig.ts +0 -489
  60. 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.13",
4
+ "version": "0.0.1-beta.145",
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
+ "@gisatcz/cross-package-react-context": "^0.2.0",
46
+ "@tanstack/react-store": "0.0.1-beta.134"
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/history.ts ADDED
@@ -0,0 +1,292 @@
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: RouterLocation
7
+ listen: (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 ParsedPath {
18
+ href: string
19
+ pathname: string
20
+ search: string
21
+ hash: string
22
+ }
23
+
24
+ export interface RouterLocation extends ParsedPath {
25
+ state: any
26
+ }
27
+
28
+ type BlockerFn = (retry: () => void, cancel: () => void) => void
29
+
30
+ const pushStateEvent = 'pushstate'
31
+ const popStateEvent = 'popstate'
32
+ const beforeUnloadEvent = 'beforeunload'
33
+
34
+ const beforeUnloadListener = (event: Event) => {
35
+ event.preventDefault()
36
+ // @ts-ignore
37
+ return (event.returnValue = '')
38
+ }
39
+
40
+ const stopBlocking = () => {
41
+ removeEventListener(beforeUnloadEvent, beforeUnloadListener, {
42
+ capture: true,
43
+ })
44
+ }
45
+
46
+ function createHistory(opts: {
47
+ getLocation: () => RouterLocation
48
+ listener: false | ((onUpdate: () => void) => () => void)
49
+ pushState: (path: string, state: any) => void
50
+ replaceState: (path: string, state: any) => void
51
+ go: (n: number) => void
52
+ back: () => void
53
+ forward: () => void
54
+ createHref: (path: string) => string
55
+ }): RouterHistory {
56
+ let location = opts.getLocation()
57
+ let unsub = () => {}
58
+ let listeners = new Set<() => void>()
59
+ let blockers: BlockerFn[] = []
60
+ let queue: (() => void)[] = []
61
+
62
+ const tryFlush = () => {
63
+ if (blockers.length) {
64
+ blockers[0]?.(tryFlush, () => {
65
+ blockers = []
66
+ stopBlocking()
67
+ })
68
+ return
69
+ }
70
+
71
+ while (queue.length) {
72
+ queue.shift()?.()
73
+ }
74
+
75
+ if (!opts.listener) {
76
+ onUpdate()
77
+ }
78
+ }
79
+
80
+ const queueTask = (task: () => void) => {
81
+ queue.push(task)
82
+ tryFlush()
83
+ }
84
+
85
+ const onUpdate = () => {
86
+ location = opts.getLocation()
87
+ listeners.forEach((listener) => listener())
88
+ }
89
+
90
+ return {
91
+ get location() {
92
+ return location
93
+ },
94
+ listen: (cb: () => void) => {
95
+ if (listeners.size === 0) {
96
+ unsub =
97
+ typeof opts.listener === 'function'
98
+ ? opts.listener(onUpdate)
99
+ : () => {}
100
+ }
101
+ listeners.add(cb)
102
+
103
+ return () => {
104
+ listeners.delete(cb)
105
+ if (listeners.size === 0) {
106
+ unsub()
107
+ }
108
+ }
109
+ },
110
+ push: (path: string, state: any) => {
111
+ queueTask(() => {
112
+ opts.pushState(path, state)
113
+ })
114
+ },
115
+ replace: (path: string, state: any) => {
116
+ queueTask(() => {
117
+ opts.replaceState(path, state)
118
+ })
119
+ },
120
+ go: (index) => {
121
+ queueTask(() => {
122
+ opts.go(index)
123
+ })
124
+ },
125
+ back: () => {
126
+ queueTask(() => {
127
+ opts.back()
128
+ })
129
+ },
130
+ forward: () => {
131
+ queueTask(() => {
132
+ opts.forward()
133
+ })
134
+ },
135
+ createHref: (str) => opts.createHref(str),
136
+ block: (cb) => {
137
+ blockers.push(cb)
138
+
139
+ if (blockers.length === 1) {
140
+ addEventListener(beforeUnloadEvent, beforeUnloadListener, {
141
+ capture: true,
142
+ })
143
+ }
144
+
145
+ return () => {
146
+ blockers = blockers.filter((b) => b !== cb)
147
+
148
+ if (!blockers.length) {
149
+ stopBlocking()
150
+ }
151
+ }
152
+ },
153
+ }
154
+ }
155
+
156
+ export function createBrowserHistory(opts?: {
157
+ getHref?: () => string
158
+ createHref?: (path: string) => string
159
+ }): RouterHistory {
160
+ const getHref =
161
+ opts?.getHref ??
162
+ (() =>
163
+ `${window.location.pathname}${window.location.search}${window.location.hash}`)
164
+ const createHref = opts?.createHref ?? ((path) => path)
165
+ const getLocation = () => parseLocation(getHref(), history.state)
166
+
167
+ return createHistory({
168
+ getLocation,
169
+ listener: (onUpdate) => {
170
+ window.addEventListener(pushStateEvent, onUpdate)
171
+ window.addEventListener(popStateEvent, onUpdate)
172
+
173
+ var pushState = window.history.pushState
174
+ window.history.pushState = function () {
175
+ let res = pushState.apply(history, arguments as any)
176
+ onUpdate()
177
+ return res
178
+ }
179
+ var replaceState = window.history.replaceState
180
+ window.history.replaceState = function () {
181
+ let res = replaceState.apply(history, arguments as any)
182
+ onUpdate()
183
+ return res
184
+ }
185
+
186
+ return () => {
187
+ window.history.pushState = pushState
188
+ window.history.replaceState = replaceState
189
+ window.removeEventListener(pushStateEvent, onUpdate)
190
+ window.removeEventListener(popStateEvent, onUpdate)
191
+ }
192
+ },
193
+ pushState: (path, state) => {
194
+ window.history.pushState(
195
+ { ...state, key: createRandomKey() },
196
+ '',
197
+ createHref(path),
198
+ )
199
+ },
200
+ replaceState: (path, state) => {
201
+ window.history.replaceState(
202
+ { ...state, key: createRandomKey() },
203
+ '',
204
+ createHref(path),
205
+ )
206
+ },
207
+ back: () => window.history.back(),
208
+ forward: () => window.history.forward(),
209
+ go: (n) => window.history.go(n),
210
+ createHref: (path) => createHref(path),
211
+ })
212
+ }
213
+
214
+ export function createHashHistory(): RouterHistory {
215
+ return createBrowserHistory({
216
+ getHref: () => window.location.hash.substring(1),
217
+ createHref: (path) => `#${path}`,
218
+ })
219
+ }
220
+
221
+ export function createMemoryHistory(
222
+ opts: {
223
+ initialEntries: string[]
224
+ initialIndex?: number
225
+ } = {
226
+ initialEntries: ['/'],
227
+ },
228
+ ): RouterHistory {
229
+ const entries = opts.initialEntries
230
+ let index = opts.initialIndex ?? entries.length - 1
231
+ let currentState = {}
232
+
233
+ const getLocation = () => parseLocation(entries[index]!, currentState)
234
+
235
+ return createHistory({
236
+ getLocation,
237
+ listener: false,
238
+ pushState: (path, state) => {
239
+ currentState = {
240
+ ...state,
241
+ key: createRandomKey(),
242
+ }
243
+ entries.push(path)
244
+ index++
245
+ },
246
+ replaceState: (path, state) => {
247
+ currentState = {
248
+ ...state,
249
+ key: createRandomKey(),
250
+ }
251
+ entries[index] = path
252
+ },
253
+ back: () => {
254
+ index--
255
+ },
256
+ forward: () => {
257
+ index = Math.min(index + 1, entries.length - 1)
258
+ },
259
+ go: (n) => window.history.go(n),
260
+ createHref: (path) => path,
261
+ })
262
+ }
263
+
264
+ function parseLocation(href: string, state: any): RouterLocation {
265
+ let hashIndex = href.indexOf('#')
266
+ let searchIndex = href.indexOf('?')
267
+
268
+ return {
269
+ href,
270
+ pathname: href.substring(
271
+ 0,
272
+ hashIndex > 0
273
+ ? searchIndex > 0
274
+ ? Math.min(hashIndex, searchIndex)
275
+ : hashIndex
276
+ : searchIndex > 0
277
+ ? searchIndex
278
+ : href.length,
279
+ ),
280
+ hash: hashIndex > -1 ? href.substring(hashIndex) : '',
281
+ search:
282
+ searchIndex > -1
283
+ ? href.slice(searchIndex, hashIndex === -1 ? undefined : hashIndex)
284
+ : '',
285
+ state,
286
+ }
287
+ }
288
+
289
+ // Thanks co-pilot!
290
+ function createRandomKey() {
291
+ return (Math.random() + 1).toString(36).substring(7)
292
+ }
package/src/index.ts CHANGED
@@ -1,19 +1,11 @@
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'
15
8
  export * from './routeInfo'
16
- export * from './routeMatch'
17
9
  export * from './router'
18
10
  export * from './searchParams'
19
11
  export * from './utils'