如何使用自定义模式制作 React 应用程序的生产版本?

How to make a production build of React app with a custom mode?

使用 CRA 和 "react-scripts": "3.3.1" 构建了一个应用程序,但是我对构建模式有疑问,例如在 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 用于 "STAGING" 环境。我该怎么做?

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

事实证明,它已经记录在这里: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"
  }
}