From 34106bc9c715247211273bb9c64712f04bd4879d Mon Sep 17 00:00:00 2001 From: liudaodanOo <75828211+liudaodanOo@users.noreply.github.com> Date: Tue, 9 Apr 2024 16:14:11 +0800 Subject: [PATCH] fix(compiler-sfc): also search for `.tsx` when type import's extension is omitted (#10637) Co-authored-by: liuxiaofei Closes #10635 --- .../compileScript/resolveType.spec.ts | 21 +++++++++++++++++++ .../compiler-sfc/src/script/resolveType.ts | 2 ++ 2 files changed, 23 insertions(+) diff --git a/packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts b/packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts index 0c5c95cd17f..f3be58a301c 100644 --- a/packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts +++ b/packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts @@ -561,6 +561,27 @@ describe('resolveType', () => { expect(deps && [...deps]).toStrictEqual(Object.keys(files)) }) + // #10635 + test('relative tsx', () => { + const files = { + '/foo.tsx': 'export type P = { foo: number }', + '/bar/index.tsx': 'export type PP = { bar: string }', + } + const { props, deps } = resolve( + ` + import { P } from './foo' + import { PP } from './bar' + defineProps

() + `, + files, + ) + expect(props).toStrictEqual({ + foo: ['Number'], + bar: ['String'], + }) + expect(deps && [...deps]).toStrictEqual(Object.keys(files)) + }) + test.runIf(process.platform === 'win32')('relative ts on Windows', () => { const files = { 'C:\\Test\\FolderA\\foo.ts': 'export type P = { foo: number }', diff --git a/packages/compiler-sfc/src/script/resolveType.ts b/packages/compiler-sfc/src/script/resolveType.ts index 968c168ddb9..f6e291791a8 100644 --- a/packages/compiler-sfc/src/script/resolveType.ts +++ b/packages/compiler-sfc/src/script/resolveType.ts @@ -956,8 +956,10 @@ function resolveExt(filename: string, fs: FS) { return ( tryResolve(filename) || tryResolve(filename + `.ts`) || + tryResolve(filename + `.tsx`) || tryResolve(filename + `.d.ts`) || tryResolve(joinPaths(filename, `index.ts`)) || + tryResolve(joinPaths(filename, `index.tsx`)) || tryResolve(joinPaths(filename, `index.d.ts`)) ) }