有没有办法让“react-scripts”在构建的根目录中包含额外的文件?

Posted

技术标签:

【中文标题】有没有办法让“react-scripts”在构建的根目录中包含额外的文件?【英文标题】:Is there a way to have `react-scripts` include extra files in the root of the build? 【发布时间】: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 脚本?

【问题讨论】:

【参考方案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"

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

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

【讨论】:

以上是关于有没有办法让“react-scripts”在构建的根目录中包含额外的文件?的主要内容,如果未能解决你的问题,请参考以下文章

react-scripts 构建“第 1 行解析错误”

构建react项目,The react-scripts package provided by Create React App requires a dependency

react-scripts build 不会转换模板文字,应用程序在 IE 中不起作用

有没有办法让 fxcopcmd 总是返回 0?

带有craco的故事书-叫一个不同版本的react-scripts

'react-scripts' is not recognized as an internal or external command