简体   繁体   English

如何构造/访问非node.js应用程序的react组件?

[英]How can I structure/access the react components of my non node.js application?

I have a production application whose folder structure is much like you would expect: 我有一个生产应用程序,其文件夹结构非常像您期望的那样:

/project
   /css
   /js
   /php
   /fonts
   /images
   /index.php

I have recently begun learning to use react.js and I would like to start developing new features using react while leaving all existing functionality as is and I understand that react.js can certainly be used this way. 我最近开始学习使用react.js ,我想开始使用react开发新功能,同时保持所有现有功能不变,并且我知道react.js可以这样使用。

What isn't entirely clear, is how to structure the files in my non node.js project and access them. 尚不清楚的是如何在我的非node.js项目中构造文件并访问它们。

For example, I tried searching for this and found this article which proposes the following structure 例如,我尝试搜索此内容,发现这篇文章提出了以下结构

在此处输入图片说明

and states: 并指出:

Here index.jsx works as the entry point of the application. 在这里index.jsx用作应用程序的入口点。 It uses ReactDOM.render to render App and gets the party started. 它使用ReactDOM.render渲染App并开始聚会。 App in turn does something interesting with Note. 反过来,应用程序通过Note做一些有趣的事情。 If I wanted yet another component, you would simply add it below /components. 如果我想要另一个组件,只需将其添加到/ components下面。

Presumably this is a node.js project where index.jsx can call something like require('components/App.jsx'); 大概这是一个node.js项目,其中index.jsx可以调用require('components/App.jsx');类的东西require('components/App.jsx'); , but how do I achieve the same in my non node.js project. ,但是如何在我的非node.js项目中实现相同的目标。

Consider if I set the following folder structure: 考虑是否设置以下文件夹结构:

/project
   /css
   /js
   /php
   /fonts
   /images
   /react
      /components
         /App.jsx   
         /Note.jsx 
      /scripts
         /featureFoo.jsx
   /index.php

Given the above, I'd like to have index.php load react/scripts/featureFoo.jsx which would somehow include/use react/components/App.jsx and react/components/Note.jsx 鉴于以上所述,我想让index.php加载react/scripts/featureFoo.jsx ,它将以某种方式包括/使用react/components/App.jsxreact/components/Note.jsx

I figure I could have script tags in index.php to load all of the components then featureFoo.jsx but I feel like there is a more react way to do this. 我认为我可以在index.php中具有脚本标签来加载所有组件,然后再featureFoo.jsx但我觉得有更多的react方式可以做到这一点。

So 所以

How can I structure/access the react components of my non node.js application? 如何构造/访问非node.js应用程序的react组件?

Im tagging node.js as I feel those users may well bring insight regarding this possibly from having to deal with multiple projects/aproaches. 我对node.js标记,因为我认为那些用户很可能从不得不处理多个项目/方法中带来了有关此方面的见识。

In order to import/require other modules in a non-node.js (ie browser-based) JS application, you'll need to use a bundler such as webpack or browserify . 为了导入/需要在非Node.js的其他模块(基于浏览器IE)JS应用程序,您需要使用捆绑如的WebPackbrowserify

They work by starting at your "entry" file (index.jsx in your case) and recursively following all of your imports/requires. 它们的工作方式是从您的“入口”文件(在您的情况下为index.jsx)开始,然后递归地遵循所有导入/需求。 Then it smartly bundles everything up into a single "output" file, which includes everything your application uses, that you can link in your HTML. 然后,它将所有内容巧妙地捆绑到一个“输出”文件中,该文件包含您的应用程序使用的所有内容,您可以在HTML中进行链接。

You can also use more complex configurations with multiple entry and output files, or even dynamic loading of "chunks" at certain points in your application. 您还可以将更复杂的配置与多个输入和输出文件一起使用,甚至在应用程序中的某些点甚至动态加载“块”。 But the above is the most basic use case, and works well for most simpler projects. 但是以上是最基本的用例,对于大多数较简单的项目都适用。

These bundlers have some other cool features as well. 这些捆绑器还具有其他一些很酷的功能。 For instance, with webpack (with additional plugins and loaders) you can: 例如,使用webpack(带有其他插件和加载程序),您可以:

  1. Write ES6/ES7 JavaScript that gets compiled (via babel ) into cross-browser compatible ES5 编写将ES6 / ES7 JavaScript(通过babel编译)到跨浏览器兼容的ES5中
  2. minify/uglify your output JS 缩小/缩小您的输出JS
  3. import/require non-js files such as CSS, images, and webfonts, and choose how to process these imports (eg pre/post-process your CSS, or optimize your images) 导入/获取非js文件(如CSS,图像和Webfonts),并选择如何处理这些导入(例如,对CSS进行预处理/后处理或优化图像)
  4. Extract all imported CSS into a single .css file 将所有导入的CSS提取到单个.css文件中
  5. Use webpack-dev-server (or webpack-dev-middleware) to take advantage of hot module replacement, which makes developing JS apps easier by letting you see your changes take effect instantly in the browser without having to refresh or lose your app's state. 使用webpack-dev-server(或webpack-dev-middleware)来利用热模块替换,通过让您看到更改在浏览器中立即生效而不必刷新或丢失应用程序的状态,从而使开发JS应用程序变得更加容易。

And a lot more. 还有更多。 The world of bundlers is a pretty big ecosystem with a lot to learn, but it's totally worth getting into. 捆绑器世界是一个非常庞大的生态系统,需要学习很多东西,但是完全值得一试。

A good place to get started is the excellent Survive JS series of tutorials. 入门的好地方是出色的Survive JS系列教程。

However, if you're still learning React, then diving into webpack and the entire ecosystem of JavaScript tooling can be overwhelming. 但是,如果您仍在学习React,那么深入研究webpack和整个JavaScript工具生态系统可能会令人不知所措。 It will probably be easier and a more efficient use of your time to keep things simple at first, and then only get into bundlers etc. after you get comfortable with React. 首先,使事情变得简单,然后仅在对React感到满意之后才进入捆绑器等,这样可能会更轻松,更有效地利用您的时间。

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

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