what-router 0.4.0 → 0.4.2

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +42 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "what-router",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "description": "What Framework - File-based & programmatic router with View Transitions",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
package/src/index.js CHANGED
@@ -108,8 +108,11 @@ function compilePath(path) {
108
108
  }
109
109
 
110
110
  function matchRoute(path, routes) {
111
+ // Filter out routes without a path (layout-only routes, etc.)
112
+ const routable = routes.filter(r => r.path);
113
+
111
114
  // Sort routes by specificity (more specific first)
112
- const sorted = [...routes].sort((a, b) => {
115
+ const sorted = routable.sort((a, b) => {
113
116
  const aSpecific = (a.path.match(/:/g) || []).length + (a.path.includes('*') ? 100 : 0);
114
117
  const bSpecific = (b.path.match(/:/g) || []).length + (b.path.includes('*') ? 100 : 0);
115
118
  return aSpecific - bSpecific;
@@ -145,6 +148,7 @@ function parseQuery(search) {
145
148
  // Build the layout chain for a route
146
149
  function buildLayoutChain(route, routes) {
147
150
  const layouts = [];
151
+ if (!route.path) return layouts;
148
152
 
149
153
  // Check for nested layouts based on path segments
150
154
  const segments = route.path.split('/').filter(Boolean);
@@ -276,13 +280,13 @@ export function Link({
276
280
  return h('a', {
277
281
  href,
278
282
  class: classes,
279
- onClick: (e) => {
283
+ onclick: (e) => {
280
284
  // Only intercept left-clicks without modifiers
281
285
  if (e.ctrlKey || e.metaKey || e.shiftKey || e.altKey || e.button !== 0) return;
282
286
  e.preventDefault();
283
287
  navigate(href, { replace: rep, transition });
284
288
  },
285
- onMouseenter: shouldPrefetch ? () => prefetch(href) : undefined,
289
+ onmouseenter: shouldPrefetch ? () => prefetch(href) : undefined,
286
290
  ...rest,
287
291
  }, ...(Array.isArray(children) ? children : [children]));
288
292
  }
@@ -483,3 +487,38 @@ export function Outlet({ children }) {
483
487
  // Children passed from parent layout
484
488
  return children || null;
485
489
  }
490
+
491
+ // --- File-Based Router ---
492
+ // Consumes routes generated by what-compiler's file router (virtual:what-routes).
493
+ // Usage:
494
+ // import { routes } from 'virtual:what-routes';
495
+ // mount(<FileRouter routes={routes} />, '#app');
496
+
497
+ export function FileRouter({
498
+ routes,
499
+ layout: globalLayout,
500
+ fallback,
501
+ error: globalError,
502
+ }) {
503
+ // Convert file-router route format to Router's expected format
504
+ const routerRoutes = routes.map(r => ({
505
+ path: r.path,
506
+ component: r.component,
507
+ layout: r.layout || undefined,
508
+ // Attach page mode as metadata for build system
509
+ _mode: r.mode || 'client',
510
+ }));
511
+
512
+ return Router({
513
+ routes: routerRoutes,
514
+ globalLayout,
515
+ fallback: fallback || Default404,
516
+ });
517
+ }
518
+
519
+ function Default404() {
520
+ return h('div', { style: 'text-align:center;padding:60px 20px' },
521
+ h('h1', { style: 'font-size:48px;margin-bottom:8px' }, '404'),
522
+ h('p', { style: 'color:#64748b' }, 'Page not found'),
523
+ );
524
+ }