@@ -3,7 +3,7 @@ import type * as ts from 'typescript';
33import * as path from 'path-browserify' ;
44import { code as typeHelpersCode } from 'vue-component-type-helpers' ;
55import { code as vue2TypeHelpersCode } from 'vue-component-type-helpers/vue2' ;
6- import { createTypeScriptLanguage } from '@volar/typescript' ;
6+ import { TypeScriptProjectHost , createLanguageServiceHost , resolveFileLanguageId } from '@volar/typescript' ;
77
88import type {
99 MetaCheckerOptions ,
@@ -71,9 +71,7 @@ function createCheckerWorker(
7171 let projectVersion = 0 ;
7272
7373 const scriptSnapshots = new Map < string , ts . IScriptSnapshot > ( ) ;
74- const projectHost : vue . TypeScriptProjectHost = {
75- ...ts . sys ,
76- configFileName,
74+ const projectHost : TypeScriptProjectHost = {
7775 getCurrentDirectory : ( ) => rootPath ,
7876 getProjectVersion : ( ) => projectVersion . toString ( ) ,
7977 getCompilationSettings : ( ) => parsedCommandLine . options ,
@@ -88,12 +86,10 @@ function createCheckerWorker(
8886 }
8987 return scriptSnapshots . get ( fileName ) ;
9088 } ,
91- scriptIdToFileName : id => id ,
92- fileNameToScriptId : id => id ,
9389 } ;
9490
9591 return {
96- ...baseCreate ( ts , projectHost , parsedCommandLine . vueOptions , checkerOptions , globalComponentName ) ,
92+ ...baseCreate ( ts , configFileName , projectHost , parsedCommandLine . vueOptions , checkerOptions , globalComponentName ) ,
9793 updateFile ( fileName : string , text : string ) {
9894 fileName = fileName . replace ( windowsPathReg , '/' ) ;
9995 scriptSnapshots . set ( fileName , ts . ScriptSnapshot . fromString ( text ) ) ;
@@ -118,16 +114,17 @@ function createCheckerWorker(
118114
119115export function baseCreate (
120116 ts : typeof import ( 'typescript' ) ,
121- host : vue . TypeScriptProjectHost ,
117+ configFileName : string | undefined ,
118+ projectHost : TypeScriptProjectHost ,
122119 vueCompilerOptions : vue . VueCompilerOptions ,
123120 checkerOptions : MetaCheckerOptions ,
124121 globalComponentName : string ,
125122) {
126123 const globalComponentSnapshot = ts . ScriptSnapshot . fromString ( '<script setup lang="ts"></script>' ) ;
127124 const metaSnapshots : Record < string , ts . IScriptSnapshot > = { } ;
128- const getScriptFileNames = host . getScriptFileNames ;
129- const getScriptSnapshot = host . getScriptSnapshot ;
130- host . getScriptFileNames = ( ) => {
125+ const getScriptFileNames = projectHost . getScriptFileNames ;
126+ const getScriptSnapshot = projectHost . getScriptSnapshot ;
127+ projectHost . getScriptFileNames = ( ) => {
131128 const names = getScriptFileNames ( ) ;
132129 return [
133130 ...names ,
@@ -136,7 +133,7 @@ export function baseCreate(
136133 getMetaFileName ( globalComponentName ) ,
137134 ] ;
138135 } ;
139- host . getScriptSnapshot = fileName => {
136+ projectHost . getScriptSnapshot = fileName => {
140137 if ( isMetaFileName ( fileName ) ) {
141138 if ( ! metaSnapshots [ fileName ] ) {
142139 metaSnapshots [ fileName ] = ts . ScriptSnapshot . fromString ( getMetaScriptContent ( fileName ) ) ;
@@ -151,20 +148,42 @@ export function baseCreate(
151148 }
152149 } ;
153150
154- const vueLanguagePlugin = vue . createVueLanguagePlugin (
151+ const vueLanguagePlugin = vue . createVueLanguagePlugin < string > (
155152 ts ,
156153 id => id ,
157154 ts . sys . useCaseSensitiveFileNames ,
158- ( ) => host . getProjectVersion ?.( ) ?? '' ,
159- ( ) => host . getScriptFileNames ( ) ,
160- host . getCompilationSettings ( ) ,
155+ ( ) => projectHost . getProjectVersion ?.( ) ?? '' ,
156+ ( ) => projectHost . getScriptFileNames ( ) ,
157+ projectHost . getCompilationSettings ( ) ,
161158 vueCompilerOptions ,
162159 ) ;
163- const language = createTypeScriptLanguage (
164- ts ,
165- [ vueLanguagePlugin ] ,
166- host ,
160+ const language = vue . createLanguage (
161+ [
162+ vueLanguagePlugin ,
163+ {
164+ getLanguageId ( fileName ) {
165+ return resolveFileLanguageId ( fileName ) ;
166+ } ,
167+ } ,
168+ ] ,
169+ new vue . FileMap ( ts . sys . useCaseSensitiveFileNames ) ,
170+ fileName => {
171+ const snapshot = projectHost . getScriptSnapshot ( fileName ) ;
172+ if ( snapshot ) {
173+ language . scripts . set ( fileName , snapshot ) ;
174+ }
175+ else {
176+ language . scripts . delete ( fileName ) ;
177+ }
178+ } ,
167179 ) ;
180+ language . typescript = {
181+ sys : ts . sys ,
182+ configFileName,
183+ asFileName : s => s ,
184+ asScriptId : s => s ,
185+ ...createLanguageServiceHost ( ts , ts . sys , language , s => s , projectHost ) ,
186+ } ;
168187 const { languageServiceHost } = language . typescript ! ;
169188 const tsLs = ts . createLanguageService ( languageServiceHost ) ;
170189
@@ -311,7 +330,7 @@ ${vueCompilerOptions.target < 3 ? vue2TypeHelpersCode : typeHelpersCode}
311330
312331 // fill defaults
313332 const printer = ts . createPrinter ( checkerOptions . printer ) ;
314- const snapshot = host . getScriptSnapshot ( componentPath ) ! ;
333+ const snapshot = projectHost . getScriptSnapshot ( componentPath ) ! ;
315334
316335 const vueFile = language . scripts . get ( componentPath ) ?. generated ?. root ;
317336 const vueDefaults = vueFile && exportName === 'default'
@@ -460,7 +479,7 @@ function createSchemaResolvers(
460479 symbolNode : ts . Expression ,
461480 { rawType, schema : options , noDeclarations } : MetaCheckerOptions ,
462481 ts : typeof import ( 'typescript' ) ,
463- language : vue . Language ,
482+ language : vue . Language < string > ,
464483) {
465484 const visited = new Set < ts . Type > ( ) ;
466485
0 commit comments