diff --git a/src/core/declaration.ts b/src/core/declaration.ts index fa6241c4..9dc9a9ed 100644 --- a/src/core/declaration.ts +++ b/src/core/declaration.ts @@ -114,11 +114,9 @@ export function getDeclaration(ctx: Context, filepath: string, originalImports?: directive: stringifyDeclarationImports({ ...originalImports?.directive, ...imports.directive }), } - const head = ctx.options.version === 2.7 - ? `export {} + const head = `export {} -declare module 'vue' {` - : `export {} +import { GlobalComponents } from 'vue' declare module 'vue' {` @@ -142,6 +140,12 @@ ${head}` }` } code += '\n}\n' + if (Object.keys(declarations.component).length > 0) { + code += ` +declare global { + ${declarations.component.map(d => d.replace(/(.+):/, 'const $1:')).join('\n ')} +}` + } return code } diff --git a/test/__snapshots__/dts.test.ts.snap b/test/__snapshots__/dts.test.ts.snap index ea2af4da..e3db46a4 100644 --- a/test/__snapshots__/dts.test.ts.snap +++ b/test/__snapshots__/dts.test.ts.snap @@ -8,6 +8,8 @@ exports[`dts > components only 1`] = ` // Read more: https://github.com/vuejs/core/pull/3399 export {} +import { GlobalComponents } from 'vue' + declare module 'vue' { export interface GlobalComponents { RouterLink: typeof import('vue-router')['RouterLink'] @@ -15,7 +17,12 @@ declare module 'vue' { TestComp: typeof import('test/component/TestComp')['default'] } } -" + +declare global { + const RouterLink: typeof import('vue-router')['RouterLink'] + const RouterView: typeof import('vue-router')['RouterView'] + const TestComp: typeof import('test/component/TestComp')['default'] +}" `; exports[`dts > directive only 1`] = ` @@ -26,6 +33,8 @@ exports[`dts > directive only 1`] = ` // Read more: https://github.com/vuejs/core/pull/3399 export {} +import { GlobalComponents } from 'vue' + declare module 'vue' { export interface ComponentCustomProperties { vLoading: typeof import('test/directive/Loading')['default'] @@ -42,6 +51,8 @@ exports[`dts > getDeclaration 1`] = ` // Read more: https://github.com/vuejs/core/pull/3399 export {} +import { GlobalComponents } from 'vue' + declare module 'vue' { export interface GlobalComponents { RouterLink: typeof import('vue-router')['RouterLink'] @@ -52,7 +63,12 @@ declare module 'vue' { vLoading: typeof import('test/directive/Loading')['default'] } } -" + +declare global { + const RouterLink: typeof import('vue-router')['RouterLink'] + const RouterView: typeof import('vue-router')['RouterView'] + const TestComp: typeof import('test/component/TestComp')['default'] +}" `; exports[`dts > parseDeclaration - has icon component like 1`] = ` @@ -100,6 +116,8 @@ exports[`dts > vue 2.7 components only 1`] = ` // Read more: https://github.com/vuejs/core/pull/3399 export {} +import { GlobalComponents } from 'vue' + declare module 'vue' { export interface GlobalComponents { RouterLink: typeof import('vue-router')['RouterLink'] @@ -107,7 +125,12 @@ declare module 'vue' { TestComp: typeof import('test/component/TestComp')['default'] } } -" + +declare global { + const RouterLink: typeof import('vue-router')['RouterLink'] + const RouterView: typeof import('vue-router')['RouterView'] + const TestComp: typeof import('test/component/TestComp')['default'] +}" `; exports[`dts > writeDeclaration - keep unused 1`] = ` @@ -118,6 +141,8 @@ exports[`dts > writeDeclaration - keep unused 1`] = ` // Read more: https://github.com/vuejs/core/pull/3399 export {} +import { GlobalComponents } from 'vue' + declare module 'vue' { export interface GlobalComponents { RouterLink: typeof import('vue-router')['RouterLink'] @@ -131,7 +156,13 @@ declare module 'vue' { vSome: typeof import('test/directive/Some')['default'] } } -" + +declare global { + const RouterLink: typeof import('vue-router')['RouterLink'] + const RouterView: typeof import('vue-router')['RouterView'] + const SomeComp: typeof import('test/component/SomeComp')['default'] + const TestComp: typeof import('test/component/TestComp')['default'] +}" `; exports[`dts > writeDeclaration 1`] = ` @@ -142,6 +173,8 @@ exports[`dts > writeDeclaration 1`] = ` // Read more: https://github.com/vuejs/core/pull/3399 export {} +import { GlobalComponents } from 'vue' + declare module 'vue' { export interface GlobalComponents { RouterLink: typeof import('vue-router')['RouterLink'] @@ -152,5 +185,10 @@ declare module 'vue' { vLoading: typeof import('test/directive/Loading')['default'] } } -" + +declare global { + const RouterLink: typeof import('vue-router')['RouterLink'] + const RouterView: typeof import('vue-router')['RouterView'] + const TestComp: typeof import('test/component/TestComp')['default'] +}" `;