Last active
February 11, 2024 22:29
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!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