fix(nuxt): set nuxt.options.pages to detected configuration (#31101)

This commit is contained in:
Bobbie Goede 2025-02-24 22:17:19 +01:00 committed by GitHub
parent 69c740383c
commit cb80715c1a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 27 additions and 2 deletions

View File

@ -47,11 +47,12 @@ export default defineNuxtModule({
configKey: 'pages', configKey: 'pages',
}, },
defaults: nuxt => ({ defaults: nuxt => ({
enabled: undefined as undefined | boolean, enabled: typeof nuxt.options.pages === 'boolean' ? nuxt.options.pages : undefined as undefined | boolean,
pattern: `**/*{${nuxt.options.extensions.join(',')}}` as string | string[], pattern: `**/*{${nuxt.options.extensions.join(',')}}` as string | string[],
}), }),
async setup (_options, nuxt) { async setup (_options, nuxt) {
const options = typeof _options === 'boolean' ? { enabled: _options, pattern: `**/*{${nuxt.options.extensions.join(',')}}` } : _options const options = typeof _options === 'boolean' ? { enabled: _options ?? nuxt.options.pages, pattern: `**/*{${nuxt.options.extensions.join(',')}}` } : { ..._options }
options.pattern = Array.isArray(options.pattern) ? [...new Set(options.pattern)] : options.pattern
const useExperimentalTypedPages = nuxt.options.experimental.typedPages const useExperimentalTypedPages = nuxt.options.experimental.typedPages
const builtInRouterOptions = await findPath(resolve(runtimeDir, 'router.options')) || resolve(runtimeDir, 'router.options') const builtInRouterOptions = await findPath(resolve(runtimeDir, 'router.options')) || resolve(runtimeDir, 'router.options')
@ -95,6 +96,7 @@ export default defineNuxtModule({
return false return false
} }
options.enabled = await isPagesEnabled() options.enabled = await isPagesEnabled()
nuxt.options.pages = options
if (nuxt.options.dev && options.enabled) { if (nuxt.options.dev && options.enabled) {
// Add plugin to check if pages are enabled without NuxtPage being instantiated // Add plugin to check if pages are enabled without NuxtPage being instantiated

View File

@ -4,6 +4,7 @@ import { normalize } from 'pathe'
import { withoutTrailingSlash } from 'ufo' import { withoutTrailingSlash } from 'ufo'
import { logger, tryUseNuxt, useNuxt } from '@nuxt/kit' import { logger, tryUseNuxt, useNuxt } from '@nuxt/kit'
import { loadNuxt } from '../src' import { loadNuxt } from '../src'
import type { NuxtConfig } from '../schema'
const repoRoot = withoutTrailingSlash(normalize(fileURLToPath(new URL('../../../', import.meta.url)))) const repoRoot = withoutTrailingSlash(normalize(fileURLToPath(new URL('../../../', import.meta.url))))
@ -107,3 +108,21 @@ describe('loadNuxt', () => {
expect(loggerWarn).not.toHaveBeenCalled() expect(loggerWarn).not.toHaveBeenCalled()
}) })
}) })
const pagesDetectionTests: [test: string, overrides: NuxtConfig, result: NuxtConfig['pages']][] = [
['pages dir', {}, { enabled: true }],
['pages dir empty', { dir: { pages: 'empty-dir' } }, { enabled: false }],
['user config', { pages: false }, { enabled: false }],
['user config', { pages: { enabled: false } }, { enabled: false }],
['user config', { pages: { enabled: true, pattern: '**/*{.vue}' } }, { enabled: true, pattern: '**/*{.vue}' }],
]
const pagesFixtureDir = withoutTrailingSlash(normalize(fileURLToPath(new URL('./pages-fixture', import.meta.url))))
describe('pages detection', () => {
it.each(pagesDetectionTests)('%s `%s`', async (_, overrides, result) => {
const nuxt = await loadNuxt({ cwd: pagesFixtureDir, overrides, ready: true })
// @ts-expect-error should resolve to object?
expect(nuxt.options.pages).toMatchObject(result)
await nuxt.close()
})
})

View File

@ -0,0 +1 @@
export default defineNuxtConfig({})

View File

@ -0,0 +1,3 @@
<template>
<div />
</template>