程序化使用

Nuxt Kit 提供了一组实用程序来帮助你以编程方式使用 Nuxt。这些函数允许你加载 Nuxt、构建 Nuxt 以及加载 Nuxt 配置。

当你想以编程方式使用 Nuxt 时,例如在构建 CLI 工具测试实用程序 时,编程式使用会很有帮助。

¥Programmatic usage can be helpful when you want to use Nuxt programmatically, for example, when building a CLI tool or test utils.

loadNuxt

以编程方式加载 Nuxt。它将加载 Nuxt 配置,实例化并返回带有 Nuxt 实例的 Promise。

¥Load Nuxt programmatically. It will load the Nuxt configuration, instantiate and return the promise with Nuxt instance.

类型

¥Type

async function loadNuxt (loadOptions?: LoadNuxtOptions): Promise<Nuxt>

interface LoadNuxtOptions extends LoadNuxtConfigOptions {
  dev?: boolean
  ready?: boolean
}

参数

¥Parameters

loadOptions

类型:LoadNuxtOptions

¥Type: LoadNuxtOptions

默认:{}

¥Default: {}

Nuxt 的加载条件。loadNuxt 在底层使用 c12,因此它接受与 c12.loadConfig 相同的选项,并附加了一些选项:

¥Loading conditions for Nuxt. loadNuxt uses c12 under the hood, so it accepts the same options as c12.loadConfig with some additional options:

  • dev(可选)
    类型:boolean
    默认:false
    如果设置为 true,Nuxt 将以开发模式加载。
  • ready(可选)
    类型:boolean
    默认:true
    如果设置为 true,Nuxt 将在调用 loadNuxt 后即可使用。如果设置为 false,你需要调用 nuxt.ready() 以确保 Nuxt 已准备好使用。

buildNuxt

以编程方式构建 Nuxt。它将调用构建器(当前为 @nuxt/vite-builder@nuxt/webpack-builder)来打包应用。

¥Build Nuxt programmatically. It will invoke the builder (currently @nuxt/vite-builder or @nuxt/webpack-builder) to bundle the application.

类型

¥Type

async function buildNuxt (nuxt: Nuxt): Promise<any>

参数

¥Parameters

nuxt

类型:Nuxt

¥Type: Nuxt

必需:true

¥Required: true

要构建的 Nuxt 实例。它可以通过 useNuxt() 调用从上下文中获取。

¥Nuxt instance to build. It can be retrieved from the context via useNuxt() call.

loadNuxtConfig

加载 Nuxt 配置。它将返回包含配置对象的 promise。

¥Load Nuxt configuration. It will return the promise with the configuration object.

类型

¥Type

async function loadNuxtConfig (options: LoadNuxtConfigOptions): Promise<NuxtOptions>

参数

¥Parameters

options

类型:LoadNuxtConfigOptions

¥Type: LoadNuxtConfigOptions

必需:true

¥Required: true

c12 loadConfig 调用中传递的选项。

¥Options to pass in c12 loadConfig call.

writeTypes

提供 tsconfig.json 选项时生成响应式大小

¥Generates tsconfig.json and writes it to the project buildDir.

类型

¥Type

function writeTypes (nuxt?: Nuxt): void

interface Nuxt {
  options: NuxtOptions
  hooks: Hookable<NuxtHooks>
  hook: Nuxt['hooks']['hook']
  callHook: Nuxt['hooks']['callHook']
  addHooks: Nuxt['hooks']['addHooks']
  ready: () => Promise<void>
  close: () => Promise<void>
  server?: any
  vfs: Record<string, string>
  apps: Record<string, NuxtApp>
}

参数

¥Parameters

nuxt

类型:Nuxt

¥Type: Nuxt

必需:true

¥Required: true

要构建的 Nuxt 实例。它可以通过 useNuxt() 调用从上下文中获取。

¥Nuxt instance to build. It can be retrieved from the context via useNuxt() call.