Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support for Plugin API #1687

Merged
merged 1 commit into from Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/alpine-language-core/src/index.ts
@@ -1,5 +1,5 @@
import * as vue from '@volar/vue-language-core';
import useHtmlFilePlugin from './plugins/file-html';
import * as useHtmlFilePlugin from './plugins/file-html';

export type LanguageServiceHost = vue.LanguageServiceHost;

Expand Down
4 changes: 2 additions & 2 deletions packages/alpine-language-core/src/plugins/file-html.ts
@@ -1,5 +1,5 @@
import { VueLanguagePlugin } from '@volar/vue-language-core';
import useVueHtmlFilePlugin from '@volar/vue-language-core/out/plugins/file-html';
import * as useVueHtmlFilePlugin from '@volar/vue-language-core/out/plugins/file-html';

const plugin: VueLanguagePlugin = (ctx) => {

Expand Down Expand Up @@ -33,4 +33,4 @@ const plugin: VueLanguagePlugin = (ctx) => {
}
};
};
export default plugin;
export = plugin;
5 changes: 5 additions & 0 deletions packages/vue-language-core/schemas/vue-tsconfig.schema.json
Expand Up @@ -22,6 +22,11 @@
"default": false,
"markdownDescription": "Strict props, component type-checking in templates."
},
"plugins": {
"type": "array",
"default": [],
"markdownDescription": "Plugins to be used in the SFC compiler."
},
"experimentalRuntimeMode": {
"type": "string",
"default": "runtime-dom",
Expand Down
1 change: 1 addition & 0 deletions packages/vue-language-core/src/generators/script.ts
Expand Up @@ -597,6 +597,7 @@ export function generate(
const useGlobalThisTypeInCtx = fileName.endsWith('.html');

codeGen.addText(`let __VLS_ctx!: ${useGlobalThisTypeInCtx ? 'typeof globalThis &' : ''}`);
codeGen.addText(`__VLS_types.PickNotAny<__VLS_Ctx, {}> & `);
if (sfc.scriptSetup) {
codeGen.addText(`InstanceType<__VLS_types.PickNotAny<typeof __VLS_Component, new () => {}>> & `);
}
Expand Down
42 changes: 27 additions & 15 deletions packages/vue-language-core/src/lsContext.ts
Expand Up @@ -5,29 +5,29 @@ import * as localTypes from './utils/localTypes';
import { createSourceFile, EmbeddedFile, VueLanguagePlugin } from './sourceFile';
import { createDocumentRegistry } from './documentRegistry';

import useHtmlFilePlugin from './plugins/file-html';
import useMdFilePlugin from './plugins/file-md';
import useVueFilePlugin from './plugins/file-vue';
import useVueSfcCustomBlocks from './plugins/vue-sfc-customblocks';
import useVueSfcScriptsFormat from './plugins/vue-sfc-scripts';
import useVueSfcStyles from './plugins/vue-sfc-styles';
import useVueSfcTemplate from './plugins/vue-sfc-template';
import useHtmlPlugin from './plugins/vue-template-html';
import usePugPlugin from './plugins/vue-template-pug';
import * as useHtmlFilePlugin from './plugins/file-html';
import * as useMdFilePlugin from './plugins/file-md';
import * as useVueFilePlugin from './plugins/file-vue';
import * as useVueSfcCustomBlocks from './plugins/vue-sfc-customblocks';
import * as useVueSfcScriptsFormat from './plugins/vue-sfc-scripts';
import * as useVueSfcStyles from './plugins/vue-sfc-styles';
import * as useVueSfcTemplate from './plugins/vue-sfc-template';
import * as useHtmlPlugin from './plugins/vue-template-html';
import * as usePugPlugin from './plugins/vue-template-pug';
import useVueTsx from './plugins/vue-tsx';
import { getVueCompilerOptions } from './utils/ts';

export type LanguageContext = ReturnType<typeof createLanguageContext>;

export function getPlugins(
ts: typeof import('typescript/lib/tsserverlibrary'),
rootDir: string,
compilerOptions: ts.CompilerOptions,
vueCompilerOptions: VueCompilerOptions,
_vueCompilerOptions: VueCompilerOptions,
extraPlugins: VueLanguagePlugin[] = [],
) {

const _plugins: VueLanguagePlugin[] = [
...extraPlugins,
useVueFilePlugin,
useMdFilePlugin,
useHtmlFilePlugin,
Expand All @@ -38,13 +38,25 @@ export function getPlugins(
useVueSfcScriptsFormat,
useVueSfcTemplate,
useVueTsx,
...extraPlugins,
];
const vueCompilerOptions = getVueCompilerOptions(_vueCompilerOptions);
for (const pluginPath of vueCompilerOptions.plugins) {
try {
const importPath = require.resolve(pluginPath, { paths: [rootDir] });
const plugin = require(importPath);
_plugins.push(plugin);
}
catch (error) {
console.error(error);
}
}
const pluginCtx: Parameters<VueLanguagePlugin>[0] = {
modules: {
typescript: ts,
},
compilerOptions,
vueCompilerOptions: getVueCompilerOptions(vueCompilerOptions),
vueCompilerOptions: vueCompilerOptions,
};
const plugins = _plugins.map(plugin => plugin(pluginCtx));

Expand Down Expand Up @@ -76,9 +88,9 @@ export function createLanguageContext(

const documentRegistry = createDocumentRegistry();
const compilerOptions = host.getCompilationSettings();
const vueCompilerOptions = host.getVueCompilationSettings();
const vueCompilerOptions = getVueCompilerOptions(host.getVueCompilationSettings());
const tsFileVersions = new Map<string, string>();
const sharedTypesScript = ts.ScriptSnapshot.fromString(localTypes.getTypesCode(vueCompilerOptions.target ?? 3));
const sharedTypesScript = ts.ScriptSnapshot.fromString(localTypes.getTypesCode(vueCompilerOptions.target));
const scriptSnapshots = new Map<string, [string, ts.IScriptSnapshot]>();
const fileVersions = new WeakMap<EmbeddedFile, string>();
const _tsHost: Partial<ts.LanguageServiceHost> = {
Expand Down Expand Up @@ -159,7 +171,7 @@ export function createLanguageContext(
}
},
};
const plugins = getPlugins(ts, compilerOptions, vueCompilerOptions, extraPlugins);
const plugins = getPlugins(ts, host.getCurrentDirectory(), compilerOptions, vueCompilerOptions, extraPlugins);

return {
typescriptLanguageServiceHost: new Proxy(_tsHost as ts.LanguageServiceHost, {
Expand Down
2 changes: 1 addition & 1 deletion packages/vue-language-core/src/plugins/empty.ts
Expand Up @@ -3,4 +3,4 @@ import { VueLanguagePlugin } from '../sourceFile';
const plugin: VueLanguagePlugin = () => {
return {};
};
export default plugin;
export = plugin;
2 changes: 1 addition & 1 deletion packages/vue-language-core/src/plugins/file-html.ts
Expand Up @@ -86,4 +86,4 @@ const plugin: VueLanguagePlugin = () => {
}
};
}
export default plugin;
export = plugin;
2 changes: 1 addition & 1 deletion packages/vue-language-core/src/plugins/file-md.ts
Expand Up @@ -85,4 +85,4 @@ const plugin: VueLanguagePlugin = () => {
}
};
}
export default plugin;
export = plugin;
2 changes: 1 addition & 1 deletion packages/vue-language-core/src/plugins/file-vue.ts
Expand Up @@ -14,4 +14,4 @@ const plugin: VueLanguagePlugin = () => {
}
};
}
export default plugin;
export = plugin;
Expand Up @@ -54,4 +54,4 @@ const plugin: VueLanguagePlugin = () => {
},
};
}
export default plugin;
export = plugin;
2 changes: 1 addition & 1 deletion packages/vue-language-core/src/plugins/vue-sfc-scripts.ts
Expand Up @@ -46,4 +46,4 @@ const plugin: VueLanguagePlugin = () => {
},
};
}
export default plugin;
export = plugin;
2 changes: 1 addition & 1 deletion packages/vue-language-core/src/plugins/vue-sfc-styles.ts
Expand Up @@ -54,4 +54,4 @@ const plugin: VueLanguagePlugin = () => {
},
};
}
export default plugin;
export = plugin;
2 changes: 1 addition & 1 deletion packages/vue-language-core/src/plugins/vue-sfc-template.ts
Expand Up @@ -48,4 +48,4 @@ const plugin: VueLanguagePlugin = () => {
},
};
}
export default plugin;
export = plugin;
Expand Up @@ -17,4 +17,4 @@ const plugin: VueLanguagePlugin = ({ vueCompilerOptions }) => {
},
};
};
export default plugin;
export = plugin;
2 changes: 1 addition & 1 deletion packages/vue-language-core/src/plugins/vue-template-pug.ts
Expand Up @@ -54,4 +54,4 @@ const plugin: VueLanguagePlugin = ({ vueCompilerOptions }) => {
},
};
};
export default plugin;
export = plugin;
11 changes: 7 additions & 4 deletions packages/vue-language-core/src/plugins/vue-tsx.ts
Expand Up @@ -46,8 +46,9 @@ const plugin: VueLanguagePlugin = ({ modules, vueCompilerOptions, compilerOption
};
const tsx = _gen?.tsxGen.value;
if (tsx) {
embeddedFile.codeGen = tsx.codeGen;
embeddedFile.teleportMappings = tsx.teleports;
embeddedFile.codeGen.addText(tsx.codeGen.getText());
embeddedFile.codeGen.mappings = [...tsx.codeGen.mappings];
embeddedFile.teleportMappings = [...tsx.teleports];
}
}
else if (suffix.match(/^\.__VLS_template_format\.tsx$/)) {
Expand All @@ -64,15 +65,17 @@ const plugin: VueLanguagePlugin = ({ modules, vueCompilerOptions, compilerOption
embeddedFile.isTsHostFile = false;

if (_gen?.htmlGen.value) {
embeddedFile.codeGen = _gen.htmlGen.value.formatCodeGen;
embeddedFile.codeGen.addText(_gen.htmlGen.value.formatCodeGen.getText());
embeddedFile.codeGen.mappings = [..._gen.htmlGen.value.formatCodeGen.mappings];
}
}
else if (suffix.match(/^\.__VLS_template\.css$/)) {

embeddedFile.parentFileName = fileName + '.' + sfc.template?.lang;

if (_gen?.htmlGen.value) {
embeddedFile.codeGen = _gen.htmlGen.value.cssCodeGen;
embeddedFile.codeGen.addText(_gen.htmlGen.value.cssCodeGen.getText());
embeddedFile.codeGen.mappings = [..._gen.htmlGen.value.cssCodeGen.mappings];
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion packages/vue-language-core/src/sourceFile.ts
Expand Up @@ -10,7 +10,7 @@ import type * as ts from 'typescript/lib/tsserverlibrary';

export type VueLanguagePlugin = (ctx: {
modules: {
typescript: typeof ts,
typescript: typeof import('typescript/lib/tsserverlibrary');
},
compilerOptions: ts.CompilerOptions,
vueCompilerOptions: _VueCompilerOptions,
Expand Down
1 change: 1 addition & 0 deletions packages/vue-language-core/src/types.ts
Expand Up @@ -12,6 +12,7 @@ export type VueCompilerOptions = Partial<_VueCompilerOptions>;
export interface _VueCompilerOptions {
target: 2 | 2.7 | 3;
strictTemplates: boolean;
plugins: string[];

// experimental
experimentalRuntimeMode: 'runtime-dom' | 'runtime-uni-app';
Expand Down
1 change: 1 addition & 0 deletions packages/vue-language-core/src/utils/ts.ts
Expand Up @@ -50,6 +50,7 @@ export function getVueCompilerOptions(vueOptions: VueCompilerOptions): _VueCompi

target: vueOptions.target ?? 3,
strictTemplates: vueOptions.strictTemplates ?? false,
plugins: vueOptions.plugins ?? [],

// experimental
experimentalRuntimeMode: vueOptions.experimentalRuntimeMode ?? 'runtime-dom',
Expand Down
2 changes: 1 addition & 1 deletion packages/vue-language-service/src/documentService.ts
Expand Up @@ -104,7 +104,7 @@ export function getDocumentService(
}
},
};
const vuePlugins = vue.getPlugins(ts, {}, {}, []);
const vuePlugins = vue.getPlugins(ts, '', {}, {}, []);

return {
format: format.register(context),
Expand Down