@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
@@ -1,870 +0,0 @@
1
- /**
2
- * router-core
3
- *
4
- * Copyright (c) TanStack
5
- *
6
- * This source code is licensed under the MIT license found in the
7
- * LICENSE.md file in the root directory of this source tree.
8
- *
9
- * @license MIT
10
- */
11
- 'use strict';
12
-
13
- Object.defineProperty(exports, '__esModule', { value: true });
14
-
15
- var _rollupPluginBabelHelpers = require('../../../_virtual/_rollupPluginBabelHelpers.js');
16
- var index = require('../../../node_modules/history/index.js');
17
- var tinyInvariant = require('../../../node_modules/tiny-invariant/dist/esm/tiny-invariant.js');
18
- var path = require('./path.js');
19
- var route = require('./route.js');
20
- var routeMatch = require('./routeMatch.js');
21
- var searchParams = require('./searchParams.js');
22
- var utils = require('./utils.js');
23
-
24
- var _window$document;
25
- // Detect if we're in the DOM
26
- const isServer = typeof window === 'undefined' || !((_window$document = window.document) != null && _window$document.createElement); // This is the default history object if none is defined
27
-
28
- const createDefaultHistory = () => isServer ? index.createMemoryHistory() : index.createBrowserHistory();
29
-
30
- function getInitialRouterState() {
31
- return {
32
- status: 'idle',
33
- location: null,
34
- matches: [],
35
- actions: {},
36
- loaders: {},
37
- lastUpdated: Date.now(),
38
- isFetching: false,
39
- isPreloading: false
40
- };
41
- }
42
-
43
- function createRouter(userOptions) {
44
- var _userOptions$stringif, _userOptions$parseSea;
45
-
46
- const history = (userOptions == null ? void 0 : userOptions.history) || createDefaultHistory();
47
-
48
- const originalOptions = _rollupPluginBabelHelpers["extends"]({
49
- defaultLoaderGcMaxAge: 5 * 60 * 1000,
50
- defaultLoaderMaxAge: 0,
51
- defaultPreloadMaxAge: 2000,
52
- defaultPreloadDelay: 50
53
- }, userOptions, {
54
- stringifySearch: (_userOptions$stringif = userOptions == null ? void 0 : userOptions.stringifySearch) != null ? _userOptions$stringif : searchParams.defaultStringifySearch,
55
- parseSearch: (_userOptions$parseSea = userOptions == null ? void 0 : userOptions.parseSearch) != null ? _userOptions$parseSea : searchParams.defaultParseSearch
56
- });
57
-
58
- let router = {
59
- types: undefined,
60
- // public api
61
- history,
62
- options: originalOptions,
63
- listeners: [],
64
- // Resolved after construction
65
- basepath: '',
66
- routeTree: undefined,
67
- routesById: {},
68
- location: undefined,
69
- //
70
- navigationPromise: Promise.resolve(),
71
- resolveNavigation: () => {},
72
- matchCache: {},
73
- state: getInitialRouterState(),
74
- reset: () => {
75
- router.state = getInitialRouterState();
76
- router.notify();
77
- },
78
- startedLoadingAt: Date.now(),
79
- subscribe: listener => {
80
- router.listeners.push(listener);
81
- return () => {
82
- router.listeners = router.listeners.filter(x => x !== listener);
83
- };
84
- },
85
- getRoute: id => {
86
- return router.routesById[id];
87
- },
88
- notify: () => {
89
- const isFetching = router.state.status === 'loading' || router.state.matches.some(d => d.isFetching);
90
- const isPreloading = Object.values(router.matchCache).some(d => d.match.isFetching && !router.state.matches.find(dd => dd.matchId === d.match.matchId));
91
-
92
- if (router.state.isFetching !== isFetching || router.state.isPreloading !== isPreloading) {
93
- router.state = _rollupPluginBabelHelpers["extends"]({}, router.state, {
94
- isFetching,
95
- isPreloading
96
- });
97
- }
98
-
99
- cascadeLoaderData(router.state.matches);
100
- router.listeners.forEach(listener => listener(router));
101
- },
102
- dehydrateState: () => {
103
- return _rollupPluginBabelHelpers["extends"]({}, utils.pick(router.state, ['status', 'location', 'lastUpdated']), {
104
- matches: router.state.matches.map(match => utils.pick(match, ['matchId', 'status', 'routeLoaderData', 'loaderData', 'isInvalid', 'invalidAt']))
105
- });
106
- },
107
- hydrateState: dehydratedState => {
108
- // Match the routes
109
- const matches = router.matchRoutes(router.location.pathname, {
110
- strictParseParams: true
111
- });
112
- matches.forEach((match, index) => {
113
- const dehydratedMatch = dehydratedState.matches[index];
114
- tinyInvariant["default"](dehydratedMatch, 'Oh no! Dehydrated route matches did not match the active state of the router 😬');
115
- Object.assign(match, dehydratedMatch);
116
- });
117
- matches.forEach(match => match.__.validate());
118
- router.state = _rollupPluginBabelHelpers["extends"]({}, router.state, dehydratedState, {
119
- matches
120
- });
121
- },
122
- mount: () => {
123
- const next = router.__.buildLocation({
124
- to: '.',
125
- search: true,
126
- hash: true
127
- }); // If the current location isn't updated, trigger a navigation
128
- // to the current location. Otherwise, load the current location.
129
-
130
-
131
- if (next.href !== router.location.href) {
132
- router.__.commitLocation(next, true);
133
- }
134
-
135
- if (!router.state.matches.length) {
136
- router.load();
137
- }
138
-
139
- const unsub = router.history.listen(event => {
140
- router.load(router.__.parseLocation(event.location, router.location));
141
- }); // addEventListener does not exist in React Native, but window does
142
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
143
-
144
- if (!isServer && window.addEventListener) {
145
- // Listen to visibillitychange and focus
146
- window.addEventListener('visibilitychange', router.onFocus, false);
147
- window.addEventListener('focus', router.onFocus, false);
148
- }
149
-
150
- return () => {
151
- unsub();
152
-
153
- if (!isServer && window.removeEventListener) {
154
- // Be sure to unsubscribe if a new handler is set
155
- window.removeEventListener('visibilitychange', router.onFocus);
156
- window.removeEventListener('focus', router.onFocus);
157
- }
158
- };
159
- },
160
- onFocus: () => {
161
- router.load();
162
- },
163
- update: opts => {
164
- const newHistory = (opts == null ? void 0 : opts.history) !== router.history;
165
-
166
- if (!router.location || newHistory) {
167
- if (opts != null && opts.history) {
168
- router.history = opts.history;
169
- }
170
-
171
- router.location = router.__.parseLocation(router.history.location);
172
- router.state.location = router.location;
173
- }
174
-
175
- Object.assign(router.options, opts);
176
- const {
177
- basepath,
178
- routeConfig
179
- } = router.options;
180
- router.basepath = path.cleanPath("/" + (basepath != null ? basepath : ''));
181
-
182
- if (routeConfig) {
183
- router.routesById = {};
184
- router.routeTree = router.__.buildRouteTree(routeConfig);
185
- }
186
-
187
- return router;
188
- },
189
- cancelMatches: () => {
190
- var _router$state$pending, _router$state$pending2;
191
- [...router.state.matches, ...((_router$state$pending = (_router$state$pending2 = router.state.pending) == null ? void 0 : _router$state$pending2.matches) != null ? _router$state$pending : [])].forEach(match => {
192
- match.cancel();
193
- });
194
- },
195
- load: async next => {
196
- const id = Math.random();
197
- router.startedLoadingAt = id;
198
-
199
- if (next) {
200
- // Ingest the new location
201
- router.location = next;
202
- } // Cancel any pending matches
203
-
204
-
205
- router.cancelMatches(); // Match the routes
206
-
207
- const matches = router.matchRoutes(router.location.pathname, {
208
- strictParseParams: true
209
- });
210
-
211
- if (typeof document !== 'undefined') {
212
- router.state = _rollupPluginBabelHelpers["extends"]({}, router.state, {
213
- pending: {
214
- matches: matches,
215
- location: router.location
216
- },
217
- status: 'loading'
218
- });
219
- } else {
220
- router.state = _rollupPluginBabelHelpers["extends"]({}, router.state, {
221
- matches: matches,
222
- location: router.location,
223
- status: 'loading'
224
- });
225
- }
226
-
227
- router.notify(); // Load the matches
228
-
229
- await router.loadMatches(matches);
230
-
231
- if (router.startedLoadingAt !== id) {
232
- // Ignore side-effects of match loading
233
- return router.navigationPromise;
234
- }
235
-
236
- const previousMatches = router.state.matches;
237
- const exiting = [],
238
- staying = [];
239
- previousMatches.forEach(d => {
240
- if (matches.find(dd => dd.matchId === d.matchId)) {
241
- staying.push(d);
242
- } else {
243
- exiting.push(d);
244
- }
245
- });
246
- const entering = matches.filter(d => {
247
- return !previousMatches.find(dd => dd.matchId === d.matchId);
248
- });
249
- const now = Date.now();
250
- exiting.forEach(d => {
251
- var _ref, _d$options$loaderGcMa, _ref2, _d$options$loaderMaxA;
252
-
253
- d.__.onExit == null ? void 0 : d.__.onExit({
254
- params: d.params,
255
- search: d.routeSearch
256
- }); // Clear idle error states when match leaves
257
-
258
- if (d.status === 'error' && !d.isFetching) {
259
- d.status = 'idle';
260
- d.error = undefined;
261
- }
262
-
263
- const gc = Math.max((_ref = (_d$options$loaderGcMa = d.options.loaderGcMaxAge) != null ? _d$options$loaderGcMa : router.options.defaultLoaderGcMaxAge) != null ? _ref : 0, (_ref2 = (_d$options$loaderMaxA = d.options.loaderMaxAge) != null ? _d$options$loaderMaxA : router.options.defaultLoaderMaxAge) != null ? _ref2 : 0);
264
-
265
- if (gc > 0) {
266
- router.matchCache[d.matchId] = {
267
- gc: gc == Infinity ? Number.MAX_SAFE_INTEGER : now + gc,
268
- match: d
269
- };
270
- }
271
- });
272
- staying.forEach(d => {
273
- d.options.onTransition == null ? void 0 : d.options.onTransition({
274
- params: d.params,
275
- search: d.routeSearch
276
- });
277
- });
278
- entering.forEach(d => {
279
- d.__.onExit = d.options.onMatch == null ? void 0 : d.options.onMatch({
280
- params: d.params,
281
- search: d.search
282
- });
283
- delete router.matchCache[d.matchId];
284
- });
285
-
286
- if (router.startedLoadingAt !== id) {
287
- // Ignore side-effects of match loading
288
- return;
289
- }
290
-
291
- matches.forEach(match => {
292
- // Clear actions
293
- if (match.action) {
294
- match.action.current = undefined;
295
- match.action.submissions = [];
296
- }
297
- });
298
- router.state = _rollupPluginBabelHelpers["extends"]({}, router.state, {
299
- location: router.location,
300
- matches,
301
- pending: undefined,
302
- status: 'idle'
303
- });
304
- router.notify();
305
- router.resolveNavigation();
306
- },
307
- cleanMatchCache: () => {
308
- const now = Date.now();
309
- Object.keys(router.matchCache).forEach(matchId => {
310
- const entry = router.matchCache[matchId]; // Don't remove loading matches
311
-
312
- if (entry.match.status === 'loading') {
313
- return;
314
- } // Do not remove successful matches that are still valid
315
-
316
-
317
- if (entry.gc > 0 && entry.gc > now) {
318
- return;
319
- } // Everything else gets removed
320
-
321
-
322
- delete router.matchCache[matchId];
323
- });
324
- },
325
- loadRoute: async function loadRoute(navigateOpts) {
326
- if (navigateOpts === void 0) {
327
- navigateOpts = router.location;
328
- }
329
-
330
- const next = router.buildNext(navigateOpts);
331
- const matches = router.matchRoutes(next.pathname, {
332
- strictParseParams: true
333
- });
334
- await router.loadMatches(matches);
335
- return matches;
336
- },
337
- preloadRoute: async function preloadRoute(navigateOpts, loaderOpts) {
338
- var _ref3, _ref4, _loaderOpts$maxAge, _ref5, _ref6, _loaderOpts$gcMaxAge;
339
-
340
- if (navigateOpts === void 0) {
341
- navigateOpts = router.location;
342
- }
343
-
344
- const next = router.buildNext(navigateOpts);
345
- const matches = router.matchRoutes(next.pathname, {
346
- strictParseParams: true
347
- });
348
- await router.loadMatches(matches, {
349
- preload: true,
350
- maxAge: (_ref3 = (_ref4 = (_loaderOpts$maxAge = loaderOpts.maxAge) != null ? _loaderOpts$maxAge : router.options.defaultPreloadMaxAge) != null ? _ref4 : router.options.defaultLoaderMaxAge) != null ? _ref3 : 0,
351
- gcMaxAge: (_ref5 = (_ref6 = (_loaderOpts$gcMaxAge = loaderOpts.gcMaxAge) != null ? _loaderOpts$gcMaxAge : router.options.defaultPreloadGcMaxAge) != null ? _ref6 : router.options.defaultLoaderGcMaxAge) != null ? _ref5 : 0
352
- });
353
- return matches;
354
- },
355
- matchRoutes: (pathname, opts) => {
356
- var _router$state$pending3, _router$state$pending4;
357
-
358
- router.cleanMatchCache();
359
- const matches = [];
360
-
361
- if (!router.routeTree) {
362
- return matches;
363
- }
364
-
365
- const existingMatches = [...router.state.matches, ...((_router$state$pending3 = (_router$state$pending4 = router.state.pending) == null ? void 0 : _router$state$pending4.matches) != null ? _router$state$pending3 : [])];
366
-
367
- const recurse = async routes => {
368
- var _parentMatch$params, _router$options$filte, _foundRoute$childRout;
369
-
370
- const parentMatch = utils.last(matches);
371
- let params = (_parentMatch$params = parentMatch == null ? void 0 : parentMatch.params) != null ? _parentMatch$params : {};
372
- const filteredRoutes = (_router$options$filte = router.options.filterRoutes == null ? void 0 : router.options.filterRoutes(routes)) != null ? _router$options$filte : routes;
373
- let foundRoutes = [];
374
-
375
- const findMatchInRoutes = (parentRoutes, routes) => {
376
- routes.some(route => {
377
- var _route$childRoutes, _route$childRoutes2, _route$options$caseSe;
378
-
379
- if (!route.routePath && (_route$childRoutes = route.childRoutes) != null && _route$childRoutes.length) {
380
- return findMatchInRoutes([...foundRoutes, route], route.childRoutes);
381
- }
382
-
383
- const fuzzy = !!(route.routePath !== '/' || (_route$childRoutes2 = route.childRoutes) != null && _route$childRoutes2.length);
384
- const matchParams = path.matchPathname(pathname, {
385
- to: route.fullPath,
386
- fuzzy,
387
- caseSensitive: (_route$options$caseSe = route.options.caseSensitive) != null ? _route$options$caseSe : router.options.caseSensitive
388
- });
389
-
390
- if (matchParams) {
391
- let parsedParams;
392
-
393
- try {
394
- var _route$options$parseP;
395
-
396
- parsedParams = (_route$options$parseP = route.options.parseParams == null ? void 0 : route.options.parseParams(matchParams)) != null ? _route$options$parseP : matchParams;
397
- } catch (err) {
398
- if (opts != null && opts.strictParseParams) {
399
- throw err;
400
- }
401
- }
402
-
403
- params = _rollupPluginBabelHelpers["extends"]({}, params, parsedParams);
404
- }
405
-
406
- if (!!matchParams) {
407
- foundRoutes = [...parentRoutes, route];
408
- }
409
-
410
- return !!foundRoutes.length;
411
- });
412
- return !!foundRoutes.length;
413
- };
414
-
415
- findMatchInRoutes([], filteredRoutes);
416
-
417
- if (!foundRoutes.length) {
418
- return;
419
- }
420
-
421
- foundRoutes.forEach(foundRoute => {
422
- var _router$matchCache$ma;
423
-
424
- const interpolatedPath = path.interpolatePath(foundRoute.routePath, params);
425
- const matchId = path.interpolatePath(foundRoute.routeId, params, true);
426
- const match = existingMatches.find(d => d.matchId === matchId) || ((_router$matchCache$ma = router.matchCache[matchId]) == null ? void 0 : _router$matchCache$ma.match) || routeMatch.createRouteMatch(router, foundRoute, {
427
- parentMatch,
428
- matchId,
429
- params,
430
- pathname: path.joinPaths([pathname, interpolatedPath])
431
- });
432
- matches.push(match);
433
- });
434
- const foundRoute = utils.last(foundRoutes);
435
-
436
- if ((_foundRoute$childRout = foundRoute.childRoutes) != null && _foundRoute$childRout.length) {
437
- recurse(foundRoute.childRoutes);
438
- }
439
- };
440
-
441
- recurse([router.routeTree]);
442
- cascadeLoaderData(matches);
443
- return matches;
444
- },
445
- loadMatches: async (resolvedMatches, loaderOpts) => {
446
- const matchPromises = resolvedMatches.map(async match => {
447
- // Validate the match (loads search params etc)
448
- match.__.validate();
449
-
450
- match.load(loaderOpts);
451
- const search = match.search;
452
-
453
- if (search.__data && search.__data.matchId !== match.matchId) {
454
- return;
455
- }
456
-
457
- if (match.__.loadPromise) {
458
- // Wait for the first sign of activity from the match
459
- await match.__.loadPromise;
460
- }
461
- });
462
- router.notify();
463
- await Promise.all(matchPromises);
464
- },
465
- loadMatchData: async routeMatch => {
466
- if (isServer || !router.options.useServerData) {
467
- var _await$routeMatch$opt;
468
-
469
- return (_await$routeMatch$opt = await (routeMatch.options.loader == null ? void 0 : routeMatch.options.loader({
470
- // parentLoaderPromise: routeMatch.parentMatch?.__.dataPromise,
471
- params: routeMatch.params,
472
- search: routeMatch.routeSearch,
473
- signal: routeMatch.__.abortController.signal
474
- }))) != null ? _await$routeMatch$opt : {};
475
- } else {
476
- const next = router.buildNext({
477
- to: '.',
478
- search: d => _rollupPluginBabelHelpers["extends"]({}, d != null ? d : {}, {
479
- __data: {
480
- matchId: routeMatch.matchId
481
- }
482
- })
483
- });
484
- const res = await fetch(next.href, {
485
- method: 'GET' // signal: routeMatch.__.abortController.signal,
486
-
487
- });
488
-
489
- if (res.ok) {
490
- return res.json();
491
- }
492
-
493
- throw new Error('Failed to fetch match data');
494
- }
495
- },
496
- invalidateRoute: opts => {
497
- var _router$state$pending5, _router$state$pending6;
498
-
499
- const next = router.buildNext(opts);
500
- const unloadedMatchIds = router.matchRoutes(next.pathname).map(d => d.matchId);
501
- [...router.state.matches, ...((_router$state$pending5 = (_router$state$pending6 = router.state.pending) == null ? void 0 : _router$state$pending6.matches) != null ? _router$state$pending5 : [])].forEach(match => {
502
- if (unloadedMatchIds.includes(match.matchId)) {
503
- match.invalidate();
504
- }
505
- });
506
- },
507
- reload: () => router.__.navigate({
508
- fromCurrent: true,
509
- replace: true,
510
- search: true
511
- }),
512
- resolvePath: (from, path$1) => {
513
- return path.resolvePath(router.basepath, from, path.cleanPath(path$1));
514
- },
515
- matchRoute: (location, opts) => {
516
- var _location$from;
517
-
518
- // const location = router.buildNext(opts)
519
- location = _rollupPluginBabelHelpers["extends"]({}, location, {
520
- to: location.to ? router.resolvePath((_location$from = location.from) != null ? _location$from : '', location.to) : undefined
521
- });
522
- const next = router.buildNext(location);
523
-
524
- if (opts != null && opts.pending) {
525
- var _router$state$pending7;
526
-
527
- if (!((_router$state$pending7 = router.state.pending) != null && _router$state$pending7.location)) {
528
- return false;
529
- }
530
-
531
- return !!path.matchPathname(router.state.pending.location.pathname, _rollupPluginBabelHelpers["extends"]({}, opts, {
532
- to: next.pathname
533
- }));
534
- }
535
-
536
- return !!path.matchPathname(router.state.location.pathname, _rollupPluginBabelHelpers["extends"]({}, opts, {
537
- to: next.pathname
538
- }));
539
- },
540
- navigate: async _ref7 => {
541
- let {
542
- from,
543
- to = '.',
544
- search,
545
- hash,
546
- replace,
547
- params
548
- } = _ref7;
549
- // If this link simply reloads the current route,
550
- // make sure it has a new key so it will trigger a data refresh
551
- // If this `to` is a valid external URL, return
552
- // null for LinkUtils
553
- const toString = String(to);
554
- const fromString = String(from);
555
- let isExternal;
556
-
557
- try {
558
- new URL("" + toString);
559
- isExternal = true;
560
- } catch (e) {}
561
-
562
- tinyInvariant["default"](!isExternal, 'Attempting to navigate to external url with router.navigate!');
563
- return router.__.navigate({
564
- from: fromString,
565
- to: toString,
566
- search,
567
- hash,
568
- replace,
569
- params
570
- });
571
- },
572
- buildLink: _ref8 => {
573
- var _preload, _ref9;
574
-
575
- let {
576
- from,
577
- to = '.',
578
- search,
579
- params,
580
- hash,
581
- target,
582
- replace,
583
- activeOptions,
584
- preload,
585
- preloadMaxAge: userPreloadMaxAge,
586
- preloadGcMaxAge: userPreloadGcMaxAge,
587
- preloadDelay: userPreloadDelay,
588
- disabled
589
- } = _ref8;
590
-
591
- // If this link simply reloads the current route,
592
- // make sure it has a new key so it will trigger a data refresh
593
- // If this `to` is a valid external URL, return
594
- // null for LinkUtils
595
- try {
596
- new URL("" + to);
597
- return {
598
- type: 'external',
599
- href: to
600
- };
601
- } catch (e) {}
602
-
603
- const nextOpts = {
604
- from,
605
- to,
606
- search,
607
- params,
608
- hash,
609
- replace
610
- };
611
- const next = router.buildNext(nextOpts);
612
- preload = (_preload = preload) != null ? _preload : router.options.defaultPreload;
613
- const preloadDelay = (_ref9 = userPreloadDelay != null ? userPreloadDelay : router.options.defaultPreloadDelay) != null ? _ref9 : 0; // Compare path/hash for matches
614
-
615
- const pathIsEqual = router.state.location.pathname === next.pathname;
616
- const currentPathSplit = router.state.location.pathname.split('/');
617
- const nextPathSplit = next.pathname.split('/');
618
- const pathIsFuzzyEqual = nextPathSplit.every((d, i) => d === currentPathSplit[i]);
619
- const hashIsEqual = router.state.location.hash === next.hash; // Combine the matches based on user options
620
-
621
- const pathTest = activeOptions != null && activeOptions.exact ? pathIsEqual : pathIsFuzzyEqual;
622
- const hashTest = activeOptions != null && activeOptions.includeHash ? hashIsEqual : true; // The final "active" test
623
-
624
- const isActive = pathTest && hashTest; // The click handler
625
-
626
- const handleClick = e => {
627
- if (!disabled && !isCtrlEvent(e) && !e.defaultPrevented && (!target || target === '_self') && e.button === 0) {
628
- e.preventDefault();
629
-
630
- if (pathIsEqual && !search && !hash) {
631
- router.invalidateRoute(nextOpts);
632
- } // All is well? Navigate!)
633
-
634
-
635
- router.__.navigate(nextOpts);
636
- }
637
- }; // The click handler
638
-
639
-
640
- const handleFocus = e => {
641
- if (preload) {
642
- router.preloadRoute(nextOpts, {
643
- maxAge: userPreloadMaxAge,
644
- gcMaxAge: userPreloadGcMaxAge
645
- });
646
- }
647
- };
648
-
649
- const handleEnter = e => {
650
- const target = e.target || {};
651
-
652
- if (preload) {
653
- if (target.preloadTimeout) {
654
- return;
655
- }
656
-
657
- target.preloadTimeout = setTimeout(() => {
658
- target.preloadTimeout = null;
659
- router.preloadRoute(nextOpts, {
660
- maxAge: userPreloadMaxAge,
661
- gcMaxAge: userPreloadGcMaxAge
662
- });
663
- }, preloadDelay);
664
- }
665
- };
666
-
667
- const handleLeave = e => {
668
- const target = e.target || {};
669
-
670
- if (target.preloadTimeout) {
671
- clearTimeout(target.preloadTimeout);
672
- target.preloadTimeout = null;
673
- }
674
- };
675
-
676
- return {
677
- type: 'internal',
678
- next,
679
- handleFocus,
680
- handleClick,
681
- handleEnter,
682
- handleLeave,
683
- isActive,
684
- disabled
685
- };
686
- },
687
- buildNext: opts => {
688
- const next = router.__.buildLocation(opts);
689
-
690
- const matches = router.matchRoutes(next.pathname);
691
-
692
- const __preSearchFilters = matches.map(match => {
693
- var _match$options$preSea;
694
-
695
- return (_match$options$preSea = match.options.preSearchFilters) != null ? _match$options$preSea : [];
696
- }).flat().filter(Boolean);
697
-
698
- const __postSearchFilters = matches.map(match => {
699
- var _match$options$postSe;
700
-
701
- return (_match$options$postSe = match.options.postSearchFilters) != null ? _match$options$postSe : [];
702
- }).flat().filter(Boolean);
703
-
704
- return router.__.buildLocation(_rollupPluginBabelHelpers["extends"]({}, opts, {
705
- __preSearchFilters,
706
- __postSearchFilters
707
- }));
708
- },
709
- __: {
710
- buildRouteTree: rootRouteConfig => {
711
- const recurseRoutes = (routeConfigs, parent) => {
712
- return routeConfigs.map(routeConfig => {
713
- const routeOptions = routeConfig.options;
714
- const route$1 = route.createRoute(routeConfig, routeOptions, parent, router);
715
- const existingRoute = router.routesById[route$1.routeId];
716
-
717
- if (existingRoute) {
718
- if (process.env.NODE_ENV !== 'production') {
719
- console.warn("Duplicate routes found with id: " + String(route$1.routeId), router.routesById, route$1);
720
- }
721
-
722
- throw new Error();
723
- }
724
- router.routesById[route$1.routeId] = route$1;
725
- const children = routeConfig.children;
726
- route$1.childRoutes = children != null && children.length ? recurseRoutes(children, route$1) : undefined;
727
- return route$1;
728
- });
729
- };
730
-
731
- const routes = recurseRoutes([rootRouteConfig]);
732
- return routes[0];
733
- },
734
- parseLocation: (location, previousLocation) => {
735
- var _location$hash$split$;
736
-
737
- const parsedSearch = router.options.parseSearch(location.search);
738
- return {
739
- pathname: location.pathname,
740
- searchStr: location.search,
741
- search: utils.replaceEqualDeep(previousLocation == null ? void 0 : previousLocation.search, parsedSearch),
742
- hash: (_location$hash$split$ = location.hash.split('#').reverse()[0]) != null ? _location$hash$split$ : '',
743
- href: "" + location.pathname + location.search + location.hash,
744
- state: location.state,
745
- key: location.key
746
- };
747
- },
748
- navigate: location => {
749
- const next = router.buildNext(location);
750
- return router.__.commitLocation(next, location.replace);
751
- },
752
- buildLocation: function buildLocation(dest) {
753
- var _dest$from, _router$basepath, _dest$to, _last, _dest$params, _dest$__preSearchFilt, _functionalUpdate, _dest$__preSearchFilt2, _dest$__postSearchFil;
754
-
755
- if (dest === void 0) {
756
- dest = {};
757
- }
758
-
759
- // const resolvedFrom: Location = {
760
- // ...router.location,
761
- const fromPathname = dest.fromCurrent ? router.location.pathname : (_dest$from = dest.from) != null ? _dest$from : router.location.pathname;
762
-
763
- let pathname = path.resolvePath((_router$basepath = router.basepath) != null ? _router$basepath : '/', fromPathname, "" + ((_dest$to = dest.to) != null ? _dest$to : '.'));
764
-
765
- const fromMatches = router.matchRoutes(router.location.pathname, {
766
- strictParseParams: true
767
- });
768
- const toMatches = router.matchRoutes(pathname);
769
-
770
- const prevParams = _rollupPluginBabelHelpers["extends"]({}, (_last = utils.last(fromMatches)) == null ? void 0 : _last.params);
771
-
772
- let nextParams = ((_dest$params = dest.params) != null ? _dest$params : true) === true ? prevParams : utils.functionalUpdate(dest.params, prevParams);
773
-
774
- if (nextParams) {
775
- toMatches.map(d => d.options.stringifyParams).filter(Boolean).forEach(fn => {
776
- Object.assign({}, nextParams, fn(nextParams));
777
- });
778
- }
779
-
780
- pathname = path.interpolatePath(pathname, nextParams != null ? nextParams : {}); // Pre filters first
781
-
782
- const preFilteredSearch = (_dest$__preSearchFilt = dest.__preSearchFilters) != null && _dest$__preSearchFilt.length ? dest.__preSearchFilters.reduce((prev, next) => next(prev), router.location.search) : router.location.search; // Then the link/navigate function
783
-
784
- const destSearch = dest.search === true ? preFilteredSearch // Preserve resolvedFrom true
785
- : dest.search ? (_functionalUpdate = utils.functionalUpdate(dest.search, preFilteredSearch)) != null ? _functionalUpdate : {} // Updater
786
- : (_dest$__preSearchFilt2 = dest.__preSearchFilters) != null && _dest$__preSearchFilt2.length ? preFilteredSearch // Preserve resolvedFrom filters
787
- : {}; // Then post filters
788
-
789
- const postFilteredSearch = (_dest$__postSearchFil = dest.__postSearchFilters) != null && _dest$__postSearchFil.length ? dest.__postSearchFilters.reduce((prev, next) => next(prev), destSearch) : destSearch;
790
- const search = utils.replaceEqualDeep(router.location.search, postFilteredSearch);
791
- const searchStr = router.options.stringifySearch(search);
792
- let hash = dest.hash === true ? router.location.hash : utils.functionalUpdate(dest.hash, router.location.hash);
793
- hash = hash ? "#" + hash : '';
794
- return {
795
- pathname,
796
- search,
797
- searchStr,
798
- state: router.location.state,
799
- hash,
800
- href: "" + pathname + searchStr + hash,
801
- key: dest.key
802
- };
803
- },
804
- commitLocation: (next, replace) => {
805
- const id = '' + Date.now() + Math.random();
806
- if (router.navigateTimeout) clearTimeout(router.navigateTimeout);
807
- let nextAction = 'replace';
808
-
809
- if (!replace) {
810
- nextAction = 'push';
811
- }
812
-
813
- const isSameUrl = router.__.parseLocation(history.location).href === next.href;
814
-
815
- if (isSameUrl && !next.key) {
816
- nextAction = 'replace';
817
- }
818
-
819
- if (nextAction === 'replace') {
820
- history.replace({
821
- pathname: next.pathname,
822
- hash: next.hash,
823
- search: next.searchStr
824
- }, _rollupPluginBabelHelpers["extends"]({
825
- id
826
- }, next.state));
827
- } else {
828
- history.push({
829
- pathname: next.pathname,
830
- hash: next.hash,
831
- search: next.searchStr
832
- }, {
833
- id
834
- });
835
- }
836
-
837
- router.navigationPromise = new Promise(resolve => {
838
- const previousNavigationResolve = router.resolveNavigation;
839
-
840
- router.resolveNavigation = () => {
841
- previousNavigationResolve();
842
- resolve();
843
- };
844
- });
845
- return router.navigationPromise;
846
- }
847
- }
848
- };
849
- router.update(userOptions); // Allow frameworks to hook into the router creation
850
-
851
- router.options.createRouter == null ? void 0 : router.options.createRouter(router);
852
- return router;
853
- }
854
-
855
- function isCtrlEvent(e) {
856
- return !!(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey);
857
- }
858
-
859
- function cascadeLoaderData(matches) {
860
- matches.forEach((match, index) => {
861
- const parent = matches[index - 1];
862
-
863
- if (parent) {
864
- match.loaderData = utils.replaceEqualDeep(match.loaderData, _rollupPluginBabelHelpers["extends"]({}, parent.loaderData, match.routeLoaderData));
865
- }
866
- });
867
- }
868
-
869
- exports.createRouter = createRouter;
870
- //# sourceMappingURL=router.js.map