[英]Adding html and js code under React src folder
I have folder(say SDK
) inside my React public folder containing a bunch of html and js/jQuery files.我的 React 公共文件夹中有一个文件夹(比如
SDK
),其中包含一堆 html 和 js/jQuery 文件。
I'm accessing these files inside my react code as a iframe with src http://example.com/SDK/homepage.html .我在我的反应代码中访问这些文件作为 iframe 与 src http://example.com/SDK/homepage.html 。
How can I move this SDK folder from public/SDK to src/SDK in the React project and then access homepage.html in the iframe in my React code?如何在 React 项目中将此 SDK 文件夹从 public/SDK 移动到 src/SDK,然后在我的 React 代码中访问 iframe 中的 homepage.html?
I've bootstrapped the app with Create React App and using yarn build
to build it.我已经使用 Create React App 引导应用程序并使用
yarn build
来构建它。
Well, the official way is exactly like you are doing it: put it into the public folder (see https://create-react-app.dev/docs/using-the-public-folder/ ).好吧,官方方式与您所做的完全一样:将其放入公共文件夹(请参阅https://create-react-app.dev/docs/using-the-public-folder/ )。
If you, for what ever reason, still want to put it in src
, then you would need your build tool to copy the files during build time.如果您出于任何原因仍想将其放在
src
,那么您将需要您的构建工具在构建期间复制文件。
If you use Webpack then you could use the copy plug in https://webpack.js.org/plugins/copy-webpack-plugin如果您使用Webpack,那么您可以使用https://webpack.js.org/plugins/copy-webpack-plugin 中的复制插件
When you are using create-react-app and you do not want to eject (so you could use the webpack solution), then you could also write a small script within your package.yml
to copy the files into the build
folder.当您使用create-react-app并且不想弹出时(因此您可以使用 webpack 解决方案),那么您还可以在
package.yml
编写一个小脚本来将文件复制到build
文件夹中。 This - however - is not so great to maintain and will not scale.然而,这不是很好维护并且不会扩展。
{
"name": "some-project",
...
"scripts": {
"start": "react-scripts start",
"build": "cp './src/SDK' './public/SDK' && react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
...
}
This is the relevant part: "build": "cp './src/SDK' './public/SDK' && react-scripts build",
这是相关部分:
"build": "cp './src/SDK' './public/SDK' && react-scripts build",
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.