From 2bbc6738c9c586ff3c0b4a8bf993d20e589098e2 Mon Sep 17 00:00:00 2001 From: "Lyu, Wei-Da" <36730922+jasonlyu123@users.noreply.github.com> Date: Fri, 11 Aug 2023 21:51:45 +0800 Subject: [PATCH] feat: try load svelteHTML from svelte core (#2117) #2109 --- .../src/plugins/typescript/service.ts | 20 ++++++++++++++----- packages/typescript-plugin/src/index.ts | 18 ++++++++++++++--- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/packages/language-server/src/plugins/typescript/service.ts b/packages/language-server/src/plugins/typescript/service.ts index 6335c7940..40f856428 100644 --- a/packages/language-server/src/plugins/typescript/service.ts +++ b/packages/language-server/src/plugins/typescript/service.ts @@ -1,7 +1,7 @@ -import { basename, dirname, resolve } from 'path'; +import { basename, dirname, join, resolve } from 'path'; import ts from 'typescript'; import { TextDocumentContentChangeEvent } from 'vscode-languageserver-protocol'; -import { getPackageInfo, importSvelte } from '../../importPackage'; +import { getPackageInfo } from '../../importPackage'; import { Document } from '../../lib/documents'; import { configLoader } from '../../lib/documents/configLoader'; import { FileMap, FileSet } from '../../lib/documents/fileCollection'; @@ -216,11 +216,21 @@ async function createLanguageService( // Fall back to dirname svelteTsPath = __dirname; } - const VERSION = importSvelte(tsconfigPath || workspacePath).VERSION; + const sveltePackageInfo = getPackageInfo('svelte', tsconfigPath || workspacePath); + + const isSvelte3 = sveltePackageInfo.version.major === 3; + const svelteHtmlDeclaration = isSvelte3 + ? undefined + : join(sveltePackageInfo.path, 'svelte-html.d.ts'); + const svelteHtmlFallbackIfNotExist = + svelteHtmlDeclaration && tsSystem.fileExists(svelteHtmlDeclaration) + ? svelteHtmlDeclaration + : './svelte-jsx-v4.d.ts'; + const svelteTsxFiles = ( - VERSION.split('.')[0] === '3' + isSvelte3 ? ['./svelte-shims.d.ts', './svelte-jsx.d.ts', './svelte-native-jsx.d.ts'] - : ['./svelte-shims-v4.d.ts', './svelte-jsx-v4.d.ts', './svelte-native-jsx.d.ts'] + : ['./svelte-shims-v4.d.ts', svelteHtmlFallbackIfNotExist, './svelte-native-jsx.d.ts'] ).map((f) => tsSystem.resolvePath(resolve(svelteTsPath, f))); let languageServiceReducedMode = false; diff --git a/packages/typescript-plugin/src/index.ts b/packages/typescript-plugin/src/index.ts index 026c6d182..3c9653d54 100644 --- a/packages/typescript-plugin/src/index.ts +++ b/packages/typescript-plugin/src/index.ts @@ -1,4 +1,4 @@ -import { dirname, resolve } from 'path'; +import { dirname, join, resolve } from 'path'; import { decorateLanguageService, isPatched } from './language-service'; import { Logger } from './logger'; import { patchModuleLoader } from './module-loader'; @@ -192,10 +192,22 @@ function init(modules: { typescript: typeof ts }): ts.server.PluginModule { configFilePath ? { paths: [configFilePath] } : undefined ); const VERSION = require(sveltePath).VERSION; + const isSvelte3 = VERSION.split('.')[0] === '3'; + const svelteHtmlDeclaration = isSvelte3 + ? undefined + : join(dirname(sveltePath), 'svelte-html.d.ts'); + const svelteHtmlFallbackIfNotExist = + svelteHtmlDeclaration && modules.typescript.sys.fileExists(svelteHtmlDeclaration) + ? svelteHtmlDeclaration + : './svelte-jsx-v4.d.ts'; const svelteTsxFiles = ( - VERSION.split('.')[0] === '3' + isSvelte3 ? ['./svelte-shims.d.ts', './svelte-jsx.d.ts', './svelte-native-jsx.d.ts'] - : ['./svelte-shims-v4.d.ts', './svelte-jsx-v4.d.ts', './svelte-native-jsx.d.ts'] + : [ + './svelte-shims-v4.d.ts', + svelteHtmlFallbackIfNotExist, + './svelte-native-jsx.d.ts' + ] ).map((f) => modules.typescript.sys.resolvePath(resolve(svelteTsPath, f))); resolvedSvelteTsxFiles = svelteTsxFiles;