@@ -6,14 +6,44 @@ import { VueCompilerOptions } from './types';
6
6
import * as sharedTypes from './utils/directorySharedTypes' ;
7
7
import type * as ts from 'typescript/lib/tsserverlibrary' ;
8
8
9
+ // tried to do something similar to
10
+ // vue compiler-sfc
11
+ // https://github.com/vuejs/core/blob/bdf3492aee2a970ab7e73cf833d729679c731111/packages/compiler-sfc/src/script/resolveType.ts#L845
12
+ // below utils copied from compiler-sfc util script, can it be shared?
13
+ // https://github.com/vuejs/core/blob/bdf3492aee2a970ab7e73cf833d729679c731111/packages/compiler-sfc/src/script/utils.ts#LL83C1-L100C2
14
+ const identity = ( str : string ) => str ;
15
+ const fileNameLowerCaseRegExp = / [ ^ \u0130 \u0131 \u00DF a - z 0 - 9 \\ / : \- _ \. ] + / g;
16
+ const toLowerCase = ( str : string ) => str . toLowerCase ( ) ;
17
+
18
+ function toFileNameLowerCase ( x : string ) {
19
+ return fileNameLowerCaseRegExp . test ( x )
20
+ ? x . replace ( fileNameLowerCaseRegExp , toLowerCase )
21
+ : x ;
22
+ }
23
+
24
+ export function createGetCanonicalFileName ( useCaseSensitiveFileNames : boolean ) {
25
+ return useCaseSensitiveFileNames ? identity : toFileNameLowerCase ;
26
+ }
27
+ let moduleCache : ts . ModuleResolutionCache | null = null ;
28
+ function getModuleResolutionCache (
29
+ ts : typeof import ( "typescript/lib/tsserverlibrary" ) ,
30
+ options : ts . CompilerOptions
31
+ ) {
32
+ if ( moduleCache === null ) {
33
+ moduleCache = ts . createModuleResolutionCache (
34
+ process . cwd ( ) ,
35
+ createGetCanonicalFileName ( ts . sys . useCaseSensitiveFileNames ) ,
36
+ options
37
+ ) ;
38
+ }
39
+ return moduleCache ;
40
+ }
41
+
9
42
export function createLanguageModules (
10
43
ts : typeof import ( 'typescript/lib/tsserverlibrary' ) ,
11
44
compilerOptions : ts . CompilerOptions ,
12
45
vueCompilerOptions : VueCompilerOptions ,
13
46
) : embedded . LanguageModule [ ] {
14
-
15
- patchResolveModuleNames ( ts , vueCompilerOptions ) ;
16
-
17
47
const vueLanguagePlugin = getDefaultVueLanguagePlugins (
18
48
ts ,
19
49
compilerOptions ,
@@ -65,6 +95,29 @@ export function createLanguageModules(
65
95
}
66
96
return host . getScriptSnapshot ( fileName ) ;
67
97
} ,
98
+ resolveModuleNameLiterals (
99
+ moduleLiterals ,
100
+ containingFile ,
101
+ redirectedReference ,
102
+ options ,
103
+ sourceFile
104
+ ) {
105
+ return moduleLiterals . map ( ( _ , index ) => {
106
+ let moduleName = moduleLiterals [ index ] . text ;
107
+ if ( sourceFile . impliedNodeFormat === ts . ModuleKind . ESNext && moduleName . endsWith ( '.vue' ) ) {
108
+ moduleName = `${ moduleName } .js` ;
109
+ }
110
+ return ts . resolveModuleName (
111
+ moduleName ,
112
+ containingFile ,
113
+ options ,
114
+ this ,
115
+ getModuleResolutionCache ( ts , options ) ,
116
+ redirectedReference ,
117
+ sourceFile . impliedNodeFormat
118
+ )
119
+ } ) ;
120
+ }
68
121
} ;
69
122
} ,
70
123
} ;
@@ -80,25 +133,3 @@ export function createLanguageModules(
80
133
return moduleFileDirs . map ( dir => path . join ( dir , sharedTypes . baseName ) ) ;
81
134
}
82
135
}
83
-
84
- function patchResolveModuleNames (
85
- ts : typeof import ( 'typescript/lib/tsserverlibrary' ) ,
86
- vueCompilerOptions : VueCompilerOptions ,
87
- ) {
88
- try {
89
- // from https://github.com/johnsoncodehk/volar/pull/1543
90
- if ( ! ( ( ts as any ) . __vuePatchResolveModuleNames ) ) {
91
- ( ts as any ) . __vuePatchResolveModuleNames = true ;
92
- const resolveModuleNames = ts . resolveModuleName ;
93
- ts . resolveModuleName = ( ...args ) => {
94
- if ( args [ 6 ] === ts . ModuleKind . ESNext && vueCompilerOptions . extensions . some ( ext => args [ 0 ] . endsWith ( ext ) ) ) {
95
- args [ 6 ] = ts . ModuleKind . CommonJS ;
96
- }
97
- return resolveModuleNames ( ...args ) ;
98
- } ;
99
- }
100
- }
101
- catch ( e ) {
102
- // console.warn('[volar] patchResolveModuleNames failed', e);
103
- }
104
- }
0 commit comments