Skip to content

Instantly share code, notes, and snippets.

@CrocoDillon
Last active February 11, 2024 22:42
Show Gist options
  • Save CrocoDillon/9990078 to your computer and use it in GitHub Desktop.
Save CrocoDillon/9990078 to your computer and use it in GitHub Desktop.
Export your awesome module using AMD, CommonJS, Node.js or just as global.
/*
* Inspiration (well… copy pasting more or less) from:
* https://github.com/ScottHamper/Cookies/blob/0.3.1/src/cookies.js#L127-L140
*
* Thanks Scott!
*/
(function (global) {
'use strict';
var MyModule = function () {
/* Your awesome module logic here… */
};
/* …and here */
MyModule.foo = 'bar';
// AMD support
if (typeof define === 'function' && define.amd) {
define(function () { return MyModule; });
// CommonJS and Node.js module support.
} else if (typeof exports !== 'undefined') {
// Support Node.js specific `module.exports` (which can be a function)
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = MyModule;
}
// But always support CommonJS module 1.1.1 spec (`exports` cannot be a function)
exports.MyModule = MyModule;
} else {
global.MyModule = MyModule;
}
})(this);
/*
* Inspiration from:
* https://github.com/jquery/jquery/blob/master/src/intro.js
*
* Thanks jQuery!
*
* This is another way to handle dependencies (in this case, a window with a document)
*/
(function( global, factory ) {
if ( typeof module === "object" && typeof module.exports === "object" ) {
// For CommonJS and CommonJS-like environments where a proper window is present,
// execute the factory and get jQuery
// For environments that do not inherently posses a window with a document
// (such as Node.js), expose a jQuery-making factory as module.exports
module.exports = global.document ?
factory( global, true ) :
function( w ) {
if ( !w.document ) {
throw new Error( "jQuery requires a window with a document" );
}
return factory( w );
};
} else {
factory( global );
}
// Pass this if window is not defined yet
}(typeof window !== "undefined" ? window : this, function( window, noGlobal ) {
// Awesome module gets created here
}));
/*
* Inspiration from:
* https://github.com/addyosmani/memoize.js/blob/master/memoize.js
*
* Thanks Addy!
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.memoize = factory();
}
}(this, function() {
"use strict";
var memoize = function(func) {
/* Your awesome module logic here */
};
return memoize;
}));
@YanLIU0822
Copy link

Hey I am going through the jquery 3.2.1, find the first part of this, but coz i am not familiar with module, i cant figure out this part is for. would you mind give me a little explanation? thank you

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