Skip to content

Commit 089d36d

Browse files
authored
fix: correctly resolve types from relative paths on Windows (#9446)
close #8671 close vuejs/vue-loader#2048
1 parent e9e2778 commit 089d36d

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

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

+33-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { normalize } from 'node:path'
12
import { Identifier } from '@babel/types'
23
import { SFCScriptCompileOptions, parse } from '../../src'
34
import { ScriptCompileContext } from '../../src/script/context'
@@ -478,6 +479,33 @@ describe('resolveType', () => {
478479
expect(deps && [...deps]).toStrictEqual(Object.keys(files))
479480
})
480481

482+
test.runIf(process.platform === 'win32')('relative ts on Windows', () => {
483+
const files = {
484+
'C:\\Test\\foo.ts': 'export type P = { foo: number }',
485+
'C:\\Test\\bar.d.ts':
486+
'type X = { bar: string }; export { X as Y };' +
487+
// verify that we can parse syntax that is only valid in d.ts
488+
'export const baz: boolean'
489+
}
490+
const { props, deps } = resolve(
491+
`
492+
import { P } from './foo'
493+
import { Y as PP } from './bar'
494+
defineProps<P & PP>()
495+
`,
496+
files,
497+
{},
498+
'C:\\Test\\Test.vue'
499+
)
500+
expect(props).toStrictEqual({
501+
foo: ['Number'],
502+
bar: ['String']
503+
})
504+
expect(deps && [...deps].map(normalize)).toStrictEqual(
505+
Object.keys(files).map(normalize)
506+
)
507+
})
508+
481509
// #8244
482510
test('utility type in external file', () => {
483511
const files = {
@@ -898,19 +926,20 @@ describe('resolveType', () => {
898926
function resolve(
899927
code: string,
900928
files: Record<string, string> = {},
901-
options?: Partial<SFCScriptCompileOptions>
929+
options?: Partial<SFCScriptCompileOptions>,
930+
sourceFileName: string = '/Test.vue'
902931
) {
903932
const { descriptor } = parse(`<script setup lang="ts">\n${code}\n</script>`, {
904-
filename: '/Test.vue'
933+
filename: sourceFileName
905934
})
906935
const ctx = new ScriptCompileContext(descriptor, {
907936
id: 'test',
908937
fs: {
909938
fileExists(file) {
910-
return !!files[file]
939+
return !!(files[file] ?? files[normalize(file)])
911940
},
912941
readFile(file) {
913-
return files[file]
942+
return files[file] ?? files[normalize(file)]
914943
}
915944
},
916945
...options

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ function importSourceToScope(
778778
if (!resolved) {
779779
if (source.startsWith('.')) {
780780
// relative import - fast path
781-
const filename = joinPaths(scope.filename, '..', source)
781+
const filename = joinPaths(dirname(scope.filename), source)
782782
resolved = resolveExt(filename, fs)
783783
} else {
784784
// module or aliased import - use full TS resolution, only supported in Node

0 commit comments

Comments
 (0)