From 9ef153ca5358e521572605fdaa5cadf3f026d01d Mon Sep 17 00:00:00 2001 From: Artem Kozlov Date: Mon, 25 Jul 2016 20:27:53 +0200 Subject: [PATCH] Add support for commonjs. --- .jshintrc | 3 ++- src/openseadragon.js | 28 ++++++++++++++++++---------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/.jshintrc b/.jshintrc index 626284b2..013e31e8 100644 --- a/.jshintrc +++ b/.jshintrc @@ -10,6 +10,7 @@ "globals": { "OpenSeadragon": true, - "define": false + "define": false, + "module": false } } diff --git a/src/openseadragon.js b/src/openseadragon.js index 06f6d189..de7399e8 100644 --- a/src/openseadragon.js +++ b/src/openseadragon.js @@ -685,19 +685,10 @@ * @param {OpenSeadragon.Options} options - Viewer options. * @returns {OpenSeadragon.Viewer} */ -window.OpenSeadragon = window.OpenSeadragon || function( options ){ - +function OpenSeadragon( options ){ return new OpenSeadragon.Viewer( options ); - -}; - -if (typeof define === 'function' && define.amd) { - define(function () { - return (window.OpenSeadragon); - }); } - (function( $ ){ @@ -2596,3 +2587,20 @@ if (typeof define === 'function' && define.amd) { } }(OpenSeadragon)); + + +// Universal Module Definition, supports CommonJS, AMD and simple script tag +(function (root, factory) { + if (typeof define === 'function' && define.amd) { + // expose as amd module + define([], factory); + } else if (typeof module === 'object' && module.exports) { + // expose as commonjs module + module.exports = factory(); + } else { + // expose as window.OpenSeadragon + root.OpenSeadragon = factory(); + } +}(this, function () { + return OpenSeadragon; +}));