UNPKG

kinetic-slider

Version:

A WebGL-powered kinetic slider component using PIXI.js

1 lines 12 kB
{"version":3,"file":"ImportHelpers.cjs","sources":["../../src/ImportHelpers.ts"],"sourcesContent":["/**\n * Helper to handle dynamic imports for the KineticSlider\n */\nimport type { gsap as GSAPType } from 'gsap';\nimport type * as PixiJS from 'pixi.js';\nimport type * as PixiFilters from 'pixi-filters';\n\n// Cache for imported modules\nconst importCache = new Map<string, any>();\n\n/**\n * Load GSAP and related plugins\n */\nexport const loadGSAP = async (): Promise<typeof GSAPType> => {\n if (importCache.has('gsap')) {\n console.log('Using cached GSAP module');\n return importCache.get('gsap');\n }\n\n try {\n console.log('Loading GSAP module...');\n const gsapModule = await import('gsap');\n\n // Store in cache\n importCache.set('gsap', gsapModule.gsap);\n console.log('GSAP module loaded successfully');\n\n return gsapModule.gsap;\n } catch (error) {\n console.error('Failed to load GSAP:', error);\n throw error;\n }\n};\n\n/**\n * Load GSAP PixiPlugin\n */\nexport const loadPixiPlugin = async (): Promise<any> => {\n if (importCache.has('pixiPlugin')) {\n console.log('Using cached GSAP PixiPlugin');\n return importCache.get('pixiPlugin');\n }\n\n try {\n console.log('Loading GSAP PixiPlugin...');\n\n // Check if we're in a browser environment\n if (typeof window !== 'undefined') {\n // Load GSAP first if not already loaded\n if (!importCache.has('gsap')) {\n await loadGSAP();\n }\n\n // Get GSAP instance\n const gsap = importCache.get('gsap');\n\n // Check if PixiPlugin is already available in GSAP\n if (gsap.plugins && gsap.plugins.PixiPlugin) {\n const PixiPlugin = gsap.plugins.PixiPlugin;\n importCache.set('pixiPlugin', PixiPlugin);\n console.log('GSAP PixiPlugin loaded from gsap.plugins');\n return PixiPlugin;\n }\n\n // Otherwise try to import it directly\n try {\n const { default: PixiPlugin } = await import('gsap/PixiPlugin');\n importCache.set('pixiPlugin', PixiPlugin);\n console.log('GSAP PixiPlugin loaded successfully');\n return PixiPlugin;\n } catch (importError) {\n console.warn('Failed to import PixiPlugin directly, trying to get from GSAP:', importError);\n\n // As a fallback, try to access it from the GSAP instance\n if (gsap.PixiPlugin) {\n importCache.set('pixiPlugin', gsap.PixiPlugin);\n console.log('GSAP PixiPlugin loaded from gsap instance');\n return gsap.PixiPlugin;\n }\n\n throw new Error('PixiPlugin not found in GSAP');\n }\n } else {\n // When running in Node.js during build, return a mock\n console.log('Running in Node.js environment, using mock PixiPlugin');\n const mockPlugin = {\n name: 'PixiPlugin (Mock)',\n registerPIXI: () => console.log('Mock registerPIXI called')\n };\n importCache.set('pixiPlugin', mockPlugin);\n return mockPlugin;\n }\n } catch (error) {\n console.warn('Failed to load PixiPlugin for GSAP:', error);\n return null;\n }\n};\n\n/**\n * Load PixiJS\n */\nexport const loadPixi = async (): Promise<typeof PixiJS> => {\n if (importCache.has('pixi')) {\n console.log('Using cached PixiJS module');\n return importCache.get('pixi');\n }\n\n try {\n console.log('Loading PixiJS module...');\n const pixiModule = await import('pixi.js');\n\n // Store in cache\n importCache.set('pixi', pixiModule);\n console.log('PixiJS module loaded successfully');\n\n return pixiModule;\n } catch (error) {\n console.error('Failed to load PixiJS:', error);\n throw error;\n }\n};\n\n/**\n * Load Pixi Filters\n */\nexport const loadPixiFilters = async (): Promise<typeof PixiFilters> => {\n if (importCache.has('pixiFilters')) {\n console.log('Using cached Pixi Filters module');\n return importCache.get('pixiFilters');\n }\n\n try {\n console.log('Loading Pixi Filters module...');\n const filtersModule = await import('pixi-filters');\n\n // Store in cache\n importCache.set('pixiFilters', filtersModule);\n console.log('Pixi Filters module loaded successfully');\n\n return filtersModule;\n } catch (error) {\n console.error('Failed to load Pixi Filters:', error);\n throw error;\n }\n};\n\n/**\n * Register GSAP PixiPlugin with the required PIXI classes\n */\nexport const registerGSAPPixiPlugin = (gsap: typeof GSAPType, pixi: typeof PixiJS) => {\n try {\n const PixiPlugin = importCache.get('pixiPlugin');\n if (!PixiPlugin) {\n console.warn('PixiPlugin not available for registration');\n return false;\n }\n\n // Skip actual registration if it's a mock\n if (PixiPlugin.name === 'PixiPlugin (Mock)') {\n console.log('Using mock PixiPlugin, skipping actual registration');\n return true;\n }\n\n // Extract needed classes from pixi module\n const { Application, Sprite, Container, Text, DisplacementFilter } = pixi;\n\n // Register the plugin with PIXI classes\n gsap.registerPlugin(PixiPlugin);\n PixiPlugin.registerPIXI({\n Application,\n Sprite,\n Container,\n Text,\n DisplacementFilter\n });\n\n console.log('GSAP PixiPlugin registered successfully');\n return true;\n } catch (error) {\n console.error('Failed to register GSAP PixiPlugin:', error);\n return false;\n }\n};\n\n/**\n * Load all required libraries for the KineticSlider\n */\nexport const loadKineticSliderDependencies = async (): Promise<{\n gsap: typeof GSAPType;\n pixi: typeof PixiJS;\n pixiFilters: typeof PixiFilters;\n pixiPlugin: any;\n}> => {\n try {\n console.log('Loading KineticSlider dependencies...');\n\n // Load core modules\n const gsap = await loadGSAP();\n const pixi = await loadPixi();\n const pixiPlugin = await loadPixiPlugin();\n const pixiFilters = await loadPixiFilters();\n\n // Register PixiPlugin if available\n if (pixiPlugin) {\n registerGSAPPixiPlugin(gsap, pixi);\n }\n\n console.log('All KineticSlider dependencies loaded successfully');\n\n return { gsap, pixi, pixiFilters, pixiPlugin };\n } catch (error) {\n console.error('Failed to load KineticSlider dependencies:', error);\n throw error;\n }\n};\n\n/**\n * Check if all necessary dependencies are loaded\n */\nexport const areDependenciesLoaded = () => {\n return (\n importCache.has('gsap') &&\n importCache.has('pixi') &&\n importCache.has('pixiFilters')\n );\n};\n\n/**\n * Load all hooks for the KineticSlider\n */\nexport const loadKineticSliderHooks = async (): Promise<any> => {\n if (importCache.has('hooks')) {\n console.log('Using cached KineticSlider hooks');\n return importCache.get('hooks');\n }\n\n try {\n console.log('Loading KineticSlider hooks...');\n const hooks = await import('./hooks');\n\n // Store in cache\n importCache.set('hooks', hooks);\n console.log('KineticSlider hooks loaded successfully');\n\n return hooks;\n } catch (error) {\n console.error('Failed to load KineticSlider hooks:', error);\n throw error;\n }\n};"],"names":[],"mappings":";;AAQA,MAAM,WAAA,uBAAkB,G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