@@ -61,6 +61,13 @@ export interface PluginInterface {
61
61
dispose ?( ) : void | Promise < void > // 可选的清理逻辑
62
62
}
63
63
64
+ // 载入内置插件
65
+ // 必须显式静态载入 Bun 才会正确打包
66
+ import * as routerPlugin from './builtin-plugins/router'
67
+
68
+ // 内置插件注册表
69
+ const builtinPlugins : PluginInterface [ ] = [ routerPlugin ]
70
+
64
71
// 插件加载器
65
72
export class PluginLoader {
66
73
private plugins : PluginInterface [ ] = [ ]
@@ -73,76 +80,61 @@ export class PluginLoader {
73
80
}
74
81
private fullConfig : z . infer < typeof ConfigSchema > = ConfigSchema . parse ( { } ) // 保存完整的配置
75
82
76
- async loadBuiltInPlugins (
83
+ // 加载单个内置插件
84
+ private async loadBuiltinPlugin (
85
+ plugin : PluginInterface ,
77
86
config : z . infer < typeof ConfigSchema >
78
87
) : Promise < void > {
79
- const builtinPluginsDir = path . resolve ( __dirname , 'builtin-plugins' )
80
- const builtinFiles = await fs . readdir ( builtinPluginsDir )
81
- for ( const file of builtinFiles ) {
82
- const ext = path . extname ( file )
83
- if (
84
- ext === '.ts' ||
85
- ext === '.js' ||
86
- ext === '.cjs' ||
87
- ext === '.mjs' ||
88
- ext === '.tsx' ||
89
- ext === '.jsx'
90
- ) {
91
- const pluginPath = path . resolve ( builtinPluginsDir , file )
92
- try {
93
- // 动态导入插件
94
- const pluginModule = await import ( pluginPath )
95
- const plugin : PluginInterface = pluginModule . default || pluginModule
88
+ try {
89
+ // 获取插件的配置
90
+ let pluginConfig = config . plugins ? config . plugins [ plugin . name ] || { } : { }
96
91
97
- // 检查插件的基础结构
98
- if ( ! plugin || typeof plugin . apply !== 'function' || ! plugin . name ) {
99
- throw new Error ( `Invalid plugin format: ${ pluginPath } ` )
100
- }
101
-
102
- // 获取插件的配置
103
- let pluginConfig = config . plugins
104
- ? config . plugins [ plugin . name ] || { }
105
- : { }
106
-
107
- // 如果插件提供了 ConfigSchema,则验证配置
108
- if ( plugin . ConfigSchema ) {
109
- pluginConfig = plugin . ConfigSchema . parse ( pluginConfig )
110
- }
92
+ // 如果插件提供了 ConfigSchema,则验证配置
93
+ if ( plugin . ConfigSchema ) {
94
+ pluginConfig = plugin . ConfigSchema . parse ( pluginConfig )
95
+ }
111
96
112
- // 创建插件的上下文
113
- const context : Context = {
114
- config : pluginConfig ,
115
- fullConfig : this . fullConfig , // 传入完整的配置
116
- on : ( event , handler , pre = false ) => {
117
- if ( this . eventHandlers [ event ] ) {
118
- if ( pre ) {
119
- this . eventHandlers [ event ] . unshift ( handler )
120
- } else this . eventHandlers [ event ] . push ( handler )
121
- }
122
- } ,
97
+ // 创建插件的上下文
98
+ const context : Context = {
99
+ config : pluginConfig ,
100
+ fullConfig : this . fullConfig ,
101
+ on : ( event , handler , pre = false ) => {
102
+ if ( this . eventHandlers [ event ] ) {
103
+ if ( pre ) {
104
+ this . eventHandlers [ event ] . unshift ( handler )
105
+ } else this . eventHandlers [ event ] . push ( handler )
123
106
}
107
+ } ,
108
+ }
124
109
125
- // 调用插件的 apply 方法进行初始化
126
- await plugin . apply ( context )
127
-
128
- // 插件加入已加载列表
129
- this . plugins . push ( plugin )
130
-
131
- logger . debug ( `Builtin plugin loaded: ${ plugin . name } ` )
132
- } catch ( e : unknown ) {
133
- if ( e instanceof z . ZodError ) {
134
- const errorStr = fromError ( e ) . message
135
- logger . error (
136
- `Failed to load builtin plugin ${ pluginPath } : ${ errorStr } `
137
- )
138
- } else {
139
- logger . error ( e , `Failed to load builtin plugin ${ pluginPath } ` )
140
- }
141
- }
110
+ // 调用插件的 apply 方法进行初始化
111
+ await plugin . apply ( context )
112
+
113
+ // 插件加入已加载列表
114
+ this . plugins . push ( plugin )
115
+
116
+ logger . debug ( `Builtin plugin loaded: ${ plugin . name } ` )
117
+ } catch ( e : unknown ) {
118
+ if ( e instanceof z . ZodError ) {
119
+ const errorStr = fromError ( e ) . message
120
+ logger . error (
121
+ `Failed to load builtin plugin ${ plugin . name } : ${ errorStr } `
122
+ )
123
+ } else {
124
+ logger . error ( e , `Failed to load builtin plugin ${ plugin . name } ` )
142
125
}
143
126
}
144
127
}
145
128
129
+ // 加载所有内置插件
130
+ private async loadBuiltinPlugins (
131
+ config : z . infer < typeof ConfigSchema >
132
+ ) : Promise < void > {
133
+ for ( const plugin of builtinPlugins ) {
134
+ await this . loadBuiltinPlugin ( plugin , config )
135
+ }
136
+ }
137
+
146
138
// 加载插件
147
139
async loadPlugins (
148
140
pluginsDirectory : string ,
@@ -154,7 +146,7 @@ export class PluginLoader {
154
146
this . fullConfig = config
155
147
156
148
// 加载内置插件
157
- await this . loadBuiltInPlugins ( config )
149
+ await this . loadBuiltinPlugins ( config )
158
150
159
151
if ( ! ( await fs . stat ( pluginsDirectory ) . catch ( ( ) => false ) ) ) {
160
152
logger . warn ( `Plugins directory not found: ${ pluginsDirectory } ` )
0 commit comments