Skip to content

Commit

Permalink
fix(language-core): reference global types file with relative path (v…
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk authored Oct 30, 2024
1 parent 1ef17d0 commit 21a0b30
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
3 changes: 2 additions & 1 deletion packages/language-core/lib/codegen/script/componentSelf.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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}`;
Expand Down
9 changes: 7 additions & 2 deletions packages/language-core/lib/codegen/script/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -37,7 +38,7 @@ export const codeFeatures = {
};

export interface ScriptCodegenOptions {
fileBaseName: string;
fileName: string;
ts: typeof ts;
compilerOptions: ts.CompilerOptions;
vueCompilerOptions: VueCompilerOptions;
Expand All @@ -58,7 +59,11 @@ export function* generateScript(options: ScriptCodegenOptions): Generator<Code,
if (options.vueCompilerOptions.__setupedGlobalTypes) {
const globalTypes = options.vueCompilerOptions.__setupedGlobalTypes;
if (typeof globalTypes === 'object') {
yield `/// <reference types="${globalTypes.absolutePath}" />${newLine}`;
let relativePath = path.relative(path.dirname(options.fileName), globalTypes.absolutePath);
if (relativePath !== globalTypes.absolutePath && !relativePath.startsWith('./') && !relativePath.startsWith('../')) {
relativePath = './' + relativePath;
}
yield `/// <reference types="${relativePath}" />${newLine}`;
}
else {
yield `/// <reference types=".vue-global-types/${options.vueCompilerOptions.lib}_${options.vueCompilerOptions.target}_${options.vueCompilerOptions.strictTemplates}.d.ts" />${newLine}`;
Expand Down
6 changes: 4 additions & 2 deletions packages/language-core/lib/codegen/script/template.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -13,7 +14,7 @@ function* generateTemplateCtx(options: ScriptCodegenOptions): Generator<Code> {

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

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)) {
Expand Down Expand Up @@ -55,7 +56,8 @@ function* generateTemplateComponents(options: ScriptCodegenOptions): Generator<C
nameType = options.sfc.script.content.substring(nameOption.start, nameOption.end);
}
else if (options.sfc.scriptSetup) {
nameType = `'${options.scriptSetupRanges?.options.name ?? options.fileBaseName.substring(0, options.fileBaseName.lastIndexOf('.'))}'`;
const baseName = path.basename(options.fileName);
nameType = `'${options.scriptSetupRanges?.options.name ?? baseName.substring(0, baseName.lastIndexOf('.'))}'`;
}
if (nameType) {
exps.push(`{} as {
Expand Down
3 changes: 1 addition & 2 deletions packages/language-core/lib/plugins/vue-tsx.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Mapping } from '@volar/language-core';
import { computed } from 'alien-signals';
import { posix as path } from 'path-browserify';
import { generateScript } from '../codegen/script';
import { generateTemplate } from '../codegen/template';
import { parseScriptRanges } from '../parsers/scriptRanges';
Expand Down Expand Up @@ -180,7 +179,7 @@ function createTsx(
let generatedLength = 0;
const codegen = generateScript({
ts,
fileBaseName: path.basename(fileName),
fileName,
sfc: _sfc,
lang: lang.get(),
scriptRanges: scriptRanges.get(),
Expand Down

0 comments on commit 21a0b30

Please sign in to comment.