Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support vue 2.7 #529

Merged
merged 14 commits into from Oct 24, 2022
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -357,6 +357,9 @@ Components({
// filters for transforming targets
include: [/\.vue$/, /\.vue\?vue/],
exclude: [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/, /[\\/]\.nuxt[\\/]/],

// The dts file have different namespace if the vue is version 2.7
vueVersion: '2.7.0'
})
```

Expand Down
15 changes: 12 additions & 3 deletions src/core/declaration.ts
Expand Up @@ -114,15 +114,24 @@ export function getDeclaration(ctx: Context, filepath: string, originalImports?:
directive: stringifyDeclarationImports({ ...originalImports?.directive, ...imports.directive }),
}

let code = `// generated by unplugin-vue-components
// We suggest you to commit this file into source control
// Read more: https://github.com/vuejs/core/pull/3399
const isVue27 = ctx.options.vueVersion ? ctx.options.vueVersion.startsWith('2.7') : false
let dtsHead = ''
isVue27
? dtsHead = `
export {}

declare module 'vue' {`
: dtsHead = `
azaleta marked this conversation as resolved.
Show resolved Hide resolved
import '@vue/runtime-core'

export {}

declare module '@vue/runtime-core' {`

let code = `// generated by unplugin-vue-components
// We suggest you to commit this file into source control
// Read more: https://github.com/vuejs/core/pull/3399${dtsHead}`

if (Object.keys(declarations.component).length > 0) {
code += `
export interface GlobalComponents {
Expand Down
2 changes: 1 addition & 1 deletion src/core/options.ts
Expand Up @@ -4,7 +4,7 @@ import { getPackageInfoSync, isPackageExists } from 'local-pkg'
import type { ComponentResolver, ComponentResolverObject, Options, ResolvedOptions } from '../types'
import { detectTypeImports } from './type-imports/detect'

export const defaultOptions: Omit<Required<Options>, 'include' | 'exclude' | 'transformer' | 'globs' | 'directives' | 'types'> = {
export const defaultOptions: Omit<Required<Options>, 'include' | 'exclude' | 'transformer' | 'globs' | 'directives' | 'types' | 'vueVersion'> = {
dirs: 'src/components',
extensions: 'vue',
deep: true,
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Expand Up @@ -171,6 +171,11 @@ export interface Options {
* Only provide types of components in library (registered globally)
**/
types?: TypeImport[]

/**
* Passing Project Vue Version for Declaration
*/
vueVersion?: String
azaleta marked this conversation as resolved.
Show resolved Hide resolved
}

export type ResolvedOptions = Omit<
Expand Down
16 changes: 16 additions & 0 deletions test/__snapshots__/dts.test.ts.snap
Expand Up @@ -18,6 +18,22 @@ declare module '@vue/runtime-core' {
"
`;

exports[`dts > components only vue2.7 1`] = `
"// generated by unplugin-vue-components
// We suggest you to commit this file into source control
// Read more: https://github.com/vuejs/core/pull/3399
export {}

declare module 'vue' {
export interface GlobalComponents {
RouterLink: typeof import('vue-router')['RouterLink']
RouterView: typeof import('vue-router')['RouterView']
TestComp: typeof import('test/component/TestComp')['default']
}
}
"
`;

exports[`dts > directive only 1`] = `
"// generated by unplugin-vue-components
// We suggest you to commit this file into source control
Expand Down
13 changes: 13 additions & 0 deletions test/dts.test.ts
Expand Up @@ -95,6 +95,19 @@ const _directive_loading = _resolveDirective("loading")`
expect(declarations).toMatchSnapshot()
})

test('components only vue2.7', async () => {
const ctx = new Context({
resolvers: resolver,
directives: true,
})
ctx.options.vueVersion = '2.7.1'
const code = 'const _component_test_comp = _resolveComponent("test-comp")'
await ctx.transform(code, '')

const declarations = getDeclaration(ctx, 'test.d.ts')
expect(declarations).toMatchSnapshot()
})

test('directive only', async () => {
const ctx = new Context({
resolvers: resolver,
Expand Down