@yumerijs/core 1.1.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/core.js +14 -46
  2. package/package.json +1 -1
package/dist/core.js CHANGED
@@ -155,108 +155,76 @@ class Core {
155
155
  * @returns Promise<void>
156
156
  */
157
157
  async loadPlugins() {
158
- // 检查 plugins 配置是否存在且是对象类型
159
158
  if (!this.config || typeof this.config.plugins !== 'object' || this.config.plugins === null) {
160
159
  this.logger.info('No plugins configuration found or it is not an object. No plugins to load.');
161
160
  return;
162
161
  }
163
- // 获取所有需要加载的插件名,过滤掉以~开头的插件(禁用的插件)
164
162
  const pluginNamesToLoad = Object.keys(this.config.plugins).filter(name => !name.startsWith('~'));
165
163
  if (pluginNamesToLoad.length === 0) {
166
164
  this.logger.info('No enabled plugins found in configuration. All plugins might be disabled or configuration is empty.');
167
165
  return;
168
166
  }
169
- // 记录被禁用的插件
170
167
  const disabledPlugins = Object.keys(this.config.plugins).filter(name => name.startsWith('~'));
171
168
  if (disabledPlugins.length > 0) {
172
169
  this.logger.info(`Skipping disabled plugins: ${disabledPlugins.join(', ')}`);
173
170
  }
174
171
  const loadedPluginNames = [];
175
- // 使用对象来跟踪尝试加载的插件名
176
172
  let loadAttempted = {};
177
- // 使用插件名数组来管理剩余待加载的插件
178
173
  let remainingPluginNames = [...pluginNamesToLoad];
179
- // 不加载名称以~开头的插件
180
- remainingPluginNames = remainingPluginNames.filter(name => !name.startsWith('~'));
181
- // 多次尝试加载插件,直到所有插件加载完毕或检测到循环依赖
182
174
  while (remainingPluginNames.length > 0) {
183
175
  let loadedInThisPass = false;
184
- // 用于存储下一轮尝试加载的插件名
185
176
  const nextRemainingPluginNames = [];
186
- // 遍历当前剩余待加载的插件名
187
177
  for (const pluginName of remainingPluginNames) {
188
- // 如果插件已经加载或已尝试加载,则跳过
189
- if (loadedPluginNames.includes(pluginName) || loadAttempted[`${pluginName}`]) {
178
+ if (loadedPluginNames.includes(pluginName)) {
190
179
  continue;
191
180
  }
192
- // 标记该插件已尝试加载
193
- loadAttempted[`${pluginName}`] = true;
194
181
  try {
195
- this.logger.info(`Attempting to load plugin: ${pluginName}`);
196
- // 使用插件名加载插件实例和模块
182
+ // this.logger.info(`Attempting to load plugin: ${pluginName}`);
197
183
  const pluginInstance = await this.pluginLoader.load(pluginName);
198
- //const pluginModule = await this.importPluginModule(pluginName);
199
184
  if (pluginInstance) {
200
185
  const depend = pluginInstance.depend;
201
186
  const provide = pluginInstance.provide;
202
- // 检查依赖是否满足 (此处仍然检查 this.components,请确保 this.components 在此阶段已包含必要的基准组件)
203
- const unmetDependencies = depend?.filter(dep => !this.components.hasOwnProperty(dep)) || [];
187
+ // 用 providedComponents 作为依赖判断来源(比 components 更准确)
188
+ const unmetDependencies = depend?.filter(dep => !this.providedComponents.hasOwnProperty(dep)) || [];
204
189
  if (unmetDependencies.length === 0) {
205
- // 将加载成功并满足依赖的插件存储起来
206
- this.plugins[`${pluginName}`] = Object.assign(pluginInstance, { depend, provide });
207
- //this.pluginModules[`${pluginName}`] = pluginModule;
208
- //this.logger.info(`Plugin ${pluginName} loaded.`);
190
+ this.plugins[pluginName] = Object.assign(pluginInstance, { depend, provide });
209
191
  loadedPluginNames.push(pluginName);
210
- loadedInThisPass = true; // 标记本轮有插件加载成功
211
- if (loadedInThisPass && pluginInstance && pluginInstance.apply) {
192
+ loadedInThisPass = true;
193
+ if (pluginInstance.apply) {
212
194
  await pluginInstance.apply(new context_1.Context(this, pluginName), await this.getPluginConfig(pluginName));
213
- // 使用 pluginName
214
195
  this.pluginLoader.logger.info(`apply plugin ${pluginName}`);
215
196
  }
216
- // 记录该插件提供了哪些组件
217
197
  if (provide) {
218
198
  for (const componentName of provide) {
219
199
  if (this.providedComponents.hasOwnProperty(componentName)) {
220
- this.logger.error(`Multiple plugins provide the component "${componentName}". Provided by "${this.providedComponents[`${componentName}`]}" and "${pluginName}". Only the first loaded will be active.`);
200
+ this.logger.error(`Multiple plugins provide the component "${componentName}". Provided by "${this.providedComponents[componentName]}" and "${pluginName}". Only the first loaded will be active.`);
221
201
  }
222
202
  else {
223
- this.providedComponents[`${componentName}`] = pluginName;
203
+ this.providedComponents[componentName] = pluginName;
224
204
  }
225
205
  }
226
206
  }
227
207
  }
228
208
  else {
229
- // 如果存在未满足的依赖,将插件名放回下一轮尝试加载列表
230
209
  nextRemainingPluginNames.push(pluginName);
231
- /*this.logger.warn(
232
- `Plugin ${pluginName} has unmet dependencies: ${unmetDependencies.join(', ')}. Will try again later.`
233
- );*/
210
+ // 不加入 loadAttempted,让下一轮重试
234
211
  }
235
212
  }
236
- else {
237
- //this.logger.warn(`Plugin "${pluginName}" could not be loaded or its module could not be imported.`);
238
- }
239
213
  }
240
214
  catch (err) {
241
215
  this.pluginLoader.logger.error(`Failed to load or process plugin ${pluginName}:`, err);
242
- // 如果加载或处理失败,不将其加入下一轮尝试列表,但也不标记为已加载成功
216
+ loadAttempted[pluginName] = true; // 真正失败才标记
243
217
  }
244
218
  }
245
- // 如果本轮没有插件加载成功,并且剩余插件列表没有变化,则检测到循环或无法解决的依赖
246
219
  if (!loadedInThisPass && nextRemainingPluginNames.length === remainingPluginNames.length && remainingPluginNames.length > 0) {
247
- this.logger.error('Detected circular or unresolvable plugin dependencies. Remaining plugins:', nextRemainingPluginNames.map(name => name) // 映射回插件名
248
- );
249
- break; // 防止无限循环
220
+ this.logger.error('Detected circular or unresolvable plugin dependencies. Remaining plugins:', nextRemainingPluginNames);
221
+ break;
250
222
  }
251
- // 更新剩余待加载插件列表为下一轮的列表
252
223
  remainingPluginNames = nextRemainingPluginNames;
253
224
  }
254
- // 警告未加载成功的插件
255
225
  if (remainingPluginNames.length > 0) {
256
- this.logger.warn('Some plugins could not be fully loaded due to unresolved dependencies or errors:', remainingPluginNames.map(name => name) // 映射回插件名
257
- );
226
+ this.logger.warn('Some plugins could not be fully loaded due to unresolved dependencies or errors:', remainingPluginNames);
258
227
  }
259
- // 开发环境下监听插件变化
260
228
  if (process.env.NODE_ENV === 'development') {
261
229
  this.watchPlugins(this);
262
230
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yumerijs/core",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "Core module for yumeri",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",