Skip to content

Commit

Permalink
feat: built-in @vueuse/math
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jul 29, 2022
1 parent ad15f50 commit 5a17ad1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/presets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ import uniApp from './uni-app'
import solid from './solid'
import solidAppRouter from './solid-app-router'
import { jotai, jotaiUtils } from './jotai'
import vueuseMath from './vueuse-math'

export const presets = {
'ahooks': ahooks,
'@nuxtjs/composition-api': nuxtCompositionApi,
'@vue/composition-api': vueCompositionApi,
'@vueuse/core': vueuseCore,
'@vueuse/math': vueuseMath,
'@vueuse/head': vueuseHead,
'mobx': mobx,
'mobx-react-lite': mobxReactLite,
Expand Down
34 changes: 34 additions & 0 deletions src/presets/vueuse-math.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { readFileSync } from 'fs'
import { resolveModule } from 'local-pkg'
import type { PackageIndexes } from '@vueuse/metadata'
import type { ImportsMap } from '../types'

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 || {}
}

0 comments on commit 5a17ad1

Please sign in to comment.