jeudi 19 février 2015

Browserify universal module definition - browser/node different dependencies

How is it best to setup my UMD to inject different dependencies based on if its being run in a node or a browser environment?


I have the following UMD for my module which should work in both browser and node environments. I am struggling however because Node requires both the 'ws' and 'atob' dependencies whereas the browser doesn't.


Normally, the third condition would run here, but because I am using Browserify, the second condition is met...and the un-required dependencies are fetched.



(function (root, factory) {

if (typeof define === 'function' && define.amd) {

// AMD
define(['q'], ['ws'], ['atob'], factory);

} else if (typeof exports === 'object') {

// Node OR Browserify
module.exports = factory(require('q'), require('ws'), require('atob'));


} else {

// Browser globals (root is window) Q and WebSocket is already available
Library = factory(root.Q, root.WebSocket, root.atob);

}
}(this, function (Q, WebSocket, atob) {


return {
//stuff
};

}));


Is it possible for Browserify to run the module definition as if it were a browser? In which case, I could use..



typeof window === 'undefined'

Aucun commentaire:

Enregistrer un commentaire