Jump to content
Search Community

registerPlugin

asoelist test
Moderator Tag

Go to solution Solved by GreenSock,

Recommended Posts

Hey Forum, I have a "back to basic" question. I haven't been paying attention to this detail while practicing gsap. Now I need to ask this question. 

 

According to gsap documents it is recommended to register plugins so that build tools don't drop them during tree shaking. 

 

I used gsap.min.js, ScrollTrigger.min.js, Draggable.min.js, and MotionPathPlugin.min.js.

image.thumb.png.c434120c7a903a6269135362c8f8e7e6.png

 

So if I want to add one more library say CustomEase.min.js, I need to register this as well:

image.thumb.png.2514fd2753cda81c6ec9c89da023508a.png

 

Is that all I have to do?

 

Thanks!

Link to comment
Share on other sites

Hi,

 

The loadScript method creates a script tag or adds those elements as modules so you can import them in other files? By using those CDN links my guess is that they are added as script tags.

 

When using CDN links on a script tag like this:

<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.2/gsap.min.js"></script>

Normally there is no need to register the plugins.

 

Tree shaking works in build tools like webpack when the tools are installed using npm/yarn and imported using either ES Modules or UMD Modules.

 

Most definitely registering the plugins is not going to hurt or create any performance issue, but it might not be always necessary depending on the setup you have in your app.

 

Hopefully this helps.

Happy Tweening!

  • Like 1
Link to comment
Share on other sites

Thanks for replying @Rodrigo,

Loading gsap libraries dynamically allows me to insert gsap when it's needed. Here is my loadScript: 

image.thumb.png.45467d6cc3cd8ef0fb9c412a26b2f6d8.png

 

And like you said I appended (and the other 3 libraries)

"<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.2/gsap.min.js"></script>"

into the document.head. 

 

But sometimes I got this error from google's developer's tools: 

image.png.bde566e8636a3eabf73123c376d623e9.png

 

The offending code is this:

image.thumb.png.48fd01e32444d6b2f2e0b0ac4ecb24f6.png

 

Somehow if I don't register this "DrawSVGPlugin" , the program won't run. (other plugins are more "lenient"). But even though I register "DrawSVGPlugin", sometimes if gives the error "gsap is not defined". It complains but the program still runs.

 

(I don't use webpack because it's another learning curve for me. Although I use the Javascript module feature for code reuse. Somehow I find that webpack don't play nice with my web framework so I leave webpack for the next iteration).

 

Since I don't use webpack, I don't really have to deal with "tree shaking" issue and register is not compulsory? 

Link to comment
Share on other sites

  • Solution

It's best to just always register the plugins. Let's not create confusion by saying it's optional. It'll only automatically register them if they're loaded into the global scope AFTER the main GSAP file has already loaded, and that's just a convenience that I'd rather not teach people to rely on. 

 

I assume you're getting that error because at the time you try to register one of the plugins, the gsap.min.js file hasn't loaded yet. You've got to make sure that loads FIRST because obviously you can't register a plugin with GSAP if GSAP doesn't even exist yet (hasn't loaded). 

 

Summary: don't register any plugins until GSAP exists (has fully loaded). If you're still having trouble, please provide a minimal demo that clearly illustrates the problem and we'd be happy to take a peek. 

 

Thanks for being a Club GreenSock member! 💚

  • Like 1
Link to comment
Share on other sites

1 hour ago, GreenSock said:

 

 

I assume you're getting that error because at the time you try to register one of the plugins, the gsap.min.js file hasn't loaded yet. You've got to make sure that loads FIRST because obviously you can't register a plugin with GSAP if GSAP doesn't even exist yet (hasn't loaded). 

 

 

Perhaps this is the solution to make sure gsap.min.js is loaded first:

image.thumb.png.a80d33c7a48b07ddb2ef9a95cbfdccbc.png

 

Thanks @GreenSock and @Rodrigo. It clears up my understanding of registerPlugin.

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...