@aliretail/react-utils
Version:
75 lines (66 loc) • 1.72 kB
JavaScript
;
exports.__esModule = true;
exports.getCurrentEnv = getCurrentEnv;
exports["default"] = exports.ENV = void 0;
/**
* 环境判断等相关工具
*/
var ENV = {
// 本地开发
local: 'local',
// 日常
daily: 'development',
// 预发
pre: 'pre',
// 生产
prod: 'production'
};
exports.ENV = ENV;
var ENV_VALUES = Object.values(ENV);
/**
* 根据传入 hostname 判断当前处于什么环境
*
* - 首先从 window._APIMAP_ENV 取,如果是有效返回值之一,那么返回该值;
* - 否则根据 hostname 来判断,如果没显式传 hostname,那么用 location.hostname
*
* @param {string} hostname 传入的 hostname,如果不传,那么默认为 location.hostname
* @returns {ENV_VARIABLES} 返回 local、development、pre 或 production 之一
*/
function getCurrentEnv(hostname) {
if (hostname === void 0) {
hostname = window.location.hostname;
}
if (window._APIMAP_ENV !== undefined && ENV_VALUES.includes(window._APIMAP_ENV)) {
return window._APIMAP_ENV;
}
if (hostname === '127.0.0.1' || hostname === 'localhost') {
return ENV.local;
} // 默认返回production
return ENV.prod;
}
var utils = {
env: '',
get: function get() {
if (!this.env) {
this.env = getCurrentEnv();
}
return this.env;
},
set: function set(newEnv) {
this.env = newEnv;
},
isLocal: function isLocal() {
return this.get() === ENV.local;
},
isDaily: function isDaily() {
return this.get() === ENV.daily;
},
isPre: function isPre() {
return this.get() === ENV.pre;
},
isProd: function isProd() {
return this.get() === ENV.prod;
}
};
var _default = utils;
exports["default"] = _default;