@@ -21,8 +21,8 @@ import * as interpolationDecorators from './lib/interpolationDecorators';
2121import * as reactivityVisualization from './lib/reactivityVisualization' ;
2222import * as welcome from './lib/welcome' ;
2323
24- let client : lsp . BaseLanguageClient | undefined ;
25- let needRestart = false ;
24+ const serverPath = resolveServerPath ( ) ;
25+ const neededRestart = ! patchTypeScriptExtension ( ) ;
2626
2727for (
2828 const incompatibleExtensionId of [
4444}
4545
4646export = defineExtension ( ( ) => {
47+ let client : lsp . BaseLanguageClient | undefined ;
48+
4749 const context = extensionContext . value ! ;
4850 const volarLabs = createLabsInfo ( ) ;
4951 const activeTextEditor = useActiveTextEditor ( ) ;
@@ -59,7 +61,7 @@ export = defineExtension(() => {
5961
6062 nextTick ( ( ) => stop ( ) ) ;
6163
62- if ( needRestart ) {
64+ if ( neededRestart ) {
6365 vscode . window . showInformationMessage (
6466 'Please restart the extension host to activate Vue support.' ,
6567 'Restart Extension Host' ,
@@ -75,9 +77,12 @@ export = defineExtension(() => {
7577 return ;
7678 }
7779
78- watch ( ( ) => config . server . includeLanguages , async ( ) => {
80+ watch ( ( ) => [
81+ config . server . path ,
82+ config . server . includeLanguages ,
83+ ] , async ( ) => {
7984 const reload = await vscode . window . showInformationMessage (
80- 'Please restart extension host to apply the new language settings.' ,
85+ 'Please restart extension host to apply the new server settings.' ,
8186 'Restart Extension Host' ,
8287 ) ;
8388 if ( reload ) {
@@ -95,7 +100,14 @@ export = defineExtension(() => {
95100 ) ;
96101 }
97102
98- volarLabs . addLanguageClient ( client = launch ( context ) ) ;
103+ if ( config . server . path && ! serverPath ) {
104+ vscode . window . showErrorMessage ( 'Cannot find @vue/language-server.' ) ;
105+ return ;
106+ }
107+
108+ client = launch ( serverPath ?? vscode . Uri . joinPath ( context . extensionUri , 'dist' , 'language-server.js' ) . fsPath ) ;
109+
110+ volarLabs . addLanguageClient ( client ) ;
99111
100112 const selectors = config . server . includeLanguages ;
101113
@@ -123,19 +135,18 @@ export = defineExtension(() => {
123135 return volarLabs . extensionExports ;
124136} ) ;
125137
126- function launch ( context : vscode . ExtensionContext ) {
127- const serverModule = vscode . Uri . joinPath ( context . extensionUri , 'dist' , 'language-server.js' ) ;
138+ function launch ( serverPath : string ) {
128139 const client = new lsp . LanguageClient (
129140 'vue' ,
130141 'Vue' ,
131142 {
132143 run : {
133- module : serverModule . fsPath ,
144+ module : serverPath ,
134145 transport : lsp . TransportKind . ipc ,
135146 options : { } ,
136147 } ,
137148 debug : {
138- module : serverModule . fsPath ,
149+ module : serverPath ,
139150 transport : lsp . TransportKind . ipc ,
140151 options : { execArgv : [ '--nolazy' , '--inspect=' + 6009 ] } ,
141152 } ,
@@ -179,21 +190,52 @@ function launch(context: vscode.ExtensionContext) {
179190 return client ;
180191}
181192
182- const tsExtension = vscode . extensions . getExtension ( 'vscode.typescript-language-features' ) ! ;
183- if ( tsExtension . isActive ) {
184- needRestart = true ;
193+ function resolveServerPath ( ) {
194+ const tsPluginPackPath = path . join ( __dirname , '..' , 'node_modules' , 'vue-typescript-plugin-pack' , 'index.js' ) ;
195+
196+ if ( ! config . server . path ) {
197+ fs . writeFileSync ( tsPluginPackPath , `module.exports = require("../../dist/typescript-plugin.js");` ) ;
198+ return ;
199+ }
200+
201+ if ( path . isAbsolute ( config . server . path ) ) {
202+ const entryFile = require . resolve ( './index.js' , { paths : [ config . server . path ] } ) ;
203+ const tsPluginPath = require . resolve ( '@vue/typescript-plugin' , { paths : [ path . dirname ( entryFile ) ] } ) ;
204+ fs . writeFileSync ( tsPluginPackPath , `module.exports = require("${ tsPluginPath } ");` ) ;
205+ return entryFile ;
206+ }
207+
208+ for ( const { uri } of vscode . workspace . workspaceFolders ?? [ ] ) {
209+ if ( uri . scheme !== 'file' ) {
210+ continue ;
211+ }
212+ try {
213+ const serverPath = path . join ( uri . fsPath , config . server . path ) ;
214+ const entryFile = require . resolve ( './index.js' , { paths : [ serverPath ] } ) ;
215+ const tsPluginPath = require . resolve ( '@vue/typescript-plugin' , { paths : [ path . dirname ( entryFile ) ] } ) ;
216+ fs . writeFileSync ( tsPluginPackPath , `module.exports = require("${ tsPluginPath } ");` ) ;
217+ return entryFile ;
218+ }
219+ catch { }
220+ }
185221}
186- else {
222+
223+ function patchTypeScriptExtension ( ) {
224+ const tsExtension = vscode . extensions . getExtension ( 'vscode.typescript-language-features' ) ! ;
225+ if ( tsExtension . isActive ) {
226+ return false ;
227+ }
228+
187229 const fs = require ( 'node:fs' ) ;
188230 const readFileSync = fs . readFileSync ;
189231 const extensionJsPath = require . resolve ( './dist/extension.js' , { paths : [ tsExtension . extensionPath ] } ) ;
190232 const { publisher, name } = require ( './package.json' ) ;
191233 const vueExtension = vscode . extensions . getExtension ( `${ publisher } .${ name } ` ) ! ;
192- const vuePluginName = 'vue-typescript-plugin-pack' ;
234+ const tsPluginName = 'vue-typescript-plugin-pack' ;
193235
194236 vueExtension . packageJSON . contributes . typescriptServerPlugins = [
195237 {
196- name : vuePluginName ,
238+ name : tsPluginName ,
197239 enableForWorkspaceTypeScriptVersions : true ,
198240 configNamespace : 'typescript' ,
199241 languages : config . server . includeLanguages ,
@@ -218,7 +260,7 @@ else {
218260 // sort plugins for johnsoncodehk.tsslint, zardoy.ts-essential-plugins
219261 text = text . replace (
220262 '"--globalPlugins",i.plugins' ,
221- s => s + `.sort((a,b)=>(b.name==="${ vuePluginName } "?-1:0)-(a.name==="${ vuePluginName } "?-1:0))` ,
263+ s => s + `.sort((a,b)=>(b.name==="${ tsPluginName } "?-1:0)-(a.name==="${ tsPluginName } "?-1:0))` ,
222264 ) ;
223265
224266 return text ;
@@ -232,4 +274,5 @@ else {
232274 const patchedModule = require ( extensionJsPath ) ;
233275 Object . assign ( loadedModule . exports , patchedModule ) ;
234276 }
277+ return true ;
235278}
0 commit comments