Skip to content

Commit 98d539f

Browse files
Jungzlantfu
andauthored
feat(preset): add ahooks (unplugin#156)
Co-authored-by: Anthony Fu <[email protected]>
1 parent f1a319b commit 98d539f

File tree

6 files changed

+81
-2
lines changed

6 files changed

+81
-2
lines changed

examples/vite-react/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"serve": "vite preview"
88
},
99
"dependencies": {
10+
"ahooks": "^3.3.8",
1011
"i18next": "^21.6.16",
1112
"react": "^18.0.0",
1213
"react-dom": "^18.0.0",

examples/vite-react/src/App.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import PageB from './views/PageB'
66
import './i18n'
77

88
function App() {
9+
useTitle('Page Title')
910
const [count, setCount] = useState(0)
1011
const { t } = useTranslation()
1112

examples/vite-react/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default defineConfig({
1313
jsx: 'react',
1414
}),
1515
AutoImport({
16-
imports: ['react', 'react-router-dom', 'react-i18next'],
16+
imports: ['react', 'react-router-dom', 'react-i18next', 'ahooks'],
1717
dts: './src/auto-imports.d.ts',
1818
resolvers: [
1919
IconsResolver({

pnpm-lock.yaml

Lines changed: 47 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/presets/ahooks.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { readFileSync } from 'fs'
2+
import { resolveModule } from 'local-pkg'
3+
import type { PackageIndexes } from '@vueuse/metadata'
4+
import type { ImportsMap } from '../types'
5+
6+
let _cache: ImportsMap | undefined
7+
8+
export default (): ImportsMap => {
9+
if (!_cache) {
10+
let indexesJson: PackageIndexes | undefined
11+
try {
12+
const path = resolveModule('ahooks/metadata.json')
13+
indexesJson = JSON.parse(readFileSync(path!, 'utf-8'))
14+
}
15+
catch (error) {
16+
console.error(error)
17+
throw new Error('[auto-import] failed to load ahooks, have you installed it?')
18+
}
19+
if (indexesJson) {
20+
_cache = {
21+
ahooks: indexesJson
22+
.functions
23+
.flatMap(i => [i.name, ...i.alias || []]),
24+
}
25+
}
26+
}
27+
28+
return _cache || {}
29+
}

src/presets/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable object-shorthand */
2+
import ahooks from './ahooks'
23
import mobx from './mobx'
34
import mobxReactLite from './mobx-react-lite'
45
import nuxtCompositionApi from './nuxt-composition-api'
@@ -35,6 +36,7 @@ import solidAppRouter from './solid-app-router'
3536
import { jotai, jotaiUtils } from './jotai'
3637

3738
export const presets = {
39+
'ahooks': ahooks,
3840
'@nuxtjs/composition-api': nuxtCompositionApi,
3941
'@vue/composition-api': vueCompositionApi,
4042
'@vueuse/core': vueuseCore,

0 commit comments

Comments
 (0)