Skip to content

Instantly share code, notes, and snippets.

@ethyde
Created December 12, 2016 15:13
Show Gist options
  • Save ethyde/a36af6cdc06a8d99642e1ffa2fad29de to your computer and use it in GitHub Desktop.
Save ethyde/a36af6cdc06a8d99642e1ffa2fad29de to your computer and use it in GitHub Desktop.

Revisions

  1. ethyde created this gist Dec 12, 2016.
    26 changes: 26 additions & 0 deletions universal_amd_cjs_plainjs.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    /*
    * From http://ifandelse.com/its-not-hard-making-your-library-support-amd-and-commonjs/
    * http://ricostacruz.com/cheatsheets/umdjs.html
    */

    (function (root, factory) {
    if(typeof define === "function" && define.amd) {
    // Now we're wrapping the factory and assigning the return
    // value to the root (window) and returning it as well to
    // the AMD loader.
    define(["postal"], function(postal){
    return (root.myModule = factory(postal));
    });
    } else if(typeof module === "object" && module.exports) {
    // I've not encountered a need for this yet, since I haven't
    // run into a scenario where plain modules depend on CommonJS
    // *and* I happen to be loading in a CJS browser environment
    // but I'm including it for the sake of being thorough
    module.exports = (root.myModule = factory(require("postal")));
    } else {
    root.myModule = factory(root.postal);
    }
    }(this, function(postal) {
    // module code here....
    return myModule;
    }));