From 01123bf1f7451eecb1e463f584c896afca56edca Mon Sep 17 00:00:00 2001 From: Bogdan Kolesnyk Date: Fri, 27 Oct 2023 16:07:44 +0200 Subject: [PATCH] fix(compiler-sfc): malformed filename on windows using path.posix.join() --- .../__tests__/compileScript/resolveType.spec.ts | 15 +++++++++------ packages/compiler-sfc/src/script/resolveType.ts | 10 ++++++++-- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts b/packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts index fc600f1a518..5f421708af0 100644 --- a/packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts +++ b/packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts @@ -481,25 +481,28 @@ describe('resolveType', () => { test.runIf(process.platform === 'win32')('relative ts on Windows', () => { const files = { - 'C:\\Test\\foo.ts': 'export type P = { foo: number }', - 'C:\\Test\\bar.d.ts': + 'C:\\Test\\FolderA\\foo.ts': 'export type P = { foo: number }', + 'C:\\Test\\FolderA\\bar.d.ts': 'type X = { bar: string }; export { X as Y };' + // verify that we can parse syntax that is only valid in d.ts - 'export const baz: boolean' + 'export const baz: boolean', + 'C:\\Test\\FolderB\\buz.ts': 'export type Z = { buz: string }' } const { props, deps } = resolve( ` import { P } from './foo' import { Y as PP } from './bar' - defineProps

() + import { Z as PPP } from '../FolderB/buz' + defineProps

() `, files, {}, - 'C:\\Test\\Test.vue' + 'C:\\Test\\FolderA\\Test.vue' ) expect(props).toStrictEqual({ foo: ['Number'], - bar: ['String'] + bar: ['String'], + buz: ['String'] }) expect(deps && [...deps].map(normalize)).toStrictEqual( Object.keys(files).map(normalize) diff --git a/packages/compiler-sfc/src/script/resolveType.ts b/packages/compiler-sfc/src/script/resolveType.ts index 215081dc0b7..7756e41a394 100644 --- a/packages/compiler-sfc/src/script/resolveType.ts +++ b/packages/compiler-sfc/src/script/resolveType.ts @@ -39,8 +39,9 @@ import { parse as babelParse } from '@babel/parser' import { parse } from '../parse' import { createCache } from '../cache' import type TS from 'typescript' -import { extname, dirname } from 'path' +import { extname, dirname, join } from 'path' import { minimatch as isMatch } from 'minimatch' +import * as process from 'process' /** * TypeResolveContext is compatible with ScriptCompileContext @@ -776,7 +777,12 @@ function importSourceToScope( let resolved: string | undefined = scope.resolvedImportSources[source] if (!resolved) { - if (source.startsWith('.')) { + if (source.startsWith('..')) { + const osSpecificJoinFn = process.platform === 'win32' ? join : joinPaths + + const filename = osSpecificJoinFn(dirname(scope.filename), source) + resolved = resolveExt(filename, fs) + } else if (source.startsWith('.')) { // relative import - fast path const filename = joinPaths(dirname(scope.filename), source) resolved = resolveExt(filename, fs)