Jump to content
Search Community

How to import gsap lib in the best way?

Reginna test
Moderator Tag

Recommended Posts

I built the project animation with gsap on Nuxt.js and tried to analyze bundles.
The size of gsap is bigger than other module.

I tried two ways to build my project:

// use dist
import { gsap } from 'gsap/dist/gsap'
import { ScrollTrigger } from 'gsap/dist/ScrollTrigger'
import { ScrollToPlugin } from 'gsap/dist/ScrollToPlugin'

// use normal
import { gsap } from 'gsap'
import { ScrollTrigger } from 'gsap/ScrollTrigger'
import { ScrollToPlugin } from 'gsap/ScrollToPlugin'

 

If I included ScrollTrigger.js and ScrollToPlugin.js, the size of gsap was around 31 KB.

1. 

gsap-0001.thumb.png.2cb0ecf548d632b93cfff9195abed5b8.png

 

2.

gsap-0002.thumb.png.8acd59ce36ffc925b708dac217c5f95a.png

 

I tried to import gasp with gsap-core and got error: "gsap.utils.checkPrefix is not a function"

// import gsap-core
import { gsap } from 'gsap/gsap-core'

Error.jpg.5208023227fc2934eccd65f27dd55a17.jpg

 

The full code:

import Vue from 'vue'

// import { gsap } from 'gsap/gsap-core'
import { gsap } from 'gsap/dist/gsap'
import { ScrollTrigger } from 'gsap/dist/ScrollTrigger'
import { ScrollToPlugin } from 'gsap/dist/ScrollToPlugin'

// [START] className Plugin for Gsap v3
// Gsap v3 remove tween className add and remove. Gsap Official recommend use vanilla JavaScript: className.add() & className.remove()
// Or use Plugin. Reference: https://codepen.io/GreenSock/pen/BaNRejV?editors=0010
// Try to use vanilla JavaScript add or remove class, non-essential to use this plugin. It is recommended to use stagger when you need it.
const classNamePlugin = {
	...
}
// [END] className Plugin for Gsap v3

if (process.client) {
	gsap.registerPlugin(ScrollTrigger, ScrollToPlugin, classNamePlugin)
}

Vue.prototype.$gsap = gsap
Vue.prototype.$ScrollTrigger = ScrollTrigger

 

Is it possible to import gsap in another way to reduce the size?

 

---

 

gsap version: 3.5.1

See the Pen BaNRejV?editors=0010 by GreenSock (@GreenSock) on CodePen

Link to comment
Share on other sites

Hi there. There's also no gsap-core folder, so that's not going to work - UMD and ES Modules are also the same code, just different formats. There's not a way to import GSAP to make it smaller. However you import it - it's the same code.

It may be worth giving this a read.


There are also other smaller animation libraries, but you won't get all the functionality, cross browser support and flexibility. It's a trade off.

  • Like 1
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...