creat-react-app 多环境打包配置

662 阅读1分钟

1、安装dotenv-cli工具

 yarn global add dotenv-cli

2、修改package.json文件配置

"scripts": {
    "start": "react-app-rewired start",
    "build:dev": "dotenv -e .env.development react-app-rewired build",
    "build:pro": "dotenv -e .env.production react-app-rewired build",
    "test": "react-app-rewired test",
    "eject": "react-scripts eject"
  },

3、添加全局变量

.env.development
    REACT_APP_ROOT='//******.*****.com'    
.env.production
    REACT_APP_ROOT='//******.*****.com'   

4、运行

<!--测试-->
yarn build:dev
<!--生产-->
yarn build:pro