@tanstack/router-core 0.0.1-beta.4 → 0.0.1-beta.41

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