Skip to content

Commit

Permalink
feat: add resolveTemplateCompilerOptions api for VueLanguagePlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Oct 11, 2022
1 parent ec5946d commit 079e9ba
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
Expand Up @@ -14,16 +14,20 @@ const plugin: VueLanguagePlugin = ({ modules, vueCompilerOptions }) => {

version: 1,

resolveTemplateCompilerOptions(options) {
return {
...options,
...vueCompilerOptions.experimentalTemplateCompilerOptions,
};
},

compileSFCTemplate(lang, template, options) {

if (lang === 'html') {

const compiler = modules['@vue/compiler-dom'];

return compiler.compile(template, {
...options,
...vueCompilerOptions.experimentalTemplateCompilerOptions,
});
return compiler.compile(template, options);
}
},

Expand Down
22 changes: 15 additions & 7 deletions vue-language-tools/vue-language-core/src/sourceFile.ts
Expand Up @@ -123,18 +123,26 @@ export class VueSourceFile implements SourceFile {
}
}

const errors: CompilerDom.CompilerError[] = [];
const warnings: CompilerDom.CompilerError[] = [];
let options: CompilerDom.CompilerOptions = {
onError: (err: CompilerDom.CompilerError) => errors.push(err),
onWarn: (err: CompilerDom.CompilerError) => warnings.push(err),
expressionPlugins: ['typescript'],
};

for (const plugin of plugins) {
if (plugin.resolveTemplateCompilerOptions) {
options = plugin.resolveTemplateCompilerOptions(options);
}
}

for (const plugin of plugins) {

const errors: CompilerDom.CompilerError[] = [];
const warnings: CompilerDom.CompilerError[] = [];
let result: CompilerDom.CodegenResult | undefined;

try {
result = plugin.compileSFCTemplate?.(sourceFile.sfc.template.lang, sourceFile.sfc.template.content, {
onError: (err: CompilerDom.CompilerError) => errors.push(err),
onWarn: (err: CompilerDom.CompilerError) => warnings.push(err),
expressionPlugins: ['typescript'],
});
result = plugin.compileSFCTemplate?.(sourceFile.sfc.template.lang, sourceFile.sfc.template.content, options);
}
catch (e) {
const err = e as CompilerDom.CompilerError;
Expand Down
3 changes: 2 additions & 1 deletion vue-language-tools/vue-language-core/src/types.ts
Expand Up @@ -46,7 +46,8 @@ export type VueLanguagePlugin = (ctx: {
order?: number;
parseSFC?(fileName: string, content: string): SFCParseResult | undefined;
updateSFC?(oldResult: SFCParseResult, textChange: { start: number, end: number, newText: string; }): SFCParseResult | undefined;
compileSFCTemplate?(lang: string, template: string, options?: CompilerDom.CompilerOptions): CompilerDom.CodegenResult | undefined;
resolveTemplateCompilerOptions?(options: CompilerDom.CompilerOptions): CompilerDom.CompilerOptions;
compileSFCTemplate?(lang: string, template: string, options: CompilerDom.CompilerOptions): CompilerDom.CodegenResult | undefined;
updateSFCTemplate?(oldResult: CompilerDom.CodegenResult, textChange: { start: number, end: number, newText: string; }): CompilerDom.CodegenResult | undefined;
getEmbeddedFileNames?(fileName: string, sfc: Sfc): string[];
resolveEmbeddedFile?(fileName: string, sfc: Sfc, embeddedFile: VueEmbeddedFile): void;
Expand Down
3 changes: 1 addition & 2 deletions vue-language-tools/vue-language-plugin-pug/src/index.ts
Expand Up @@ -2,7 +2,7 @@ import type { VueLanguagePlugin } from '@volar/vue-language-core';
import * as pug from '@volar/pug-language-service';
import { SourceMapBase } from '@volar/source-map';

const plugin: VueLanguagePlugin = ({ modules, vueCompilerOptions }) => {
const plugin: VueLanguagePlugin = ({ modules }) => {

return {

Expand All @@ -22,7 +22,6 @@ const plugin: VueLanguagePlugin = ({ modules, vueCompilerOptions }) => {
const compiler = modules['@vue/compiler-dom'];
const completed = compiler.compile(pugFile.htmlCode, {
...options,
...vueCompilerOptions.experimentalTemplateCompilerOptions,
onWarn(warning) {
options?.onWarn?.(createProxyObject(warning));
},
Expand Down

0 comments on commit 079e9ba

Please sign in to comment.