Last Updated: February 25, 2016
·
667
· snichme

Make jQuery plugins ready for AMD

jQuery with all its plugins its great, but in my last project I found that a lot of the plugins doesn't support AMD which was what we used in that project.

This forced me to change all the scripts and this makes it harder for updating the code later on.

So my suggestion for writing a jQuery plugin is to do this:

(function (factory) {
  if (typeof define === 'function' && define.amd) {
    define(['jquery'], factory);
  } else {
    factory(jQuery);
  }
}(function ($) {
  $.fn.jqueryPlugin = function () {};
}));

This way the plugin works both the "normal" way and for AMD systems.