【问题标题】:How to make a production build of React app with a custom mode?如何使用自定义模式制作 React 应用程序的生产版本?
【发布时间】:2020-02-26 05:11:42
【问题描述】:

使用带有"react-scripts": "3.3.1" 的 CRA 构建了一个应用程序,但是我在构建模式方面遇到了问题,例如在 Vue-CLI 中我可以这样做:

npm run build -- --mode staging
// any ENV name i want

但在 CRA 中,有:

"scripts": {
  "start": "react-scripts start",
  "build": "react-scripts build",
  "test": "react-scripts test",
  "eject": "react-scripts eject"
},

我尝试过类似的方法:

yarn build --mode staging

但它使用.env.production 变量。我有 .env.staging 用于“暂存”环境。我该怎么做?

我在一些生产服务器上使用不同的 API 端点,这就是我需要这种模式的原因。

【问题讨论】:

    标签: reactjs npm yarnpkg


    【解决方案1】:

    事实证明,它已经记录在这里:https://create-react-app.dev/docs/deployment#customizing-environment-variables-for-arbitrary-build-environments

    我们只需要yarn add env-cmd,然后:

    {
      "scripts": {
        "build:staging": "env-cmd -f .env.staging npm run build"
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-07
      • 2019-10-10
      • 2017-10-28
      • 2018-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-26
      相关资源
      最近更新 更多