Skip to content

Commit

Permalink
feat: try load svelteHTML from svelte core (#2117)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonlyu123 authored Aug 11, 2023
1 parent dddedf0 commit 2bbc673
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
20 changes: 15 additions & 5 deletions packages/language-server/src/plugins/typescript/service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
Expand Down
18 changes: 15 additions & 3 deletions packages/typescript-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 2bbc673

Please sign in to comment.