Skip to content

Commit bde0f88

Browse files
authored
(perf) avoid duplicate patching in the ts plugin (#1404)
When the tsconfig.json is updated, tsserver would reload the project and it would cause the plugin to be loaded again.
1 parent b87ea2e commit bde0f88

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

packages/typescript-plugin/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { dirname, resolve } from 'path';
2-
import { decorateLanguageService } from './language-service';
2+
import { decorateLanguageService, isPatched } from './language-service';
33
import { Logger } from './logger';
44
import { patchModuleLoader } from './module-loader';
55
import { SvelteSnapshotManager } from './svelte-snapshots';
@@ -16,6 +16,11 @@ function init(modules: { typescript: typeof ts }) {
1616
return info.languageService;
1717
}
1818

19+
if (isPatched(info.languageService)) {
20+
logger.log('Already patched');
21+
return info.languageService;
22+
}
23+
1924
configManager.updateConfigFromPluginConfig(info.config);
2025
if (configManager.getConfig().enable) {
2126
logger.log('Starting Svelte plugin');

packages/typescript-plugin/src/language-service/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ import { decorateFindReferences } from './find-references';
1010
import { decorateGetImplementation } from './implementation';
1111
import { decorateRename } from './rename';
1212

13+
const sveltePluginPatchSymbol = Symbol('sveltePluginPatchSymbol');
14+
15+
export function isPatched(ls: ts.LanguageService) {
16+
return (ls as any)[sveltePluginPatchSymbol] === true;
17+
}
18+
1319
export function decorateLanguageService(
1420
ls: ts.LanguageService,
1521
snapshotManager: SvelteSnapshotManager,
@@ -43,6 +49,11 @@ function createProxyHandler(configManager: ConfigManager): ProxyHandler<ts.Langu
4349

4450
return {
4551
get(target, p) {
52+
// always return patch symbol whether the plugin is enabled or not
53+
if (p === sveltePluginPatchSymbol) {
54+
return true;
55+
}
56+
4657
if (!configManager.getConfig().enable) {
4758
return target[p as keyof ts.LanguageService];
4859
}

0 commit comments

Comments
 (0)