Skip to content

Commit 2bbc673

Browse files
authored
feat: try load svelteHTML from svelte core (#2117)
#2109
1 parent dddedf0 commit 2bbc673

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

packages/language-server/src/plugins/typescript/service.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { basename, dirname, resolve } from 'path';
1+
import { basename, dirname, join, resolve } from 'path';
22
import ts from 'typescript';
33
import { TextDocumentContentChangeEvent } from 'vscode-languageserver-protocol';
4-
import { getPackageInfo, importSvelte } from '../../importPackage';
4+
import { getPackageInfo } from '../../importPackage';
55
import { Document } from '../../lib/documents';
66
import { configLoader } from '../../lib/documents/configLoader';
77
import { FileMap, FileSet } from '../../lib/documents/fileCollection';
@@ -216,11 +216,21 @@ async function createLanguageService(
216216
// Fall back to dirname
217217
svelteTsPath = __dirname;
218218
}
219-
const VERSION = importSvelte(tsconfigPath || workspacePath).VERSION;
219+
const sveltePackageInfo = getPackageInfo('svelte', tsconfigPath || workspacePath);
220+
221+
const isSvelte3 = sveltePackageInfo.version.major === 3;
222+
const svelteHtmlDeclaration = isSvelte3
223+
? undefined
224+
: join(sveltePackageInfo.path, 'svelte-html.d.ts');
225+
const svelteHtmlFallbackIfNotExist =
226+
svelteHtmlDeclaration && tsSystem.fileExists(svelteHtmlDeclaration)
227+
? svelteHtmlDeclaration
228+
: './svelte-jsx-v4.d.ts';
229+
220230
const svelteTsxFiles = (
221-
VERSION.split('.')[0] === '3'
231+
isSvelte3
222232
? ['./svelte-shims.d.ts', './svelte-jsx.d.ts', './svelte-native-jsx.d.ts']
223-
: ['./svelte-shims-v4.d.ts', './svelte-jsx-v4.d.ts', './svelte-native-jsx.d.ts']
233+
: ['./svelte-shims-v4.d.ts', svelteHtmlFallbackIfNotExist, './svelte-native-jsx.d.ts']
224234
).map((f) => tsSystem.resolvePath(resolve(svelteTsPath, f)));
225235

226236
let languageServiceReducedMode = false;

packages/typescript-plugin/src/index.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { dirname, resolve } from 'path';
1+
import { dirname, join, resolve } from 'path';
22
import { decorateLanguageService, isPatched } from './language-service';
33
import { Logger } from './logger';
44
import { patchModuleLoader } from './module-loader';
@@ -192,10 +192,22 @@ function init(modules: { typescript: typeof ts }): ts.server.PluginModule {
192192
configFilePath ? { paths: [configFilePath] } : undefined
193193
);
194194
const VERSION = require(sveltePath).VERSION;
195+
const isSvelte3 = VERSION.split('.')[0] === '3';
196+
const svelteHtmlDeclaration = isSvelte3
197+
? undefined
198+
: join(dirname(sveltePath), 'svelte-html.d.ts');
199+
const svelteHtmlFallbackIfNotExist =
200+
svelteHtmlDeclaration && modules.typescript.sys.fileExists(svelteHtmlDeclaration)
201+
? svelteHtmlDeclaration
202+
: './svelte-jsx-v4.d.ts';
195203
const svelteTsxFiles = (
196-
VERSION.split('.')[0] === '3'
204+
isSvelte3
197205
? ['./svelte-shims.d.ts', './svelte-jsx.d.ts', './svelte-native-jsx.d.ts']
198-
: ['./svelte-shims-v4.d.ts', './svelte-jsx-v4.d.ts', './svelte-native-jsx.d.ts']
206+
: [
207+
'./svelte-shims-v4.d.ts',
208+
svelteHtmlFallbackIfNotExist,
209+
'./svelte-native-jsx.d.ts'
210+
]
199211
).map((f) => modules.typescript.sys.resolvePath(resolve(svelteTsPath, f)));
200212

201213
resolvedSvelteTsxFiles = svelteTsxFiles;

0 commit comments

Comments
 (0)