AMD specs and how to turn AMD into COMMONJS

Source: Internet
Author: User

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: {}};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.