Nuxt/docs/3.api/4.advanced/2.kit.md

171 lines
4.1 KiB
Markdown
Raw Normal View History

---
title: "Kit Utilities"
description: Nuxt Kit provides composable utilities to help interacting with Nuxt Hooks and Nuxt Builder.
---
# Kit Utilities
2022-11-16 17:21:08 +00:00
::ReadMore{link="/docs/guide/going-further/kit"}
::
2023-07-28 14:36:01 +00:00
# Utilities
2023-07-28 14:36:01 +00:00
## Modules
[source code](https://github.com/nuxt/nuxt/blob/main/packages/kit/src/module)
2023-07-28 14:46:02 +00:00
### `installModule`
2023-07-28 14:51:17 +00:00
Install specified Nuxt module programmatically. This is helpful when your module depends on other modules. You can pass the module options as an object to `inlineOptions` and they will be passed to the module's `setup` function.
2023-07-28 14:46:02 +00:00
#### Type
```ts
async function installModule (moduleToInstall: string | NuxtModule, inlineOptions?: any, nuxt?: Nuxt)
```
#### Parameters
##### `moduleToInstall`
**Type**: `string` | `NuxtModule`
**Required**: `true`
The module to install. Can be either a string with the module name or a module object itself.
##### `inlineOptions`
**Type**: `any`
**Default**: `{}`
An object with the module options to be passed to the module's `setup` function.
##### `nuxt`
**Type**: `Nuxt`
**Default**: `useNuxt()`
Nuxt instance. If not provided, it will be retrieved from the context via `useNuxt()` call.
2023-07-28 14:46:02 +00:00
#### Examples
2023-07-28 14:50:59 +00:00
```ts
import { defineNuxtModule, installModule } from '@nuxt/kit'
export default defineNuxtModule<ModuleOptions>({
async setup (options, nuxt) {
// will install @nuxtjs/fontaine with Roboto font and Impact fallback
await installModule('@nuxtjs/fontaine', {
// module configuration
fonts: [
{
family: 'Roboto',
fallbacks: ['Impact'],
fallbackName: 'fallback-a',
}
]
})
}
})
```
2023-07-28 14:36:01 +00:00
## Programmatic Usage
[source code](https://github.com/nuxt/nuxt/blob/main/packages/kit/src/loader)
- `loadNuxt(loadOptions)`
- `buildNuxt(nuxt)`
- `loadNuxtConfig(loadOptions)`
2023-07-28 14:36:01 +00:00
## Compatibility
[source code](https://github.com/nuxt/nuxt/blob/main/packages/kit/src/compatibility.ts)
- `checkNuxtCompatibility(constraints)`
- `assertNuxtCompatibility(constraints)`
- `hasNuxtCompatibility(constraints)`
- `isNuxt2()`
- `isNuxt3()`
- `getNuxtVersion()`
2023-07-28 14:36:01 +00:00
## Auto-imports
[source code](https://github.com/nuxt/nuxt/blob/main/packages/kit/src/imports.ts)
- `addImports(imports)`
- `addImportsDir(importDirs, { prepend? })`
- `addImportsSources(importSources)`
2023-07-28 14:36:01 +00:00
## Components
[source code](https://github.com/nuxt/nuxt/blob/main/packages/kit/src/components.ts)
- `addComponentsDir(dir)`
- `addComponent(componentObject)`
2023-07-28 14:36:01 +00:00
## Context
[source code](https://github.com/nuxt/nuxt/blob/main/packages/kit/src/context.ts)
- `useNuxt()`
2023-07-28 14:36:01 +00:00
## Pages
[source code](https://github.com/nuxt/nuxt/blob/main/packages/kit/src/pages.ts)
- `extendPages (callback: pages => void)`
- `extendRouteRules (route: string, rule: NitroRouteConfig, options: ExtendRouteRulesOptions)`
- `addRouteMiddleware (input: NuxtMiddleware | NuxtMiddleware[], options: AddRouteMiddlewareOptions)`
2023-07-28 14:36:01 +00:00
## Plugins
[source code](https://github.com/nuxt/nuxt/blob/main/packages/kit/src/plugin.ts)
- `addPlugin(pluginOptions, { append? })`
- `addPluginTemplate(pluginOptions, { append? })`
2023-07-28 14:36:01 +00:00
## Templates
[source code](https://github.com/nuxt/nuxt/blob/main/packages/kit/src/template.ts)
- `addTemplate(templateOptions)`
- `addTypeTemplate(templateOptions)`
- `updateTemplates({ filter?: ResolvedNuxtTemplate => boolean })`
2023-07-28 14:36:01 +00:00
## Nitro
[source code](https://github.com/nuxt/nuxt/blob/main/packages/kit/src/nitro.ts)
- `addServerHandler (handler)`
- `addDevServerHandler (handler)`
- `useNitro()` (only usable after `ready` hook)
- `addServerPlugin`
- `addPrerenderRoutes`
2023-07-28 14:36:01 +00:00
## Resolving
[source code](https://github.com/nuxt/nuxt/blob/main/packages/kit/src/resolve.ts)
- `resolvePath (path, resolveOptions?)`
- `resolveAlias (path, aliases?)`
- `findPath (paths, resolveOptions?)`
- `createResolver (base)`
2023-07-28 14:36:01 +00:00
## Logging
[source code](https://github.com/nuxt/nuxt/blob/main/packages/kit/src/logger.ts)
- `useLogger(scope?)`
2023-07-28 14:36:01 +00:00
## Builder
[source code](https://github.com/nuxt/nuxt/blob/main/packages/kit/src/build.ts)
- `extendWebpackConfig(callback, options?)`
- `extendViteConfig(callback, options?)`
- `addWebpackPlugin(webpackPlugin, options?)`
- `addVitePlugin(vitePlugin, options?)`