greensock / GSAP

GSAP (GreenSock Animation Platform), a JavaScript animation library for the modern web
https://gsap.com
19.56k stars 1.72k forks source link

Webpack bundle only TweenLite #246

Closed raphaelkottakal closed 6 years ago

raphaelkottakal commented 6 years ago

Hi, I'm using: import { TweenLite } from 'gsap'; to load only TweenLite. But when using webpack-bundle-analyzer I found it's loading the entire TweenMax module(parsed size: 364kb). This is bloating up the final bundle js. Also tried using import TweenLite from 'gsap/TweenLite';(parsed size: 84kb) so the final bundle is smaller. But TweenLite is not working with that.

Need help on how to keep my final bundle small, as I'm not using all the features in TweenMax.

jackdoyle commented 6 years ago

"gsap" points to TweenMax, actually. That's why you're getting all that. You were correct in importing from "gsap/TweenLite" but the reason things may not be working is if perhaps you're trying to animate CSS-related values. That requires CSSPlugin (which is, of course, inside TweenMax). So you'd need to import CSSPlugin from "gsap/CSSPlugin". Does that clear things up?

raphaelkottakal commented 6 years ago

Hey Jack,

It works!! Thanks, you're awesome!

inomdzhon commented 6 years ago

Hi @jackdoyle,

Thank you for your powerful library! 😃


I have same issue with bundle size when I need import ScrollToPlugin and TweenMax together.

Ok, I can import all included in TweenMax separately. It's even better for me, because I don't use all pieces of TweenMax (example, roundProps and EasePack). And I can do like this:

import TweenLite from 'gsap/TweenLite'; 
import 'gsap/CSSPlugin'; 
import 'gsap/ScrollToPlugin'; 

But. May be I'll want use yoyo method, which pieces of TweenMax, then how I can import this? If I do this:

import TweenMax from 'gsap'; 
import 'gsap/ScrollToPlugin'; 

I'll have import TweenLite twice, because TweenMax default included it and ScrollToPlugin required it.


And optional questions:

  1. why can't we to allocate features of TweenMax in separate module (like TweenLite) and create other module (example, gsap.js), which included all pieces: TimelineLite, TimelineMax, TweenLite, TweenMax, etc.
  2. TweenMax could require all needed module. Why we concatenate?

Thanks.

jackdoyle commented 6 years ago

@inomdzhon Yeah, sorry about that. We're working on an ES6 version of the library for version 2.0.0, but that's probably a few months out at least. It's a lot of work :) But that should allow you to get more modular control of things (like imports). For now, you're right, there are certain situations in which you might get a redundant TweenLite in your bundle or something.

inomdzhon commented 6 years ago

@jackdoyle Sounds good! Looking forward to it)

daviddelusenet commented 6 years ago

I'm encountering the same problem. I'm doing this: import { TweenLite, TimelineLite } from 'gsap'

If I refactor that to this:

import TimelineLite from 'gsap/TimelineLite';
import TweenLite from 'gsap/TimelineLite';

My animations stop working. This is because in every component I use gsap I'm doing an import which breaks gsap.

Any idea on how to fix this?

EDIT

I now did the following: removed all gsap imports from my application. After that I added the following line to my App.js (root of my project): import 'gsap';. And this works since gsap is global.

However, in this setup TweenMax still gets imported. If I do this:

import 'gsap/TimelineLite';
import 'gsap/TweenLite';

My animations stop working. So how do I only import TimelineLite/TweenLite?

jackdoyle commented 6 years ago

@daviddelusenet tough to say without seeing a reduced test case, but we're working on a more ES6-friendly release of GSAP which you can preview here: https://greensock.com/temp/GSAP-ES6.zip (this link will only be valid for the next week or two). Does that work any better for you?

(Also notice that there's a new "gsap/all" that imports/exports all of the standard/public classes in case that's easier for you)

inomdzhon commented 6 years ago

@jackdoyle when the planned release about this?

jackdoyle commented 6 years ago

@inomdzhon Hopefully pretty soon, but we're looking for more feedback from the community and some key players first. Have you tested it in your environment? Does it work well for you?

inomdzhon commented 6 years ago

@jackdoyle I will check in soon 👍

inomdzhon commented 6 years ago

@jackdoyle I checked out.

Current release

Stats:

gsap_old_syntax

ES6-friendly release of GSAP

Really better stats:

gsap_es6

But anyway problem stayed. I imported only lite versions of Tween and Timeline and Power1, Sine easing, but since we import all modules in gsap/index.js, I have all of them in bundle.

I removed TweenMax, TimelineMax and unnecessary Easing from gsap/index.js and decrease bundle size.

Stats after:

gsap_es6_lite

Plugin problem

And one more point. For using plugin, example, ScrollToPlugin we must import it like import { TweenLite, ScrollToPlugin } from 'gsap';, but it's mean it's unused variable. Maybe make more obviously method, example, TweenLite.setPlugin(ScrollToPlugin)? This applies to AttrPlugin, CSSPlugin and all global things.

jackdoyle commented 6 years ago

Thanks for the feedback!

index.js forces all those things into the bundle on purpose, to maintain backward compatibility for people who did something like:

import "gsap";

If your goal is to reduce the payload and enable maximum tree shaking, did you try either importing each thing directly or just use the "gsap/all" file?

import { TweenLite, TimelineLite, ScrollToPlugin, Power1, Sine } from "gsap/all"; 

or

import TweenLite from "gsap/TweenLite";
import TimelineLite from "gsap/TimelineLite";
...
benjaminbours commented 6 years ago

Hello, i had the same problem.

The GSAP-ES6 release solved it for me. Thank you jackdoyle 👍

tonix-tuft commented 4 years ago

Does import { TweenLite } from "gsap"; load the CSSPlugin for automatic vendor prefixing of styles?

Or should I import TweenMax with import { TweenMax } from "gsap"; to get CSSPlugin loaded automatically?

OSUblake commented 4 years ago

This is an old topic. You should do it like this.

import { gsap } from "gsap";

It includes the CSSPlugin. Please read the installation documentation and watch the npm video. https://greensock.com/docs/v3/Installation

tonix-tuft commented 4 years ago

@OSUblake Thank you for the feedback.

I have just seen the video. So, is this enough to automatically apply autoprefixing with CSSPlugin:

import { gsap } from "gsap";

gsap.set(element, {
    css: {
         x: 123,
         y: 456,
         scale: 1.2
    }
});

?

Thank you!

jackdoyle commented 4 years ago

@tonix-tuft you no longer need to wrap things in a "css:{}" object. GSAP 3 automatically figures that out for you. And yes, it'll discern if you're running a really old browser that needs a prefix for transforms and apply that for you. All you need to do is:

import { gsap } from "gsap";

gsap.set(element, {
  x: 123,
  y: 456,
  scale: 1.2
});

If you haven't checked out the "getting started" article, it's a great place to get a handle on the basics. https://greensock.com/get-started

If you have any other questions, the best place to go is https://greensock.com/forums - it's a fantastic community.

Happy tweening!

tonix-tuft commented 4 years ago

Thank you very much! I am new to gsap and will certainly take a look!