Last active
June 1, 2021 22:02
-
-
Save ryanburnette/cbb228681c4a288ec07c07fded0169f1 to your computer and use it in GitHub Desktop.
A universal definition pattern for JavaScript libraries. Supports require(), AMD, and browser.
This file contains 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() { | |
function myModule() { | |
} | |
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') { | |
module.exports = myModule; | |
} | |
else { | |
if (typeof define === 'function' && define.amd) { | |
define([], function() { | |
return myModule; | |
}); | |
} | |
else { | |
window.myModule = myModule; | |
} | |
} | |
})(); |
- I definitely think it's safe to drop AMD.
- What's the point of declaring the var in the global scope?
What's the point of declaring the var in the global scope?
So that it won't interfere with module bundlers that do weird things with window
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Let's say assume AMD is dead. Can I do this?
Or do I have to do this?