Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
fix(nuxt, nuxi): improve pages creation and removal dx (#8502)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Oct 27, 2022
1 parent 7302bf2 commit 1f6b3be
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
23 changes: 18 additions & 5 deletions packages/nuxi/src/commands/dev.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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')
}
}
})
Expand Down
26 changes: 21 additions & 5 deletions packages/nuxt/src/pages/module.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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 `<NuxtLayout>` integration
addTemplate({
filename: 'pages.mjs',
getContents: () => 'export { useRoute } from \'#app\''
})
addComponent({
name: 'NuxtPage',
filePath: resolve(distDir, 'pages/runtime/page-placeholder')
})
return
}

Expand Down
11 changes: 11 additions & 0 deletions packages/nuxt/src/pages/runtime/page-placeholder.ts
Original file line number Diff line number Diff line change
@@ -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?.()
}
})

0 comments on commit 1f6b3be

Please sign in to comment.