fix(nuxt, nuxi): improve pages creation and removal dx (#8502)

This commit is contained in:
pooya parsa 2022-10-27 17:50:24 +02:00 committed by GitHub
parent 7302bf2f6c
commit 1f6b3be7d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 10 deletions

View File

@ -1,5 +1,6 @@
import type { AddressInfo } from 'node:net' import type { AddressInfo } from 'node:net'
import { RequestListener } from 'node:http' import { RequestListener } from 'node:http'
import { existsSync, readdirSync } from 'node:fs'
import { resolve, relative, normalize } from 'pathe' import { resolve, relative, normalize } from 'pathe'
import chokidar from 'chokidar' import chokidar from 'chokidar'
import { debounce } from 'perfect-debounce' import { debounce } from 'perfect-debounce'
@ -129,16 +130,28 @@ export default defineNuxtCommand({
const isDirChange = ['addDir', 'unlinkDir'].includes(event) const isDirChange = ['addDir', 'unlinkDir'].includes(event)
const isFileChange = ['add', 'unlink'].includes(event) const isFileChange = ['add', 'unlink'].includes(event)
const reloadDirs = [currentNuxt.options.dir.pages, 'components', 'composables'] const pagesDir = resolve(currentNuxt.options.srcDir, currentNuxt.options.dir.pages)
.map(d => resolve(currentNuxt.options.srcDir, d)) const reloadDirs = ['components', 'composables'].map(d => resolve(currentNuxt.options.srcDir, d))
if (isDirChange) { if (isDirChange) {
if (reloadDirs.includes(file)) { 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)$/)) { 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')
} }
} }
}) })

View File

@ -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 { defineNuxtModule, addTemplate, addPlugin, addVitePlugin, addWebpackPlugin, findPath, addComponent, updateTemplates } from '@nuxt/kit'
import { relative, resolve } from 'pathe' import { relative, resolve } from 'pathe'
import { genString, genImport, genObjectFromRawEntries } from 'knitwork' import { genString, genImport, genObjectFromRawEntries } from 'knitwork'
@ -18,16 +18,32 @@ export default defineNuxtModule({
layer => resolve(layer.config.srcDir, layer.config.dir?.pages || 'pages') 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 // 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')) addPlugin(resolve(distDir, 'app/plugins/router'))
// Add vue-router import for `<NuxtLayout>` integration
addTemplate({ addTemplate({
filename: 'pages.mjs', filename: 'pages.mjs',
getContents: () => 'export { useRoute } from \'#app\'' getContents: () => 'export { useRoute } from \'#app\''
}) })
addComponent({
name: 'NuxtPage',
filePath: resolve(distDir, 'pages/runtime/page-placeholder')
})
return return
} }

View File

@ -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 `<NuxtPage>`')
}
return () => props.slots.default?.()
}
})