@liteforge/router 0.1.0

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.
package/dist/index.js ADDED
@@ -0,0 +1,44 @@
1
+ // =============================================================================
2
+ // Types
3
+ // =============================================================================
4
+ // =============================================================================
5
+ // Route Matching
6
+ // =============================================================================
7
+ export {
8
+ // Path parsing utilities
9
+ parsePath, parseQuery, stringifyQuery, createLocation, normalizePath, joinPaths,
10
+ // Route compilation
11
+ compilePath, compileRoute, compileRoutes,
12
+ // Route matching
13
+ matchRoute, matchRoutes, findRouteByName, generatePath, isPathActive, flattenRoutes, } from './route-matcher.js';
14
+ // =============================================================================
15
+ // History
16
+ // =============================================================================
17
+ export { createMemoryHistory, createBrowserHistory, createHashHistory, targetToHref, } from './history.js';
18
+ // =============================================================================
19
+ // Guards
20
+ // =============================================================================
21
+ export { defineGuard, GuardRegistry, runGuards, collectRouteGuards, normalizeGuardResult,
22
+ // Built-in guards
23
+ createAuthGuard, createRoleGuard, createConfirmGuard, createGuestGuard, } from './guards.js';
24
+ // =============================================================================
25
+ // Middleware
26
+ // =============================================================================
27
+ export { defineMiddleware, composeMiddleware, runMiddleware,
28
+ // Built-in middleware
29
+ createLoggerMiddleware, createScrollMiddleware, createTitleMiddleware, createAnalyticsMiddleware, createLoadingMiddleware, } from './middleware.js';
30
+ // =============================================================================
31
+ // Router
32
+ // =============================================================================
33
+ export { createRouter, setActiveRouter, getActiveRouter, getActiveRouterOrNull, } from './router.js';
34
+ // =============================================================================
35
+ // Components
36
+ // =============================================================================
37
+ export { RouterOutlet, Link, NavLink } from './components.js';
38
+ // =============================================================================
39
+ // Lazy Loading
40
+ // =============================================================================
41
+ export { lazy, prefetchAll, isLazyComponent, getLazyDelay, getLazyLoading, getLazyError, createPreloadOnHover, createPreloadOnVisible,
42
+ // Inline lazy support
43
+ resolveModuleExport, isWrappedLazyComponent, detectInlineLazyImport, wrapInlineLazy, processRouteComponent, } from './lazy.js';
44
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,gFAAgF;AAChF,QAAQ;AACR,gFAAgF;AA4DhF,gFAAgF;AAChF,iBAAiB;AACjB,gFAAgF;AAEhF,OAAO;AACL,yBAAyB;AACzB,SAAS,EACT,UAAU,EACV,cAAc,EACd,cAAc,EACd,aAAa,EACb,SAAS;AAET,oBAAoB;AACpB,WAAW,EACX,YAAY,EACZ,aAAa;AAEb,iBAAiB;AACjB,UAAU,EACV,WAAW,EACX,eAAe,EACf,YAAY,EACZ,YAAY,EACZ,aAAa,GACd,MAAM,oBAAoB,CAAC;AAE5B,gFAAgF;AAChF,UAAU;AACV,gFAAgF;AAEhF,OAAO,EACL,mBAAmB,EACnB,oBAAoB,EACpB,iBAAiB,EACjB,YAAY,GACb,MAAM,cAAc,CAAC;AAQtB,gFAAgF;AAChF,SAAS;AACT,gFAAgF;AAEhF,OAAO,EACL,WAAW,EACX,aAAa,EACb,SAAS,EACT,kBAAkB,EAClB,oBAAoB;AAEpB,kBAAkB;AAClB,eAAe,EACf,eAAe,EACf,kBAAkB,EAClB,gBAAgB,GACjB,MAAM,aAAa,CAAC;AAErB,gFAAgF;AAChF,aAAa;AACb,gFAAgF;AAEhF,OAAO,EACL,gBAAgB,EAChB,iBAAiB,EACjB,aAAa;AAEb,sBAAsB;AACtB,sBAAsB,EACtB,sBAAsB,EACtB,qBAAqB,EACrB,yBAAyB,EACzB,uBAAuB,GACxB,MAAM,iBAAiB,CAAC;AAEzB,gFAAgF;AAChF,SAAS;AACT,gFAAgF;AAEhF,OAAO,EACL,YAAY,EACZ,eAAe,EACf,eAAe,EACf,qBAAqB,GACtB,MAAM,aAAa,CAAC;AAIrB,gFAAgF;AAChF,aAAa;AACb,gFAAgF;AAEhF,OAAO,EAAE,YAAY,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAI9D,gFAAgF;AAChF,eAAe;AACf,gFAAgF;AAEhF,OAAO,EACL,IAAI,EACJ,WAAW,EACX,eAAe,EACf,YAAY,EACZ,cAAc,EACd,YAAY,EACZ,oBAAoB,EACpB,sBAAsB;AACtB,sBAAsB;AACtB,mBAAmB,EACnB,sBAAsB,EACtB,sBAAsB,EACtB,cAAc,EACd,qBAAqB,GACtB,MAAM,WAAW,CAAC"}
package/dist/lazy.d.ts ADDED
@@ -0,0 +1,197 @@
1
+ import type { RouteComponent, LazyComponent, LazyImportFn, RouteLazyConfig, LazyDefaults } from './types.js';
2
+ /**
3
+ * Options for lazy component loading
4
+ */
5
+ export interface LazyOptions {
6
+ /** Custom loading component shown while loading */
7
+ loading?: () => Node;
8
+ /** Custom error component shown on failure */
9
+ error?: (error: Error, retry: () => void) => Node;
10
+ /** Timeout in milliseconds before showing error (0 = no timeout) */
11
+ timeout?: number;
12
+ /**
13
+ * Delay in milliseconds before showing loading state (default: 200).
14
+ * If the component loads before this threshold, loading state is never shown.
15
+ * Set to 0 to show loading state immediately.
16
+ */
17
+ delay?: number;
18
+ /** Minimum time to show loading state (prevents flash) */
19
+ minLoadTime?: number;
20
+ /** Whether to prefetch the component immediately */
21
+ prefetch?: boolean;
22
+ }
23
+ /**
24
+ * Create a lazy-loaded component
25
+ *
26
+ * The returned function acts as a LazyComponent that can be used in route definitions.
27
+ * It provides caching, error handling, and prefetching capabilities.
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * // Basic usage
32
+ * const LazyDashboard = lazy(() => import('./pages/Dashboard'));
33
+ *
34
+ * // With options
35
+ * const LazySettings = lazy(
36
+ * () => import('./pages/Settings'),
37
+ * {
38
+ * loading: () => document.createTextNode('Loading settings...'),
39
+ * timeout: 5000,
40
+ * }
41
+ * );
42
+ *
43
+ * // In route definition
44
+ * const routes = [
45
+ * { path: '/dashboard', component: LazyDashboard },
46
+ * { path: '/settings', component: LazySettings },
47
+ * ];
48
+ *
49
+ * // Prefetch component
50
+ * LazyDashboard.prefetch();
51
+ * ```
52
+ */
53
+ export declare function lazy(loader: () => Promise<{
54
+ default: RouteComponent;
55
+ } | RouteComponent>, options?: LazyOptions): LazyComponent & {
56
+ prefetch: () => Promise<void>;
57
+ isLoaded: () => boolean;
58
+ getLoaded: () => RouteComponent | undefined;
59
+ reset: () => void;
60
+ };
61
+ /**
62
+ * Prefetch multiple lazy components
63
+ *
64
+ * @example
65
+ * ```ts
66
+ * // Prefetch all admin pages
67
+ * prefetchAll([LazyAdminDashboard, LazyAdminUsers, LazyAdminSettings]);
68
+ * ```
69
+ */
70
+ export declare function prefetchAll(components: Array<LazyComponent & {
71
+ prefetch?: () => Promise<void>;
72
+ }>): Promise<void>;
73
+ /**
74
+ * Extended lazy component with utility methods
75
+ */
76
+ export interface LazyComponentWithMethods {
77
+ /** Call the lazy loader to get the component */
78
+ (): Promise<{
79
+ default: RouteComponent;
80
+ } | RouteComponent>;
81
+ /** Prefetch the component */
82
+ prefetch: () => Promise<void>;
83
+ /** Check if the component is loaded */
84
+ isLoaded: () => boolean;
85
+ /** Get the loaded component synchronously (undefined if not loaded) */
86
+ getLoaded: () => RouteComponent | undefined;
87
+ /** Reset the lazy component state */
88
+ reset: () => void;
89
+ /** Internal: options passed to lazy() */
90
+ readonly __options?: LazyOptions;
91
+ }
92
+ /**
93
+ * Check if a component is a lazy component
94
+ */
95
+ export declare function isLazyComponent(component: unknown): component is LazyComponentWithMethods;
96
+ /**
97
+ * Get the delay option from a lazy component (default: 200ms)
98
+ */
99
+ export declare function getLazyDelay(component: LazyComponentWithMethods): number;
100
+ /**
101
+ * Get the loading component from a lazy component
102
+ */
103
+ export declare function getLazyLoading(component: LazyComponentWithMethods): (() => Node) | undefined;
104
+ /**
105
+ * Get the error component from a lazy component
106
+ */
107
+ export declare function getLazyError(component: LazyComponentWithMethods): ((error: Error, retry: () => void) => Node) | undefined;
108
+ /**
109
+ * Create a preload function that prefetches lazy components on link hover
110
+ *
111
+ * @example
112
+ * ```ts
113
+ * const preloadOnHover = createPreloadOnHover({
114
+ * '/dashboard': LazyDashboard,
115
+ * '/settings': LazySettings,
116
+ * });
117
+ *
118
+ * // Attach to link element
119
+ * linkElement.addEventListener('mouseenter', () => preloadOnHover('/dashboard'));
120
+ * ```
121
+ */
122
+ export declare function createPreloadOnHover(componentMap: Record<string, LazyComponent & {
123
+ prefetch?: () => Promise<void>;
124
+ }>): (path: string) => void;
125
+ /**
126
+ * Create an intersection observer that prefetches lazy components when links enter viewport
127
+ *
128
+ * @example
129
+ * ```ts
130
+ * const observer = createPreloadOnVisible({
131
+ * '/dashboard': LazyDashboard,
132
+ * '/settings': LazySettings,
133
+ * });
134
+ *
135
+ * // Observe link elements
136
+ * document.querySelectorAll('a[href]').forEach(link => {
137
+ * observer.observe(link, link.getAttribute('href')!);
138
+ * });
139
+ *
140
+ * // Cleanup when done
141
+ * observer.disconnect();
142
+ * ```
143
+ */
144
+ export declare function createPreloadOnVisible(componentMap: Record<string, LazyComponent & {
145
+ prefetch?: () => Promise<void>;
146
+ }>, options?: IntersectionObserverInit): {
147
+ observe: (element: Element, path: string) => void;
148
+ unobserve: (element: Element) => void;
149
+ disconnect: () => void;
150
+ };
151
+ /**
152
+ * Resolve the component export from a dynamically imported module.
153
+ *
154
+ * @param module - The imported module object
155
+ * @param exportName - Optional named export to use (default: 'default')
156
+ * @returns The resolved component function
157
+ * @throws Error if no component can be found in the module
158
+ *
159
+ * Resolution order:
160
+ * 1. If exportName is specified, use that export
161
+ * 2. If module has 'default' export, use that
162
+ * 3. Use the first function export found (convenience fallback)
163
+ */
164
+ export declare function resolveModuleExport(module: Record<string, unknown>, exportName?: string): RouteComponent;
165
+ /**
166
+ * Check if a component is a pre-wrapped lazy component (created with lazy()).
167
+ * This is the existing lazy() wrapper with prefetch/isLoaded/reset methods.
168
+ */
169
+ export declare function isWrappedLazyComponent(component: unknown): component is LazyComponentWithMethods;
170
+ export declare function detectInlineLazyImport(component: unknown): component is LazyImportFn;
171
+ /**
172
+ * Wrap an inline lazy import with the lazy() function.
173
+ *
174
+ * This converts: `() => import('./Component.js')`
175
+ * Into a full lazy component with loading states, caching, etc.
176
+ *
177
+ * @param importFn - The inline import function
178
+ * @param exportName - Named export to use from the module
179
+ * @param routeLazyConfig - Per-route lazy config
180
+ * @param globalDefaults - Global lazy defaults from router options
181
+ */
182
+ export declare function wrapInlineLazy(importFn: LazyImportFn, exportName: string | undefined, routeLazyConfig: RouteLazyConfig | undefined, globalDefaults: LazyDefaults | undefined): LazyComponent & LazyComponentWithMethods;
183
+ /**
184
+ * Process a route component during compilation.
185
+ *
186
+ * - If it's a static component, return as-is
187
+ * - If it's already wrapped with lazy(), return as-is
188
+ * - If it's an inline lazy import, wrap it with lazy()
189
+ *
190
+ * @param component - The route component to process
191
+ * @param exportName - Named export to use (for inline lazy)
192
+ * @param routeLazyConfig - Per-route lazy config
193
+ * @param globalDefaults - Global lazy defaults
194
+ * @returns The processed component (either original or wrapped)
195
+ */
196
+ export declare function processRouteComponent(component: RouteComponent | LazyComponent | LazyImportFn | undefined, exportName: string | undefined, routeLazyConfig: RouteLazyConfig | undefined, globalDefaults: LazyDefaults | undefined): RouteComponent | LazyComponent | undefined;
197
+ //# sourceMappingURL=lazy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lazy.d.ts","sourceRoot":"","sources":["../src/lazy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,EACd,aAAa,EACb,YAAY,EACZ,eAAe,EACf,YAAY,EACb,MAAM,YAAY,CAAC;AAMpB;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,mDAAmD;IACnD,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;IACrB,8CAA8C;IAC9C,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,IAAI,KAAK,IAAI,CAAC;IAClD,oEAAoE;IACpE,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB;;;;OAIG;IACH,KAAK,CAAC,EAAE,MA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