Jump to content
GreenSock

taurus

Greensock TweenLite/TweenMax exported globals with CommonJS module

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

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

That's excellent, thank you for the constructive answer. I'm eager to see these changes in the original repo instead of forking it.

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.
×