示例

Nuxt Kit 实用程序的使用示例。

访问 Nuxt Vite 配置

¥Accessing Nuxt Vite Config

如果你正在构建需要访问 Nuxt 使用的运行时 Vite 或 webpack 配置的集成,可以使用 Kit 实用程序提取这些配置。

¥If you are building an integration that needs access to the runtime Vite or webpack config that Nuxt uses, it is possible to extract this using Kit utilities.

一些已经这样做的项目示例:

¥Some examples of projects doing this already:

以下是如何从项目访问 Vite 配置的简要示例;你可以实现类似的方法来获取 webpack 配置。

¥Here is a brief example of how you might access the Vite config from a project; you could implement a similar approach to get the webpack configuration.

import { loadNuxt, buildNuxt } from '@nuxt/kit'

// https://github.com/nuxt/nuxt/issues/14534
async function getViteConfig() {
  const nuxt = await loadNuxt({ cwd: process.cwd(), dev: false, overrides: { ssr: false } })
  return new Promise((resolve, reject) => {
    nuxt.hook('vite:extendConfig', (config, { isClient }) => {
      if (isClient) {
        resolve(config)
        throw new Error('_stop_')
      }
    })
    buildNuxt(nuxt).catch((err) => {
      if (!err.toString().includes('_stop_')) {
        reject(err)
      }
    })
  }).finally(() => nuxt.close())
}

const viteConfig = await getViteConfig()
console.log(viteConfig)