Module supporting CommonJS, AMD, and Global/Window

PHOTO EMBED

Thu Oct 19 2023 06:05:24 GMT+0000 (Coordinated Universal Time)

Saved by @daemondevin #javascript #amd #commonjs #module

// Define your class
class MyModule {
  constructor($) {
    this.$ = $;
  }
  
  // Add methods and properties here
}

// Environment detection and initialization
(function() {
  if (typeof require === 'function' && typeof exports === 'object' && typeof module === 'object') {
    // CommonJS
    module.exports = MyModule;
  } else if (typeof define === 'function' && define.amd) {
    // AMD
    define(['jquery'], function($) {
      return new MyModule($);
    });
  } else {
    // Global
    if (typeof window.jQuery !== 'undefined') {
      window.MyModule = MyModule;
    }
  }
})();
content_copyCOPY

#### Description This defines a class module called `MyModule` that is exported and invoked immediately using an IIFE to initialize support depending on which environment the module is being run under. __NOTE:__ It also includes support for jQuery