Skip to content

Commit

Permalink
fix(compiler-sfc): handle ts files with relative imports with .js ext…
Browse files Browse the repository at this point in the history
…ension

close #8339
  • Loading branch information
yyx990803 committed May 17, 2023
1 parent f69dbab commit b36addd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,26 @@ describe('resolveType', () => {
expect(deps && [...deps]).toStrictEqual(Object.keys(files))
})

// #8339
test('relative, .js import', () => {
const files = {
'/foo.d.ts':
'import { PP } from "./bar.js"; export type P = { foo: PP }',
'/bar.d.ts': 'export type PP = "foo" | "bar"'
}
const { props, deps } = resolve(
`
import { P } from './foo'
defineProps<P>()
`,
files
)
expect(props).toStrictEqual({
foo: ['String']
})
expect(deps && [...deps]).toStrictEqual(Object.keys(files))
})

test('ts module resolve', () => {
const files = {
'/node_modules/foo/package.json': JSON.stringify({
Expand Down
2 changes: 2 additions & 0 deletions packages/compiler-sfc/src/script/resolveType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,8 @@ function importSourceToScope(
}

function resolveExt(filename: string, fs: FS) {
// #8339 ts may import .js but we should resolve to corresponding ts or d.ts
filename = filename.replace(/\.js$/, '')
const tryResolve = (filename: string) => {
if (fs.fileExists(filename)) return filename
}
Expand Down

2 comments on commit b36addd

@mcpanl
Copy link

@mcpanl mcpanl commented on b36addd May 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

6

@QinCongH
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

666

Please sign in to comment.