diff --git a/packages/nuxt/src/core/app.ts b/packages/nuxt/src/core/app.ts index 0fbe555090..402bca7f1b 100644 --- a/packages/nuxt/src/core/app.ts +++ b/packages/nuxt/src/core/app.ts @@ -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 { @@ -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 } } } diff --git a/packages/nuxt/src/core/utils/names.ts b/packages/nuxt/src/core/utils/names.ts index 81621cece5..c0e665cf33 100644 --- a/packages/nuxt/src/core/utils/names.ts +++ b/packages/nuxt/src/core/utils/names.ts @@ -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+$/, '')) -}