react build 后,打包后自动将index.html copy 404.html

起因:build上传gitee,启用路由需要404.html自动跳转

当前环境 create-react-app 搭建的架子

解决方案

由于默认的时候把build.js打包,无法查看,只好eject
有不需要eject的方法,但是为了更好的研究学习,就得eject

  1. npm run eject
  2. 找到 script/build.js
  3. 找到编译index.html的命令,然后在生成index.html之后进行404.html文件的copy
  4. 添加copy命令 fs.copyFileSync('build/index.html', 'build/404.html');

思路分析

  1. 先找 build.js
  2. 找copy函数 copyPublicFolder
  3. 找生成index.html函数 return build(previousFileSizes);
  4. 找生成index.html后的then()
  5. 找到插入位置前 console.log(chalk.green('Compiled successfully. '));
原文地址:https://www.cnblogs.com/pengchenggang/p/13502665.html