简体   繁体   中英

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

Built an app using CRA with "react-scripts": "3.3.1" , however i have an issue with a build modes, for example in a Vue-CLI i could do this:

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

But in CRA, there's:

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

I've tried something like that:

yarn build --mode staging

But it uses .env.production variables. I have .env.staging for "STAGING" environment. How can i do that?

I'm using different API endpoints on a few production servers, that's why i need this mode thing.

Turns out, its already documented here: https://create-react-app.dev/docs/deployment#customizing-environment-variables-for-arbitrary-build-environments

We just have to yarn add env-cmd , then:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM