UNPKG

ayanami

Version:
40 lines (39 loc) 1.4 kB
import { Injectable } from '@asuka/di'; import omit from 'lodash/omit'; var configSets = new Set(); export var moduleNameKey = Symbol.for('__MODULE__NAME__'); export var globalKey = Symbol.for('__GLOBAL_MODULE_CACHE__'); // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types export var SSRModule = function (config) { var injectableConfig = { providers: [] }; var name; if (typeof config === 'string') { if (configSets.has(config)) { reportDuplicated(config); } name = config; configSets.add(config); } else if (config && typeof config.name === 'string') { if (configSets.has(config.name)) { reportDuplicated(config.name); } configSets.add(config.name); name = config.name; Object.assign(injectableConfig, omit(config, 'name')); } else { throw new TypeError('config in SSRModule type error, support string or InjectableConfig with name'); } return function (target) { target.prototype[moduleNameKey] = name; return Injectable(injectableConfig)(target); }; }; function reportDuplicated(moduleName) { if (process.env.NODE_ENV === 'production') { throw new Error("Duplicated Module name: " + moduleName); } // avoid to throw error after HMR console.warn("Duplicated Module name: " + moduleName); }