From 5d84351ee10b65c6a621d21ceea8054010bf6835 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90?= Date: Sat, 22 Oct 2022 01:24:43 +0800 Subject: [PATCH] refactor: options --- README.md | 2 +- src/core/declaration.ts | 12 +++++------ src/core/options.ts | 9 ++++---- src/types.ts | 2 +- test/__snapshots__/dts.test.ts.snap | 32 ++++++++++++++--------------- test/dts.test.ts | 6 +++--- 6 files changed, 31 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 055396c7..67581edd 100644 --- a/README.md +++ b/README.md @@ -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 }) ``` diff --git a/src/core/declaration.ts b/src/core/declaration.ts index d0bede51..bf44ab1b 100644 --- a/src/core/declaration.ts +++ b/src/core/declaration.ts @@ -114,14 +114,11 @@ 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 {} @@ -129,7 +126,8 @@ 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 += ` diff --git a/src/core/options.ts b/src/core/options.ts index a618041e..a0518eea 100644 --- a/src/core/options.ts +++ b/src/core/options.ts @@ -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('.')) } diff --git a/src/types.ts b/src/types.ts index 604e21e7..7b1f459c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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 } diff --git a/test/__snapshots__/dts.test.ts.snap b/test/__snapshots__/dts.test.ts.snap index 904b0bd8..6aef1cd0 100644 --- a/test/__snapshots__/dts.test.ts.snap +++ b/test/__snapshots__/dts.test.ts.snap @@ -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 @@ -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 diff --git a/test/dts.test.ts b/test/dts.test.ts index cb6bbc22..6e13b616 100644 --- a/test/dts.test.ts +++ b/test/dts.test.ts @@ -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')