解決
モジュールを基準としたパスを、不明な名前や拡張子で解決する必要がある場合があります。たとえば、モジュールと同じディレクトリにあるプラグインを追加したい場合があります。このケースを処理するために、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
ベースパスを基準としたリゾルバーを作成します。
タイプ
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'))
}
})
}
})