@tanstack/router-core 0.0.1-beta.16 → 0.0.1-beta.161

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