Stamplay / stamplay-js-sdk

:rocket: JavaScript SDK of Stamplay cloud platform
https://stamplay.com
MIT License
88 stars 18 forks source link

Expose the JS SDK as an AMD/CommonJS module and provide a way to substitute dependencies #27

Closed lolmaus closed 8 years ago

lolmaus commented 8 years ago

(moved from https://github.com/Stamplay/stamplay-product-development/issues/8)

Problems

  1. Stamplay JS SDK puts itself into the global scope and expects its dependencies to be available in the global scope. In 2016, polluting the global scope is considered to be a bad practice. All major frameworks are 100% modularized and don't rely on any globals.
  2. The SDK relies on Underscore and Q, while the host app might be already requiring their alternatives: Lodash and RSVP/Bluebird/etc. It is kinda humiliating and inefficient to be forced to include all four dependencies when you need only two.

    Suggested solutions

  3. Use a UMD module! https://github.com/umdjs/umd It's just a code snippet that you wrap your library with -- and your library automatically detects globals/AMD/CommonJS at runtime! That's a no-brainer really. You can provide a UMD-wrapped version in addition to existing builds, so that you don't do a potentially breaking change.
  4. This one's tougher. The SDK already allow replacing the Q dependency -- all you need to do is to redefine the Stamplay.makePromise method. But as for Underscore, the SDK uses the _ global immediately after the SDK script is included. The only way to substitute it is to provide your own _ global before including the SDK.

    But my Lodash lib is an AMD module and it's not available at the moment the SDK is included. And I do not want to touch any globals in the first place.

    I don't know a definite solution for this problem. Here are a couple of ways to consider:

    • Let the user build it's own version of the SDK, configuring dependency module names.
    • Tell the user to reexport their Lodash and RSVP/Bluebird modules under the names of Q and Underscore modules respectively.

Guys, use :arrow_lower_left: reaction emojis to express your demand for this feature! Don't flood the thread with +1 comments!

criroselli commented 8 years ago

Done with version 2.0.6, in dist folder stamplay-umd-nodeps.js