简体   繁体   English

模块加载在没有Node的Apache环境中响应组件

[英]Module loading react components in Apache environment without Node

I am currently working on adding a React based section to a website. 我目前正在为网站添加基于React的部分。 The data needed to update in real-time without complicating the code with the likes of jQuery. 数据需要实时更新,而不需要使用jQuery等代码复杂化。 I landed on using React with a pub/sub system to retrieve updates using Node and Socket.io, however, Node is only being used for updating data. 我使用React与pub / sub系统一起使用Node和Socket.io来检索更新,但是,Node仅用于更新数据。 The React pages are not be accessed by Node, but rather by Apache. Node不会访问React页面,而是Apache访问。 I am including React, ReactDOM, and a standalone version of Babel in the development version. 我在开发版本中包含了React,ReactDOM和独立版本的Babel。

So, my question is how can I go about separating out my React components into .js files and requiring them like in Node? 所以,我的问题是如何将我的React组件分离成.js文件并在Node中要求它们?

I looked into RequireJS and Browserify, but they didn't quite seem to get the job done. 我查看了RequireJS和Browserify,但他们似乎并没有完成任务。 I need a way that I can edit and include them separate in the dev environment, but they need to be concated and minified on live automagically. 我需要一种可以编辑的方法,并在开发环境中将它们分开包含,但是它们需要在实时自动化上进行简化和缩小。 Our build process already uses Jenkins, so if a solution was available that could hook into Jenkins, that would be optimal. 我们的构建过程已经使用了Jenkins,所以如果有一个可以挂钩Jenkins的解决方案,那将是最佳的。

I would recommend bundling your JavaScript code using webpack , a Node.js based build tool that lets you concatenate and minify your code easily. 我建议使用webpack捆绑您的JavaScript代码,这是一个基于Node.js的构建工具,可以让您轻松地连接和缩小代码。 You create a webpack.config.js file in your projects root and run it on your Jenkins build agent using node webpack . 您在项目根目录中创建webpack.config.js文件,并使用node webpack在Jenkins构建代理程序上运行它。

As Patrick Hund already recommended, you should use a module bundler. 正如Patrick Hund已经建议的那样,您应该使用模块捆绑器。 There are several options, but I use - like Patrick Hund - webpack . 有几个选项,但我使用 - 像Patrick Hund - webpack

What does webpack actually do? webpack实际上做了什么?

It loads all the files that you specify - which are mostly files required in your project - and bundles them into one file. 它会加载您指定的所有文件(主要是项目中所需的文件),并将它们捆绑到一个文件中。

How is it to be used? 它是如何使用的?

You need a config file where you specify mainly three things: 你需要一个配置文件,你主要指定三件事:

  • The entry file (the root of your hierarchy) 条目文件(层次结构的根)
  • The output files (the bundled file) 输出文件(捆绑文件)
  • The loaders of the files required in your system to work. 系统中所需文件的加载程序。

You can also specify plugins, to enable for example Hot Module Replacement, to start a dashboard of the bundling process, etc.. But that is not mandatory and to start maybe you don't need this. 您还可以指定插件,以启用例如热模块替换,启动捆绑过程的仪表板等。但这不是强制性的,要启动,您可能不需要这样。

Too much? 太多了?

Now you may think that this is too much work for development if you have to constantly rebundle all your files on every change. 现在,如果您不得不在每次更改时不断重新绑定所有文件,那么您可能会认为这对开发来说太多了。 But it comes with some prebuilt scripts that include a watch option which does this for you and more comfortable the Hot Module Replacement which updates your browser on every change without you even have to refresh it. 但是它附带了一些预先构建的脚本,其中包括一个为您执行此操作的watch选项,并且更热衷于热模块替换,它可以在每次更改时更新您的浏览器,而您甚至无需刷新它。

For production environments you can create another config file in which you leave just what you want (eg Hot Module Replacement is not recommended for production) and that's it. 对于生产环境,您可以创建另一个配置文件,您可以在其中保留您想要的内容(例如,不建议将Hot Module Replacement用于生产),就是这样。

So it all falls down to these config files. 所以这一切都归结为这些配置文件。 You can have a look at this one , which was the one I used to understand as it was minified to what I just told you. 你可以看看这个 ,我曾经理解的那个,因为它被缩小到我刚才告诉你的那个。

To integrate it to your project, you need to match the file of your index.html in which you load your js scripts and your output files and for development purposes you may want to use a dev server, which it can be the webpack-dev-server . 要将它集成到您​​的项目中,您需要匹配index.html中加载js脚本和输出文件的文件,并且出于开发目的,您可能需要使用开发服务器,它可以是webpack-dev - 服务员

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

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