docs: improved going further modules documentation (#19219)

Co-authored-by: Rafael Magalhaes <rafael.magalhaes@yapily.com>
This commit is contained in:
Rafael Magalhaes 2023-02-22 13:45:22 +00:00 committed by GitHub
parent 7a4a416f3c
commit 99a47c1dd0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -294,6 +294,42 @@ export default defineNuxtModule({
}) })
``` ```
### Adding Composables
If your module should provide composables, you can use the `addImports` utility to add them as auto-imports for Nuxt to resolve.
```ts
import { defineNuxtModule, addImports, createResolver } from '@nuxt/kit'
export default defineNuxtModule({
setup(options, nuxt) {
const resolver = createResolver(import.meta.url)
addImports({
name: 'useComposable', // name of the composable to be used
as: 'useComposable',
from: resolver.resolve('runtime/composables/useComposable') // path of composable
})
}
})
```
Alternatively, you can add an entire directory by using `addImportsDir`.
```ts
import { defineNuxtModule, addImportsDir, createResolver } from '@nuxt/kit'
export default defineNuxtModule({
setup(options, nuxt) {
const resolver = createResolver(import.meta.url)
addImportsDir(resolver.resolve('runtime/composables'))
}
})
```
::alert
Note that auto-imports are not enabled for files within `node_modules` for performance reasons, so the module starter [deliberately disables them](https://github.com/nuxt/starter/blob/module/.nuxtrc#L1) while developing a module. If you are using the module starter, auto-imports will not be enabled in your playground either.
::
### Clean Up Module ### Clean Up Module
If your module opens, handles or starts a watcher, you should close it when the Nuxt lifecycle is done. For this, use the `close` hook: If your module opens, handles or starts a watcher, you should close it when the Nuxt lifecycle is done. For this, use the `close` hook: