【问题标题】:Is there a way to have `react-scripts` include extra files in the root of the build?有没有办法让“react-scripts”在构建的根目录中包含额外的文件?
【发布时间】:2017-01-31 00:43:04
【问题描述】:

我的情况:

我在 Surge 上托管一个使用 create-react-app 创建的应用。为了让 Surge 让react-router 处理路由,我必须将 200.html 添加到根目录,并且为了处理 404,我必须将 404.html 添加到根目录。问题是,对于npm run build,只有index.htmlfavicon.ico 被添加到构建目录的根目录中。

可能的解决方案:

我可以通过在 package.json react-scripts build && cp 200.html 404.html build 中创建 build 脚本来复制其他文件。

问题:

有没有更好的方法让react-scripts 将额外文件添加到构建的根目录,而不是依赖于 package.json 中的 bash 脚本?

【问题讨论】:

    标签: reactjs react-router create-react-app react-scripts surge.sh


    【解决方案1】:

    我决定的答案是在我的 package.json 中使用我最初怀疑的 bash 脚本,但我决定在部署脚本而不是构建脚本中执行文件操作,并将 index.html 重命名为 200.html而不是复制额外的文件,as suggested by the create-react-app docs;

    "build": "react-scripts build",
    "deploy": "npm run build && mv build/index.html build/200.html && surge build mydomain.com"
    

    我认为在部署脚本而不是构建脚本中进行文件操作是正确的解决方案,因为这种更改是部署环境所独有的,这意味着,如果我部署在浪涌以外的地方,我可能不想重命名索引文件。

    如果有人有更好的建议,仍然欢迎其他解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-16
      • 1970-01-01
      • 2021-06-15
      • 2016-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多