简体   繁体   English

如何将npmjs.com存储库中的React相关节点包转换为ES5?

[英]How to convert React related node packages in npmjs.com repository to ES5?

As a newbie I am trying to understand what the logic is under the hoods for react packages in npmjs.com repository. 作为一个新手,我试图了解npmjs.com存储库中的反应包的逻辑是什么。

I find it a little bit strange since some modules that I install works flawlessly with my application (such as react-motion ), where some reject to work by giving Uncaught SyntaxError: Unexpected token import error (such as react-sortable-pane ). 我发现它有点奇怪,因为我安装的某些模块与我的应用程序完美配合(例如react-motion ),其中一些通过给出Uncaught SyntaxError: Unexpected token import拒绝工作Uncaught SyntaxError: Unexpected token import错误(例如react-sortable-pane )。

What I understood up to now is it has something to do with ES5. 我到目前为止所理解的是它与ES5有关。 The modules that are implemented with ES6 or ES7 must be converted to ES5. 使用ES6或ES7实现的模块必须转换为ES5。

My question is, how can I understand if a package is not ES5 compatible and what can I do to convert it to ES5 during or after I used yarn add command to install the package to my node_modules directory? 我的问题是,我如何理解一个软件包是否与ES5兼容,在使用yarn add命令将软件包安装到我的node_modules目录期间或之后,如何将其转换为ES5?

TL:DR Transpile your code or use: TL:DR透明你的代码或使用:

import { SortablePane, Pane } from 'react-sortable-pane/lib/react-sortable-pane.es5' ; import { SortablePane, Pane } from 'react-sortable-pane/lib/react-sortable-pane.es5' ;

First things first. 首先要做的事情。

Two main folders Src and Lib 两个主要文件夹Src和Lib

A common convention for javascript projects is to put all of your development code into a folder called src . javascript项目的一个常见约定是将所有开发代码放入名为src的文件夹中。 This folder may contain code such as ES5 or ES6 depending on what the developer wants to work with. 此文件夹可能包含ES5或ES6等代码,具体取决于开发人员要使用的内容。

The other main folder is usually called lib which contains all code from src that is transpiled (with babel for example from ES6 to ES5), converted and usually bundled (webpack minify for example) so that it can work in the browsers that that npm package supports (varies from package to package obviously). 另一个主文件夹通常称为lib ,其中包含来自src所有代码,这些代码被转换(例如从bab6到ES5),转换并通常捆绑(例如webpack minify),以便它可以在npm包中的浏览器中工作支持(明显不同于包装)。 This folder only contains code that is relevant to the user using the package, ie all tests are not converted and bundled because their is no reason to. 此文件夹仅包含与使用该程序包的用户相关的代码,即所有测试都不会转换和捆绑,因为它们没有理由。

Entry Point 入口点

The other important part for npm packages is the entry point for the npm package. npm包的另一个重要部分是npm包的入口点。 By default NodeJS will look for an index.js file in the imported package (I think). 默认情况下,NodeJS将在导入的包中查找index.js文件(我认为)。 This can be overwritten by supplying the main key in package.json. 这可以通过在package.json中提供main键来覆盖。

Example in react-motion's package.json: react-motion的package.json中的示例:

"main": "lib/react-motion.js"

We can see that this points to lib . 我们可以看到这指向了lib But where is the lib folder on their Github??? 但是他们的Github上的lib文件夹在哪里? It's not their because usually you don't want to check in a lib folder to source control because it's just transpiled for the npm package. 这不是他们的,因为通常你不想在lib文件夹中检查源代码控制,因为它只是针对npm包进行了转换。

We can confirm this by installing react-motion and looking in node_modules/react-motion . 我们可以通过安装react-motion并查看node_modules/react-motion来确认这一点。 The lib folder here exists with transpiled code that is ready to be used in any browser without babel. 这里的lib文件夹存在转换后的代码,可以在任何没有babel的浏览器中使用。

But why don't all npm packages do this for me!?!??! 但为什么不是所有的npm包都为我做这个!?!??!

Most do and they should do really. 大多数人都应该这样做。 I do in my packages. 我在我的包里做。

The react-sortable-plane npm package is using this instead "jsnext:main": "./lib/react-sortable-pane.js" which basically means it uses ES5 syntax everywhere but with import/export and I haven't seen before because it isn't widely used. react-sortable-plane npm包正在使用它来代替"jsnext:main": "./lib/react-sortable-pane.js"这基本上意味着它使用ES5语法到处但是有导入/导出而我还没有看到之前因为它没有被广泛使用。

See https://github.com/rollup/rollup/wiki/pkg.module#wait-it-just-means-import-and-export--not-other-future-javascript-features 请参阅https://github.com/rollup/rollup/wiki/pkg.module#wait-it-just-means-import-and-export--not-other-future-javascript-features

As to why they just use import/export with ES5 features I presume it's because import/export has become standard now but I am not sure. 至于他们为什么只使用带有ES5功能的导入/导出,我认为这是因为导入/导出现在已成为标准,但我不确定。

You will still have to transpile this package if you want older browser support or just import the .es5.js file, eg: 如果您需要较旧的浏览器支持或只是导入.es5.js文件,您仍然需要转换此包,例如:

import { SortablePane, Pane } from 'react-sortable-pane/lib/react-sortable-pane.es5';

Hope this helps. 希望这可以帮助。 I know it's confusing with so many damn environments like UMD, Common, Node etc... 我知道它与如此多的该死的环境如UMD,Common,Node等令人困惑......

The solution is to account for any version of Javascript and use a transpiler to make sure that various JS versions which might be in your code and imported modules will be covered by your transpiler's configuration. 解决方案是考虑任何版本的Javascript并使用转换器来确保您的代码和导入模块中的各种JS版本将被您的转换器配置覆盖。 Going through your modules and trying to figure out which version of Javascript they use isn't a practical exercise. 浏览你的模块并试图找出他们使用的Javascript版本不是一个实际的练习。 Most projects have a bunch of dependencies, and all those packages have their own dependencies. 大多数项目都有一堆依赖项,所有这些包都有自己的依赖项。 So you'll end up going down a rabbit hole. 所以你最终会走下一个兔子洞。

Babel is probably the most well known transpiler. Babel可能是最着名的转换器。 With the right configuration you can have ES5, 6 or 7 code and it will transpile it all into the same JS type so it can run in all standard browser versions. 使用正确的配置,您可以使用ES5,6或7代码,并将其全部转换为相同的JS类型,以便它可以在所有标准浏览器版本中运行。

Basically the answer isn't to try and deduce what ES type your modules are, it's to create a build process that can handle the different types. 基本上答案不是尝试和推断你的模块是什么ES类型,而是创建一个可以处理不同类型的构建过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM