Skip to content

Commit 2a120ee

Browse files
azaletasxzz
andauthored
feat: support vue 2.7 (#529)
Co-authored-by: 三咲智子 (Kevin) <[email protected]>
1 parent 1de372f commit 2a120ee

File tree

6 files changed

+79
-10
lines changed

6 files changed

+79
-10
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,10 @@ Components({
357357
// filters for transforming targets
358358
include: [/\.vue$/, /\.vue\?vue/],
359359
exclude: [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/, /[\\/]\.nuxt[\\/]/],
360+
361+
// Vue version of project. It will detect automatically if not specified.
362+
// Acceptable value: 2 | 2.7 | 3
363+
version: 2.7
360364
})
361365
```
362366

src/core/declaration.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,21 @@ export function getDeclaration(ctx: Context, filepath: string, originalImports?:
114114
directive: stringifyDeclarationImports({ ...originalImports?.directive, ...imports.directive }),
115115
}
116116

117-
let code = `// generated by unplugin-vue-components
118-
// We suggest you to commit this file into source control
119-
// Read more: https://github.com/vuejs/core/pull/3399
120-
import '@vue/runtime-core'
117+
const head = ctx.options.version === 2.7
118+
? `export {}
119+
120+
declare module 'vue' {`
121+
: `import '@vue/runtime-core'
121122
122123
export {}
123124
124125
declare module '@vue/runtime-core' {`
125126

127+
let code = `// generated by unplugin-vue-components
128+
// We suggest you to commit this file into source control
129+
// Read more: https://github.com/vuejs/core/pull/3399
130+
${head}`
131+
126132
if (Object.keys(declarations.component).length > 0) {
127133
code += `
128134
export interface GlobalComponents {

src/core/options.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { getPackageInfoSync, isPackageExists } from 'local-pkg'
44
import type { ComponentResolver, ComponentResolverObject, Options, ResolvedOptions } from '../types'
55
import { detectTypeImports } from './type-imports/detect'
66

7-
export const defaultOptions: Omit<Required<Options>, 'include' | 'exclude' | 'transformer' | 'globs' | 'directives' | 'types'> = {
7+
export const defaultOptions: Omit<Required<Options>, 'include' | 'exclude' | 'transformer' | 'globs' | 'directives' | 'types' | 'version'> = {
88
dirs: 'src/components',
99
extensions: 'vue',
1010
deep: true,
@@ -65,16 +65,25 @@ export function resolveOptions(options: Options, root: string): ResolvedOptions
6565
resolved.types = resolved.types || []
6666

6767
resolved.root = root
68-
resolved.transformer = options.transformer || getVueVersion(root) || 'vue3'
68+
resolved.version = resolved.version ?? getVueVersion(root)
69+
if (resolved.version < 2 || resolved.version >= 4)
70+
throw new Error(`[unplugin-vue-components] unsupported version: ${resolved.version}`)
71+
72+
resolved.transformer = options.transformer || `vue${Math.trunc(resolved.version) as 2 | 3}`
6973
resolved.directives = (typeof options.directives === 'boolean')
7074
? options.directives
7175
: !resolved.resolvers.some(i => i.type === 'directive')
7276
? false
73-
: getVueVersion(root) === 'vue3'
77+
: resolved.version >= 3
7478
return resolved
7579
}
7680

77-
function getVueVersion(root: string) {
78-
const version = getPackageInfoSync('vue', { paths: [root] })?.version || '3'
79-
return version.startsWith('2.') ? 'vue2' : 'vue3'
81+
function getVueVersion(root: string): 2 | 2.7 | 3 {
82+
const raw = getPackageInfoSync('vue', { paths: [root] })?.version || '3'
83+
const version = +(raw.split('.').slice(0, 2).join('.'))
84+
if (version === 2.7)
85+
return 2.7
86+
else if (version < 2.7)
87+
return 2
88+
return 3
8089
}

src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ export interface Options {
171171
* Only provide types of components in library (registered globally)
172172
**/
173173
types?: TypeImport[]
174+
175+
/**
176+
* Vue version of project. It will detect automatically if not specified.
177+
*/
178+
version?: 2 | 2.7 | 3
174179
}
175180

176181
export type ResolvedOptions = Omit<

test/__snapshots__/dts.test.ts.snap

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@ declare module '@vue/runtime-core' {
1818
"
1919
`;
2020

21+
exports[`dts > components only vue2.7 1`] = `
22+
"// generated by unplugin-vue-components
23+
// We suggest you to commit this file into source control
24+
// Read more: https://github.com/vuejs/core/pull/3399
25+
export {}
26+
27+
declare module 'vue' {
28+
export interface GlobalComponents {
29+
RouterLink: typeof import('vue-router')['RouterLink']
30+
RouterView: typeof import('vue-router')['RouterView']
31+
TestComp: typeof import('test/component/TestComp')['default']
32+
}
33+
}
34+
"
35+
`;
36+
2137
exports[`dts > directive only 1`] = `
2238
"// generated by unplugin-vue-components
2339
// We suggest you to commit this file into source control
@@ -92,6 +108,22 @@ exports[`dts > parseDeclaration 1`] = `
92108
}
93109
`;
94110

111+
exports[`dts > vue 2.7 components only 1`] = `
112+
"// generated by unplugin-vue-components
113+
// We suggest you to commit this file into source control
114+
// Read more: https://github.com/vuejs/core/pull/3399
115+
export {}
116+
117+
declare module 'vue' {
118+
export interface GlobalComponents {
119+
RouterLink: typeof import('vue-router')['RouterLink']
120+
RouterView: typeof import('vue-router')['RouterView']
121+
TestComp: typeof import('test/component/TestComp')['default']
122+
}
123+
}
124+
"
125+
`;
126+
95127
exports[`dts > writeDeclaration - keep unused 1`] = `
96128
"// generated by unplugin-vue-components
97129
// We suggest you to commit this file into source control

test/dts.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,19 @@ const _directive_loading = _resolveDirective("loading")`
9595
expect(declarations).toMatchSnapshot()
9696
})
9797

98+
test('vue 2.7 components only', async () => {
99+
const ctx = new Context({
100+
resolvers: resolver,
101+
directives: true,
102+
version: 2.7,
103+
})
104+
const code = 'const _component_test_comp = _c("test-comp")'
105+
await ctx.transform(code, '')
106+
107+
const declarations = getDeclaration(ctx, 'test.d.ts')
108+
expect(declarations).toMatchSnapshot()
109+
})
110+
98111
test('directive only', async () => {
99112
const ctx = new Context({
100113
resolvers: resolver,

0 commit comments

Comments
 (0)