Skip to content

Commit afc069e

Browse files
authored
feat(language-core): support configuration for language plugins (#5678)
1 parent bc1e57e commit afc069e

3 files changed

Lines changed: 39 additions & 29 deletions

File tree

‎packages/language-core/lib/compilerOptions.ts‎

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -152,25 +152,27 @@ export class CompilerOptionsResolver {
152152
}
153153
break;
154154
case 'plugins':
155-
this.plugins = (options.plugins ?? [])
156-
.flatMap<VueLanguagePlugin>((pluginPath: string) => {
157-
try {
158-
const resolve = (require as NodeJS.Require | undefined)?.resolve;
159-
const resolvedPath = resolve?.(pluginPath, { paths: [rootDir] });
160-
if (resolvedPath) {
161-
const plugin = require(resolvedPath);
162-
plugin.__moduleName = pluginPath;
163-
return plugin;
164-
}
165-
else {
166-
console.warn('[Vue] Load plugin failed:', pluginPath);
155+
for (let raw of options.plugins ?? []) {
156+
raw = typeof raw === 'string' ? { name: raw } : raw;
157+
try {
158+
const resolve = (require as NodeJS.Require | undefined)?.resolve;
159+
const resolvedPath = resolve?.(raw.name, { paths: [rootDir] });
160+
if (resolvedPath) {
161+
const plugin = require(resolvedPath);
162+
const plugins = Array.isArray(plugin) ? plugin : [plugin];
163+
for (const plugin of plugins) {
164+
plugin.__moduleConfig = raw;
165+
this.plugins.push(plugin);
167166
}
168167
}
169-
catch (error) {
170-
console.warn('[Vue] Resolve plugin path failed:', pluginPath, error);
168+
else {
169+
console.warn('[Vue] Load plugin failed:', raw.name);
171170
}
172-
return [];
173-
});
171+
}
172+
catch (error) {
173+
console.warn('[Vue] Resolve plugin path failed:', raw.name, error);
174+
}
175+
}
174176
break;
175177
default:
176178
// @ts-expect-error

‎packages/language-core/lib/plugins.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ export function createPlugins(pluginContext: Parameters<VueLanguagePlugin>[0]) {
3838
const pluginInstances = plugins
3939
.flatMap(plugin => {
4040
try {
41-
const instance = plugin(pluginContext);
42-
const moduleName = (plugin as any).__moduleName;
41+
const moduleConfig = (plugin as any).__moduleConfig ?? {};
42+
const instance = plugin({ ...pluginContext, ...moduleConfig });
4343
if (Array.isArray(instance)) {
4444
for (let i = 0; i < instance.length; i++) {
45-
instance[i]!.name ??= `${moduleName} (${i})`;
45+
instance[i]!.name ??= `${moduleConfig.name} (${i})`;
4646
}
4747
}
4848
else {
49-
instance.name ??= moduleName;
49+
instance.name ??= moduleConfig.name;
5050
}
5151
return instance;
5252
}

‎packages/language-core/lib/types.ts‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@ export { VueEmbeddedCode };
1212
export type RawVueCompilerOptions = Partial<Omit<VueCompilerOptions, 'target' | 'plugins'>> & {
1313
strictTemplates?: boolean;
1414
target?: 'auto' | 3 | 3.3 | 3.5 | 3.6 | 99 | number;
15-
plugins?: string[];
15+
plugins?: RawPlugin[];
1616
};
1717

18+
export type RawPlugin =
19+
| string
20+
| Record<string, any> & {
21+
name: string;
22+
};
23+
1824
export interface VueCodeInformation extends CodeInformation {
1925
__importCompletion?: boolean;
2026
__combineToken?: symbol;
@@ -108,14 +114,16 @@ export interface VueLanguagePluginReturn {
108114
resolveEmbeddedCode?(fileName: string, sfc: Sfc, embeddedFile: VueEmbeddedCode): void;
109115
}
110116

111-
export type VueLanguagePlugin = (ctx: {
112-
modules: {
113-
typescript: typeof ts;
114-
'@vue/compiler-dom': typeof CompilerDOM;
115-
};
116-
compilerOptions: ts.CompilerOptions;
117-
vueCompilerOptions: VueCompilerOptions;
118-
}) => VueLanguagePluginReturn | VueLanguagePluginReturn[];
117+
export type VueLanguagePlugin = (
118+
ctx: Record<string, any> & {
119+
modules: {
120+
typescript: typeof ts;
121+
'@vue/compiler-dom': typeof CompilerDOM;
122+
};
123+
compilerOptions: ts.CompilerOptions;
124+
vueCompilerOptions: VueCompilerOptions;
125+
},
126+
) => VueLanguagePluginReturn | VueLanguagePluginReturn[];
119127

120128
export interface SfcBlock {
121129
name: string;

0 commit comments

Comments
 (0)