Skip to content

Commit f18a174

Browse files
authored
fix(compiler-sfc): malformed filename on windows using path.posix.join() (#9478)
Closes: #8671, #9583 Not fixed with: #9446 Related: #9473
1 parent e422023 commit f18a174

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -481,25 +481,28 @@ describe('resolveType', () => {
481481

482482
test.runIf(process.platform === 'win32')('relative ts on Windows', () => {
483483
const files = {
484-
'C:\\Test\\foo.ts': 'export type P = { foo: number }',
485-
'C:\\Test\\bar.d.ts':
484+
'C:\\Test\\FolderA\\foo.ts': 'export type P = { foo: number }',
485+
'C:\\Test\\FolderA\\bar.d.ts':
486486
'type X = { bar: string }; export { X as Y };' +
487487
// verify that we can parse syntax that is only valid in d.ts
488-
'export const baz: boolean'
488+
'export const baz: boolean',
489+
'C:\\Test\\FolderB\\buz.ts': 'export type Z = { buz: string }'
489490
}
490491
const { props, deps } = resolve(
491492
`
492493
import { P } from './foo'
493494
import { Y as PP } from './bar'
494-
defineProps<P & PP>()
495+
import { Z as PPP } from '../FolderB/buz'
496+
defineProps<P & PP & PPP>()
495497
`,
496498
files,
497499
{},
498-
'C:\\Test\\Test.vue'
500+
'C:\\Test\\FolderA\\Test.vue'
499501
)
500502
expect(props).toStrictEqual({
501503
foo: ['Number'],
502-
bar: ['String']
504+
bar: ['String'],
505+
buz: ['String']
503506
})
504507
expect(deps && [...deps].map(normalize)).toStrictEqual(
505508
Object.keys(files).map(normalize)

packages/compiler-sfc/src/script/resolveType.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ import { parse as babelParse } from '@babel/parser'
3939
import { parse } from '../parse'
4040
import { createCache } from '../cache'
4141
import type TS from 'typescript'
42-
import { extname, dirname } from 'path'
42+
import { extname, dirname, join } from 'path'
4343
import { minimatch as isMatch } from 'minimatch'
44+
import * as process from 'process'
4445

4546
/**
4647
* TypeResolveContext is compatible with ScriptCompileContext
@@ -779,7 +780,12 @@ function importSourceToScope(
779780

780781
let resolved: string | undefined = scope.resolvedImportSources[source]
781782
if (!resolved) {
782-
if (source.startsWith('.')) {
783+
if (source.startsWith('..')) {
784+
const osSpecificJoinFn = process.platform === 'win32' ? join : joinPaths
785+
786+
const filename = osSpecificJoinFn(dirname(scope.filename), source)
787+
resolved = resolveExt(filename, fs)
788+
} else if (source.startsWith('.')) {
783789
// relative import - fast path
784790
const filename = joinPaths(dirname(scope.filename), source)
785791
resolved = resolveExt(filename, fs)

0 commit comments

Comments
 (0)