diff --git a/packages/language-core/lib/compilerOptions.ts b/packages/language-core/lib/compilerOptions.ts index 2fe54b821b..6674e305a1 100644 --- a/packages/language-core/lib/compilerOptions.ts +++ b/packages/language-core/lib/compilerOptions.ts @@ -133,24 +133,26 @@ export class CompilerOptionsResolver { } break; case 'plugins': - this.plugins = (options.plugins ?? []) - .flatMap((pluginPath: string) => { - try { - const resolvedPath = resolvePath(pluginPath, rootDir); - if (resolvedPath) { - const plugin = require(resolvedPath); - plugin.__moduleName = pluginPath; - return plugin; - } - else { - console.warn('[Vue] Load plugin failed:', pluginPath); + for (let raw of options.plugins ?? []) { + raw = typeof raw === 'string' ? { name: raw } : raw; + try { + const resolvedPath = resolvePath(raw.name, rootDir); + if (resolvedPath) { + const plugin = require(resolvedPath); + const plugins = Array.isArray(plugin) ? plugin : [plugin]; + for (const plugin of plugins) { + plugin.__moduleConfig = raw; + this.plugins.push(plugin); } } - catch (error) { - console.warn('[Vue] Resolve plugin path failed:', pluginPath, error); + else { + console.warn('[Vue] Load plugin failed:', raw.name); } - return []; - }); + } + catch (error) { + console.warn('[Vue] Resolve plugin path failed:', raw.name, error); + } + } break; default: // @ts-expect-error diff --git a/packages/language-core/lib/plugins.ts b/packages/language-core/lib/plugins.ts index 4c7d4ea3c0..4977a774aa 100644 --- a/packages/language-core/lib/plugins.ts +++ b/packages/language-core/lib/plugins.ts @@ -38,15 +38,15 @@ export function createPlugins(pluginContext: Parameters[0]) { const pluginInstances = plugins .flatMap(plugin => { try { - const instance = plugin(pluginContext); - const moduleName = (plugin as any).__moduleName; + const moduleConfig = (plugin as any).__moduleConfig ?? {}; + const instance = plugin({ ...pluginContext, ...moduleConfig }); if (Array.isArray(instance)) { for (let i = 0; i < instance.length; i++) { - instance[i]!.name ??= `${moduleName} (${i})`; + instance[i]!.name ??= `${moduleConfig.name} (${i})`; } } else { - instance.name ??= moduleName; + instance.name ??= moduleConfig.name; } return instance; } diff --git a/packages/language-core/lib/types.ts b/packages/language-core/lib/types.ts index 14c205bceb..395f0a6ce1 100644 --- a/packages/language-core/lib/types.ts +++ b/packages/language-core/lib/types.ts @@ -13,9 +13,15 @@ export type RawVueCompilerOptions = Partial & { + name: string; + }; + export interface VueCodeInformation extends CodeInformation { __combineOffset?: number; __linkedToken?: symbol; @@ -108,14 +114,16 @@ export type VueLanguagePluginReturn = { resolveEmbeddedCode?(fileName: string, sfc: Sfc, embeddedFile: VueEmbeddedCode): void; }; -export type VueLanguagePlugin = (ctx: { - modules: { - typescript: typeof ts; - '@vue/compiler-dom': typeof CompilerDOM; - }; - compilerOptions: ts.CompilerOptions; - vueCompilerOptions: VueCompilerOptions; -}) => VueLanguagePluginReturn | VueLanguagePluginReturn[]; +export type VueLanguagePlugin = ( + ctx: Record & { + modules: { + typescript: typeof ts; + '@vue/compiler-dom': typeof CompilerDOM; + }; + compilerOptions: ts.CompilerOptions; + vueCompilerOptions: VueCompilerOptions; + }, +) => VueLanguagePluginReturn | VueLanguagePluginReturn[]; export interface SfcBlock { name: string;