FEATURE: Support UMD and NPM (#226)

FEATURE: Support UMD and NPM (#226)

This gives us a package.json we can publish to NPM as well as a universal module definition that supports browser globals as well as ES6 imports in Ember CLI via auto import.

diff --git a/assets/message-bus.js b/assets/message-bus.js
index cdc9b23..85a145d 100644
--- a/assets/message-bus.js
+++ b/assets/message-bus.js
@@ -1,5 +1,19 @@
 /*jshint bitwise: false*/
-(function(exports, document, jQuery) {
+
+(function (root, factory) {
+  if (typeof define === 'function' && define.amd) {
+    // AMD. Register as an anonymous module.
+    define([], function (b) {
+      // Also create a global in case some scripts
+      // that are loaded still are looking for
+      // a global even when an AMD loader is in use.
+      return (root.MessageBus = factory());
+    });
+  } else {
+    // Browser globals
+    root.MessageBus = factory();
+  }
+}(typeof self !== 'undefined' ? self : this, function () {
   "use strict";
 
   // http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
@@ -359,7 +373,7 @@
     shouldLongPollCallback: undefined,
     baseUrl: baseUrl,
     headers: {},
-    ajax: jQuery && jQuery.ajax,
+    ajax: typeof jQuery !== "undefined" && jQuery.ajax,
     diagnostics: function() {
       console.log("Stopped: " + stopped + " Started: " + started);
       console.log("Current callbacks");
@@ -535,5 +549,5 @@
       return removed;
     }
   };
-  exports.MessageBus = me;
-})(window, document, window.jQuery);
+  return me;
+}));
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..64b591b
--- /dev/null
+++ b/package.json
@@ -0,0 +1,20 @@
+{
+  "name": "message-bus-client",
+  "version": "3.3.0",
+  "description": "A message bus client in Javascript",
+  "main": "assets/message-bus.js",
+  "keywords": "es6, modules",
+  "files": ["assets/message-bus.js"],
+  "jsnext:main": "assets/message-bus.js",
+  "module": "assets/message-bus.js",
+  "repository": {
+    "type": "git",
+    "url": "git+https://github.com/discourse/message_bus.git"
+  },
+  "author": "Sam Saffron, Robin Ward",
+  "license": "MIT",
+  "bugs": {
+    "url": "https://github.com/discourse/message_bus/issues"
+  },
+  "homepage": "https://github.com/discourse/message_bus#readme"
+}

GitHub sha: e5ec7a96

1 Like

This commit appears in #226 which was merged by eviltrout.