我有一个reactjs项目,该项目生成静态文件,用于npm run build
之后。这些文件是index.html
,main.ef19974a.js
和main.f06643f6.css
。
html
文件对每个其他文件都有引用,但作为绝对路径。这意味着,当客户端提供html
文件时,它尝试在主机计算机上搜索/static/js/main.ef19974a.js
,而不是与所提供的index.html
文件相同的目录中的static/js/main.ef19974a.js
搜索。
有没有一种方法使React在构建步骤中创建相对路径参考或允许我们提供工作目录?
暂时工作的解决方法是手动修改所得的index.html
文件以引用js
和css
文件,但是当我包含其他资源(例如未从index.html
引用的图像)时失败,并默认回到这些文件的绝对路径中。/p>
您可以使用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"
},
...