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;