diff --git a/server/src/config.ts b/server/src/config.ts index 521edaf163..e50cae4166 100644 --- a/server/src/config.ts +++ b/server/src/config.ts @@ -77,6 +77,7 @@ export interface VLSConfig { experimental: { templateInterpolationService: boolean; }; + extensions: string[]; }; } @@ -140,7 +141,8 @@ export function getDefaultVLSConfig(): VLSFullConfig { }, experimental: { templateInterpolationService: false - } + }, + extensions: [] }, css: {}, html: { diff --git a/server/src/modes/script/javascript.ts b/server/src/modes/script/javascript.ts index f5032f69ea..b8e1b36272 100644 --- a/server/src/modes/script/javascript.ts +++ b/server/src/modes/script/javascript.ts @@ -46,12 +46,7 @@ import { getComponentInfo } from './componentInfo'; import { DependencyService, RuntimeLibrary } from '../../services/dependencyService'; import { CodeActionData, CodeActionDataKind, OrganizeImportsActionData, RefactorActionData } from '../../types'; import { IServiceHost } from '../../services/typescriptService/serviceHost'; -import { - isVirtualVueTemplateFile, - isVueFile, - toCompletionItemKind, - toSymbolKind -} from '../../services/typescriptService/util'; +import { isVirtualVueTemplateFile, toCompletionItemKind, toSymbolKind } from '../../services/typescriptService/util'; import * as Previewer from './previewer'; import { isVCancellationRequested, VCancellationToken } from '../../utils/cancellationToken'; import { EnvironmentService } from '../../services/EnvironmentService'; @@ -755,7 +750,7 @@ export async function getJavascriptMode( return []; } - const oldFileIsVue = isVueFile(oldPath); + const oldFileIsVue = env.isVueFile(oldPath); const formatSettings: ts.FormatCodeSettings = getFormatCodeSettings(env.getConfig()); const preferences = getUserPreferencesByLanguageId( (sourceFile as any).scriptKind === tsModule.ScriptKind.JS ? 'javascript' : 'typescript' @@ -773,7 +768,7 @@ export async function getJavascriptMode( continue; } const doc = getSourceDoc(fileName, program); - const bothNotVueFile = !oldFileIsVue && !isVueFile(fileName); + const bothNotVueFile = !oldFileIsVue && !env.isVueFile(fileName); if (bothNotVueFile) { continue; } diff --git a/server/src/services/EnvironmentService.ts b/server/src/services/EnvironmentService.ts index 29a89a0352..eb3cacfa03 100644 --- a/server/src/services/EnvironmentService.ts +++ b/server/src/services/EnvironmentService.ts @@ -11,6 +11,7 @@ export interface EnvironmentService { getVueVersion(): VueVersion; getSnippetFolder(): string; getGlobalComponentInfos(): BasicComponentInfo[]; + isVueFile(path: string): boolean; } export function createEnvironmentService( @@ -35,6 +36,7 @@ export function createEnvironmentService( getPackagePath: () => packagePath, getVueVersion: () => inferVueVersion(packagePath), getSnippetFolder: () => snippetFolder, - getGlobalComponentInfos: () => globalComponentInfos + getGlobalComponentInfos: () => globalComponentInfos, + isVueFile: (path: string) => ['.vue', ...$config.vetur.extensions].some(ext => path.endsWith(ext)) }; } diff --git a/server/src/services/typescriptService/preprocess.ts b/server/src/services/typescriptService/preprocess.ts index b7a10c7ba7..690f0b91c2 100644 --- a/server/src/services/typescriptService/preprocess.ts +++ b/server/src/services/typescriptService/preprocess.ts @@ -14,10 +14,11 @@ import { } from './transformTemplate'; import { templateSourceMap } from './serviceHost'; import { generateSourceMap } from './sourceMap'; -import { isVirtualVueTemplateFile, isVueFile } from './util'; +import { isVirtualVueTemplateFile } from './util'; import { ChildComponent } from '../vueInfoService'; import { kebabCase, snakeCase } from 'lodash'; import { RuntimeLibrary } from '../dependencyService'; +import { EnvironmentService } from '../EnvironmentService'; const importedComponentName = '__vlsComponent'; @@ -52,7 +53,8 @@ export function parseVueTemplate(text: string): string { export function createUpdater( tsModule: RuntimeLibrary['typescript'], - allChildComponentsInfo: Map + allChildComponentsInfo: Map, + env: EnvironmentService ) { const clssf = tsModule.createLanguageServiceSourceFile; const ulssf = tsModule.updateLanguageServiceSourceFile; @@ -71,7 +73,7 @@ export function createUpdater( return; } - if (isVueFile(fileName)) { + if (env.isVueFile(fileName)) { modifyVueScript(tsModule, sourceFile); modificationTracker.add(sourceFile); return; diff --git a/server/src/services/typescriptService/serviceHost.ts b/server/src/services/typescriptService/serviceHost.ts index 09be5b0793..9611abcb05 100644 --- a/server/src/services/typescriptService/serviceHost.ts +++ b/server/src/services/typescriptService/serviceHost.ts @@ -9,7 +9,7 @@ import { getFileFsPath, getFilePath, normalizeFileNameToFsPath } from '../../uti import * as bridge from './bridge'; import { getVueSys } from './vueSys'; import { TemplateSourceMap, stringifySourceMapNodes } from './sourceMap'; -import { isVirtualVueTemplateFile, isVueFile } from './util'; +import { isVirtualVueTemplateFile } from './util'; import { logger } from '../../log'; import { ModuleResolutionCache } from './moduleResolutionCache'; import { globalScope } from './transformTemplate'; @@ -25,12 +25,13 @@ const NEWLINE = process.platform === 'win32' ? '\r\n' : '\n'; */ const allChildComponentsInfo = new Map(); -function patchTS(tsModule: RuntimeLibrary['typescript']) { +function patchTS(tsModule: RuntimeLibrary['typescript'], env: EnvironmentService) { // Patch typescript functions to insert `import Vue from 'vue'` and `new Vue` around export default. // NOTE: this is a global hack that all ts instances after is changed const { createLanguageServiceSourceFile, updateLanguageServiceSourceFile } = createUpdater( tsModule, - allChildComponentsInfo + allChildComponentsInfo, + env ); (tsModule as any).createLanguageServiceSourceFile = createLanguageServiceSourceFile; (tsModule as any).updateLanguageServiceSourceFile = updateLanguageServiceSourceFile; @@ -89,7 +90,7 @@ export function getServiceHost( env: EnvironmentService, updatedScriptRegionDocuments: LanguageModelCache ): IServiceHost { - patchTS(tsModule); + patchTS(tsModule, env); let currentScriptDoc: TextDocument; @@ -298,7 +299,7 @@ export function getServiceHost( return (tsModule as any).getScriptKindFromFileName(fileName); } - if (isVueFile(fileName)) { + if (env.isVueFile(fileName)) { const uri = URI.file(fileName); const fileFsPath = normalizeFileNameToFsPath(fileName); let doc = localScriptRegionDocuments.get(fileFsPath); @@ -354,7 +355,7 @@ export function getServiceHost( return cachedResolvedModule; } - if (!isVueFile(name)) { + if (!env.isVueFile(name)) { const tsResolvedModule = tsModule.resolveModuleName(name, containingFile, options, tsModule.sys) .resolvedModule; @@ -446,7 +447,7 @@ export function getServiceHost( } // js/ts files in workspace - if (!isVueFile(fileFsPath)) { + if (!env.isVueFile(fileFsPath)) { if (projectFileSnapshots.has(fileFsPath)) { return projectFileSnapshots.get(fileFsPath); } diff --git a/server/src/services/typescriptService/util.ts b/server/src/services/typescriptService/util.ts index abbe7e80e3..f187ab0cc9 100644 --- a/server/src/services/typescriptService/util.ts +++ b/server/src/services/typescriptService/util.ts @@ -1,10 +1,6 @@ import type ts from 'typescript'; import { CompletionItemKind, SymbolKind } from 'vscode-languageserver'; -export function isVueFile(path: string) { - return path.endsWith('.vue'); -} - /** * If the path ends with `.vue.ts`, it's a `.vue` file pre-processed by Vetur * to be used in TS Language Service diff --git a/vti/src/initParams.ts b/vti/src/initParams.ts index 4157652992..5e2f4db97b 100644 --- a/vti/src/initParams.ts +++ b/vti/src/initParams.ts @@ -75,7 +75,8 @@ function getDefaultVLSConfig() { }, experimental: { templateInterpolationService: false - } + }, + extentions: [] }, css: {}, html: {