Skip to content

Commit

Permalink
refactor: options
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Oct 22, 2022
1 parent ca7d930 commit 5d84351
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -358,7 +358,7 @@ Components({
include: [/\.vue$/, /\.vue\?vue/],
exclude: [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/, /[\\/]\.nuxt[\\/]/],

// The dts file have different namespace if the vue is version 2.7
// Vue version of project. It will detect automatically if not specified.
version: 2.7
})
```
Expand Down
12 changes: 5 additions & 7 deletions src/core/declaration.ts
Expand Up @@ -114,22 +114,20 @@ export function getDeclaration(ctx: Context, filepath: string, originalImports?:
directive: stringifyDeclarationImports({ ...originalImports?.directive, ...imports.directive }),
}

const isVue27 = ctx.options.version ? ctx.options.version === 2.7 : false
const dtsHead = isVue27
? `
export {}
const head = ctx.options.version === 2.7
? `export {}
declare module 'vue' {`
: `
import '@vue/runtime-core'
: `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}`
// Read more: https://github.com/vuejs/core/pull/3399
${head}`

if (Object.keys(declarations.component).length > 0) {
code += `
Expand Down
9 changes: 5 additions & 4 deletions src/core/options.ts
Expand Up @@ -65,16 +65,17 @@ export function resolveOptions(options: Options, root: string): ResolvedOptions
resolved.types = resolved.types || []

resolved.root = root
resolved.transformer = options.transformer || getVueVersion(root) || 'vue3'
resolved.version = resolved.version ?? getVueVersion(root)
resolved.transformer = options.transformer || `vue${Math.trunc(resolved.version) as 2 | 3}`
resolved.directives = (typeof options.directives === 'boolean')
? options.directives
: !resolved.resolvers.some(i => i.type === 'directive')
? false
: getVueVersion(root) === 'vue3'
: resolved.version >= 3
return resolved
}

function getVueVersion(root: string) {
function getVueVersion(root: string): number {
const version = getPackageInfoSync('vue', { paths: [root] })?.version || '3'
return version.startsWith('2.') ? 'vue2' : 'vue3'
return +(version.split('.').slice(0, 2).join('.'))
}
2 changes: 1 addition & 1 deletion src/types.ts
Expand Up @@ -173,7 +173,7 @@ export interface Options {
types?: TypeImport[]

/**
* Passing Project Vue Version for Declaration
* Vue version of project. It will detect automatically if not specified.
*/
version?: number
}
Expand Down
32 changes: 16 additions & 16 deletions test/__snapshots__/dts.test.ts.snap
Expand Up @@ -18,22 +18,6 @@ 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 Expand Up @@ -108,6 +92,22 @@ exports[`dts > parseDeclaration 1`] = `
}
`;

exports[`dts > vue 2.7 components only 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 > writeDeclaration - keep unused 1`] = `
"// generated by unplugin-vue-components
// We suggest you to commit this file into source control
Expand Down
6 changes: 3 additions & 3 deletions test/dts.test.ts
Expand Up @@ -95,13 +95,13 @@ const _directive_loading = _resolveDirective("loading")`
expect(declarations).toMatchSnapshot()
})

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

const declarations = getDeclaration(ctx, 'test.d.ts')
Expand Down

0 comments on commit 5d84351

Please sign in to comment.