diff --git a/packages/nuxi/src/commands/dev.ts b/packages/nuxi/src/commands/dev.ts index b014f1e304..6864894555 100644 --- a/packages/nuxi/src/commands/dev.ts +++ b/packages/nuxi/src/commands/dev.ts @@ -1,5 +1,6 @@ import type { AddressInfo } from 'node:net' import { RequestListener } from 'node:http' +import { existsSync, readdirSync } from 'node:fs' import { resolve, relative, normalize } from 'pathe' import chokidar from 'chokidar' import { debounce } from 'perfect-debounce' @@ -129,16 +130,28 @@ export default defineNuxtCommand({ const isDirChange = ['addDir', 'unlinkDir'].includes(event) const isFileChange = ['add', 'unlink'].includes(event) - const reloadDirs = [currentNuxt.options.dir.pages, 'components', 'composables'] - .map(d => resolve(currentNuxt.options.srcDir, d)) + const pagesDir = resolve(currentNuxt.options.srcDir, currentNuxt.options.dir.pages) + const reloadDirs = ['components', 'composables'].map(d => resolve(currentNuxt.options.srcDir, d)) if (isDirChange) { if (reloadDirs.includes(file)) { - dLoad(true, `Directory \`${relativePath}/\` ${event === 'addDir' ? 'created' : 'removed'}`) + return dLoad(true, `Directory \`${relativePath}/\` ${event === 'addDir' ? 'created' : 'removed'}`) } - } else if (isFileChange) { + } + + if (isFileChange) { if (file.match(/(app|error|app\.config)\.(js|ts|mjs|jsx|tsx|vue)$/)) { - dLoad(true, `\`${relativePath}\` ${event === 'add' ? 'created' : 'removed'}`) + return dLoad(true, `\`${relativePath}\` ${event === 'add' ? 'created' : 'removed'}`) + } + } + + if (file.startsWith(pagesDir)) { + const hasPages = existsSync(pagesDir) ? readdirSync(pagesDir).length > 0 : false + if (currentNuxt && !currentNuxt.options.pages && hasPages) { + return dLoad(true, 'Pages enabled') + } + if (currentNuxt && currentNuxt.options.pages && !hasPages) { + return dLoad(true, 'Pages disabled') } } }) diff --git a/packages/nuxt/src/pages/module.ts b/packages/nuxt/src/pages/module.ts index a059281e6b..b4e5fe7bee 100644 --- a/packages/nuxt/src/pages/module.ts +++ b/packages/nuxt/src/pages/module.ts @@ -1,4 +1,4 @@ -import { existsSync } from 'node:fs' +import { existsSync, readdirSync } from 'node:fs' import { defineNuxtModule, addTemplate, addPlugin, addVitePlugin, addWebpackPlugin, findPath, addComponent, updateTemplates } from '@nuxt/kit' import { relative, resolve } from 'pathe' import { genString, genImport, genObjectFromRawEntries } from 'knitwork' @@ -18,16 +18,32 @@ export default defineNuxtModule({ layer => resolve(layer.config.srcDir, layer.config.dir?.pages || 'pages') ) - const isRouterOptionsPresent = nuxt.options._layers.some(layer => existsSync(resolve(layer.config.srcDir, 'app/router.options.ts'))) - // Disable module (and use universal router) if pages dir do not exists or user has disabled it - if ((nuxt.options.pages === false || (nuxt.options.pages !== true && !pagesDirs.some(dir => existsSync(dir)))) && !isRouterOptionsPresent) { + const isNonEmptyDir = (dir: string) => existsSync(dir) && readdirSync(dir).length + const isPagesEnabled = () => { + if (typeof nuxt.options.pages === 'boolean') { + return nuxt.options.pages + } + 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 + } + return false + } + nuxt.options.pages = isPagesEnabled() + + if (!nuxt.options.pages) { addPlugin(resolve(distDir, 'app/plugins/router')) - // Add vue-router import for `` integration addTemplate({ filename: 'pages.mjs', getContents: () => 'export { useRoute } from \'#app\'' }) + addComponent({ + name: 'NuxtPage', + filePath: resolve(distDir, 'pages/runtime/page-placeholder') + }) return } diff --git a/packages/nuxt/src/pages/runtime/page-placeholder.ts b/packages/nuxt/src/pages/runtime/page-placeholder.ts new file mode 100644 index 0000000000..c005052bc9 --- /dev/null +++ b/packages/nuxt/src/pages/runtime/page-placeholder.ts @@ -0,0 +1,11 @@ +import { defineComponent } from 'vue' + +export default defineComponent({ + name: 'NuxtPage', + setup (_, props) { + if (process.dev) { + console.warn('Create a Vue component in the `pages/` directory to enable ``') + } + return () => props.slots.default?.() + } +})