UMD和CommonJS(CJS)软件包文件夹有何不同,应该使用哪个? [英] How are UMD and CommonJS (CJS) package folders different, and which should I use?

查看:629
本文介绍了UMD和CommonJS(CJS)软件包文件夹有何不同,应该使用哪个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用package.json安装了reactjs和react-dom

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

"dependencies": {
   "bootstrap": "^v4.1.1",
   "popper.js": "^1.14.3",
   "react": "^v16.4.1",
   "react-dom": "^16.4.1"
}

它下载了react文件夹和react-dom

It downloaded react folder and react-dom folder correctly.

两个文件夹都有cjs和umd文件夹,并且它们都有很多js文件。

Both folders have cjs and umd folder and they have a lot of js files.

我无法找到两个文件夹中文件之间的差异。

For me it's unable to find difference between files in two folders.

就像这样:

URL: node_modules/react/umd
  react-development.js
  react-production.min.js



URL : node_modules/react/cjs
   react-development.js
   react-production.min.js

与react-dom几乎相同。它也有cjs和umd文件夹,我不知道应该使用哪个文件夹中的哪个文件来开发React应用程序或网站。

almost same with react-dom. It also has cjs and umd folder and I don't know which file from which folder should I use to develop a react application or website.

推荐答案

JavaScript最初仅用于交互式浏览器。与Node一起,可在非浏览器上下文中使用。由于这个因素和其他因素,模块的格式不兼容:

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:


  • CommonJS 规范描述了 exports 对象的使用,该对象是用于声明和发现什么名称的API从模块导出。不允许在交互式浏览器中加载CommonJS模块。 NodeJS是CommonJS格式的最流行实现。

  • 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.

异步模块定义(AMD)描述了如何在假定将JavaScript模块加载到交互式浏览器中的情况下捆绑JavaScript模块。 RequireJS是比较流行的模块支持库之一,它使用AMD模块。

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.

因为AMD和CommonJS都非常流行并且彼此难以理解。 , 通用模块定义(UMD)是一种尝试制作可被使用的模块的模式

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.

最近, ECMAScript 2015定义了 export import 语法(不同于上述所有内容)以支持模块。

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.

今天,最可能的答案是:使用UMD模块。将来的某个时候可能是:使用ECMAScript模块;但是我们(2019)尚未就如何分配这些内容达成共识。

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.

这篇关于UMD和CommonJS(CJS)软件包文件夹有何不同,应该使用哪个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆