Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. ### REACT ENV 환경설정
  2. react에서 지원하는 환경변수를 사용하여 app을 실행 또는 빌드하는 시점에 분기를 태우고,
  3. 그에따라 적용할수 있는 config파일을 하나 생성하겠다.
  4. react 환경변수는 REACT_APP_ 으로 시작해야 react내부에서 process.env로 참조할수 있다는 점을 주의하자.
  5.  
  6. ```javascript
  7. // ex) package.json
  8. {
  9. "scripts": {
  10. "start": "react-scripts start",
  11. "build": "react-scripts build",
  12. "build:stg": "NODE_OPTIONS=\"--max-old-space-size=4096\" REACT_APP_ENV=staging npm run build",
  13. "build:prd": "NODE_OPTIONS=\"--max-old-space-size=4096\" REACT_APP_ENV=production npm run build",
  14. "test": "react-scripts test",
  15. "eject": "react-scripts eject"
  16. }
  17. }
  18. ```
  19.  
  20. ```javascript
  21. // ex) src/config/index.js
  22.  
  23. const DEV = "development";
  24. const STAGING = "staging";
  25. const PRODUCTION = "production";
  26.  
  27. export const EnvironmentTypes = {
  28. DEV,
  29. STAGING,
  30. PRODUCTION,
  31. };
  32.  
  33. export const ENV = (() => {
  34. switch (process.env.REACT_APP_ENV) {
  35. case "production":
  36. return PRODUCTION;
  37. case "staging":
  38. return STAGING;
  39. default:
  40. return DEV;
  41. }
  42. })();
  43.  
  44. export const DEBUG = ENV === DEV;
  45.  
  46. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement