[英]Switch from parcel v1 -> CRA, how to serve static web file with reload during development
Backstory: I was originally going to switch to parcel v2, ran into a lot of issues, and really starting to dislike parcel.背景故事:我原本打算切换到parcel v2,遇到了很多问题,并且真的开始不喜欢parcel。 I managed to resolve some issues even though their error messages are very unintuitive.
我设法解决了一些问题,即使它们的错误消息非常不直观。 Got to a point that "parcel build" is working but "parcel serve" just doesn't, and can't find an answer online.
到了“包裹构建”正在运行但“包裹服务”不起作用的地步,并且无法在网上找到答案。 At that point, I decided to switch to Create React App because it's more "industry" standard.
那时,我决定改用 Create React App,因为它更符合“行业”标准。 (Thank you for listening to me rant.)
(谢谢你听我咆哮。)
When I was using parcel v1, my setup for the dev environment is running "parcel index.html" (which has hot module replacement), and I'm serving the static files in my backend.当我使用parcel v1 时,我的开发环境设置正在运行“parcel index.html”(具有热模块替换),并且我在后端提供静态文件。
But I don't know how to do that with create-react-script.但我不知道如何使用 create-react-script 做到这一点。 "react-scripts start" doesn't build to the "build" folder, "react-scripts build" only build once and no reload.
“react-scripts start”不会构建到“build”文件夹,“react-scripts build”只构建一次,不重新加载。 I still want to serve the static file.
我仍然想提供静态文件。 What should I do?
我应该怎么办?
If you're just doing this for development purposes, you could use a proxy from your backend server, pointing to the server run by the Create React App.如果您只是出于开发目的这样做,您可以使用后端服务器的代理,指向由 Create React App 运行的服务器。
For example, with Fastify, you can serve your React app on 1234, and proxy to that from your server, with something like fastify-http-proxy
:例如,使用 Fastify,您可以在 1234 上为您的 React 应用程序提供服务,并从您的服务器代理到该应用程序,例如
fastify-http-proxy
:
const proxy = require('fastify-http-proxy');
// Normal routes here
fastify.register(proxy, { upstream: 'http://localhost:1234' });
fastify.listen(process.env.PORT, '0.0.0.0');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.