UNPKG

modular-core

Version:

JavaScript application modular support

148 lines (147 loc) 4.4 kB
/*! * Copyright (c) 2021 han_feng@foxmail.com * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE * OR OTHER DEALINGS IN THE SOFTWARE. */ import LogInfo from './LogInfo'; import { ExtensionPoint, DefaultExtensionPoint, Preprocessor } from './ExtensionPoint'; export interface Activator { start(modular: Modular, module: ModuleConfig): void; } export interface ModuleConfig { name: string; dependencies?: string[]; extensionPoints?: { [index: string]: ExtensionPoint; }; extensions?: { [index: string]: any; }; preprocessors?: { [index: string]: Preprocessor; }; activator?: Activator; } export interface ApplicationConfig extends ModuleConfig { version?: string; } export interface ModularOptions { modules: ModuleConfig[]; application?: ApplicationConfig; strict?: boolean; } /** * 模块化应用实现类 */ export default class Modular { readonly strict: boolean; private inited; private readonly logs; private application; private modules; private extensionPoints; private attributes; /** * 构造函数 * @param config 初始配置 */ constructor(options?: ModularOptions); /** * 获取应用配置 */ getApplication(): ApplicationConfig; /** * 获取指定名称的模块配置 * @param name 模块名称 */ getModule(name: string): ModuleConfig | undefined; /** * 获取全部模块配置 */ getModules(): ModuleConfig[]; /** * 获取指定名称的扩展配置对象 * @param name 扩展点名称 * @returns 当扩展点类型为 Multiple 时,返回扩展配置对象数组; * 当扩展点类型为 Single 时,返回最后加入的扩展配置对象; * 当扩展点类型为 Mixin 时,返回所有扩展配置对象混合后的结果 */ getExtension(name: string): any; /** * 获取原始配置对象数组,这些配置对象未经任何加工处理 * @param name 扩展点名称 */ getExtensions(name: string): any[] | null; /** * 获取指定名称的扩展点定义 * @param name 扩展点名称 */ getExtensionPoint(name: string): DefaultExtensionPoint | null; /** * 获取全部有效的扩展点定义 */ getExtensionPoints(): { [index: string]: DefaultExtensionPoint; }; /** * 获取属性名集合 */ getAttributeNames(): string[]; /** * 设置属性 * @param name 属性名 * @param value 属性值 */ setAttribute(name: string, value: any): void; /** * 获取属性值 * @param name 属性名 */ getAttribute(name: string): any; /** * 启动模块化应用 */ start(): void; /** * 获取日志记录 */ getLogs(): LogInfo[]; /** * 记录日志 * @param info 日志信息对象 */ private log; /** * 创建模块名称映射集合 * @param modules 模块配置对象集合 */ private createNameMapping; /** * 初始化 */ private init; /** * 深度遍历加载 Modules * @param module 当前模块 * @param modulesLoader 模块加载器 * @param nameMapping 模块名称索引 * @param cache 当前处理中的模块缓存 */ private loadDepens; }