Search code examples
javascriptdynamics-crmbrowserifybluebird

Exporting internal library dependency to outer scope with browserify


I'm using browserify for requiring bluebird for cross-browser compliant usage of promises in my WebApi Wrapper for Dynamics CRM. It works great so far, however I don't like that I can't do Promise.all for example on my returned call results in IE, without including bluebird again in my top page. For this reason I'd like my browserify bundle to expose Promise to the global scope. Of course it would work to just do global.Promise = require("bluebird"), but that feels kind of dirty. Using a standalone bundle I made it, that my standalone exposes my client as property and Promise, too. However names get longer this way and I would like to just be able to use Promise (without my standalone wrapper) everywhere.

What do you think about that? Is that possible or should I not do that?

What I do right now is define my client as IIFE, and inside it do the following: module.exports = { Client: WebApiClient, Promise: Promise };

and browserifying like browserify src/js/WebApiClient.js -d --standalone XrmWebApi -o Publish/WebApiClient.js. This way I'm currently able to use XrmWebApi.Client and XrmWebApi.Promise, but I would very much like to get rid of the XrmWebApi for calling the promises.

Thanks for your help.

Kind Regards, Florian


Solution

  • Create a new file for your global definitions and then add that as an entry point in your bundle step.

    src/js/globals.js:

    var bluebirdPromise = require("bluebird");
    global.Promise = global.Promise || bluebirdPromise;
    

    browserify src/js/globals.js src/js/WebApiClient.js -d -o Publish/WebApiClient.js

    You will probably want to remove references to bluebird in your file src/js/WebApiClient.js and its dependencies and just use Promise.