2022-10-27 15:50:24 +00:00
|
|
|
import { existsSync, readdirSync } from 'node:fs'
|
2023-05-09 17:08:07 +00:00
|
|
|
import { mkdir, readFile } from 'node:fs/promises'
|
2023-04-07 16:02:47 +00:00
|
|
|
import { addComponent, addPlugin, addTemplate, addVitePlugin, addWebpackPlugin, defineNuxtModule, findPath, updateTemplates } from '@nuxt/kit'
|
2023-05-09 17:08:07 +00:00
|
|
|
import { dirname, join, relative, resolve } from 'pathe'
|
2023-04-07 16:02:47 +00:00
|
|
|
import { genImport, genObjectFromRawEntries, genString } from 'knitwork'
|
2022-01-27 11:13:32 +00:00
|
|
|
import escapeRE from 'escape-string-regexp'
|
2022-07-07 15:07:37 +00:00
|
|
|
import { joinURL } from 'ufo'
|
2023-03-11 21:16:01 +00:00
|
|
|
import type { NuxtApp, NuxtPage } from 'nuxt/schema'
|
2023-05-09 17:08:07 +00:00
|
|
|
import { createRoutesContext } from 'unplugin-vue-router'
|
|
|
|
import { resolveOptions } from 'unplugin-vue-router/options'
|
|
|
|
import type { EditableTreeNode, Options as TypedRouterOptions } from 'unplugin-vue-router'
|
2023-03-11 21:16:01 +00:00
|
|
|
|
2021-08-11 21:26:47 +00:00
|
|
|
import { distDir } from '../dirs'
|
2023-04-07 16:02:47 +00:00
|
|
|
import { normalizeRoutes, resolvePagesRoutes } from './utils'
|
2022-12-11 21:44:52 +00:00
|
|
|
import type { PageMetaPluginOptions } from './page-meta'
|
|
|
|
import { PageMetaPlugin } from './page-meta'
|
2021-05-20 11:42:41 +00:00
|
|
|
|
2023-05-22 20:25:42 +00:00
|
|
|
const OPTIONAL_PARAM_RE = /^\/?:.*(\?|\(\.\*\)\*)$/
|
|
|
|
|
2021-05-20 11:42:41 +00:00
|
|
|
export default defineNuxtModule({
|
2022-01-05 18:09:53 +00:00
|
|
|
meta: {
|
2022-04-13 17:18:51 +00:00
|
|
|
name: 'pages'
|
2022-01-05 18:09:53 +00:00
|
|
|
},
|
2023-05-09 17:08:07 +00:00
|
|
|
async setup (_options, nuxt) {
|
|
|
|
const useExperimentalTypedPages = nuxt.options.experimental.typedPages
|
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
const pagesDirs = nuxt.options._layers.map(
|
|
|
|
layer => resolve(layer.config.srcDir, layer.config.dir?.pages || 'pages')
|
|
|
|
)
|
2021-07-28 11:35:24 +00:00
|
|
|
|
2022-04-13 17:18:51 +00:00
|
|
|
// Disable module (and use universal router) if pages dir do not exists or user has disabled it
|
2022-10-27 15:50:24 +00:00
|
|
|
const isNonEmptyDir = (dir: string) => existsSync(dir) && readdirSync(dir).length
|
2023-03-09 11:46:08 +00:00
|
|
|
const userPreference = nuxt.options.pages
|
2023-05-15 12:47:30 +00:00
|
|
|
const isPagesEnabled = async () => {
|
2023-03-09 11:46:08 +00:00
|
|
|
if (typeof userPreference === 'boolean') {
|
|
|
|
return userPreference
|
2022-10-27 15:50:24 +00:00
|
|
|
}
|
|
|
|
if (nuxt.options._layers.some(layer => existsSync(resolve(layer.config.srcDir, 'app/router.options.ts')))) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if (pagesDirs.some(dir => isNonEmptyDir(dir))) {
|
|
|
|
return true
|
|
|
|
}
|
2023-05-15 12:47:30 +00:00
|
|
|
|
|
|
|
const pages = await resolvePagesRoutes()
|
|
|
|
await nuxt.callHook('pages:extend', pages)
|
|
|
|
if (pages.length) { return true }
|
|
|
|
|
2022-10-27 15:50:24 +00:00
|
|
|
return false
|
|
|
|
}
|
2023-05-15 12:47:30 +00:00
|
|
|
nuxt.options.pages = await isPagesEnabled()
|
2022-10-27 15:50:24 +00:00
|
|
|
|
2023-03-09 11:46:08 +00:00
|
|
|
// Restart Nuxt when pages dir is added or removed
|
|
|
|
const restartPaths = nuxt.options._layers.flatMap(layer => [
|
|
|
|
join(layer.config.srcDir, 'app/router.options.ts'),
|
|
|
|
join(layer.config.srcDir, layer.config.dir?.pages || 'pages')
|
|
|
|
])
|
2023-05-15 12:47:30 +00:00
|
|
|
nuxt.hooks.hook('builder:watch', async (event, path) => {
|
2023-03-09 11:46:08 +00:00
|
|
|
const fullPath = join(nuxt.options.srcDir, path)
|
|
|
|
if (restartPaths.some(path => path === fullPath || fullPath.startsWith(path + '/'))) {
|
2023-05-15 12:47:30 +00:00
|
|
|
const newSetting = await isPagesEnabled()
|
2023-03-09 11:46:08 +00:00
|
|
|
if (nuxt.options.pages !== newSetting) {
|
|
|
|
console.info('Pages', newSetting ? 'enabled' : 'disabled')
|
|
|
|
return nuxt.callHook('restart')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-05-22 13:48:08 +00:00
|
|
|
// adds support for #vue-router alias (used for types) with and without pages integration
|
|
|
|
addTemplate({
|
|
|
|
filename: 'vue-router.d.ts',
|
|
|
|
getContents: () => `export * from '${useExperimentalTypedPages ? 'vue-router/auto' : 'vue-router'}'`
|
|
|
|
})
|
|
|
|
|
|
|
|
nuxt.options.alias['#vue-router'] = join(nuxt.options.buildDir, 'vue-router')
|
|
|
|
|
2022-10-27 15:50:24 +00:00
|
|
|
if (!nuxt.options.pages) {
|
2022-02-21 13:03:42 +00:00
|
|
|
addPlugin(resolve(distDir, 'app/plugins/router'))
|
2022-10-24 08:36:49 +00:00
|
|
|
addTemplate({
|
|
|
|
filename: 'pages.mjs',
|
|
|
|
getContents: () => 'export { useRoute } from \'#app\''
|
|
|
|
})
|
2022-10-27 15:50:24 +00:00
|
|
|
addComponent({
|
|
|
|
name: 'NuxtPage',
|
2023-03-06 11:33:40 +00:00
|
|
|
priority: 10, // built-in that we do not expect the user to override
|
2022-10-27 15:50:24 +00:00
|
|
|
filePath: resolve(distDir, 'pages/runtime/page-placeholder')
|
|
|
|
})
|
2021-07-28 11:35:24 +00:00
|
|
|
return
|
|
|
|
}
|
2021-05-20 11:42:41 +00:00
|
|
|
|
2023-05-22 13:48:08 +00:00
|
|
|
addTemplate({
|
|
|
|
filename: 'vue-router.mjs',
|
|
|
|
// TODO: use `vue-router/auto` when we have support for page metadata
|
|
|
|
getContents: () => 'export * from \'vue-router\';'
|
|
|
|
})
|
|
|
|
|
2023-05-09 17:08:07 +00:00
|
|
|
if (useExperimentalTypedPages) {
|
|
|
|
const declarationFile = './types/typed-router.d.ts'
|
|
|
|
|
|
|
|
const options: TypedRouterOptions = {
|
|
|
|
routesFolder: [],
|
|
|
|
dts: resolve(nuxt.options.buildDir, declarationFile),
|
|
|
|
logs: nuxt.options.debug,
|
|
|
|
async beforeWriteFiles (rootPage) {
|
|
|
|
rootPage.children.forEach(child => child.delete())
|
|
|
|
const pages = await resolvePagesRoutes()
|
|
|
|
await nuxt.callHook('pages:extend', pages)
|
|
|
|
function addPage (parent: EditableTreeNode, page: NuxtPage) {
|
|
|
|
// @ts-expect-error TODO: either fix types upstream or figure out another
|
|
|
|
// way to add a route without a file, which must be possible
|
|
|
|
const route = parent.insert(page.path, page.file)
|
|
|
|
if (page.meta) {
|
|
|
|
route.addToMeta(page.meta)
|
|
|
|
}
|
|
|
|
if (page.alias) {
|
|
|
|
route.addAlias(page.alias)
|
|
|
|
}
|
|
|
|
if (page.name) {
|
|
|
|
route.name = page.name
|
|
|
|
}
|
|
|
|
// TODO: implement redirect support
|
|
|
|
// if (page.redirect) {}
|
|
|
|
if (page.children) {
|
|
|
|
page.children.forEach(child => addPage(route, child))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const page of pages) {
|
|
|
|
addPage(rootPage, page)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nuxt.hook('prepare:types', ({ references }) => {
|
|
|
|
// This file will be generated by unplugin-vue-router
|
|
|
|
references.push({ path: declarationFile })
|
|
|
|
})
|
|
|
|
|
|
|
|
const context = createRoutesContext(resolveOptions(options))
|
|
|
|
const dtsFile = resolve(nuxt.options.buildDir, declarationFile)
|
|
|
|
await mkdir(dirname(dtsFile), { recursive: true })
|
|
|
|
await context.scanPages(false)
|
|
|
|
|
|
|
|
if (nuxt.options._prepare) {
|
|
|
|
// TODO: could we generate this from context instead?
|
|
|
|
const dts = await readFile(dtsFile, 'utf-8')
|
|
|
|
addTemplate({
|
|
|
|
filename: 'types/typed-router.d.ts',
|
|
|
|
getContents: () => dts
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regenerate types/typed-router.d.ts when adding or removing pages
|
|
|
|
nuxt.hook('builder:generateApp', async (options) => {
|
|
|
|
if (!options?.filter || options.filter({ filename: 'routes.mjs' } as any)) {
|
|
|
|
await context.scanPages()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
const runtimeDir = resolve(distDir, 'pages/runtime')
|
|
|
|
|
2021-11-02 09:39:42 +00:00
|
|
|
// Add $router types
|
|
|
|
nuxt.hook('prepare:types', ({ references }) => {
|
2023-05-09 17:08:07 +00:00
|
|
|
references.push({ types: useExperimentalTypedPages ? 'vue-router/auto' : 'vue-router' })
|
2021-11-02 09:39:42 +00:00
|
|
|
})
|
|
|
|
|
2022-11-10 13:52:04 +00:00
|
|
|
// Add vue-router route guard imports
|
|
|
|
nuxt.hook('imports:sources', (sources) => {
|
|
|
|
const routerImports = sources.find(s => s.from === '#app' && s.imports.includes('onBeforeRouteLeave'))
|
|
|
|
if (routerImports) {
|
2023-05-09 17:08:07 +00:00
|
|
|
routerImports.from = '#vue-router'
|
2022-11-10 13:52:04 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-07-28 11:35:24 +00:00
|
|
|
// Regenerate templates when adding or removing pages
|
2021-05-20 11:42:41 +00:00
|
|
|
nuxt.hook('builder:watch', async (event, path) => {
|
2022-01-27 11:13:32 +00:00
|
|
|
const dirs = [
|
|
|
|
nuxt.options.dir.pages,
|
2022-06-27 12:10:29 +00:00
|
|
|
nuxt.options.dir.layouts,
|
2022-01-27 11:13:32 +00:00
|
|
|
nuxt.options.dir.middleware
|
|
|
|
].filter(Boolean)
|
|
|
|
|
2022-06-10 14:50:47 +00:00
|
|
|
const pathPattern = new RegExp(`(^|\\/)(${dirs.map(escapeRE).join('|')})/`)
|
2023-05-22 20:25:42 +00:00
|
|
|
if (event !== 'change' && pathPattern.test(path)) {
|
2022-11-02 10:28:41 +00:00
|
|
|
await updateTemplates({
|
|
|
|
filter: template => template.filename === 'routes.mjs'
|
|
|
|
})
|
2021-05-20 11:42:41 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
nuxt.hook('app:resolve', (app) => {
|
2021-10-12 12:51:41 +00:00
|
|
|
// Add default layout for pages
|
2022-08-12 17:47:58 +00:00
|
|
|
if (app.mainComponent!.includes('@nuxt/ui-templates')) {
|
2021-10-12 12:51:41 +00:00
|
|
|
app.mainComponent = resolve(runtimeDir, 'app.vue')
|
2021-05-20 11:42:41 +00:00
|
|
|
}
|
2022-10-10 10:18:20 +00:00
|
|
|
app.middleware.unshift({
|
|
|
|
name: 'validate',
|
|
|
|
path: resolve(runtimeDir, 'validate'),
|
|
|
|
global: true
|
|
|
|
})
|
2021-05-20 11:42:41 +00:00
|
|
|
})
|
|
|
|
|
2023-06-29 09:14:35 +00:00
|
|
|
nuxt.hook('nitro:init', (nitro) => {
|
|
|
|
if (nuxt.options.dev || !nitro.options.static) { return }
|
|
|
|
// Prerender all non-dynamic page routes when generating app
|
2022-07-12 10:56:40 +00:00
|
|
|
const prerenderRoutes = new Set<string>()
|
2023-07-12 07:26:52 +00:00
|
|
|
nuxt.hook('pages:extend', (pages) => {
|
|
|
|
prerenderRoutes.clear()
|
|
|
|
const processPages = (pages: NuxtPage[], currentPath = '/') => {
|
|
|
|
for (const page of pages) {
|
|
|
|
// Add root of optional dynamic paths and catchalls
|
|
|
|
if (OPTIONAL_PARAM_RE.test(page.path) && !page.children?.length) { prerenderRoutes.add(currentPath) }
|
|
|
|
// Skip dynamic paths
|
|
|
|
if (page.path.includes(':')) { continue }
|
|
|
|
const route = joinURL(currentPath, page.path)
|
|
|
|
prerenderRoutes.add(route)
|
|
|
|
if (page.children) { processPages(page.children, route) }
|
2022-07-07 15:07:37 +00:00
|
|
|
}
|
2023-07-12 07:26:52 +00:00
|
|
|
}
|
|
|
|
processPages(pages)
|
2022-07-07 15:07:37 +00:00
|
|
|
})
|
|
|
|
nuxt.hook('nitro:build:before', (nitro) => {
|
2022-07-12 10:56:40 +00:00
|
|
|
for (const route of nitro.options.prerender.routes || []) {
|
2022-11-03 14:52:03 +00:00
|
|
|
// Skip default route value as we only generate it if it is already
|
|
|
|
// in the detected routes from `~/pages`.
|
|
|
|
if (route === '/') { continue }
|
2022-07-12 10:56:40 +00:00
|
|
|
prerenderRoutes.add(route)
|
|
|
|
}
|
|
|
|
nitro.options.prerender.routes = Array.from(prerenderRoutes)
|
2022-07-07 15:07:37 +00:00
|
|
|
})
|
2023-06-29 09:14:35 +00:00
|
|
|
})
|
2022-07-07 15:07:37 +00:00
|
|
|
|
2022-08-23 14:22:11 +00:00
|
|
|
nuxt.hook('imports:extend', (imports) => {
|
|
|
|
imports.push(
|
2022-07-07 17:28:23 +00:00
|
|
|
{ name: 'definePageMeta', as: 'definePageMeta', from: resolve(runtimeDir, 'composables') },
|
2023-05-09 17:08:07 +00:00
|
|
|
{ name: 'useLink', as: 'useLink', from: '#vue-router' }
|
2022-07-07 17:28:23 +00:00
|
|
|
)
|
2021-12-17 09:15:03 +00:00
|
|
|
})
|
|
|
|
|
2022-01-17 18:27:23 +00:00
|
|
|
// Extract macros from pages
|
2022-11-02 10:28:41 +00:00
|
|
|
const pageMetaOptions: PageMetaPluginOptions = {
|
2022-01-19 18:07:54 +00:00
|
|
|
dev: nuxt.options.dev,
|
2023-05-03 14:14:12 +00:00
|
|
|
sourcemap: nuxt.options.sourcemap.server || nuxt.options.sourcemap.client
|
2022-01-17 18:27:23 +00:00
|
|
|
}
|
2023-04-10 21:57:13 +00:00
|
|
|
nuxt.hook('modules:done', () => {
|
2023-05-02 11:17:41 +00:00
|
|
|
addVitePlugin(() => PageMetaPlugin.vite(pageMetaOptions))
|
|
|
|
addWebpackPlugin(() => PageMetaPlugin.webpack(pageMetaOptions))
|
2023-04-10 21:57:13 +00:00
|
|
|
})
|
2022-01-17 18:27:23 +00:00
|
|
|
|
2023-01-19 13:01:21 +00:00
|
|
|
// Add prefetching support for middleware & layouts
|
|
|
|
addPlugin(resolve(runtimeDir, 'plugins/prefetch.client'))
|
|
|
|
|
2021-11-17 11:28:36 +00:00
|
|
|
// Add router plugin
|
2023-01-19 13:01:21 +00:00
|
|
|
addPlugin(resolve(runtimeDir, 'plugins/router'))
|
2021-06-30 16:32:22 +00:00
|
|
|
|
2023-03-03 14:07:42 +00:00
|
|
|
const getSources = (pages: NuxtPage[]): string[] => pages
|
|
|
|
.filter(p => Boolean(p.file))
|
|
|
|
.flatMap(p =>
|
|
|
|
[relative(nuxt.options.srcDir, p.file as string), ...getSources(p.children || [])]
|
|
|
|
)
|
2022-08-16 11:19:39 +00:00
|
|
|
|
|
|
|
// Do not prefetch page chunks
|
|
|
|
nuxt.hook('build:manifest', async (manifest) => {
|
2023-02-27 19:36:27 +00:00
|
|
|
if (nuxt.options.dev) { return }
|
2022-08-16 11:19:39 +00:00
|
|
|
const pages = await resolvePagesRoutes()
|
|
|
|
await nuxt.callHook('pages:extend', pages)
|
|
|
|
|
|
|
|
const sourceFiles = getSources(pages)
|
2023-05-09 17:08:07 +00:00
|
|
|
|
2022-08-16 11:19:39 +00:00
|
|
|
for (const key in manifest) {
|
|
|
|
if (manifest[key].isEntry) {
|
|
|
|
manifest[key].dynamicImports =
|
|
|
|
manifest[key].dynamicImports?.filter(i => !sourceFiles.includes(i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-07-28 11:35:24 +00:00
|
|
|
// Add routes template
|
|
|
|
addTemplate({
|
|
|
|
filename: 'routes.mjs',
|
|
|
|
async getContents () {
|
2022-03-22 18:12:54 +00:00
|
|
|
const pages = await resolvePagesRoutes()
|
2021-11-09 10:16:23 +00:00
|
|
|
await nuxt.callHook('pages:extend', pages)
|
2022-02-07 13:45:47 +00:00
|
|
|
const { routes, imports } = normalizeRoutes(pages)
|
|
|
|
return [...imports, `export default ${routes}`].join('\n')
|
2021-07-28 11:35:24 +00:00
|
|
|
}
|
|
|
|
})
|
2021-06-30 16:32:22 +00:00
|
|
|
|
2022-10-24 08:36:49 +00:00
|
|
|
// Add vue-router import for `<NuxtLayout>` integration
|
|
|
|
addTemplate({
|
|
|
|
filename: 'pages.mjs',
|
|
|
|
getContents: () => 'export { useRoute } from \'vue-router\''
|
|
|
|
})
|
|
|
|
|
2022-11-02 08:53:23 +00:00
|
|
|
// Optimize vue-router to ensure we share the same injection symbol
|
|
|
|
nuxt.options.vite.optimizeDeps = nuxt.options.vite.optimizeDeps || {}
|
|
|
|
nuxt.options.vite.optimizeDeps.include = nuxt.options.vite.optimizeDeps.include || []
|
|
|
|
nuxt.options.vite.optimizeDeps.include.push('vue-router')
|
|
|
|
|
2023-01-30 18:22:15 +00:00
|
|
|
nuxt.options.vite.resolve = nuxt.options.vite.resolve || {}
|
|
|
|
nuxt.options.vite.resolve.dedupe = nuxt.options.vite.resolve.dedupe || []
|
|
|
|
nuxt.options.vite.resolve.dedupe.push('vue-router')
|
|
|
|
|
2022-03-15 16:57:41 +00:00
|
|
|
// Add router options template
|
|
|
|
addTemplate({
|
|
|
|
filename: 'router.options.mjs',
|
|
|
|
getContents: async () => {
|
2022-10-19 12:43:03 +00:00
|
|
|
// Scan and register app/router.options files
|
2022-04-04 08:23:11 +00:00
|
|
|
const routerOptionsFiles = (await Promise.all(nuxt.options._layers.map(
|
|
|
|
async layer => await findPath(resolve(layer.config.srcDir, 'app/router.options'))
|
2022-08-22 10:12:02 +00:00
|
|
|
))).filter(Boolean) as string[]
|
2022-04-04 08:23:11 +00:00
|
|
|
|
2022-10-19 12:43:03 +00:00
|
|
|
// Add default options
|
2022-10-19 14:48:16 +00:00
|
|
|
routerOptionsFiles.push(resolve(runtimeDir, 'router.options'))
|
2022-10-19 12:43:03 +00:00
|
|
|
|
2022-03-15 16:57:41 +00:00
|
|
|
const configRouterOptions = genObjectFromRawEntries(Object.entries(nuxt.options.router.options)
|
|
|
|
.map(([key, value]) => [key, genString(value as string)]))
|
2022-04-04 08:23:11 +00:00
|
|
|
|
2022-03-15 16:57:41 +00:00
|
|
|
return [
|
2022-04-04 08:23:11 +00:00
|
|
|
...routerOptionsFiles.map((file, index) => genImport(file, `routerOptions${index}`)),
|
2022-03-15 16:57:41 +00:00
|
|
|
`const configRouterOptions = ${configRouterOptions}`,
|
|
|
|
'export default {',
|
|
|
|
'...configRouterOptions,',
|
2022-04-04 08:23:11 +00:00
|
|
|
// We need to reverse spreading order to respect layers priority
|
|
|
|
...routerOptionsFiles.map((_, index) => `...routerOptions${index},`).reverse(),
|
2022-03-15 16:57:41 +00:00
|
|
|
'}'
|
|
|
|
].join('\n')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-01-25 12:29:11 +00:00
|
|
|
addTemplate({
|
2022-02-07 10:20:01 +00:00
|
|
|
filename: 'types/middleware.d.ts',
|
2022-06-27 12:10:29 +00:00
|
|
|
getContents: ({ app }: { app: NuxtApp }) => {
|
2022-01-25 12:29:11 +00:00
|
|
|
const composablesFile = resolve(runtimeDir, 'composables')
|
2022-06-27 12:10:29 +00:00
|
|
|
const namedMiddleware = app.middleware.filter(mw => !mw.global)
|
2022-01-25 12:29:11 +00:00
|
|
|
return [
|
|
|
|
'import type { NavigationGuard } from \'vue-router\'',
|
2022-02-07 13:45:47 +00:00
|
|
|
`export type MiddlewareKey = ${namedMiddleware.map(mw => genString(mw.name)).join(' | ') || 'string'}`,
|
|
|
|
`declare module ${genString(composablesFile)} {`,
|
2022-01-25 12:29:11 +00:00
|
|
|
' interface PageMeta {',
|
|
|
|
' middleware?: MiddlewareKey | NavigationGuard | Array<MiddlewareKey | NavigationGuard>',
|
|
|
|
' }',
|
|
|
|
'}'
|
|
|
|
].join('\n')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-01-26 11:56:24 +00:00
|
|
|
addTemplate({
|
2022-02-07 10:20:01 +00:00
|
|
|
filename: 'types/layouts.d.ts',
|
2022-06-27 12:10:29 +00:00
|
|
|
getContents: ({ app }: { app: NuxtApp }) => {
|
2022-01-26 11:56:24 +00:00
|
|
|
const composablesFile = resolve(runtimeDir, 'composables')
|
|
|
|
return [
|
|
|
|
'import { ComputedRef, Ref } from \'vue\'',
|
2022-03-14 10:47:24 +00:00
|
|
|
`export type LayoutKey = ${Object.keys(app.layouts).map(name => genString(name)).join(' | ') || 'string'}`,
|
2022-02-07 13:45:47 +00:00
|
|
|
`declare module ${genString(composablesFile)} {`,
|
2022-01-26 11:56:24 +00:00
|
|
|
' interface PageMeta {',
|
|
|
|
' layout?: false | LayoutKey | Ref<LayoutKey> | ComputedRef<LayoutKey>',
|
|
|
|
' }',
|
|
|
|
'}'
|
|
|
|
].join('\n')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-10-14 08:36:52 +00:00
|
|
|
// Add <NuxtPage>
|
|
|
|
addComponent({
|
|
|
|
name: 'NuxtPage',
|
2023-03-06 11:33:40 +00:00
|
|
|
priority: 10, // built-in that we do not expect the user to override
|
2022-10-14 08:36:52 +00:00
|
|
|
filePath: resolve(distDir, 'pages/runtime/page')
|
|
|
|
})
|
|
|
|
|
2022-03-14 10:47:24 +00:00
|
|
|
// Add declarations for middleware keys
|
2022-01-27 11:13:32 +00:00
|
|
|
nuxt.hook('prepare:types', ({ references }) => {
|
2022-02-07 10:20:01 +00:00
|
|
|
references.push({ path: resolve(nuxt.options.buildDir, 'types/middleware.d.ts') })
|
|
|
|
references.push({ path: resolve(nuxt.options.buildDir, 'types/layouts.d.ts') })
|
2023-05-09 17:08:07 +00:00
|
|
|
references.push({ path: resolve(nuxt.options.buildDir, 'vue-router.d.ts') })
|
2022-01-27 11:13:32 +00:00
|
|
|
})
|
2021-05-20 11:42:41 +00:00
|
|
|
}
|
|
|
|
})
|