From 6f8b7a90437abc6557a9eedabfdbe437025e4743 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 27 Jun 2022 12:47:54 +0100 Subject: [PATCH] feat(kit): add `addLayout` (#5537) --- packages/kit/src/index.ts | 1 + packages/kit/src/layout.ts | 42 ++++++++++++++++++++++++++++ packages/kit/src/module/container.ts | 18 +++--------- 3 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 packages/kit/src/layout.ts diff --git a/packages/kit/src/index.ts b/packages/kit/src/index.ts index b283f63f3c..931396243b 100644 --- a/packages/kit/src/index.ts +++ b/packages/kit/src/index.ts @@ -14,6 +14,7 @@ export * from './compatibility' export * from './components' export * from './context' export * from './ignore' +export * from './layout' export * from './pages' export * from './plugin' export * from './resolve' diff --git a/packages/kit/src/layout.ts b/packages/kit/src/layout.ts new file mode 100644 index 0000000000..5bd2f986da --- /dev/null +++ b/packages/kit/src/layout.ts @@ -0,0 +1,42 @@ +import type { NuxtTemplate } from '@nuxt/schema' +import { join, parse, relative } from 'pathe' +import { kebabCase } from 'scule' +import { isNuxt2 } from './compatibility' +import { useNuxt } from './context' +import { logger } from './logger' +import { addTemplate } from './template' + +export function addLayout (tmpl: NuxtTemplate, name?: string) { + const nuxt = useNuxt() + const { filename, src } = addTemplate(tmpl) + const layoutName = kebabCase(name || parse(tmpl.filename).name).replace(/["']/g, '') + + if (isNuxt2(nuxt)) { + // Nuxt 2 adds layouts in options + const layout = nuxt.options.layouts[layoutName] + if (layout) { + return logger.warn( + `Not overriding \`${layoutName}\` (provided by \`${layout}\`) with \`${src || filename}\`.` + ) + } + nuxt.options.layouts[layoutName] = `./${filename}` + if (name === 'error') { + this.addErrorLayout(filename) + } + return + } + + // 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) + return logger.warn( + `Not overriding \`${layoutName}\` (provided by \`~/${relativePath}\`) with \`${src || filename}\`.` + ) + } + app.layouts[layoutName] = { + file: join('#build', filename), + name: layoutName + } + }) +} diff --git a/packages/kit/src/module/container.ts b/packages/kit/src/module/container.ts index 8a75b40c6f..4cdd89e0fd 100644 --- a/packages/kit/src/module/container.ts +++ b/packages/kit/src/module/container.ts @@ -1,8 +1,8 @@ -import { parse, relative } from 'pathe' +import { relative } from 'pathe' import type { Nuxt, NuxtPluginTemplate, NuxtTemplate, ModuleContainer } from '@nuxt/schema' -import { logger } from '../logger' import { chainFn } from '../internal/task' import { addTemplate } from '../template' +import { addLayout } from '../layout' import { addServerMiddleware } from '../server' import { isNuxt2 } from '../compatibility' import { addPluginTemplate } from '../plugin' @@ -61,18 +61,8 @@ export function useModuleContainer (nuxt: Nuxt = useNuxt()): ModuleContainer { return addPluginTemplate(pluginTemplate) }, - addLayout (tmpl: NuxtTemplate, name: string) { - const { filename, src } = addTemplate(tmpl) - const layoutName = name || parse(src).name - const layout = nuxt.options.layouts[layoutName] - - if (layout) { - logger.warn(`Duplicate layout registration, "${layoutName}" has been registered as "${layout}"`) - } - nuxt.options.layouts[layoutName] = `./${filename}` - if (name === 'error') { - this.addErrorLayout(filename) - } + addLayout (tmpl: NuxtTemplate, name?: string) { + return addLayout(tmpl, name) }, addErrorLayout (dst: string) {