Nuxt/packages/kit/src/layout.ts

27 lines
940 B
TypeScript
Raw Normal View History

2022-06-27 11:47:54 +00:00
import type { NuxtTemplate } from '@nuxt/schema'
import { join, parse, relative } from 'pathe'
import { kebabCase } from 'scule'
import { useNuxt } from './context'
import { logger } from './logger'
import { addTemplate } from './template'
export function addLayout (this: any, template: NuxtTemplate | string, name?: string) {
2022-06-27 11:47:54 +00:00
const nuxt = useNuxt()
const { filename, src } = addTemplate(template)
const layoutName = kebabCase(name || parse(filename).name).replace(/["']/g, '')
2022-06-27 11:47:54 +00:00
// Nuxt 3 adds layouts on app
nuxt.hook('app:templates', (app) => {
if (layoutName in app.layouts) {
const relativePath = relative(nuxt.options.srcDir, app.layouts[layoutName]!.file)
2022-06-27 11:47:54 +00:00
return logger.warn(
`Not overriding \`${layoutName}\` (provided by \`~/${relativePath}\`) with \`${src || filename}\`.`,
2022-06-27 11:47:54 +00:00
)
}
app.layouts[layoutName] = {
file: join('#build', filename),
name: layoutName,
2022-06-27 11:47:54 +00:00
}
})
}