mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-30 15:22:39 +00:00
feat: custom pages directory
This commit is contained in:
parent
1582df149d
commit
10ac3ed2b4
@ -137,15 +137,15 @@ module.exports = class Builder {
|
||||
// Check if pages dir exists and warn if not
|
||||
this._nuxtPages = typeof this.options.build.createRoutes !== 'function'
|
||||
if (this._nuxtPages) {
|
||||
if (!existsSync(join(this.options.srcDir, 'pages'))) {
|
||||
if (!existsSync(join(this.options.srcDir, this.options.pagesDir))) {
|
||||
let dir = this.options.srcDir
|
||||
if (existsSync(join(this.options.srcDir, '..', 'pages'))) {
|
||||
if (existsSync(join(this.options.srcDir, '..', this.options.pagesDir))) {
|
||||
throw new Error(
|
||||
`No \`pages\` directory found in ${dir}. Did you mean to run \`nuxt\` in the parent (\`../\`) directory?`
|
||||
`No \`${this.options.pagesDir}\` directory found in ${dir}. Did you mean to run \`nuxt\` in the parent (\`../\`) directory?`
|
||||
)
|
||||
} else {
|
||||
throw new Error(
|
||||
`Couldn't find a \`pages\` directory in ${dir}. Please create one under the project root`
|
||||
`Couldn't find a \`${this.options.pagesDir}\` directory in ${dir}. Please create one under the project root`
|
||||
)
|
||||
}
|
||||
}
|
||||
@ -314,7 +314,7 @@ module.exports = class Builder {
|
||||
if (this._nuxtPages) {
|
||||
// Use nuxt.js createRoutes bases on pages/
|
||||
const files = {}
|
||||
;(await glob('pages/**/*.{vue,js}', {
|
||||
;(await glob(`${this.options.pagesDir}/**/*.{vue,js}`, {
|
||||
cwd: this.options.srcDir,
|
||||
ignore: this.options.ignore
|
||||
})).forEach(f => {
|
||||
@ -325,7 +325,8 @@ module.exports = class Builder {
|
||||
})
|
||||
templateVars.router.routes = createRoutes(
|
||||
Object.values(files),
|
||||
this.options.srcDir
|
||||
this.options.srcDir,
|
||||
this.options.pagesDir
|
||||
)
|
||||
} else {
|
||||
templateVars.router.routes = this.options.build.createRoutes(
|
||||
@ -646,9 +647,9 @@ module.exports = class Builder {
|
||||
]
|
||||
if (this._nuxtPages) {
|
||||
patterns.push(
|
||||
r(src, 'pages'),
|
||||
r(src, 'pages/*.{vue,js}'),
|
||||
r(src, 'pages/**/*.{vue,js}')
|
||||
r(src, this.options.pagesDir),
|
||||
r(src, `${this.options.pagesDir}/*.{vue,js}`),
|
||||
r(src, `${this.options.pagesDir}/**/*.{vue,js}`)
|
||||
)
|
||||
}
|
||||
patterns = _.map(patterns, p => upath.normalizeSafe(p))
|
||||
|
@ -284,6 +284,7 @@ Options.defaults = {
|
||||
name: 'layout',
|
||||
mode: 'out-in'
|
||||
},
|
||||
pagesDir: 'pages',
|
||||
router: {
|
||||
mode: 'history',
|
||||
base: '/',
|
||||
|
@ -254,11 +254,11 @@ function cleanChildrenRoutes(routes, isChild = false) {
|
||||
return routes
|
||||
}
|
||||
|
||||
exports.createRoutes = function createRoutes(files, srcDir) {
|
||||
exports.createRoutes = function createRoutes(files, srcDir, pagesDir) {
|
||||
let routes = []
|
||||
files.forEach(file => {
|
||||
let keys = file
|
||||
.replace(/^pages/, '')
|
||||
.replace(`/^${pagesDir}/`, '')
|
||||
.replace(/\.(vue|js)$/, '')
|
||||
.replace(/\/{2,}/g, '/')
|
||||
.split('/')
|
||||
|
35
test/custom.pages.dir.js
Normal file
35
test/custom.pages.dir.js
Normal file
@ -0,0 +1,35 @@
|
||||
import test from 'ava'
|
||||
import { resolve } from 'path'
|
||||
import { Nuxt, Builder } from '..'
|
||||
import { interceptLog } from './helpers/console'
|
||||
|
||||
let nuxt = null
|
||||
let builder = null
|
||||
|
||||
// Init nuxt.js and create server listening on localhost:4000
|
||||
test.before('Init Nuxt.js', async t => {
|
||||
const rootDir = resolve(__dirname, 'fixtures/custom-pages-dir')
|
||||
let config = require(resolve(rootDir, 'nuxt.config.js'))
|
||||
config.rootDir = rootDir
|
||||
config.dev = false
|
||||
|
||||
const logSpy = await interceptLog(async () => {
|
||||
nuxt = new Nuxt(config)
|
||||
builder = new Builder(nuxt)
|
||||
await builder.build()
|
||||
await nuxt.listen(4007, 'localhost')
|
||||
})
|
||||
|
||||
t.true(logSpy.calledWithMatch('DONE'))
|
||||
t.true(logSpy.calledWithMatch('OPEN'))
|
||||
})
|
||||
|
||||
test('/', async t => {
|
||||
const { html } = await nuxt.renderRoute('/')
|
||||
t.true(html.includes('<h1>I have custom pages directory</h1>'))
|
||||
})
|
||||
|
||||
// Close server and ask nuxt to stop listening to file changes
|
||||
test.after.always('Closing server and nuxt.js', async t => {
|
||||
await nuxt.close()
|
||||
})
|
5
test/fixtures/custom-pages-dir/custom-pages/index.vue
vendored
Normal file
5
test/fixtures/custom-pages-dir/custom-pages/index.vue
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
<template>
|
||||
<div>
|
||||
<h1>I have custom pages directory</h1>
|
||||
</div>
|
||||
</template>
|
3
test/fixtures/custom-pages-dir/nuxt.config.js
vendored
Normal file
3
test/fixtures/custom-pages-dir/nuxt.config.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
module.exports = {
|
||||
pagesDir: 'custom-pages'
|
||||
}
|
5
test/fixtures/custom-pages-dir/package.json
vendored
Normal file
5
test/fixtures/custom-pages-dir/package.json
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "custom-pages-dir",
|
||||
"version": "1.0.0",
|
||||
"dependencies": {}
|
||||
}
|
Loading…
Reference in New Issue
Block a user