Skip to content

Instantly share code, notes, and snippets.

@koo6357
Last active January 5, 2021 04:26
Show Gist options
  • Save koo6357/e95193f14dfe2d93a54b6421e5137ac7 to your computer and use it in GitHub Desktop.
Save koo6357/e95193f14dfe2d93a54b6421e5137ac7 to your computer and use it in GitHub Desktop.
REACT ENV 환경설정

REACT ENV 환경설정

react에서 지원하는 환경변수를 사용하여 app을 실행 또는 빌드하는 시점에 분기를 태우고, 그에따라 적용할수 있는 config파일을 하나 생성하겠다. react 환경변수는 REACT_APP_ 으로 시작해야 react내부에서 process.env로 참조할수 있다는 점을 주의하자.

// ex) package.json
{
  "scripts": {
      "start": "react-scripts start",
      "build": "react-scripts build",
      "build:stg": "NODE_OPTIONS=\"--max-old-space-size=4096\" REACT_APP_ENV=staging npm run build",
      "build:prd": "NODE_OPTIONS=\"--max-old-space-size=4096\" REACT_APP_ENV=production npm run build",
      "test": "react-scripts test",
      "eject": "react-scripts eject"
  } 
}
// ex) src/config/index.js

const DEV = "development";
const STAGING = "staging";
const PRODUCTION = "production";

export const EnvironmentTypes = {
  DEV,
  STAGING,
  PRODUCTION,
};

export const ENV = (() => {
  switch (process.env.REACT_APP_ENV) {
    case "production":
      return PRODUCTION;
    case "staging":
      return STAGING;
    default:
      return DEV;
  }
})();

export const DEBUG = ENV === DEV;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment