import gsap from "gsap"; import { ScrollTrigger } from "gsap/ScrollTrigger"; const PLUGINS = [ "CSSRulePlugin", "CustomBounce", "CustomEase", "CustomWiggle", "Draggable", "DrawSVGPlugin", "EaselPlugin", "EasePack", "Flip", "GSDevTools", "InertiaPlugin", "MorphSVGPlugin", "MotionPathHelper", "MotionPathPlugin", "Observer", "Physics2DPlugin", "PhysicsPropsPlugin", "PixiPlugin", "ScrambleTextPlugin", "ScrollSmoother", "ScrollToPlugin", "ScrollTrigger", "SplitText", "TextPlugin", ] as const; type Plugins = typeof PLUGINS[number]; // In order to dynamically load all the GSAP plugins const pluginMap = { CustomEase: () => import("gsap/CustomEase"), Draggable: () => import("gsap/Draggable"), CSSRulePlugin: () => import("gsap/CSSRulePlugin"), EaselPlugin: () => import("gsap/EaselPlugin"), EasePack: () => import("gsap/EasePack"), Flip: () => import("gsap/Flip"), MotionPathPlugin: () => import("gsap/MotionPathPlugin"), Observer: () => import("gsap/Observer"), PixiPlugin: () => import("gsap/PixiPlugin"), ScrollToPlugin: () => import("gsap/ScrollToPlugin"), ScrollTrigger: () => import("gsap/ScrollTrigger"), TextPlugin: () => import("gsap/TextPlugin"), DrawSVGPlugin: () => import("gsap/DrawSVGPlugin"), Physics2DPlugin: () => import("gsap/Physics2DPlugin"), PhysicsPropsPlugin: () => import("gsap/PhysicsPropsPlugin"), ScrambleTextPlugin: () => import("gsap/ScrambleTextPlugin"), CustomBounce: () => import("gsap/CustomBounce"), CustomWiggle: () => import("gsap/CustomWiggle"), GSDevTools: () => import("gsap/GSDevTools"), InertiaPlugin: () => import("gsap/InertiaPlugin"), MorphSVGPlugin: () => import("gsap/MorphSVGPlugin"), MotionPathHelper: () => import("gsap/MotionPathHelper"), ScrollSmoother: () => import("gsap/ScrollSmoother"), SplitText: () => import("gsap/SplitText"), } as const; type PluginMap = typeof pluginMap; type Plugins = keyof PluginMap; // Resolves the module type for a given key, then picks the named export matching the key // this allows to have the type definitions for autocomplete in your code editor type PluginModule = Awaited>; type PluginExport = PluginModule[K & keyof PluginModule]; export default function() { // Register all the GSAP Plugins you want at this point gsap.registerPlugin(ScrollTrigger); /* If you want to lazy load some of the plugins that are not widely used in your app (for example in just a couple of components or a single route), you can use this method */ async function lazyLoadPlugin(plugin: K): Promise> { const loader = pluginMap[plugin]; const m = await loader(); const p = (m as any)[plugin]; gsap.registerPlugin(p); return p; } return { gsap, ScrollTrigger, lazyLoadPlugin, }; };