ReactJS构build文件的资源path

我有一个ReactJS项目,生成npm run build后的服务的静态文件。 这些文件是一个index.htmlmain.ef19974a.jsmain.f06643f6.css

html文件具有对这些其他文件的引用,但是作为绝对path。 这意味着当客户端服务于html文件时,它会尝试在主机上search/static/js/main.ef19974a.js ,而不是从index.html目录searchstatic/js/main.ef19974a.js index.html文件正在上传。

有没有办法使React在构build步骤中创build相对path引用,还是允许我们提供工作目录?

临时工作的解决方法是手动修改生成的index.html文件以引用jscss文件,但是当我包含其他资源( index.htmlindex.html引用的图像并且默认返回到这些文件的绝对path)时会失败。

您可以使用homepage键在package.json设置包含index.html的文件夹:

  ... "devDependencies": { "react-scripts": "1.0.10" }, "homepage": "/myapp", // website will be hosted at http://hostname/myapp "scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test --env=jsdom", "toolbox": "react-toolbox-themr", "eject": "react-scripts eject" }, ...