Skip to content

Commit 8236f63

Browse files
authored
fix: load user's Svelte version in TS plugin (#2351)
Necessary for Svelte 5 which contains syntax our current built-in compiler version can't handle #2297
1 parent c924fba commit 8236f63

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

packages/typescript-plugin/src/index.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import { SvelteSnapshotManager } from './svelte-snapshots';
66
import type ts from 'typescript/lib/tsserverlibrary';
77
import { ConfigManager, Configuration } from './config-manager';
88
import { ProjectSvelteFilesManager } from './project-svelte-files';
9-
import { getConfigPathForProject, isSvelteProject } from './utils';
9+
import {
10+
getConfigPathForProject,
11+
getProjectDirectory,
12+
importSvelteCompiler,
13+
isSvelteProject
14+
} from './utils';
1015

1116
function init(modules: { typescript: typeof ts }): ts.server.PluginModule {
1217
const configManager = new ConfigManager();
@@ -112,7 +117,8 @@ function init(modules: { typescript: typeof ts }): ts.server.PluginModule {
112117
info.project.projectService,
113118
svelteOptions,
114119
logger,
115-
configManager
120+
configManager,
121+
importSvelteCompiler(getProjectDirectory(info.project))
116122
);
117123

118124
const projectSvelteFilesManager = parsedCommandLine
@@ -171,12 +177,10 @@ function init(modules: { typescript: typeof ts }): ts.server.PluginModule {
171177
return [];
172178
}
173179

174-
const configFilePath = project.getCompilerOptions().configFilePath;
180+
const configFilePath = getProjectDirectory(project);
175181

176182
// Needed so the ambient definitions are known inside the tsx files
177-
const svelteTsxFiles = resolveSvelteTsxFiles(
178-
typeof configFilePath === 'string' ? configFilePath : undefined
179-
);
183+
const svelteTsxFiles = resolveSvelteTsxFiles(configFilePath);
180184

181185
if (!configFilePath) {
182186
svelteTsxFiles.forEach((file) => {

packages/typescript-plugin/src/svelte-snapshots.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,9 @@ export class SvelteSnapshotManager {
276276
private projectService: ts.server.ProjectService,
277277
private svelteOptions: { namespace: string },
278278
private logger: Logger,
279-
private configManager: ConfigManager
279+
private configManager: ConfigManager,
280+
/** undefined if no node_modules with Svelte next to tsconfig.json */
281+
private svelteCompiler: typeof import('svelte/compiler') | undefined
280282
) {
281283
this.patchProjectServiceReadFile();
282284
}
@@ -373,7 +375,10 @@ export class SvelteSnapshotManager {
373375
filename: path.split('/').pop(),
374376
isTsFile,
375377
mode: 'ts',
376-
typingsNamespace: this.svelteOptions.namespace
378+
typingsNamespace: this.svelteOptions.namespace,
379+
// Don't search for compiler from current path - could be a different one from which we have loaded the svelte2tsx globals
380+
parse: this.svelteCompiler?.parse,
381+
version: this.svelteCompiler?.VERSION
377382
});
378383
code = result.code;
379384
mapper = new SourceMapper(result.map.mappings);

packages/typescript-plugin/src/utils.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,23 @@ function hasConfigInConjunction(packageJsonPath: string, project: ts.server.Proj
279279
project.fileExists(join(dir, 'jsconfig.json'))
280280
);
281281
}
282+
283+
export function importSvelteCompiler(
284+
fromPath: string | undefined
285+
): typeof import('svelte/compiler') | undefined {
286+
if (!fromPath) return undefined;
287+
288+
try {
289+
const sveltePath = require.resolve('svelte/compiler', { paths: [fromPath] });
290+
const compiler = require(sveltePath);
291+
292+
if (compiler.VERSION.split('.')[0] === '3') {
293+
// use built-in version for Svelte 3
294+
return undefined;
295+
}
296+
297+
return compiler;
298+
} catch (e) {
299+
// ignore
300+
}
301+
}

0 commit comments

Comments
 (0)