-
-
Notifications
You must be signed in to change notification settings - Fork 208
/
Copy pathvueuse-math.ts
35 lines (32 loc) · 1.12 KB
/
vueuse-math.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import type { PackageIndexes } from '@vueuse/metadata'
import type { ImportsMap } from '../types'
import { readFileSync } from 'node:fs'
import process from 'node:process'
import { resolveModule } from 'local-pkg'
let _cache: ImportsMap | undefined
export default (): ImportsMap => {
if (!_cache) {
let indexesJson: PackageIndexes | undefined
try {
const corePath = resolveModule('@vueuse/core') || process.cwd()
const path = resolveModule('@vueuse/metadata/index.json')
|| resolveModule('@vueuse/metadata/index.json', { paths: [corePath] })
indexesJson = JSON.parse(readFileSync(path!, 'utf-8'))
}
catch (error) {
console.error(error)
throw new Error('[auto-import] failed to load @vueuse/math, have you installed it?')
}
if (indexesJson) {
_cache = {
'@vueuse/math': indexesJson
.functions
.filter(i => ['math'].includes(i.package))
.flatMap(i => [i.name, ...i.alias || []])
// only include functions with 4 characters or more
.filter((i: string) => i && i.length >= 4),
}
}
}
return _cache || {}
}