How are UMD and CommonJS (CJS) package folders different, and which should I use?

后端 未结 1 956
误落风尘
误落风尘 2021-02-03 22:48

I installed reactjs and react-dom like this with package.json

\"dependencies\": {
   \"bootstrap\": \"^v4.1.1\",
   \"popper.js\": \"^1.14.3\",
   \"react\": \"^         


        
1条回答
  •  醉话见心
    2021-02-03 23:42

    JavaScript was originally for interactive browsers only. With Node, it is used in non-browser contexts. Because of this and other factors, there are incompatible formats for modules:

    • The “CommonJS” specification describes the use of an exports object which is the API to declare and discover what names are exported from a module. No allowance is made for loading a CommonJS module in an interactive browser. NodeJS is the most popular implementation of the CommonJS format.

    • The “Asynchronous Module Definition” (AMD) describes how to bundle JavaScript modules on the assumption they will be loaded in an interactive browser. RequireJS is one of the more popular module-support libraries, and it consumes AMD modules.

    • Because AMD and CommonJS are both very popular and mutually unintelligible to each other, the “Universal Module Definition” (UMD) is a pattern that attempts to make a module that can be consumed by both, at the cost of a more complicated format.

    • More recently, ECMAScript 2015 defines export and import syntax (different from all the above) to support modules.

    Which should you use? You'll need to answer that based on what in your build system will be consuming those modules.

    Today, the most likely answer is: use the UMD module. Some time in the future it may be: use ECMAScript modules; but we don't yet (2019) have consensus on how those will be distributed.

    0 讨论(0)
提交回复
热议问题