Skip to content

Instantly share code, notes, and snippets.

@totty90
Last active August 29, 2015 14:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save totty90/106f9577482154f9ab4e to your computer and use it in GitHub Desktop.
Save totty90/106f9577482154f9ab4e to your computer and use it in GitHub Desktop.
Requirejs and Commonjs crossmodule standard boilerplate
(typeof define==="function"?define:function(f){var r=f(require,exports,module);module.exports=(r!==void 0?r:module.exports);})(function(require,exports,module){
return 27;
})
(typeof define==="function"?define:function(f){var r=f(require,exports,module);module.exports=(r!==void 0?r:module.exports);})(function(require,exports,module){
module.exports = 27;
})
console.log(require('moduleA')); // 27
define(['moduleA'], function(moduleA){
console.log(moduleA); // 27
})
define(function(require, exports, module){
console.log(require('moduleA')); // 27
})
@totty90
Copy link
Author

totty90 commented Jul 11, 2014

Module we are making:

  • moduleA.js: returning the module;
  • moduleB.js: setting the module.exports variable;

Both way works the same. But remember to don't do both: you should not return a module and also set the module.exports variable;

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