Skip to content

Commit

Permalink
feat(compiler-sfc): expose type import deps on compiled script block
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Apr 15, 2023
1 parent 075498c commit 8d8ddd6
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 49 deletions.
100 changes: 54 additions & 46 deletions packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,81 +264,85 @@ describe('resolveType', () => {
})

describe('external type imports', () => {
const files = {
'/foo.ts': 'export type P = { foo: number }',
'/bar.d.ts': 'type X = { bar: string }; export { X as Y }'
}
test('relative ts', () => {
expect(
resolve(
`
const { props, deps } = resolve(
`
import { P } from './foo'
import { Y as PP } from './bar'
defineProps<P & PP>()
`,
{
'/foo.ts': 'export type P = { foo: number }',
'/bar.d.ts': 'type X = { bar: string }; export { X as Y }'
}
).props
).toStrictEqual({
`,
files
)
expect(props).toStrictEqual({
foo: ['Number'],
bar: ['String']
})
expect(deps && [...deps]).toStrictEqual(Object.keys(files))
})

test('relative vue', () => {
expect(
resolve(
`
const files = {
'/foo.vue':
'<script lang="ts">export type P = { foo: number }</script>',
'/bar.vue':
'<script setup lang="tsx">export type P = { bar: string }</script>'
}
const { props, deps } = resolve(
`
import { P } from './foo.vue'
import { P as PP } from './bar.vue'
defineProps<P & PP>()
`,
{
'/foo.vue':
'<script lang="ts">export type P = { foo: number }</script>',
'/bar.vue':
'<script setup lang="tsx">export type P = { bar: string }</script>'
}
).props
).toStrictEqual({
`,
files
)
expect(props).toStrictEqual({
foo: ['Number'],
bar: ['String']
})
expect(deps && [...deps]).toStrictEqual(Object.keys(files))
})

test('relative (chained)', () => {
expect(
resolve(
`
const files = {
'/foo.ts': `import type { P as PP } from './nested/bar.vue'
export type P = { foo: number } & PP`,
'/nested/bar.vue':
'<script setup lang="ts">export type P = { bar: string }</script>'
}
const { props, deps } = resolve(
`
import { P } from './foo'
defineProps<P>()
`,
{
'/foo.ts': `import type { P as PP } from './nested/bar.vue'
export type P = { foo: number } & PP`,
'/nested/bar.vue':
'<script setup lang="ts">export type P = { bar: string }</script>'
}
).props
).toStrictEqual({
`,
files
)
expect(props).toStrictEqual({
foo: ['Number'],
bar: ['String']
})
expect(deps && [...deps]).toStrictEqual(Object.keys(files))
})

test('relative (chained, re-export)', () => {
expect(
resolve(
`
const files = {
'/foo.ts': `export { P as PP } from './bar'`,
'/bar.ts': 'export type P = { bar: string }'
}
const { props, deps } = resolve(
`
import { PP as P } from './foo'
defineProps<P>()
`,
{
'/foo.ts': `export { P as PP } from './bar'`,
'/bar.ts': 'export type P = { bar: string }'
}
).props
).toStrictEqual({
`,
files
)
expect(props).toStrictEqual({
bar: ['String']
})
expect(deps && [...deps]).toStrictEqual(Object.keys(files))
})

test('ts module resolve', () => {
Expand All @@ -357,7 +361,7 @@ describe('resolveType', () => {
'/pp.ts': 'export type PP = { bar: string }'
}

const { props } = resolve(
const { props, deps } = resolve(
`
import { P } from 'foo'
import { PP } from 'bar'
Expand All @@ -370,6 +374,10 @@ describe('resolveType', () => {
foo: ['Number'],
bar: ['String']
})
expect(deps && [...deps]).toStrictEqual([
'/node_modules/foo/index.d.ts',
'/pp.ts'
])
})
})

Expand Down Expand Up @@ -447,6 +455,6 @@ function resolve(code: string, files: Record<string, string> = {}) {
return {
props,
calls: raw.calls,
raw
deps: ctx.deps
}
}
3 changes: 2 additions & 1 deletion packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,8 @@ export function compileScript(
}) as unknown as RawSourceMap)
: undefined,
scriptAst: scriptAst?.body,
scriptSetupAst: scriptSetupAst?.body
scriptSetupAst: scriptSetupAst?.body,
deps: ctx.deps ? [...ctx.deps] : undefined
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/compiler-sfc/src/script/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class ScriptCompileContext {
/**
* to be exposed on compiled script block for HMR cache busting
*/
deps?: string[]
deps?: Set<string>

constructor(
public descriptor: SFCDescriptor,
Expand Down
4 changes: 3 additions & 1 deletion packages/compiler-sfc/src/script/resolveType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,9 @@ function resolveTypeFromImport(
}

if (resolved) {
// TODO (hmr) register dependency file on ctx
// (hmr) register dependency file on ctx
;(ctx.deps || (ctx.deps = new Set())).add(resolved)

return resolveTypeReference(
ctx,
node,
Expand Down

0 comments on commit 8d8ddd6

Please sign in to comment.