简体   繁体   English

找不到React中所需的模块

[英]Can't find modules required in React

I was going through the React codebase, and I noticed how React's require doesn't quite behave like in Nodejs. 我正在浏览React代码库,我注意到React的require并不像Nodejs那样。 I don't get what's going on here. 我不明白这里发生了什么。

Looking at line 19 on ReactClass.js for instance, there's a require('emptyObject') , but emptyObject isn't listed in package.json, nor does it say anywhere where that module's coming from. 例如,查看ReactClass.js上的第19行,有一个require('emptyObject') ,但是package.json中没有列出emptyObject,也没有说该模块的来源。

https://github.com/facebook/react/blob/master/src/isomorphic/classic/class/ReactClass.js#L19 https://github.com/facebook/react/blob/master/src/isomorphic/classic/class/ReactClass.js#L19

I did find "emptyObject" on npmjs , but the API there seems different from the one used in React; 我确实在npmjs上找到了“emptyObject” ,但是那里的API似乎与React中使用的API不同; the .isEmpty grepped in React isn't related to emptyObject. React中的.isEmpty与emptyObject无关。

So where is emptyObject getting loaded from, and how is React's require doing what it's doing? 那么emptyObject从哪里加载,以及React require做什么呢? This is not intuitive. 这不直观。 At all. 完全没有。

The location of the emptyObject module which React refers to is https://github.com/facebook/fbjs/blob/master/packages/fbjs/src/core/emptyObject.js#L9 Note that it doesn't follow the CommonJS module system. React引用的emptyObject模块的位置是https://github.com/facebook/fbjs/blob/master/packages/fbjs/src/core/emptyObject.js#L9请注意,它不遵循CommonJS模块系统。

To make it easier for Facebook to share and consume our own JavaScript. 为了让Facebook更容易分享和使用我们自己的JavaScript。 Primarily this will allow us to ship code without worrying too much about where it lives, keeping with the spirit of @providesModule but working in the broader JavaScript ecosystem. 首先,这将允许我们发布代码而不必过多担心它的存在,遵循@providesModule的精神,但在更广泛的JavaScript生态系统中工作。

From https://github.com/facebook/fbjs#purpose 来自https://github.com/facebook/fbjs#purpose

The way of defining a module by adding @providesModule in the license header and loading those modules with require in Node is called Haste, a customized module system built for Facebook's open source projects. 通过在许可证头中添加@providesModule并在Node中加载带有require模块来定义模块的方式称为Haste,这是为Facebook的开源项目构建的自定义模块系统。

In fact, unless you would like to understand the inner workings of React or contribute to Facebook's open source projects, you don't need to know that. 事实上,除非您想了解React的内部工作原理或为Facebook的开源项目做出贡献,否则您不需要知道这一点。 In other words, it's not recommended to use Haste to write your own project. 换句话说,不建议使用Haste来编写自己的项目。

Along the same lines, the invariant module being loaded at line 10 of ReactClass.js is declared at https://github.com/facebook/fbjs/blob/master/packages/fbjs/src/__forks__/invariant.js#L9 沿着相同的路线,在ReactClass.js第10行加载的invariant模块在https://github.com/facebook/fbjs/blob/master/packages/fbjs/src/__forks__/invariant.js#L9中声明。

As far as I know, both Eclipse and WebStorm don't support Haste so IDE can't help. 据我所知,Eclipse和WebStorm都不支持Haste,因此IDE无法提供帮助。 But with Haste, the name of file and module should be the same, so you can find a module by searching for the filename, ie double shift in Webstorm and Ctrl + Shift + r in Eclipse. 但是使用Haste,文件和模块的名称应该相同,因此您可以通过搜索文件名找到一个模块,即Webstorm中的双移和Eclipse中的Ctrl + Shift + r However, the emptyObject you asked about or invariant are not part of React so it's still cumbersome to find their origin. 但是,您询问或invariantemptyObject不是React的一部分,因此找到它们的来源仍然很麻烦。

Otherwise, there is a team that shares and organizes what they learn from hacking React that I contribute to occasionally and they have linked those require s by following Haste to the corresponding origin file eg https://annot.io/github.com/facebook/react/blob/cc3dc21/src/isomorphic/classic/class/ReactClass.js?l=19 You may want to see that. 否则,有一个团队分享和组织他们从黑客React中学到的东西我偶尔会做出贡献,并且他们通过跟随Haste到相应的原始文件来链接这些require ,例如https://annot.io/github.com/facebook /react/blob/cc3dc21/src/isomorphic/classic/class/ReactClass.js?l=19您可能希望看到这一点。

I noticed how React's require doesn't quite behave like in Nodejs. 我注意到React的require并不像Nodejs那样。

Right. 对。 Facebook has its own module loader. Facebook有自己的模块加载器。 All modules have unique identifiers, provided by the @providesModule directive in each module. 所有模块都具有唯一标识符,由每个模块中的@providesModule指令提供。 This allows you to use the identifier to load the module, instead of the file path. 这允许您使用标识符来加载模块,而不是文件路径。

Of course that doesn't work in a Node.js based environment. 当然,这在基于Node.js的环境中不起作用。 So when React or any other Facebook project is published to npm, all require calls are rewritten automatically to something that Node understands. 因此,当React或任何其他Facebook项目发布到npm时,所有require调用都会自动重写为Node理解的内容。

This functionality is provided by fbjs which contains shared dependencies and build helpers for all Facebook projects. 此功能由fbjs提供,其中包含所有Facebook项目的共享依赖项和构建帮助程序。 This is where you find the emptyObject module . 这是您找到emptyObject模块的位置

If you look at React's gulp file , you can see how the module maps are constructed and that a custom Babel plugin is used to convert all require calls. 如果你看看React的gulp文件 ,你可以看到如何构建模块映射,以及使用自定义Babel插件来转换所有require调用。

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

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