ReactJS构建文件的资源路径



我有一个reactjs项目,该项目生成静态文件,用于npm run build之后。这些文件是index.htmlmain.ef19974a.jsmain.f06643f6.css

html文件对每个其他文件都有引用,但作为绝对路径。这意味着,当客户端提供html文件时,它尝试在主机计算机上搜索/static/js/main.ef19974a.js,而不是与所提供的index.html文件相同的目录中的static/js/main.ef19974a.js搜索。

有没有一种方法使React在构建步骤中创建相对路径参考或允许我们提供工作目录?

暂时工作的解决方法是手动修改所得的index.html文件以引用jscss文件,但是当我包含其他资源(例如未从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"
    },
    ... 

最新更新