nishimura.clubnishimura.club

React ScriptでBuild時に環境ごとのenvを適用させる

作成日
2022-07-19
更新日
2022-07-19
.env.development
REACT_APP_AUTHORIZATION_TOKEN="" REACT_APP_LAMBDA_URL=""
  • .env.development
  • .env.staging
  • .env.production

というenvファイルがある。yarn dev時には、env.developmentが適用され、build時には.env.productionが問答無用で適用されていた。

下記のような npm scriptを書くと、解決できた。

"scripts": { "build": "sh -ac '. ./.env.${REACT_APP_ENV}; react-scripts build'", "build:dev": "yarn tsc && yarn test && REACT_APP_ENV=development yarn build", "build:staging": "yarn tsc && yarn test && REACT_APP_ENV=staging yarn build", "build:prod": "yarn tsc && yarn test && REACT_APP_ENV=production yarn build", },

Related

Tags

ReactJS

Share