Skip to content

Commit 21a0b30

Browse files
fix(language-core): reference global types file with relative path (#4966)
1 parent 1ef17d0 commit 21a0b30

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

packages/language-core/lib/codegen/script/componentSelf.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as path from 'path-browserify';
12
import type { Code } from '../../types';
23
import { endOfLine, generateSfcBlockSection, newLine } from '../common';
34
import type { TemplateCodegenContext } from '../template/context';
@@ -61,7 +62,7 @@ export function* generateComponentSelf(
6162
yield `})${endOfLine}`; // defineComponent {
6263
}
6364
else if (options.sfc.script) {
64-
yield `let __VLS_self!: typeof import('./${options.fileBaseName}').default${endOfLine}`;
65+
yield `let __VLS_self!: typeof import('./${path.basename(options.fileName)}').default${endOfLine}`;
6566
}
6667
else {
6768
yield `const __VLS_self = (await import('${options.vueCompilerOptions.lib}')).defineComponent({})${endOfLine}`;

packages/language-core/lib/codegen/script/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Mapping } from '@volar/language-core';
2+
import * as path from 'path-browserify';
23
import type * as ts from 'typescript';
34
import type { ScriptRanges } from '../../parsers/scriptRanges';
45
import type { ScriptSetupRanges } from '../../parsers/scriptSetupRanges';
@@ -37,7 +38,7 @@ export const codeFeatures = {
3738
};
3839

3940
export interface ScriptCodegenOptions {
40-
fileBaseName: string;
41+
fileName: string;
4142
ts: typeof ts;
4243
compilerOptions: ts.CompilerOptions;
4344
vueCompilerOptions: VueCompilerOptions;
@@ -58,7 +59,11 @@ export function* generateScript(options: ScriptCodegenOptions): Generator<Code,
5859
if (options.vueCompilerOptions.__setupedGlobalTypes) {
5960
const globalTypes = options.vueCompilerOptions.__setupedGlobalTypes;
6061
if (typeof globalTypes === 'object') {
61-
yield `/// <reference types="${globalTypes.absolutePath}" />${newLine}`;
62+
let relativePath = path.relative(path.dirname(options.fileName), globalTypes.absolutePath);
63+
if (relativePath !== globalTypes.absolutePath && !relativePath.startsWith('./') && !relativePath.startsWith('../')) {
64+
relativePath = './' + relativePath;
65+
}
66+
yield `/// <reference types="${relativePath}" />${newLine}`;
6267
}
6368
else {
6469
yield `/// <reference types=".vue-global-types/${options.vueCompilerOptions.lib}_${options.vueCompilerOptions.target}_${options.vueCompilerOptions.strictTemplates}.d.ts" />${newLine}`;

packages/language-core/lib/codegen/script/template.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as path from 'path-browserify';
12
import type * as ts from 'typescript';
23
import type { Code } from '../../types';
34
import { getSlotsPropertyName, hyphenateTag } from '../../utils/shared';
@@ -13,7 +14,7 @@ function* generateTemplateCtx(options: ScriptCodegenOptions): Generator<Code> {
1314

1415
exps.push(`{} as InstanceType<__VLS_PickNotAny<typeof __VLS_self, new () => {}>>`);
1516

16-
if (options.vueCompilerOptions.petiteVueExtensions.some(ext => options.fileBaseName.endsWith(ext))) {
17+
if (options.vueCompilerOptions.petiteVueExtensions.some(ext => options.fileName.endsWith(ext))) {
1718
exps.push(`globalThis`);
1819
}
1920
if (options.sfc.styles.some(style => style.module)) {
@@ -55,7 +56,8 @@ function* generateTemplateComponents(options: ScriptCodegenOptions): Generator<C
5556
nameType = options.sfc.script.content.substring(nameOption.start, nameOption.end);
5657
}
5758
else if (options.sfc.scriptSetup) {
58-
nameType = `'${options.scriptSetupRanges?.options.name ?? options.fileBaseName.substring(0, options.fileBaseName.lastIndexOf('.'))}'`;
59+
const baseName = path.basename(options.fileName);
60+
nameType = `'${options.scriptSetupRanges?.options.name ?? baseName.substring(0, baseName.lastIndexOf('.'))}'`;
5961
}
6062
if (nameType) {
6163
exps.push(`{} as {

packages/language-core/lib/plugins/vue-tsx.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { Mapping } from '@volar/language-core';
22
import { computed } from 'alien-signals';
3-
import { posix as path } from 'path-browserify';
43
import { generateScript } from '../codegen/script';
54
import { generateTemplate } from '../codegen/template';
65
import { parseScriptRanges } from '../parsers/scriptRanges';
@@ -180,7 +179,7 @@ function createTsx(
180179
let generatedLength = 0;
181180
const codegen = generateScript({
182181
ts,
183-
fileBaseName: path.basename(fileName),
182+
fileName,
184183
sfc: _sfc,
185184
lang: lang.get(),
186185
scriptRanges: scriptRanges.get(),

0 commit comments

Comments
 (0)