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

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