@tanstack/router-core 0.0.1-alpha.3 → 0.0.1-alpha.5

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.
@@ -0,0 +1,30 @@
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 isProduction = process.env.NODE_ENV === 'production';
16
+ var prefix = 'Invariant failed';
17
+ function invariant(condition, message) {
18
+ if (condition) {
19
+ return;
20
+ }
21
+ if (isProduction) {
22
+ throw new Error(prefix);
23
+ }
24
+ var provided = typeof message === 'function' ? message() : message;
25
+ var value = provided ? "".concat(prefix, ": ").concat(provided) : prefix;
26
+ throw new Error(value);
27
+ }
28
+
29
+ exports["default"] = invariant;
30
+ //# sourceMappingURL=tiny-invariant.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tiny-invariant.js","sources":["../../../../../../../../node_modules/tiny-invariant/dist/esm/tiny-invariant.js"],"sourcesContent":["var isProduction = process.env.NODE_ENV === 'production';\nvar prefix = 'Invariant failed';\nfunction invariant(condition, message) {\n if (condition) {\n return;\n }\n if (isProduction) {\n throw new Error(prefix);\n }\n var provided = typeof message === 'function' ? message() : message;\n var value = provided ? \"\".concat(prefix, \": \").concat(provided) : prefix;\n throw new Error(value);\n}\n\nexport { invariant as default };\n"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,IAAI,YAAY,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,CAAC;AACzD,IAAI,MAAM,GAAG,kBAAkB,CAAC;AAChC,SAAS,SAAS,CAAC,SAAS,EAAE,OAAO,EAAE;AACvC,IAAI,IAAI,SAAS,EAAE;AACnB,QAAQ,OAAO;AACf,KAAK;AACL,IAAI,IAAI,YAAY,EAAE;AACtB,QAAQ,MAAM,IAAI,KAAK,CAAC,MAAM,CAAC,CAAC;AAChC,KAAK;AACL,IAAI,IAAI,QAAQ,GAAG,OAAO,OAAO,KAAK,UAAU,GAAG,OAAO,EAAE,GAAG,OAAO,CAAC;AACvE,IAAI,IAAI,KAAK,GAAG,QAAQ,GAAG,EAAE,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,MAAM,CAAC;AAC7E,IAAI,MAAM,IAAI,KAAK,CAAC,KAAK,CAAC,CAAC;AAC3B;;;;"}
@@ -14,9 +14,10 @@ Object.defineProperty(exports, '__esModule', { value: true });
14
14
 
15
15
  var _rollupPluginBabelHelpers = require('../../../_virtual/_rollupPluginBabelHelpers.js');
16
16
  var index = require('../../../node_modules/history/index.js');
17
+ var tinyInvariant = require('../../../node_modules/tiny-invariant/dist/esm/tiny-invariant.js');
17
18
  var qss = require('./qss.js');
18
19
 
19
- const createRouteConfig = function createRouteConfig(options, children, isRoot, parentId) {
20
+ const createRouteConfig = function createRouteConfig(options, children, isRoot, parentId, parentPath) {
20
21
  if (options === void 0) {
21
22
  options = {};
22
23
  }
@@ -27,8 +28,6 @@ const createRouteConfig = function createRouteConfig(options, children, isRoot,
27
28
 
28
29
  if (isRoot) {
29
30
  options.path = rootRouteId;
30
- } else {
31
- warning(!options.path, 'Routes must have a path property.');
32
31
  } // Strip the root from parentIds
33
32
 
34
33
 
@@ -36,13 +35,14 @@ const createRouteConfig = function createRouteConfig(options, children, isRoot,
36
35
  parentId = '';
37
36
  }
38
37
 
39
- let path = String(isRoot ? rootRouteId : options.path); // If the path is anything other than an index path, trim it up
38
+ let path = isRoot ? rootRouteId : options.path; // If the path is anything other than an index path, trim it up
40
39
 
41
- if (path !== '/') {
40
+ if (path && path !== '/') {
42
41
  path = trimPath(path);
43
42
  }
44
43
 
45
- let id = joinPaths([parentId, path]);
44
+ const routeId = path || options.id;
45
+ let id = joinPaths([parentId, routeId]);
46
46
 
47
47
  if (path === rootRouteId) {
48
48
  path = '/';
@@ -52,14 +52,15 @@ const createRouteConfig = function createRouteConfig(options, children, isRoot,
52
52
  id = joinPaths(['/', id]);
53
53
  }
54
54
 
55
- const fullPath = id === rootRouteId ? '/' : trimPathRight(id);
55
+ const fullPath = id === rootRouteId ? '/' : trimPathRight(joinPaths([parentPath, path]));
56
56
  return {
57
57
  id: id,
58
+ routeId: routeId,
58
59
  path: path,
59
60
  fullPath: fullPath,
60
61
  options: options,
61
62
  children,
62
- addChildren: cb => createRouteConfig(options, cb(childOptions => createRouteConfig(childOptions, undefined, false, id)), false, parentId)
63
+ addChildren: cb => createRouteConfig(options, cb(childOptions => createRouteConfig(childOptions, undefined, false, id, fullPath)), false, parentId, parentPath)
63
64
  };
64
65
  };
65
66
  const rootRouteId = '__root__';
@@ -82,6 +83,7 @@ function createRouter(userOptions) {
82
83
  let router = {
83
84
  options: originalOptions,
84
85
  listeners: [],
86
+ removeActionQueue: [],
85
87
  // Resolved after construction
86
88
  basepath: '',
87
89
  routeTree: undefined,
@@ -205,12 +207,14 @@ function createRouter(userOptions) {
205
207
  strictParseParams: true
206
208
  });
207
209
  const toMatches = router.matchRoutes(pathname);
208
- const prevParams = (_last = last(fromMatches)) == null ? void 0 : _last.params;
210
+
211
+ const prevParams = _rollupPluginBabelHelpers["extends"]({}, (_last = last(fromMatches)) == null ? void 0 : _last.params);
212
+
209
213
  let nextParams = ((_dest$params = dest.params) != null ? _dest$params : true) === true ? prevParams : functionalUpdate(dest.params, prevParams);
210
214
 
211
215
  if (nextParams) {
212
216
  toMatches.map(d => d.options.stringifyParams).filter(Boolean).forEach(fn => {
213
- Object.assign(nextParams, fn(nextParams));
217
+ Object.assign({}, nextParams, fn(nextParams));
214
218
  });
215
219
  }
216
220
 
@@ -313,22 +317,33 @@ function createRouter(userOptions) {
313
317
  router.startedLoadingAt = id;
314
318
 
315
319
  if (next) {
320
+ // If the location.href has changed
316
321
  // Ingest the new location
317
322
  router.location = next;
318
- } // Cancel any pending matches
323
+ } // Clear out old actions
319
324
 
320
325
 
326
+ router.removeActionQueue.forEach(_ref => {
327
+ let {
328
+ action,
329
+ actionState
330
+ } = _ref;
331
+
332
+ if (router.state.currentAction === actionState) {
333
+ router.state.currentAction = undefined;
334
+ }
335
+
336
+ if (action.current === actionState) {
337
+ action.current = undefined;
338
+ }
339
+ });
340
+ router.removeActionQueue = []; // Cancel any pending matches
341
+
321
342
  router.cancelMatches(); // Match the routes
322
343
 
323
344
  const unloadedMatches = router.matchRoutes(location.pathname, {
324
345
  strictParseParams: true
325
346
  });
326
- unloadedMatches.forEach((match, index) => {
327
- const parent = unloadedMatches[index - 1];
328
- const child = unloadedMatches[index + 1];
329
- if (parent) match.__.setParentMatch(parent);
330
- if (child) match.__.addChildMatch(child);
331
- });
332
347
  router.state = _rollupPluginBabelHelpers["extends"]({}, router.state, {
333
348
  pending: {
334
349
  matches: unloadedMatches,
@@ -435,59 +450,81 @@ function createRouter(userOptions) {
435
450
 
436
451
  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 : [])];
437
452
 
438
- const recurse = async (routes, parentMatch) => {
439
- var _parentMatch$params, _router$options$filte, _router$preloadCache$, _route$childRoutes2;
453
+ const recurse = async routes => {
454
+ var _parentMatch$params, _router$options$filte, _foundRoute$childRout;
440
455
 
456
+ const parentMatch = last(matches);
441
457
  let params = (_parentMatch$params = parentMatch == null ? void 0 : parentMatch.params) != null ? _parentMatch$params : {};
442
458
  const filteredRoutes = (_router$options$filte = router.options.filterRoutes == null ? void 0 : router.options.filterRoutes(routes)) != null ? _router$options$filte : routes;
443
- const route = filteredRoutes == null ? void 0 : filteredRoutes.find(route => {
444
- var _route$childRoutes, _route$options$caseSe;
445
-
446
- const fuzzy = !!(route.routePath !== '/' || (_route$childRoutes = route.childRoutes) != null && _route$childRoutes.length);
447
- const matchParams = matchPathname(pathname, {
448
- to: route.fullPath,
449
- fuzzy,
450
- caseSensitive: (_route$options$caseSe = route.options.caseSensitive) != null ? _route$options$caseSe : router.options.caseSensitive
451
- });
459
+ let foundRoutes = [];
452
460
 
453
- if (matchParams) {
454
- let parsedParams;
461
+ const findMatchInRoutes = (parentRoutes, routes) => {
462
+ routes.some(route => {
463
+ var _route$childRoutes, _route$childRoutes2, _route$options$caseSe;
455
464
 
456
- try {
457
- var _route$options$parseP;
465
+ if (!route.routePath && (_route$childRoutes = route.childRoutes) != null && _route$childRoutes.length) {
466
+ return findMatchInRoutes([...foundRoutes, route], route.childRoutes);
467
+ }
458
468
 
459
- parsedParams = (_route$options$parseP = route.options.parseParams == null ? void 0 : route.options.parseParams(matchParams)) != null ? _route$options$parseP : matchParams;
460
- } catch (err) {
461
- if (opts != null && opts.strictParseParams) {
462
- throw err;
469
+ const fuzzy = !!(route.routePath !== '/' || (_route$childRoutes2 = route.childRoutes) != null && _route$childRoutes2.length);
470
+ const matchParams = matchPathname(pathname, {
471
+ to: route.fullPath,
472
+ fuzzy,
473
+ caseSensitive: (_route$options$caseSe = route.options.caseSensitive) != null ? _route$options$caseSe : router.options.caseSensitive
474
+ });
475
+
476
+ if (matchParams) {
477
+ let parsedParams;
478
+
479
+ try {
480
+ var _route$options$parseP;
481
+
482
+ parsedParams = (_route$options$parseP = route.options.parseParams == null ? void 0 : route.options.parseParams(matchParams)) != null ? _route$options$parseP : matchParams;
483
+ } catch (err) {
484
+ if (opts != null && opts.strictParseParams) {
485
+ throw err;
486
+ }
463
487
  }
488
+
489
+ params = _rollupPluginBabelHelpers["extends"]({}, params, parsedParams);
464
490
  }
465
491
 
466
- params = _rollupPluginBabelHelpers["extends"]({}, params, parsedParams);
467
- }
492
+ if (!!matchParams) {
493
+ foundRoutes = [...parentRoutes, route];
494
+ }
468
495
 
469
- return !!matchParams;
470
- });
496
+ return !!foundRoutes.length;
497
+ });
498
+ return !!foundRoutes.length;
499
+ };
471
500
 
472
- if (!route) {
501
+ findMatchInRoutes([], filteredRoutes);
502
+
503
+ if (!foundRoutes.length) {
473
504
  return;
474
505
  }
475
506
 
476
- const interpolatedPath = interpolatePath(route.routePath, params);
477
- const matchId = interpolatePath(route.routeId, params, true);
478
- const match = existingMatches.find(d => d.matchId === matchId) || ((_router$preloadCache$ = router.preloadCache[matchId]) == null ? void 0 : _router$preloadCache$.match) || createRouteMatch(router, route, {
479
- matchId,
480
- params,
481
- pathname: joinPaths([pathname, interpolatedPath])
507
+ foundRoutes.forEach(foundRoute => {
508
+ var _router$preloadCache$;
509
+
510
+ const interpolatedPath = interpolatePath(foundRoute.routePath, params);
511
+ const matchId = interpolatePath(foundRoute.routeId, params, true);
512
+ const match = existingMatches.find(d => d.matchId === matchId) || ((_router$preloadCache$ = router.preloadCache[matchId]) == null ? void 0 : _router$preloadCache$.match) || createRouteMatch(router, foundRoute, {
513
+ matchId,
514
+ params,
515
+ pathname: joinPaths([pathname, interpolatedPath])
516
+ });
517
+ matches.push(match);
482
518
  });
483
- matches.push(match);
519
+ const foundRoute = last(foundRoutes);
484
520
 
485
- if ((_route$childRoutes2 = route.childRoutes) != null && _route$childRoutes2.length) {
486
- recurse(route.childRoutes, match);
521
+ if ((_foundRoute$childRout = foundRoute.childRoutes) != null && _foundRoute$childRout.length) {
522
+ recurse(foundRoute.childRoutes);
487
523
  }
488
524
  };
489
525
 
490
526
  recurse([router.routeTree]);
527
+ cascadeLoaderData(matches);
491
528
  return matches;
492
529
  },
493
530
  loadMatches: async (resolvedMatches, loaderOpts) => {
@@ -566,14 +603,14 @@ function createRouter(userOptions) {
566
603
  const next = router.buildNext(location);
567
604
  return router.commitLocation(next, location.replace);
568
605
  },
569
- navigate: async _ref => {
606
+ navigate: async _ref2 => {
570
607
  let {
571
608
  from,
572
609
  to = '.',
573
610
  search,
574
611
  hash,
575
612
  replace
576
- } = _ref;
613
+ } = _ref2;
577
614
  // If this link simply reloads the current route,
578
615
  // make sure it has a new key so it will trigger a data refresh
579
616
  // If this `to` is a valid external URL, return
@@ -587,14 +624,7 @@ function createRouter(userOptions) {
587
624
  isExternal = true;
588
625
  } catch (e) {}
589
626
 
590
- if (isExternal) {
591
- if (process.env.NODE_ENV !== 'production') {
592
- throw new Error('Attempting to navigate to external url with router.navigate!');
593
- }
594
-
595
- return;
596
- }
597
-
627
+ tinyInvariant["default"](!isExternal, 'Attempting to navigate to external url with router.navigate!');
598
628
  return router._navigate({
599
629
  from: fromString,
600
630
  to: toString,
@@ -602,8 +632,8 @@ function createRouter(userOptions) {
602
632
  hash
603
633
  });
604
634
  },
605
- buildLink: _ref2 => {
606
- var _preload, _ref3, _ref4;
635
+ buildLink: _ref3 => {
636
+ var _preload, _ref4, _ref5;
607
637
 
608
638
  let {
609
639
  from,
@@ -618,7 +648,7 @@ function createRouter(userOptions) {
618
648
  preloadMaxAge: userPreloadMaxAge,
619
649
  preloadDelay: userPreloadDelay,
620
650
  disabled
621
- } = _ref2;
651
+ } = _ref3;
622
652
 
623
653
  // If this link simply reloads the current route,
624
654
  // make sure it has a new key so it will trigger a data refresh
@@ -642,8 +672,8 @@ function createRouter(userOptions) {
642
672
  };
643
673
  const next = router.buildNext(nextOpts);
644
674
  preload = (_preload = preload) != null ? _preload : router.options.defaultLinkPreload;
645
- const preloadMaxAge = (_ref3 = userPreloadMaxAge != null ? userPreloadMaxAge : router.options.defaultLinkPreloadMaxAge) != null ? _ref3 : 2000;
646
- const preloadDelay = (_ref4 = userPreloadDelay != null ? userPreloadDelay : router.options.defaultLinkPreloadDelay) != null ? _ref4 : 50; // Compare path/hash for matches
675
+ const preloadMaxAge = (_ref4 = userPreloadMaxAge != null ? userPreloadMaxAge : router.options.defaultLinkPreloadMaxAge) != null ? _ref4 : 2000;
676
+ const preloadDelay = (_ref5 = userPreloadDelay != null ? userPreloadDelay : router.options.defaultLinkPreloadDelay) != null ? _ref5 : 50; // Compare path/hash for matches
647
677
 
648
678
  const pathIsEqual = router.state.location.pathname === next.pathname;
649
679
  const currentPathSplit = router.state.location.pathname.split('/');
@@ -717,11 +747,11 @@ function createRouter(userOptions) {
717
747
  },
718
748
  __experimental__createSnapshot: () => {
719
749
  return _rollupPluginBabelHelpers["extends"]({}, router.state, {
720
- matches: router.state.matches.map(_ref5 => {
750
+ matches: router.state.matches.map(_ref6 => {
721
751
  let {
722
752
  routeLoaderData: loaderData,
723
753
  matchId
724
- } = _ref5;
754
+ } = _ref6;
725
755
  return {
726
756
  matchId,
727
757
  loaderData
@@ -749,13 +779,14 @@ function createRoute(routeConfig, options, parent, router) {
749
779
  // ]).replace(new RegExp(`^${rootRouteId}`), '')
750
780
  // ) as TRouteInfo['id']
751
781
  const {
752
- id: routeId,
782
+ id,
783
+ routeId,
753
784
  path: routePath,
754
785
  fullPath
755
786
  } = routeConfig;
756
787
 
757
- const action = router.state.actions[routeId] || (() => {
758
- router.state.actions[routeId] = {
788
+ const action = router.state.actions[id] || (() => {
789
+ router.state.actions[id] = {
759
790
  pending: [],
760
791
  submit: async (submission, actionOpts) => {
761
792
  var _actionOpts$invalidat;
@@ -770,10 +801,12 @@ function createRoute(routeConfig, options, parent, router) {
770
801
  status: 'pending',
771
802
  submission
772
803
  };
804
+ action.current = actionState;
773
805
  action.latest = actionState;
774
806
  action.pending.push(actionState);
775
807
  router.state = _rollupPluginBabelHelpers["extends"]({}, router.state, {
776
- action: actionState
808
+ currentAction: actionState,
809
+ latestAction: actionState
777
810
  });
778
811
  router.notify();
779
812
 
@@ -797,20 +830,20 @@ function createRoute(routeConfig, options, parent, router) {
797
830
  actionState.status = 'error';
798
831
  } finally {
799
832
  action.pending = action.pending.filter(d => d !== actionState);
800
-
801
- if (actionState === router.state.action) {
802
- router.state.action = undefined;
803
- }
804
-
833
+ router.removeActionQueue.push({
834
+ action,
835
+ actionState
836
+ });
805
837
  router.notify();
806
838
  }
807
839
  }
808
840
  };
809
- return router.state.actions[routeId];
841
+ return router.state.actions[id];
810
842
  })();
811
843
 
812
844
  let route = {
813
- routeId,
845
+ routeId: id,
846
+ routeRouteId: routeId,
814
847
  routePath,
815
848
  fullPath,
816
849
  options,
@@ -887,16 +920,17 @@ function createRouteMatch(router, route, opts) {
887
920
  clearTimeout(routeMatch.__.pendingMinTimeout);
888
921
  delete routeMatch.__.pendingMinPromise;
889
922
  },
890
- setParentMatch: parentMatch => {
891
- routeMatch.parentMatch = parentMatch;
892
- },
893
- addChildMatch: childMatch => {
894
- if (routeMatch.childMatches.find(d => d.matchId === childMatch.matchId)) {
895
- return;
896
- }
897
-
898
- routeMatch.childMatches.push(childMatch);
899
- },
923
+ // setParentMatch: (parentMatch?: RouteMatch) => {
924
+ // routeMatch.parentMatch = parentMatch
925
+ // },
926
+ // addChildMatch: (childMatch: RouteMatch) => {
927
+ // if (
928
+ // routeMatch.childMatches.find((d) => d.matchId === childMatch.matchId)
929
+ // ) {
930
+ // return
931
+ // }
932
+ // routeMatch.childMatches.push(childMatch)
933
+ // },
900
934
  validate: () => {
901
935
  var _routeMatch$parentMat, _routeMatch$parentMat2;
902
936
 
@@ -1000,7 +1034,6 @@ function createRouteMatch(router, route, opts) {
1000
1034
  }
1001
1035
 
1002
1036
  routeMatch.routeLoaderData = replaceEqualDeep(routeMatch.routeLoaderData, data);
1003
- cascadeLoaderData(routeMatch);
1004
1037
  routeMatch.error = undefined;
1005
1038
  routeMatch.status = 'success';
1006
1039
  routeMatch.updatedAt = Date.now();
@@ -1060,16 +1093,14 @@ function createRouteMatch(router, route, opts) {
1060
1093
  return routeMatch;
1061
1094
  }
1062
1095
 
1063
- function cascadeLoaderData(routeMatch) {
1064
- if (routeMatch.parentMatch) {
1065
- routeMatch.loaderData = replaceEqualDeep(routeMatch.loaderData, _rollupPluginBabelHelpers["extends"]({}, routeMatch.parentMatch.loaderData, routeMatch.routeLoaderData));
1066
- }
1096
+ function cascadeLoaderData(matches) {
1097
+ matches.forEach((match, index) => {
1098
+ const parent = matches[index - 1];
1067
1099
 
1068
- if (routeMatch.childMatches.length) {
1069
- routeMatch.childMatches.forEach(childMatch => {
1070
- cascadeLoaderData(childMatch);
1071
- });
1072
- }
1100
+ if (parent) {
1101
+ match.loaderData = replaceEqualDeep(match.loaderData, _rollupPluginBabelHelpers["extends"]({}, parent.loaderData, match.routeLoaderData));
1102
+ }
1103
+ });
1073
1104
  }
1074
1105
 
1075
1106
  function matchPathname(currentPathname, matchLocation) {
@@ -1424,6 +1455,7 @@ function last(arr) {
1424
1455
  exports.createBrowserHistory = index.createBrowserHistory;
1425
1456
  exports.createHashHistory = index.createHashHistory;
1426
1457
  exports.createMemoryHistory = index.createMemoryHistory;
1458
+ exports.invariant = tinyInvariant["default"];
1427
1459
  exports.createRoute = createRoute;
1428
1460
  exports.createRouteConfig = createRouteConfig;
1429
1461
  exports.createRouteMatch = createRouteMatch;