UNPKG

@cainiaofe/cn-utils

Version:

菜鸟前端基础工具库

106 lines (105 loc) 4.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PROD = exports.PRE = exports.DAILY = exports.TEST = exports.LOCAL = exports.getCnEnv = void 0; var types_1 = require("./types"); function parseHostname(options) { var _a; var hostname = options.hostname; // 解析用户提供的 hostname if (hostname) { if (hostname.indexOf('//') !== -1) { _a = hostname.split('//'), hostname = _a[1]; } if (hostname.indexOf('/') !== -1) { hostname = hostname.split('/')[0]; } if (hostname.indexOf(':') !== -1) { hostname = hostname.split(':')[0]; } } else { hostname = window.location.hostname; } return hostname && hostname.trim(); } function parseCurrentScriptSrc(options) { var currentScriptSrc = options.currentScriptSrc; if (currentScriptSrc) { return currentScriptSrc; } if (window.__CURRENT_SCRIPT_SRC__) { currentScriptSrc = window.__CURRENT_SCRIPT_SRC__; } else if (window.__current_micro_config__ && window.__current_micro_config__.prefix) { currentScriptSrc = window.__current_micro_config__.prefix; } else if (window.document.currentScript) { var script = window.document.currentScript; currentScriptSrc = script.src; } return currentScriptSrc && currentScriptSrc.trim(); } var regIp = /^\d+\.\d+\.\d+\.\d+$/; function getCnEnv(options) { if (options === void 0) { options = {}; } try { // case: 读取外界注入的 window.__CN_ENV__, 优先级最高, 这样也可以在本地 public/index.html 模拟其他环境 if (typeof window !== 'undefined' && window.__CN_ENV__) { return window.__CN_ENV__; } var hostname = parseHostname(options); if (!hostname) { return types_1.PROD; } // case: localhost/127.0.0.1 if (hostname === 'localhost' || hostname === '127.0.0.1') { return types_1.LOCAL; } // case: ip 30.117.72.7 if (regIp.test(hostname)) { return types_1.LOCAL; } var parts = hostname.split('.'); var head = parts[0] || ''; var tail = parts[parts.length - 1] || ''; // case: pre.xxx pre-xxx.yyy pre2-xxx.yyy page-pre.xxxx if (head === types_1.PRE || head.indexOf("".concat(types_1.PRE, "-")) === 0 || head.indexOf("".concat(types_1.PRE, "2-")) === 0 || head.indexOf("page-".concat(types_1.PRE)) === 0) { return types_1.PRE; } // case: daily.xxx daily-xxx.yyy page-daily.xxx if (head === types_1.DAILY || head.indexOf("".concat(types_1.DAILY, "-")) === 0 || head.indexOf("page-".concat(types_1.DAILY)) === 0 || tail === types_1.TEST) { return types_1.DAILY; } // case: currentScriptSrc var currentScriptSrc = parseCurrentScriptSrc(options); if (!currentScriptSrc) { return types_1.PROD; } if (currentScriptSrc.indexOf('https://g.alicdn.com') === 0 || currentScriptSrc.indexOf('https://cn.alicdn.com') === 0) { return types_1.PROD; } if (currentScriptSrc.indexOf('https://dev.g.alicdn.com') === 0 || currentScriptSrc.indexOf('https://assets-daily.cainiao-inc.com') === 0) { return types_1.DAILY; } } catch (err) { // ignore err } return types_1.PROD; } exports.getCnEnv = getCnEnv; var types_2 = require("./types"); Object.defineProperty(exports, "LOCAL", { enumerable: true, get: function () { return types_2.LOCAL; } }); Object.defineProperty(exports, "TEST", { enumerable: true, get: function () { return types_2.TEST; } }); Object.defineProperty(exports, "DAILY", { enumerable: true, get: function () { return types_2.DAILY; } }); Object.defineProperty(exports, "PRE", { enumerable: true, get: function () { return types_2.PRE; } }); Object.defineProperty(exports, "PROD", { enumerable: true, get: function () { return types_2.PROD; } });