diff --git a/packages/language-core/lib/codegen/script/componentSelf.ts b/packages/language-core/lib/codegen/script/componentSelf.ts index e62212e150..a75cb87718 100644 --- a/packages/language-core/lib/codegen/script/componentSelf.ts +++ b/packages/language-core/lib/codegen/script/componentSelf.ts @@ -1,3 +1,4 @@ +import * as path from 'path-browserify'; import type { Code } from '../../types'; import { endOfLine, generateSfcBlockSection, newLine } from '../common'; import type { TemplateCodegenContext } from '../template/context'; @@ -61,7 +62,7 @@ export function* generateComponentSelf( yield `})${endOfLine}`; // defineComponent { } else if (options.sfc.script) { - yield `let __VLS_self!: typeof import('./${options.fileBaseName}').default${endOfLine}`; + yield `let __VLS_self!: typeof import('./${path.basename(options.fileName)}').default${endOfLine}`; } else { yield `const __VLS_self = (await import('${options.vueCompilerOptions.lib}')).defineComponent({})${endOfLine}`; diff --git a/packages/language-core/lib/codegen/script/index.ts b/packages/language-core/lib/codegen/script/index.ts index c174254352..f9865e4eaa 100644 --- a/packages/language-core/lib/codegen/script/index.ts +++ b/packages/language-core/lib/codegen/script/index.ts @@ -1,4 +1,5 @@ import type { Mapping } from '@volar/language-core'; +import * as path from 'path-browserify'; import type * as ts from 'typescript'; import type { ScriptRanges } from '../../parsers/scriptRanges'; import type { ScriptSetupRanges } from '../../parsers/scriptSetupRanges'; @@ -37,7 +38,7 @@ export const codeFeatures = { }; export interface ScriptCodegenOptions { - fileBaseName: string; + fileName: string; ts: typeof ts; compilerOptions: ts.CompilerOptions; vueCompilerOptions: VueCompilerOptions; @@ -58,7 +59,11 @@ export function* generateScript(options: ScriptCodegenOptions): Generator${newLine}`; + let relativePath = path.relative(path.dirname(options.fileName), globalTypes.absolutePath); + if (relativePath !== globalTypes.absolutePath && !relativePath.startsWith('./') && !relativePath.startsWith('../')) { + relativePath = './' + relativePath; + } + yield `/// ${newLine}`; } else { yield `/// ${newLine}`; diff --git a/packages/language-core/lib/codegen/script/template.ts b/packages/language-core/lib/codegen/script/template.ts index 0f49ee93d8..9e119e18b0 100644 --- a/packages/language-core/lib/codegen/script/template.ts +++ b/packages/language-core/lib/codegen/script/template.ts @@ -1,3 +1,4 @@ +import * as path from 'path-browserify'; import type * as ts from 'typescript'; import type { Code } from '../../types'; import { getSlotsPropertyName, hyphenateTag } from '../../utils/shared'; @@ -13,7 +14,7 @@ function* generateTemplateCtx(options: ScriptCodegenOptions): Generator { exps.push(`{} as InstanceType<__VLS_PickNotAny {}>>`); - if (options.vueCompilerOptions.petiteVueExtensions.some(ext => options.fileBaseName.endsWith(ext))) { + if (options.vueCompilerOptions.petiteVueExtensions.some(ext => options.fileName.endsWith(ext))) { exps.push(`globalThis`); } if (options.sfc.styles.some(style => style.module)) { @@ -55,7 +56,8 @@ function* generateTemplateComponents(options: ScriptCodegenOptions): Generator