Skip to content

Instantly share code, notes, and snippets.

@linclark
Last active March 4, 2020 07:10
Show Gist options
  • Save linclark/2457d5e9d3105f759ebc to your computer and use it in GitHub Desktop.
Save linclark/2457d5e9d3105f759ebc to your computer and use it in GitHub Desktop.
// This function is called immediately. The second function is passed in
// as the factory parameter to this function.
(function (factory) {
// If there is a variable named module and it has an exports property,
// then we're working in a Node-like environment. Use require to load
// the jQuery object that the module system is using and pass it in.
if(typeof module === "object" && typeof module.exports === "object") {
factory(require("jquery"), window, document);
}
// Otherwise, we're working in a browser, so just pass in the global
// jQuery object.
else {
factory(jQuery, window, document);
}
}(function($, window, document, undefined) {
// This code will receive whatever jQuery object was passed in from
// the function above and will attach the tipso plugin to it.
}));
@tonix-tuft
Copy link

What about AMD?

Can the gist be rewritten to:

// window.js
define(function() {
  return window;  // global window object
});

// document.js
define(function() {
  return document;  // global document object
});

// tipso.js (jQuery plugin code)
(function (factory) {
  if (typeof define === 'function' && define.amd) {
    // AMD
    define(['jquery', 'window', 'document'], factory);
  }
  // If there is a variable named module and it has an exports property,
  // then we're working in a Node-like environment. Use require to load
  // the jQuery object that the module system is using and pass it in.
  else if (typeof module === "object" && typeof module.exports === "object") {
    factory(require("jquery"), window, document);
  }
  // Otherwise, we're working in a browser, so just pass in the global
  // jQuery object.
  else {
    factory(jQuery, window, document);
  }
}(function($, window, document, undefined) {
  // This code will receive whatever jQuery object was passed in from
  // the function above and will attach the tipso plugin to it.
}));

?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment