Original: http://villadora.me/2014/05/23/amd-define-and-how-to-translate-amd-to-commonjs/
There has been a lot of controversy between Commonjs and AMD, and both have evolved and converged on the project. Personally, Commonjs is more developer-oriented, and for developers, it requires a clear version and management, less code and more interference, and less configuration. AMD allows anonymous modules in code, unclear relationships between module names and variables, non-proximity dependencies, and redundant dependency definitions that are not developer-friendly.
REQUIRE2COMMONJS provides command line and node module to convert AMD used in Requirejs to COMMONJS format for use by other external systems, such as CMD or node, cortex.
The current official AMD offers several ways to define a module:
1) Dependency-free module, Simple Object
12 functionreturn a + b;} 3 });
Without any reliance, the exports of the module is defined directly. In this case, to turn AMD into a COMMONJS module, you only need to change to
1 module.exports = {2 functionreturn a + b;} 3 };
Syntax tree conversion is very simple.
2) Simplified CommonJS wrapping
1 define (function (require, exports, module) {2 var a = require (' a ' ),3 b = require (' B '); 4 function () {}; 5 });
AMD now offers COMMONJS wrapping this format, much simpler. You just need to extract the function body from the factory function.
1 var a = require (' A '),2 b = require (' B '); 3 4 function () {};
3) normalized
This is the AMD format that we usually see
1 function (bakcbone, util) {2 // Other process 3 return {4 data: {}5 }; 6 });
For this format, the processing has two steps 1) to change the dependency into the form of require, noting that the parameters of the dependency declaration and factory are not necessarily consistent; 2) convert return to Module.exports
1 var Backbone = require (' Backbone '); 2 var util = require ('./util '); 3 require (' Buffer '); 4 5 module.exports = {data: {}};