Skip to content

Commit 8696e34

Browse files
authored
fix(compiler-sfc): support ${configDir} in paths for TypeScript 5.5+ (#13491)
close #13484
1 parent 93ba107 commit 8696e34

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,45 @@ describe('resolveType', () => {
11981198
expect(deps && [...deps]).toStrictEqual(['/user.ts'])
11991199
})
12001200

1201+
// #13484
1202+
test('ts module resolve w/ project reference & extends & ${configDir}', () => {
1203+
const files = {
1204+
'/tsconfig.json': JSON.stringify({
1205+
files: [],
1206+
references: [{ path: './tsconfig.app.json' }],
1207+
}),
1208+
'/tsconfig.app.json': JSON.stringify({
1209+
extends: ['./tsconfigs/base.json'],
1210+
}),
1211+
'/tsconfigs/base.json': JSON.stringify({
1212+
compilerOptions: {
1213+
paths: {
1214+
'@/*': ['${configDir}/src/*'],
1215+
},
1216+
},
1217+
include: ['${configDir}/src/**/*.ts', '${configDir}/src/**/*.vue'],
1218+
}),
1219+
'/src/types.ts':
1220+
'export type BaseProps = { foo?: string, bar?: string }',
1221+
}
1222+
1223+
const { props, deps } = resolve(
1224+
`
1225+
import { BaseProps } from '@/types.ts';
1226+
defineProps<BaseProps>()
1227+
`,
1228+
files,
1229+
{},
1230+
'/src/components/Foo.vue',
1231+
)
1232+
1233+
expect(props).toStrictEqual({
1234+
foo: ['String'],
1235+
bar: ['String'],
1236+
})
1237+
expect(deps && [...deps]).toStrictEqual(['/src/types.ts'])
1238+
})
1239+
12011240
test('ts module resolve w/ project reference folder', () => {
12021241
const files = {
12031242
'/tsconfig.json': JSON.stringify({

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,14 @@ function resolveWithTS(
10291029
if (configs.length === 1) {
10301030
matchedConfig = configs[0]
10311031
} else {
1032+
const [major, minor] = ts.versionMajorMinor.split('.').map(Number)
1033+
const getPattern = (base: string, p: string) => {
1034+
// ts 5.5+ supports ${configDir} in paths
1035+
const supportsConfigDir = major > 5 || (major === 5 && minor >= 5)
1036+
return p.startsWith('${configDir}') && supportsConfigDir
1037+
? normalizePath(p.replace('${configDir}', dirname(configPath!)))
1038+
: joinPaths(base, p)
1039+
}
10321040
// resolve which config matches the current file
10331041
for (const c of configs) {
10341042
const base = normalizePath(
@@ -1039,11 +1047,11 @@ function resolveWithTS(
10391047
const excluded: string[] | undefined = c.config.raw?.exclude
10401048
if (
10411049
(!included && (!base || containingFile.startsWith(base))) ||
1042-
included?.some(p => isMatch(containingFile, joinPaths(base, p)))
1050+
included?.some(p => isMatch(containingFile, getPattern(base, p)))
10431051
) {
10441052
if (
10451053
excluded &&
1046-
excluded.some(p => isMatch(containingFile, joinPaths(base, p)))
1054+
excluded.some(p => isMatch(containingFile, getPattern(base, p)))
10471055
) {
10481056
continue
10491057
}

0 commit comments

Comments
 (0)