mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-13 09:33:54 +00:00
refactor: add functionality to getNameFromPath
This commit is contained in:
parent
1512a2402d
commit
75990c5dbc
@ -5,7 +5,7 @@ import { compileTemplate, findPath, logger, normalizePlugin, normalizeTemplate,
|
||||
import type { Nuxt, NuxtApp, NuxtPlugin, NuxtTemplate, ResolvedNuxtTemplate } from 'nuxt/schema'
|
||||
|
||||
import * as defaultTemplates from './templates'
|
||||
import { getNameFromPath, getNameFromPathLocal, hasSuffix, uniqueBy } from './utils'
|
||||
import { getNameFromPath, hasSuffix, uniqueBy } from './utils'
|
||||
import { extractMetadata, orderMap } from './plugins/plugin-metadata'
|
||||
|
||||
export function createApp (nuxt: Nuxt, options: Partial<NuxtApp> = {}): NuxtApp {
|
||||
@ -112,7 +112,7 @@ export async function resolveApp (nuxt: Nuxt, app: NuxtApp) {
|
||||
const layoutDir = (config.rootDir === nuxt.options.rootDir ? nuxt.options : config).dir?.layouts || 'layouts'
|
||||
const layoutFiles = await resolveFiles(config.srcDir, `${layoutDir}/**/*{${nuxt.options.extensions.join(',')}}`)
|
||||
for (const file of layoutFiles) {
|
||||
const name = getNameFromPathLocal(file, resolve(config.srcDir, layoutDir))
|
||||
const name = getNameFromPath(file, resolve(config.srcDir, layoutDir))
|
||||
app.layouts[name] = app.layouts[name] || { name, file }
|
||||
}
|
||||
}
|
||||
|
@ -1,16 +1,14 @@
|
||||
import { basename, extname } from 'pathe'
|
||||
import { basename, extname, normalize } from 'pathe'
|
||||
import { kebabCase } from 'scule'
|
||||
import { withTrailingSlash } from 'ufo'
|
||||
|
||||
export function getNameFromPath (path: string) {
|
||||
return kebabCase(basename(path).replace(extname(path), '')).replace(/["']/g, '')
|
||||
export function getNameFromPath (path: string, relativeTo?: string) {
|
||||
const relativePath = relativeTo
|
||||
? normalize(path).replace(withTrailingSlash(normalize(relativeTo)), '')
|
||||
: basename(path)
|
||||
return kebabCase(relativePath.replace(/[\\/]+/g, '-').replace(/\/index\.\w+$/, '').replace(extname(relativePath), '')).replace(/["']/g, '')
|
||||
}
|
||||
|
||||
export function hasSuffix (path: string, suffix: string) {
|
||||
return basename(path).replace(extname(path), '').endsWith(suffix)
|
||||
}
|
||||
|
||||
export function getNameFromPathLocal (path: string, localDir: string) {
|
||||
const file = path.replace(withTrailingSlash(localDir), '')
|
||||
return getNameFromPath(file.replace(/[\\/]+/g, '-').replace(/\/index\.\w+$/, ''))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user