UNPKG

me-module-utils

Version:

Me Module utils

90 lines (71 loc) 2.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.relativeRequirePath = exports.getMatchedExportsName = exports.destroyInstance = exports.getMeModuleInstance = exports.initMeModules = void 0; var _env = _interopRequireDefault(require("./env")); var _MeModules = _interopRequireDefault(require("./MeModules")); var utils = _interopRequireWildcard(require("./utils")); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * 初始化me模块管理 * * @param {Object} options 配置 * @param {string} options.basePath 前端脚本根路径 * @returns {MeModules} */ const initMeModules = options => { (0, _env.default)(options); return new _MeModules.default(options); }; /** * 单例 * * @type {MeModules} */ exports.initMeModules = initMeModules; let instance = null; /** * 缓存 `MeModules` 实例化的配置 * * @type {Object} */ let cachedInitOptions = null; /** * 获取单例 * * @param {Object} options 配置 * @param {string} options.basePath 前端脚本根路径 * @return {MeModules} * @throws {Error} */ const getMeModuleInstance = options => { if (instance) { return instance; } options = !options && cachedInitOptions ? cachedInitOptions : options; // 没有传参数进来,也没有缓存 if (!options) { throw new Error('You should pass `options` to get `MeModule` instance'); } cachedInitOptions = options; instance = initMeModules(options); return instance; }; /** * 销毁单例 * * @param {boolean} destroyInitedOptions 是否清除配置缓存 */ exports.getMeModuleInstance = getMeModuleInstance; const destroyInstance = destroyInitedOptions => { instance = null; destroyInitedOptions && (cachedInitOptions = null); }; // 一些工具函数 exports.destroyInstance = destroyInstance; const { getMatchedExportsName, relativeRequirePath } = utils; exports.relativeRequirePath = relativeRequirePath; exports.getMatchedExportsName = getMatchedExportsName;