Skip to content

Instantly share code, notes, and snippets.

@anatol-grabowski
Last active June 30, 2017 07:56
Show Gist options
  • Save anatol-grabowski/a4a3d564d5d6862eb98a10b4a3062dc7 to your computer and use it in GitHub Desktop.
Save anatol-grabowski/a4a3d564d5d6862eb98a10b4a3062dc7 to your computer and use it in GitHub Desktop.
// As per http://wiki.commonjs.org/wiki/Modules/1.1
// 1. In a module, there is a free variable "require", that is a Function.
// 2. In a module, there is a free variable called "exports", that is an object.
// 3. In a module, there must be a free variable "module", that is an Object.
//x3.1 The "module" object must have a read-only, don't delete "id" property.
// No check for 3.1 since nodejs's module.id is both writable and deletable.
//x2.1 modules must use the "exports" object as the only means of exporting.
// Export through "module.exports" is much simpler though.
myModule = {}
myModule.a = 123
;(function (_exports, _name) {
let isCommonJs = (() => {
if ((typeof require !== 'function') || (require === this.require)) return false
if ((typeof exports !== 'object') || (exports === this.exports)) return false
if ((typeof module !== 'object') || (module === this.module)) return false
return true
})()
if (isCommonJs) { module.exports = _exports }
else { this[_name] = _exports }
})(myModule, 'myModule')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment