Skip to content

Instantly share code, notes, and snippets.

@hkfoster
Last active May 9, 2016 19:14
Show Gist options
  • Save hkfoster/7c91739372c80ab4617337bd1f4515fc to your computer and use it in GitHub Desktop.
Save hkfoster/7c91739372c80ab4617337bd1f4515fc to your computer and use it in GitHub Desktop.
A simple boilerplate for UMD JS modules
/**
* MyModule 0.0.1
* A short description of your module
* @author Me (@me)
* @license MIT
*/
;( function( root, factory ) {
if ( typeof define === 'function' && define.amd ) {
define( factory );
} else if ( typeof exports === 'object' ) {
module.exports = factory;
} else {
root.myModule = factory( root );
}
})( this, function( root ) {
'use strict';
// Public API function
var exports = function( element, settings ) {
// Parameter variables
var
selector = document.querySelector( element ),
defaults = {
numberVariable: 123,
stringVariable: 'abcd'
};
// Only run if selector exists
if ( !selector ) return false;
// Scoped variables
var
options = compose.extend( defaults, settings ),
optVar = options.option;
// Plugin logic...
};
// Public API
return exports;
});
// Instantiate
myModule( 'element', {
numberVariable: 456,
stringVariable: 'efg'
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment