Skip to content

Commit cfd430d

Browse files
committed
fix(language-core): try handle node next module resolution
1 parent c3d0c6a commit cfd430d

File tree

1 file changed

+56
-25
lines changed

1 file changed

+56
-25
lines changed

packages/vue-language-core/src/languageModule.ts

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,44 @@ import { VueCompilerOptions } from './types';
66
import * as sharedTypes from './utils/directorySharedTypes';
77
import type * as ts from 'typescript/lib/tsserverlibrary';
88

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\u00DFa-z0-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+
942
export function createLanguageModules(
1043
ts: typeof import('typescript/lib/tsserverlibrary'),
1144
compilerOptions: ts.CompilerOptions,
1245
vueCompilerOptions: VueCompilerOptions,
1346
): embedded.LanguageModule[] {
14-
15-
patchResolveModuleNames(ts, vueCompilerOptions);
16-
1747
const vueLanguagePlugin = getDefaultVueLanguagePlugins(
1848
ts,
1949
compilerOptions,
@@ -65,6 +95,29 @@ export function createLanguageModules(
6595
}
6696
return host.getScriptSnapshot(fileName);
6797
},
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+
}
68121
};
69122
},
70123
};
@@ -80,25 +133,3 @@ export function createLanguageModules(
80133
return moduleFileDirs.map(dir => path.join(dir, sharedTypes.baseName));
81134
}
82135
}
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

Comments
 (0)