Skip to content

Commit edc4854

Browse files
committed
fix: unable to load builtin plugins in standalone build
1 parent 7fcdc8b commit edc4854

File tree

1 file changed

+53
-61
lines changed

1 file changed

+53
-61
lines changed

Diff for: src/plugins.ts

+53-61
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ export interface PluginInterface {
6161
dispose?(): void | Promise<void> // 可选的清理逻辑
6262
}
6363

64+
// 载入内置插件
65+
// 必须显式静态载入 Bun 才会正确打包
66+
import * as routerPlugin from './builtin-plugins/router'
67+
68+
// 内置插件注册表
69+
const builtinPlugins: PluginInterface[] = [routerPlugin]
70+
6471
// 插件加载器
6572
export class PluginLoader {
6673
private plugins: PluginInterface[] = []
@@ -73,76 +80,61 @@ export class PluginLoader {
7380
}
7481
private fullConfig: z.infer<typeof ConfigSchema> = ConfigSchema.parse({}) // 保存完整的配置
7582

76-
async loadBuiltInPlugins(
83+
// 加载单个内置插件
84+
private async loadBuiltinPlugin(
85+
plugin: PluginInterface,
7786
config: z.infer<typeof ConfigSchema>
7887
): 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] || {} : {}
9691

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+
}
11196

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)
123106
}
107+
},
108+
}
124109

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}`)
142125
}
143126
}
144127
}
145128

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+
146138
// 加载插件
147139
async loadPlugins(
148140
pluginsDirectory: string,
@@ -154,7 +146,7 @@ export class PluginLoader {
154146
this.fullConfig = config
155147

156148
// 加载内置插件
157-
await this.loadBuiltInPlugins(config)
149+
await this.loadBuiltinPlugins(config)
158150

159151
if (!(await fs.stat(pluginsDirectory).catch(() => false))) {
160152
logger.warn(`Plugins directory not found: ${pluginsDirectory}`)

0 commit comments

Comments
 (0)