简体   繁体   English

在 React src 文件夹下添加 html 和 js 代码

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

相关问题 我们可以在本地部署没有源代码(src文件夹)的react js应用程序吗 - can we deploy an react js application without source(src folder) code locally 从html文件中src文件夹添加js文件的替代方法 - Alternate way to add js files from src folder in html file (React Webpack)在开发模式下运行项目时,不会加载src文件夹下的文件 - (React webpack) Files under the src folder are not loaded when run project on development mode React Js不会在../上进行src - React Js not going to src on ../ 在 react js 中获取本地 json 数据(在公共文件夹或 src 文件夹之外) - fetch local json data in react js (outside of public folder or src folder) 无法将公共文件夹中的 index.html 与 src 文件夹中的 index.js 连接起来 - Unable to connect index.html in public folder with index.js in src folder React - 在 app src 文件夹中找不到 serviceWorker.js 文件 - React - Can't find serviceWorker.js file in app src folder reactjs create-react-app在src文件夹中缺少index.html - reactjs create-react-app missing index.html in src folder 如何从 react.js 的 src 文件夹中的文件中导入常量 function? - How to import a constant function from a file that is in the src folder in react.js? React Js 可以编辑/写入存储在其 src 或公共文件夹中的文件吗 - Can React Js edit/wite to a file that is stored in its src or public folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM