Jump to content
Search Community

Greensock TweenLite/TweenMax exported globals with CommonJS module

taurus test
Moderator Tag

Go to solution Solved by OSUblake,

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

Recommended Posts

Can Greensock TweenLite/TweenMax globals be obtained when using the library as CommonJS module?

 

E.g. easing functions that are available as window.com.greensock.easing, like

let Power4 = require('./TweenMax')...

I feel myself 10 years younger when I look at all these globals. I look at the source code and see that the only export is TweenMax constructor. Can it be done somehow without resorting to globals at all?

Link to comment
Share on other sites

  • Solution

Hi @taurus
 
You can namespace GSAP if you don't want to have globals. 
 
Support for using GSAP as modules is somewhat limited right now as it goes against the current architecture. GSAP uses dependency injection, much like Angular, so it's not something that can be easily refactored out into a new version. So until we get a new version, you can do the following as a workaround. I know editing the source file is not ideal, but it will work for the time being.
 
Add the following to the file in node_modules/gsap/src/uncompressed/TweenMax.js

/*!
 * @author: Jack Doyle, jack@greensock.com
 **/

// ADD THIS
var window = {}
  , navigator = { userAgent: "" }
  , dummyElement = { style: {}, getElementsByTagName: function() { return [] } }
  , document = { createElement: function() { return dummyElement } };

// START OF CODE
var _gsScope = (typeof (module) !== "undefined"...

// ADD THIS TO NAMESPACE GLOBALS
var gsap = _gsScope.GreenSockGlobals = {};

(_gsScope._gsQueue || (_gsScope._gsQueue = [])).push( function() {

  // ...  
  _tickerActive = false; 

  // ADD THIS
  module.exports = gsap;

// END OF FILE
})((typeof(module) !== "undefined"...

Now we can use ES6 module syntax to import GSAP without any globals.

import { TweenMax, Power4 } from "gsap";

TweenMax.to("#foo", 1, { x: 100, ease: Power4.easeOut });
  • 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...