Reputation: 1892
How can I use the TypeScript compiler API to instantiate a languageService with language service plugins? I thought that createLanguageService
would do the trick, but it doesn't seem to load plugins.
Is this wrapped up in tsserver somewhere? Ideally I want to run everything in-process. Is there some sort of ServerHost
I need to instantiate to give the languageService
access to require()
node_modules?
My motivation: I want to programmatically apply plugin quick-fixes to a codebase from a CLI tool I'm building.
Upvotes: 2
Views: 514
Reputation: 1892
I think I figured it out. import * as ts from 'typescript/lib/tsserverlibrary';
(same import used when authoring plugins) and create a new ts.server.ProjectService
. Then open a source file via projectService.openClientFile
which will cause the service to automatically pick up your tsconfig and create a new ConfiguredProject
which you can retrieve via various methods. Then configuredProject.getLanguageService()
.
You have to set serverHost.require
or else the service can't load plugins and they'll be skipped. I copied TS's internal implementation from src/server/server.ts
I'm leaving out a lot of messy details, but these are the steps. Intellisense tells you everything else you need to do.
Upvotes: 4