Skip to content

Commit

Permalink
refactor: restrict version
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Oct 22, 2022
1 parent 5d84351 commit 90a14d9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -359,6 +359,7 @@ Components({
exclude: [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/, /[\\/]\.nuxt[\\/]/],

// Vue version of project. It will detect automatically if not specified.
// Acceptable value: 2 | 2.7 | 3
version: 2.7
})
```
Expand Down
14 changes: 11 additions & 3 deletions src/core/options.ts
Expand Up @@ -66,6 +66,9 @@ export function resolveOptions(options: Options, root: string): ResolvedOptions

resolved.root = root
resolved.version = resolved.version ?? getVueVersion(root)
if (resolved.version < 2 || resolved.version >= 4)
throw new Error(`[unplugin-vue-components] unsupported version: ${resolved.version}`)

resolved.transformer = options.transformer || `vue${Math.trunc(resolved.version) as 2 | 3}`
resolved.directives = (typeof options.directives === 'boolean')
? options.directives
Expand All @@ -75,7 +78,12 @@ export function resolveOptions(options: Options, root: string): ResolvedOptions
return resolved
}

function getVueVersion(root: string): number {
const version = getPackageInfoSync('vue', { paths: [root] })?.version || '3'
return +(version.split('.').slice(0, 2).join('.'))
function getVueVersion(root: string): 2 | 2.7 | 3 {
const raw = getPackageInfoSync('vue', { paths: [root] })?.version || '3'
const version = +(raw.split('.').slice(0, 2).join('.'))
if (version === 2.7)
return 2.7
else if (version >= 3)
return 3
return 2
}
2 changes: 1 addition & 1 deletion src/types.ts
Expand Up @@ -175,7 +175,7 @@ export interface Options {
/**
* Vue version of project. It will detect automatically if not specified.
*/
version?: number
version?: 2 | 2.7 | 3
}

export type ResolvedOptions = Omit<
Expand Down

0 comments on commit 90a14d9

Please sign in to comment.