mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-16 21:58:19 +00:00
feat(kit): createResolver
utility (#3111)
This commit is contained in:
parent
03d5fdde2d
commit
82a7fd561b
@ -175,14 +175,12 @@ If you have an already published and working module and want to transfer it to n
|
|||||||
Commonly, modules provide one or more run plugins to add runtime logic.
|
Commonly, modules provide one or more run plugins to add runtime logic.
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
import { resolve } from 'path'
|
import { defineNuxtModule, addPlugin, createResolver } from '@nuxt/kit'
|
||||||
import { fileURLToPath } from 'url'
|
|
||||||
import { defineNuxtModule, addPlugin } from '@nuxt/kit'
|
|
||||||
|
|
||||||
export default defineNuxtModule<ModuleOptions>({
|
export default defineNuxtModule<ModuleOptions>({
|
||||||
setup (options, nuxt) {
|
setup (options, nuxt) {
|
||||||
const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url))
|
const resolver = createResolver(import.meta.url)
|
||||||
addPlugin(resolve(runtimeDir, 'plugin'))
|
addPlugin(resolver.resolve('runtime/plugin'))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
@ -114,6 +114,7 @@ console.log(getNuxtVersion())
|
|||||||
- `resolvePath (path, resolveOptions?)`
|
- `resolvePath (path, resolveOptions?)`
|
||||||
- `resolveAlias (path, aliases?)`
|
- `resolveAlias (path, aliases?)`
|
||||||
- `findPath (paths, resolveOptions?)`
|
- `findPath (paths, resolveOptions?)`
|
||||||
|
- `createResolver (base)`
|
||||||
|
|
||||||
### Builder
|
### Builder
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { promises as fsp, existsSync } from 'fs'
|
import { promises as fsp, existsSync } from 'fs'
|
||||||
|
import { fileURLToPath } from 'url'
|
||||||
import { basename, dirname, resolve, join, normalize, isAbsolute } from 'pathe'
|
import { basename, dirname, resolve, join, normalize, isAbsolute } from 'pathe'
|
||||||
import { globby } from 'globby'
|
import { globby } from 'globby'
|
||||||
import { useNuxt } from './context'
|
import { useNuxt } from './context'
|
||||||
@ -22,18 +23,19 @@ export interface ResolvePathOptions {
|
|||||||
*/
|
*/
|
||||||
export async function resolvePath (path: string, opts: ResolvePathOptions = {}): Promise<string> {
|
export async function resolvePath (path: string, opts: ResolvePathOptions = {}): Promise<string> {
|
||||||
// Always normalize input
|
// Always normalize input
|
||||||
|
const _path = path
|
||||||
path = normalize(path)
|
path = normalize(path)
|
||||||
|
|
||||||
// Fast return if the path exists
|
// Fast return if the path exists
|
||||||
if (existsSync(path)) {
|
if (isAbsolute(path) && existsSync(path)) {
|
||||||
return path
|
return path
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use current nuxt options
|
// Use current nuxt options
|
||||||
const nuxt = useNuxt()
|
const nuxt = useNuxt()
|
||||||
const cwd = opts.cwd || nuxt.options.rootDir
|
const cwd = opts.cwd || (nuxt ? nuxt.options.rootDir : process.cwd())
|
||||||
const extensions = opts.extensions || nuxt.options.extensions
|
const extensions = opts.extensions || (nuxt ? nuxt.options.extensions : ['.ts', '.mjs', '.cjs', '.json'])
|
||||||
const modulesDir = nuxt.options.modulesDir
|
const modulesDir = nuxt ? nuxt.options.modulesDir : []
|
||||||
|
|
||||||
// Resolve aliases
|
// Resolve aliases
|
||||||
path = resolveAlias(path)
|
path = resolveAlias(path)
|
||||||
@ -67,7 +69,7 @@ export async function resolvePath (path: string, opts: ResolvePathOptions = {}):
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Try to resolve as module id
|
// Try to resolve as module id
|
||||||
const resolveModulePath = tryResolveModule(path, { paths: modulesDir })
|
const resolveModulePath = tryResolveModule(_path, { paths: [cwd, ...modulesDir] })
|
||||||
if (resolveModulePath) {
|
if (resolveModulePath) {
|
||||||
return resolveModulePath
|
return resolveModulePath
|
||||||
}
|
}
|
||||||
@ -94,7 +96,7 @@ export async function findPath (paths: string[], opts?: ResolvePathOptions): Pro
|
|||||||
*/
|
*/
|
||||||
export function resolveAlias (path: string, alias?: Record<string, string>): string {
|
export function resolveAlias (path: string, alias?: Record<string, string>): string {
|
||||||
if (!alias) {
|
if (!alias) {
|
||||||
alias = useNuxt().options.alias
|
alias = useNuxt()?.options.alias || {}
|
||||||
}
|
}
|
||||||
for (const key in alias) {
|
for (const key in alias) {
|
||||||
if (key === '@' && !path.startsWith('@/')) { continue } // Don't resolve @foo/bar
|
if (key === '@' && !path.startsWith('@/')) { continue } // Don't resolve @foo/bar
|
||||||
@ -105,6 +107,30 @@ export function resolveAlias (path: string, alias?: Record<string, string>): str
|
|||||||
return path
|
return path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface Resolver {
|
||||||
|
resolve(...path): string
|
||||||
|
resolvePath(path: string, opts?: ResolvePathOptions): Promise<string>
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a relative resolver
|
||||||
|
*/
|
||||||
|
export function createResolver (base: string | URL): Resolver {
|
||||||
|
if (!base) {
|
||||||
|
throw new Error('`base` argument is missing for createResolver(base)!')
|
||||||
|
}
|
||||||
|
|
||||||
|
base = base.toString()
|
||||||
|
if (base.startsWith('file://')) {
|
||||||
|
base = dirname(fileURLToPath(base))
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
resolve: (...path) => resolve(base as string, ...path),
|
||||||
|
resolvePath: (path, opts) => resolvePath(path, { cwd: base as string, ...opts })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// --- Internal ---
|
// --- Internal ---
|
||||||
|
|
||||||
async function existsSensitive (path: string) {
|
async function existsSensitive (path: string) {
|
||||||
|
Loading…
Reference in New Issue
Block a user