| 231 | // ========== 私有方法 ========== |
| 232 | |
| 233 | private async _loadOne(config: PluginLoadConfig): Promise<void> { |
| 234 | const info: PluginLoadInfo = { |
| 235 | packageId: config.packageId, |
| 236 | state: 'loading' |
| 237 | }; |
| 238 | this._loaded.set(config.packageId, info); |
| 239 | const start = Date.now(); |
| 240 | |
| 241 | try { |
| 242 | let plugin: IRuntimePlugin | null = null; |
| 243 | |
| 244 | switch (config.sourceType) { |
| 245 | case 'npm': |
| 246 | plugin = await this._loadNpm(config); |
| 247 | break; |
| 248 | case 'local': |
| 249 | plugin = await this._loadLocal(config); |
| 250 | break; |
| 251 | case 'static': |
| 252 | logger.warn(`Static plugin ${config.packageId} should be pre-registered`); |
| 253 | break; |
| 254 | } |
| 255 | |
| 256 | if (plugin) { |
| 257 | info.plugin = plugin; |
| 258 | info.state = 'loaded'; |
| 259 | info.loadTime = Date.now() - start; |
| 260 | runtimePluginManager.register(plugin); |
| 261 | logger.debug(`Loaded: ${config.packageId} (${info.loadTime}ms)`); |
| 262 | } else { |
| 263 | throw new Error('Plugin export not found'); |
| 264 | } |
| 265 | } catch (error) { |
| 266 | info.state = 'failed'; |
| 267 | info.error = error instanceof Error ? error.message : String(error); |
| 268 | info.loadTime = Date.now() - start; |
| 269 | logger.error(`Failed: ${config.packageId} - ${info.error}`); |
| 270 | |
| 271 | if (!this._config.continueOnFailure) { |
| 272 | throw error; |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | private async _loadNpm(config: PluginLoadConfig): Promise<IRuntimePlugin | null> { |
| 278 | const module = await import(/* @vite-ignore */ config.packageId); |