If you want your javascript code can be used in browser and on the server within commonjs - you have to consider a lot of things.
There are actually 3 enviroments we need to take care about:
- browser env. with default behaviour
- browser env. while using AMD wrapper
- serverside env. f.e. nodejs
Differences:
- in default browser you have no
module
,exports
andmodule.exports
objects andthis
references thewindow
object. - in browser using AMD wrapper you might be able to use
module
,exports
,module.exports
,setExports
andthis
can referenceexports
orwindow
object. It depends on loader implementation. - in nodejs you have
module
,exports
,module.exports
andthis
references toexports
.
expose.js is a tiny function, which takes care about all this differences and provides an api for all enviroments.
You can copy this function into your library or add it during build process.
expose
will detect the environment and depending on it, export your api.
namespace
is an optional string and is used to attach yourapi
object towindow
. You should use it if you want to support browsers without AMD.api
is also optional and is the object you want to export.env
is always an object.
Example env
object:
{
commmonjs: true, // is `true` if you are inside of commonjs env. - amd or ss
browser: true, // is `true` if you are in the browser
global: global, // always correct global object in any env.
exports: api, // your api object or the empty exports object
module: module // is set if you are in commonjs env.
}
get the env.
var env = expose();
get the env. and create a global namespace if needed.
var env = expose('mylib');
var exports = env.exports;
var global = env.global;
exports.myMethod = function(){
global.setTimeout; // always correct global
};
export the library
var $ = {};
$.myMethod = function(){};
expose('mylib', $);