构建后 index.html 中的相对路径 [英] Relative path in index.html after build

查看:22
本文介绍了构建后 index.html 中的相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个 reactjs 应用程序,我使用以下命令构建了我的项目

npm 构建

这是我的 package.json 文件:

 "脚本": {开始":反应脚本开始","build": "react-scripts build","test": "反应脚本测试 --env=jsdom","eject": "react-scripts 弹出"},

构建后我有包含构建文件和 index.html 文件的文件夹但是这个 .html 中的所有路径都是绝对路径,我想用相对路径构建

例如(index.html):现在我有:

<块引用>

<script type="text/javascript" src="/static/js/main.af2bdfd5.js"></script><link href="/static/css/main.097da2df.css" rel="样式表"><link rel="快捷图标" href="/favicon.ico">

我想要这个:

<块引用>

<script type="text/javascript" src="./static/js/main.af2bdfd5.js"></script><link href="./static/css/main.097da2df.css" rel="stylesheet"><link rel="快捷图标" href="./favicon.ico">

解决方案

//package.json{"name": "您的项目名称",版本":0.1.0","homepage": "./", # <--- 添加这一行----...}

再次运行npm run build.

这会将路径更改为 ./,这是您项目的相对路径.

Hello i have a reactjs app, and I build my project with bellow command

npm build

Here is my package.json file:

  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"},

after build i have folder with build files and index.html file But all paths in this .html are absolute, i want to build with relative path

for example (index.html): now i have:

<script type="text/javascript" src="/static/js/main.af2bdfd5.js"></script>
<link href="/static/css/main.097da2df.css" rel="stylesheet">
<link rel="shortcut icon" href="/favicon.ico">

i want this:

<script type="text/javascript" src="./static/js/main.af2bdfd5.js"></script>
<link href="./static/css/main.097da2df.css" rel="stylesheet">
<link rel="shortcut icon" href="./favicon.ico">

解决方案

// package.json
{
  "name": "your-project-name",
  "version": "0.1.0",
  "homepage": "./", # <--- Add this line ----
  ...
}

Run npm run build again.

This will change the path to ./, which is the relative path of your project.

这篇关于构建后 index.html 中的相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆