Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions packages/language-core/lib/compilerOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,24 +133,26 @@ export class CompilerOptionsResolver {
}
break;
case 'plugins':
this.plugins = (options.plugins ?? [])
.flatMap<VueLanguagePlugin>((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
Expand Down
8 changes: 4 additions & 4 deletions packages/language-core/lib/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ export function createPlugins(pluginContext: Parameters<VueLanguagePlugin>[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;
}
Expand Down
26 changes: 17 additions & 9 deletions packages/language-core/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ export type RawVueCompilerOptions = Partial<Omit<VueCompilerOptions, 'target' |
strictTemplates?: boolean;
target?: 'auto' | 3 | 3.3 | 3.5 | 3.6 | 99 | number;
globalTypesPath?: string;
plugins?: string[];
plugins?: RawPlugin[];
};

export type RawPlugin =
| string
| Record<string, any> & {
name: string;
};

export interface VueCodeInformation extends CodeInformation {
__combineOffset?: number;
__linkedToken?: symbol;
Expand Down Expand Up @@ -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<string, any> & {
modules: {
typescript: typeof ts;
'@vue/compiler-dom': typeof CompilerDOM;
};
compilerOptions: ts.CompilerOptions;
vueCompilerOptions: VueCompilerOptions;
},
) => VueLanguagePluginReturn | VueLanguagePluginReturn[];

export interface SfcBlock {
name: string;
Expand Down
Loading