Làm Sao Để Kiểm Tra Đang Chạy Môi Trường Nào Trong DvaJS

DvaJS là một  framework  nhỏ gọn xây dựng trên nền ReactJS, Redux, để xem chi tiết các bạn search trên google nhé

 

Trong qúa trình làm việc  với DvaJS. Khi ở môi trường phát triển, bạn thực thi lệnh: yarn start, còn ở môi trường production thì yarn build

Hiện tại thì DvaJS nó đang sài thư viện roadhog để quản lý việc development, production và testing

và trong trường hợp bạn muốn kiểm tra trong code của mình để load file cấu hình tương ứng với từng môi trường thì bạn sẽ làm thế nào.

Ví dụ: ứng với môi trường development bạn sẽ load file configs với  nội dung:

export const API_PATH = 'http://localhost:2018';
export const IO_PATH = 'http://localhost:2018';

còn ở môi trường production:

export const API_PATH = 'http://api.my';
export const IO_PATH = 'http://io.my';

Việc này vô cùng đơn giản, bạn để ý là trong file .webpackrc.js, nó cho phép mình làm việc này với cấu hình là define à

const path = require('path');

export default {
  entry: 'src/index.js',
  extraBabelPlugins: [['import', { libraryName: 'antd', libraryDirectory: 'es', style: true }]],
  env: {
    development: {
      extraBabelPlugins: ['dva-hmr'],
    },
  },
  define: {
    'process.env.NODE_ENV': process.env.NODE_ENV,
    'process.env.API_PATH': process.env.API_PATH
    ,
  },
  alias: {
    components: path.resolve(__dirname, 'src/components/'),
  },
  ignoreMomentLocale: true,
  theme: './src/theme.js',
  html: {
    template: './src/index.ejs',
  },
  disableDynamicImport: true,
  publicPath: '/',
  hash: true,
};

 

Khi sử dụng define như vậy thì trong code của bạn, bạn có thể truy cập được biến env từ process rồi nhé

const {
    API_PATH,
    IO_PATH
} = require( `./env.${process.env.NODE_ENV}`);

 

Rate this post

You May Also Like

About the Author: truongluu

Leave a Reply

Your email address will not be published. Required fields are marked *