Nuxt Nation カンファレンスが近づいています。11月12〜13日にご参加ください。

解決

Nuxt Kit は、パスを解決するのに役立つユーティリティのセットを提供します。これらの関数を使用すると、現在のモジュールを基準としたパスを、不明な名前または拡張子で解決できます。

モジュールを基準としたパスを、不明な名前や拡張子で解決する必要がある場合があります。たとえば、モジュールと同じディレクトリにあるプラグインを追加したい場合があります。このケースを処理するために、nuxt はパスを解決するためのユーティリティセットを提供しています。 resolvePath および resolveAlias は、現在のモジュールを基準としたパスを解決するために使用されます。findPath は、指定されたパスで最初に存在するファイルを見つけるために使用されます。createResolver は、ベースパスを基準としたリゾルバーを作成するために使用されます。

resolvePath

Nuxt のエイリアスと拡張オプションを考慮して、ファイルまたはディレクトリへのフルパスを解決します。パスを解決できない場合は、正規化された入力パスが返されます。

タイプ

async function resolvePath (path: string, options?: ResolvePathOptions): Promise<string>

パラメータ

path

: string

必須: true

解決するパス。

options

: ResolvePathOptions

デフォルト: {}

リゾルバーに渡すオプション。このオブジェクトには次のプロパティを含めることができます。

  • cwd (オプション)
    : string
    デフォルト: process.cwd()
    現在の作業ディレクトリ。
  • alias (オプション)
    : Record<string, string>
    デフォルト: {}
    エイリアスマップ。
  • extensions (オプション)
    : string[]
    デフォルト: ['.js', '.mjs', '.ts', '.jsx', '.tsx', '.json']
    試す拡張子。

// https://github.com/P4sca1/nuxt-headlessui
import { defineNuxtModule, resolvePath } from '@nuxt/kit'
import { join } from 'pathe'

const headlessComponents: ComponentGroup[] = [
  {
    relativePath: 'combobox/combobox.js',
    chunkName: 'headlessui/combobox',
    exports: [
      'Combobox',
      'ComboboxLabel',
      'ComboboxButton',
      'ComboboxInput',
      'ComboboxOptions',
      'ComboboxOption'
    ]
  },
]

export default defineNuxtModule({
  meta: {
    name: 'nuxt-headlessui',
    configKey: 'headlessui',
  },
  defaults: {
    prefix: 'Headless'
  },
  async setup (options) {
    const entrypoint = await resolvePath('@headlessui/vue')
    const root = join(entrypoint, '../components')

    for (const group of headlessComponents) {
      for (const e of group.exports) {
        addComponent(
          {
            name: e,
            export: e,
            filePath: join(root, group.relativePath),
            chunkName: group.chunkName,
            mode: 'all'
          }
        )
      }
    }
  }
})

resolveAlias

Nuxt のエイリアスオプションを考慮してパスエイリアスを解決します。

タイプ

function resolveAlias (path: string, alias?: Record<string, string>): string

パラメータ

path

: string

必須: true

解決するパス。

alias

: Record<string, string>

デフォルト: {}

エイリアスマップ。提供されていない場合は、nuxt.options.alias から読み取られます。

findPath

指定されたパスで最初に存在するファイルを解決しようとします。

タイプ

async function findPath (paths: string | string[], options?: ResolvePathOptions, pathType: 'file' | 'dir'): Promise<string | null>

interface ResolvePathOptions {
  cwd?: string
  alias?: Record<string, string>
  extensions?: string[]
}

パラメータ

paths

: string | string[]

必須: true

解決するパスまたはパスの配列。

options

: ResolvePathOptions

デフォルト: {}

リゾルバーに渡すオプション。このオブジェクトには次のプロパティを含めることができます。

  • cwd (オプション)
    : string
    デフォルト: process.cwd()
    現在の作業ディレクトリ。
  • alias (オプション)
    : Record<string, string>
    デフォルト: {}
    エイリアスマップ。
  • extensions (オプション)
    : string[]
    デフォルト: ['.js', '.mjs', '.ts', '.jsx', '.tsx', '.json']
    試す拡張子。

pathType

: 'file' | 'dir'

デフォルト: 'file'

解決するパスのタイプ。'file' に設定すると、関数はファイルを解決しようとします。'dir' に設定すると、関数はディレクトリを解決しようとします。

createResolver

ベースパスを基準としたリゾルバーを作成します。

createResolver についての Vue School ビデオをご覧ください。

タイプ

function createResolver (basePath: string | URL): Resolver

interface Resolver {
  resolve (...path: string[]): string
  resolvePath (path: string, options?: ResolvePathOptions): Promise<string>
}

interface ResolvePathOptions {
  cwd?: string
  alias?: Record<string, string>
  extensions?: string[]
}

パラメータ

basePath

: string

必須: true

解決元のベースパス。

// https://github.com/vuejs/pinia/blob/v2/packages/nuxt
import {
  defineNuxtModule,
  isNuxt2,
  createResolver,
} from '@nuxt/kit'

export default defineNuxtModule({
  setup(options, nuxt) {
    const resolver = createResolver(import.meta.url)

    nuxt.hook('modules:done', () => {
      if (isNuxt2()) {
        addPlugin(resolver.resolve('./runtime/plugin.vue2'))
      } else {
        addPlugin(resolver.resolve('./runtime/plugin.vue3'))
      }
    })
  }
})