Skip to content

Instantly share code, notes, and snippets.

@foo123
Last active February 11, 2024 22:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foo123/8b0c069445bee29b0e93 to your computer and use it in GitHub Desktop.
Save foo123/8b0c069445bee29b0e93 to your computer and use it in GitHub Desktop.
UMD JavaScript Single Module pattern (no other dependencies) for Node/CommonJS, AMD/RequireJS, Web Workers and Browser
!function( root, name, factory ) {
"use strict";
//
// export the module, umd-style (no other dependencies)
var isCommonJS = ("object" === typeof(module)) && module.exports,
isAMD = ("function" === typeof(define)) && define.amd, m;
// CommonJS, node, etc..
if ( isCommonJS )
module.exports = (module.$deps = module.$deps || {})[ name ] = module.$deps[ name ] || (factory.call( root, {NODE:module} ) || 1);
// AMD, requireJS, etc..
else if ( isAMD && ("function" === typeof(require)) && ("function" === typeof(require.specified)) && require.specified(name) )
define( name, ['require', 'exports', 'module'], function( require, exports, module ){ return factory.call( root, {AMD:module} ); } );
// browser, web worker, etc.. + AMD, other loaders
else if ( !(name in root) )
(root[ name ] = (m=factory.call( root, {} ) || 1)) && isAMD && define( name, [], function( ){ return m; } );
}( /* current root */ this,
/* module name */ "MODULE_NAME",
/* module factory */ function( exports ) {
/* main code starts here */
exports["MODULE_NAME"] = {
// ...
};
/* main code ends here */
/* export the module */
return exports["MODULE_NAME"];
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment