简体   繁体   English

从parcel v1 -> CRA 切换,如何在开发过程中重新加载静态网页文件

[英]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.

相关问题 如果在 CRA 中配置了 service worker,则在 PWA 中提供 static html 文件 - Serve static html files in PWA if service worker is configured in CRA 尝试从webpack v1语法切换到webpack v3,并遇到了奇怪的编译错误 - Attempted to switch from webpack v1 syntax to webpack v3 and encountered a weird compilation error React router v4 服务静态文件 (robot.txt) - React router v4 serve static file (robot.txt) 如何使用服务 static 文件与快递和反应? - how to use serve static file with express and react? React 或 CRA 是否优化 static 语句? 如何防止 static 语句得到优化? - Does React or CRA optimize static statement? How to prevent static statement from getting optimized? 如何在CRA v2中将代理值从package.json移动到setupProxy.js? - How to move a proxy value from package.json to setupProxy.js in CRA v2? 如何在expressJS服务器中从客户端提供index.html静态文件? - How to serve index.html static file from client-side within expressJS server? 如何在webpack dev服务器中提供json文件等静态资源? - How to serve static assets like json file in webpack dev server? 如何在 CRA 构建期间将 scss 编译包含到 css - How to include scss compilation to css during CRA build 如何在CRA公用文件夹中编译JS文件? - How to compile a JS file in CRA public folder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM