Skip to content

Commit f75aa8c

Browse files
committed
feat: add auto-imports
1 parent bc09fe0 commit f75aa8c

File tree

8 files changed

+64
-33
lines changed

8 files changed

+64
-33
lines changed

.eslintrc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"@typescript-eslint/comma-dangle": ["error", "never"],
66
"comma-dangle": ["error", "never"],
77
"yml/quotes": "off",
8-
"antfu/if-newline": "off"
8+
"antfu/if-newline": "off",
9+
"curly": ["error", "multi-line"],
10+
"object-property-newline": ["off", { "allowAllPropertiesOnSameLine": true}]
911
}
1012
}

README.md

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,51 @@
11
# ⚗️ Vue Query Nuxt
22

33
[![CI](https://github.com/Hebilicious/vue-query-nuxt/actions/workflows/ci.yaml/badge.svg)](https://github.com/Hebilicious/vue-query-nuxt/actions/workflows/ci.yaml)
4-
[![npm version](https://badge.fury.io/js/@hebilicious%2Fvue-query-nuxt.svg)](https://badge.fury.io/js/@hebilicious%2Fvue-query-nuxt)
4+
[![npm version](https://badge.fury.io/js/@hebilicious%20vue-query-nuxt.svg)](https://badge.fury.io/js/@hebilicious%20vue-query-nuxt)
55
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
66

77
🚀 Welcome to __Vue Query Nuxt__!
88

99
This Nuxt Module automatically installs and configure Vue Query for your Nuxt application.
1010
It has 0 config out-of-the box and extremely lightweight.
1111

12-
## ⚠️ Disclaimer
12+
## Features
1313

14-
_🧪 This module is in active development._
14+
- 0 config out-of-the box
15+
- All configurations options available
16+
- Auto Imports for Vue Query composables
1517

1618
Refer to the [Vue Query documentation](https://tanstack.com/query/latest/docs/vue/quick-start) for more information about Vue Query.
1719

18-
## 📦 Installation
20+
## 📦 How to use
1921

20-
21-
1. Use npm, pnpm or yarn to install the dependencies.
22+
### 1. Use npm, pnpm or yarn to install the dependencies.
2223

2324
```bash
25+
# npm
2426
npm i @hebilicious/vue-query-nuxt @tanstack/vue-query
25-
```
26-
27-
```bash
27+
# pnpm
2828
pnpm i @hebilicious/vue-query-nuxt @tanstack/vue-query
29+
# yarn
30+
yarn add @hebilicious/vue-query-nuxt @tanstack/vue-query
2931
```
3032

31-
```bash
32-
yarn i @hebilicious/vue-query-nuxt @tanstack/vue-query
33-
```
33+
### 2. Add the modules to your Nuxt modules
3434

35-
2. Add the modules to your Nuxt modules
35+
In `nuxt.config.ts` :
3636

3737
```ts
38-
// In nuxt.config.ts
3938
export default defineNuxtConfig({
4039
modules: ["@hebilicious/vue-query-nuxt"]
4140
})
4241
```
4342

44-
3. Use right away
43+
### 3. Use right away
4544

46-
```html
47-
<script setup>
48-
import { useQueryClient, useQuery, useMutation } from '@tanstack/vue-query'
45+
In a vue component :
4946

47+
```html
48+
<script setup lang="ts">
5049
// Access QueryClient instance
5150
const queryClient = useQueryClient()
5251
@@ -84,28 +83,38 @@ function onButtonClick() {
8483
</template>
8584
```
8685

87-
4. Advanced configuration
86+
### 4. Advanced configuration
8887

8988
You can specify the options under the vueQuery key in your nuxt.config.ts file.
9089
Everything is typed.
9190

91+
In `nuxt.config.ts` :
92+
9293
```ts
93-
// In nuxt.config.ts
9494
export default defineNuxtConfig({
9595
modules: ["@hebilicious/vue-query-nuxt"],
9696
vueQuery: {
97-
stateKey: "vue-query-nuxt",
97+
// useState key used by nuxt for the vue query state.
98+
stateKey: "vue-query-nuxt", // default
99+
// If you only want to import some functions, specify them here.
100+
// You can pass false or an empty array to disable this feature.
101+
// default: ["useQuery", "useQueries", "useInfiniteQuery", "useMutation", "useIsFetching", "useIsMutating", "useQueryClient"]
102+
autoImports: ["useQuery"],
103+
// Pass the vue query client options here ...
98104
queryClientOptions: {
99105
defaultOptions: { queries: { staleTime: 5000 } } // default
100106
},
107+
// Pass the vue query plugin options here ....
101108
vueQueryPluginOptions: {}
102109
}
103110
})
104111
```
112+
105113
If you need to modify the plugin that installs vue query, you can create a `vue-query.config.ts` file at the root of your project.
106114

115+
In `vue-query.config.ts` :
116+
107117
```ts
108-
// vue-query.config.ts
109118
import { library } from "@example/libray"
110119

111120
export default defineVueQueryPluginCallback((vueQueryOptions) => {
@@ -114,7 +123,7 @@ export default defineVueQueryPluginCallback((vueQueryOptions) => {
114123
})
115124
```
116125

117-
This callback will be run *directly* after the Vue Query plugin is installed, so you can use it to `provide` something.
126+
This callback will be run _directly_ after the Vue Query plugin is installed, so you can use it to `provide` something.
118127
This can be useful if you want to configure something that needs the `queryClient` or you want to provide a library in the same plugin.
119128

120129
## 📦 Contributing

packages/vue-query-nuxt/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
"name": "@hebilicious/vue-query-nuxt",
33
"type": "module",
44
"version": "0.0.5",
5+
"description": "A Nuxt module for Vue Query",
6+
"author": {
7+
"name": "Hebilicious",
8+
"email": "xsh4k3@gmail.com",
9+
"url": "https://twitter.com/its_hebilicious"
10+
},
511
"license": "MIT",
612
"repository": "Hebilicious/vue-query-nuxt",
713
"keywords": [

packages/vue-query-nuxt/src/module.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ export default defineNuxtModule<VueQueryOptions>({
2020

2121
logger.info(`Adding ${NAME} module...`)
2222

23-
const { ...options } = userOptions
2423
// 1. Set up runtime configuration
25-
nuxt.options.runtimeConfig.public[configKey] = defu(nuxt.options.runtimeConfig.public[configKey], options, {})
24+
nuxt.options.runtimeConfig.public[configKey] = defu(nuxt.options.runtimeConfig.public[configKey], userOptions, {})
2625

2726
// 2. Add plugin
2827
addPlugin(resolve("./runtime/plugin"))
@@ -39,7 +38,10 @@ export default defineNuxtModule<VueQueryOptions>({
3938
const file = await loadFile(configFile)
4039
if (file.exports.pluginCallback || file.exports.default) {
4140
logger.success("Found vue-query.config.ts file")
42-
if (!file.exports.pluginCallback) file.exports.pluginCallback = file.exports.default
41+
if (!file.exports.pluginCallback) {
42+
file.exports.pluginCallback = file.exports.default
43+
}
44+
4345
delete file.exports.default
4446
const { code } = generateCode(file) // We extract it with magicast...
4547
const shaked = await transform(code, { treeShaking: true, loader: "ts" }) // ...we clean it with esbuild.
@@ -84,6 +86,11 @@ export default defineNuxtModule<VueQueryOptions>({
8486
}
8587
})
8688

89+
// 6. Auto Imports tanstack composables
90+
if (userOptions.autoImports && userOptions.autoImports.length > 0) {
91+
addImports(userOptions.autoImports.map(name => ({ name, from: "@tanstack/vue-query" })))
92+
}
93+
8794
logger.success(`Added ${NAME} module successfully.`)
8895
}
8996
})

packages/vue-query-nuxt/src/runtime/utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,27 @@ import type { RuntimeConfig } from "nuxt/schema"
44
export const NAME = "vue-query-nuxt" as const
55
export const configKey = "vueQuery" as const
66

7+
const composables = [
8+
"useQuery",
9+
"useQueries",
10+
"useInfiniteQuery",
11+
"useMutation",
12+
"useIsFetching",
13+
"useIsMutating",
14+
"useQueryClient"
15+
] as const
16+
17+
type VueQueryComposables = typeof composables[number]
718
export interface VueQueryOptions {
819
stateKey: string
20+
autoImports: VueQueryComposables[] | [] | false
921
queryClientOptions: QueryClientConfig | undefined
1022
vueQueryPluginOptions: VueQueryPluginOptions
1123
}
1224

1325
export const defaults: VueQueryOptions = {
1426
stateKey: NAME,
27+
autoImports: [...composables],
1528
queryClientOptions: {
1629
defaultOptions: { queries: { staleTime: 5000 } }
1730
},

playgrounds/advanced/app.vue

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
<script setup lang="ts">
2-
import { useQuery } from "@tanstack/vue-query"
3-
42
const { $createHooks, $test } = useNuxtApp()
53
$test.log("hello")
64

playgrounds/simple/app.vue

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
<script setup lang="ts">
2-
import { useQuery } from "@tanstack/vue-query"
3-
42
const { data, isLoading } = useQuery({
53
queryKey: ["todos"],
64
queryFn: () => $fetch("/api/todos")

test/fixtures/todos/app.vue

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
<script setup lang="ts">
2-
import { useQuery } from "@tanstack/vue-query"
3-
42
const { data, suspense } = useQuery({
53
queryKey: ["todos"],
64
queryFn: () => $fetch("/api/todos")

0 commit comments

Comments
 (0)