/** * @file Runtime Bundle Optimizer * @description PhD-level runtime bundle optimization with device capability detection, * network-aware chunk loading, and dynamic import prioritization. * * Features: * - Device capability detection (CPU cores, memory, GPU) * - Network-aware chunk sizes * - Conditional polyfill loading * - Dynamic import prioritization * - Module replacement for lighter alternatives * - Resource timing analysis */ /** * Device capability tier */ export type DeviceTier = 'high-end' | 'mid-range' | 'low-end'; /** * Device capabilities */ export interface DeviceCapabilities { /** Number of logical CPU cores */ cpuCores: number; /** Device memory in GB (if available) */ deviceMemory: number | null; /** Hardware concurrency level */ hardwareConcurrency: number; /** GPU info (if available) */ gpu: string | null; /** Is touch device */ isTouch: boolean; /** Max texture size (WebGL) */ maxTextureSize: number | null; /** Supports WebGL */ supportsWebGL: boolean; /** Supports WebGL2 */ supportsWebGL2: boolean; /** Device tier classification */ tier: DeviceTier; } /** * Network conditions */ export interface NetworkConditions { /** Effective connection type */ effectiveType: '4g' | '3g' | '2g' | 'slow-2g' | 'unknown'; /** Downlink speed in Mbps */ downlink: number | null; /** Round-trip time in ms */ rtt: number | null; /** Data saver enabled */ saveData: boolean; /** Online status */ online: boolean; } /** * Bundle loading strategy */ export interface LoadingStrategy { /** Maximum chunk size in bytes */ maxChunkSize: number; /** Enable preloading */ preload: boolean; /** Preload distance (number of routes ahead) */ preloadDistance: number; /** Enable compression hints */ useCompression: boolean; /** Priority boost for critical chunks */ priorityBoost: boolean; /** Defer non-critical chunks */ deferNonCritical: boolean; /** Use service worker caching */ useServiceWorker: boolean; } /** * Polyfill configuration */ export interface PolyfillConfig { /** Feature name */ feature: string; /** Detection function */ detect: () => boolean; /** Polyfill loader */ load: () => Promise; /** Size in bytes */ size: number; } /** * Module alternative */ export interface ModuleAlternative { /** Original module name */ original: string; /** Lightweight alternative */ alternative: string; /** Condition for using alternative */ useAlternativeWhen: (capabilities: DeviceCapabilities, network: NetworkConditions) => boolean; /** Size difference (positive = savings) */ sizeDifference: number; } /** * Bundle optimizer configuration */ export interface BundleOptimizerConfig { /** Enable device detection */ detectDevice?: boolean; /** Enable network monitoring */ monitorNetwork?: boolean; /** Custom polyfills */ polyfills?: PolyfillConfig[]; /** Module alternatives */ alternatives?: ModuleAlternative[]; /** Enable debug logging */ debug?: boolean; } /** * Detect device capabilities */ export declare function detectDeviceCapabilities(): DeviceCapabilities; /** * Detect network conditions */ export declare function detectNetworkConditions(): NetworkConditions; /** * Runtime bundle optimizer */ export declare class BundleOptimizer { private static instance; private config; private readonly deviceCapabilities; private networkConditions; private loadedPolyfills; private loadingStrategy; private importQueue; private networkChangeCallbacks; constructor(config?: BundleOptimizerConfig); /** * Get singleton instance */ static getInstance(config?: BundleOptimizerConfig): BundleOptimizer; /** * Reset singleton (for testing) */ static reset(): void; /** * Subscribe to network changes */ onNetworkChange(callback: (conditions: NetworkConditions) => void): () => void; /** * Load required polyfills */ loadPolyfills(): Promise; /** * Check if polyfill is needed */ needsPolyfill(feature: string): boolean; /** * Get optimal module for current conditions */ getOptimalModule(moduleName: string): string; /** * Priority-based dynamic import */ importWithPriority(importFn: () => Promise, options?: { priority?: 'critical' | 'high' | 'normal' | 'low'; moduleId?: string; timeout?: number; }): Promise; /** * Preload modules based on route prediction */ preloadModules(modules: Array<() => Promise>): void; /** * Analyze loaded resources */ analyzeLoadedResources(): { totalSize: number; jsSize: number; cssSize: number; imageSize: number; largestResources: Array<{ name: string; size: number; type: string; }>; slowestResources: Array<{ name: string; duration: number; type: string; }>; }; /** * Get device capabilities */ getDeviceCapabilities(): DeviceCapabilities; /** * Get network conditions */ getNetworkConditions(): NetworkConditions; /** * Get loading strategy */ getLoadingStrategy(): LoadingStrategy; /** * Get default capabilities */ private getDefaultCapabilities; /** * Calculate optimal loading strategy */ private calculateLoadingStrategy; /** * Start network monitoring */ private startNetworkMonitoring; /** * Notify network change listeners */ private notifyNetworkChange; /** * Debug logging */ private log; } /** * Hook for bundle optimization awareness */ export declare function useBundleOptimizer(): { deviceCapabilities: DeviceCapabilities; networkConditions: NetworkConditions; loadingStrategy: LoadingStrategy; shouldLoadFeature: (featureSize: number, priority?: 'critical' | 'high' | 'normal' | 'low') => boolean; }; /** * Hook for conditional feature loading */ export declare function useConditionalLoad(loader: () => Promise, options?: { condition?: boolean; priority?: 'critical' | 'high' | 'normal' | 'low'; fallback?: T; onError?: (error: Error) => void; }): { data: T | undefined; loading: boolean; error: Error | null; }; /** * Get the global bundle optimizer instance */ export declare function getBundleOptimizer(config?: BundleOptimizerConfig): BundleOptimizer; /** * Reset the global optimizer (for testing) */ export declare function resetBundleOptimizer(): void; /** * Load polyfills */ export declare function loadPolyfills(): Promise; /** * Get optimal module name */ export declare function getOptimalModule(moduleName: string): string; /** * Format bytes helper */ export declare function formatBundleSize(bytes: number): string; declare const _default: { BundleOptimizer: typeof BundleOptimizer; getBundleOptimizer: typeof getBundleOptimizer; resetBundleOptimizer: typeof resetBundleOptimizer; detectDeviceCapabilities: typeof detectDeviceCapabilities; detectNetworkConditions: typeof detectNetworkConditions; loadPolyfills: typeof loadPolyfills; getOptimalModule: typeof getOptimalModule; formatBundleSize: typeof formatBundleSize; useBundleOptimizer: typeof useBundleOptimizer; useConditionalLoad: typeof useConditionalLoad; }; export default _default;