Skip to content

Commit aa1e77d

Browse files
committed
fix(compiler-sfc): properly parse d.ts files when resolving types
close #8285
1 parent 8dc8cf8 commit aa1e77d

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,10 @@ describe('resolveType', () => {
458458
test('relative ts', () => {
459459
const files = {
460460
'/foo.ts': 'export type P = { foo: number }',
461-
'/bar.d.ts': 'type X = { bar: string }; export { X as Y }'
461+
'/bar.d.ts':
462+
'type X = { bar: string }; export { X as Y };' +
463+
// verify that we can parse syntax that is only valid in d.ts
464+
'export const baz: boolean'
462465
}
463466
const { props, deps } = resolve(
464467
`

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ export class ScriptCompileContext {
145145

146146
export function resolveParserPlugins(
147147
lang: string,
148-
userPlugins?: ParserPlugin[]
148+
userPlugins?: ParserPlugin[],
149+
dts = false
149150
) {
150151
const plugins: ParserPlugin[] = []
151152
if (lang === 'jsx' || lang === 'tsx') {
@@ -156,7 +157,7 @@ export function resolveParserPlugins(
156157
userPlugins = userPlugins.filter(p => p !== 'jsx')
157158
}
158159
if (lang === 'ts' || lang === 'tsx') {
159-
plugins.push('typescript')
160+
plugins.push(['typescript', { dts }])
160161
if (!plugins.includes('decorators')) {
161162
plugins.push('decorators-legacy')
162163
}

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,11 @@ function parseFile(
933933
const ext = extname(filename)
934934
if (ext === '.ts' || ext === '.tsx') {
935935
return babelParse(content, {
936-
plugins: resolveParserPlugins(ext.slice(1), parserPlugins),
936+
plugins: resolveParserPlugins(
937+
ext.slice(1),
938+
parserPlugins,
939+
filename.endsWith('.d.ts')
940+
),
937941
sourceType: 'module'
938942
}).program.body
939943
} else if (ext === '.vue') {

0 commit comments

Comments
 (0)