feat: custom pages directory

This commit is contained in:
Ricardo Gobbo de Souza 2018-02-02 14:58:51 -02:00
parent 1582df149d
commit 10ac3ed2b4
7 changed files with 61 additions and 11 deletions

View File

@ -137,15 +137,15 @@ module.exports = class Builder {
// Check if pages dir exists and warn if not // Check if pages dir exists and warn if not
this._nuxtPages = typeof this.options.build.createRoutes !== 'function' this._nuxtPages = typeof this.options.build.createRoutes !== 'function'
if (this._nuxtPages) { if (this._nuxtPages) {
if (!existsSync(join(this.options.srcDir, 'pages'))) { if (!existsSync(join(this.options.srcDir, this.options.pagesDir))) {
let dir = this.options.srcDir let dir = this.options.srcDir
if (existsSync(join(this.options.srcDir, '..', 'pages'))) { if (existsSync(join(this.options.srcDir, '..', this.options.pagesDir))) {
throw new Error( 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 { } else {
throw new Error( 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) { if (this._nuxtPages) {
// Use nuxt.js createRoutes bases on pages/ // Use nuxt.js createRoutes bases on pages/
const files = {} const files = {}
;(await glob('pages/**/*.{vue,js}', { ;(await glob(`${this.options.pagesDir}/**/*.{vue,js}`, {
cwd: this.options.srcDir, cwd: this.options.srcDir,
ignore: this.options.ignore ignore: this.options.ignore
})).forEach(f => { })).forEach(f => {
@ -325,7 +325,8 @@ module.exports = class Builder {
}) })
templateVars.router.routes = createRoutes( templateVars.router.routes = createRoutes(
Object.values(files), Object.values(files),
this.options.srcDir this.options.srcDir,
this.options.pagesDir
) )
} else { } else {
templateVars.router.routes = this.options.build.createRoutes( templateVars.router.routes = this.options.build.createRoutes(
@ -646,9 +647,9 @@ module.exports = class Builder {
] ]
if (this._nuxtPages) { if (this._nuxtPages) {
patterns.push( patterns.push(
r(src, 'pages'), r(src, this.options.pagesDir),
r(src, 'pages/*.{vue,js}'), r(src, `${this.options.pagesDir}/*.{vue,js}`),
r(src, 'pages/**/*.{vue,js}') r(src, `${this.options.pagesDir}/**/*.{vue,js}`)
) )
} }
patterns = _.map(patterns, p => upath.normalizeSafe(p)) patterns = _.map(patterns, p => upath.normalizeSafe(p))

View File

@ -284,6 +284,7 @@ Options.defaults = {
name: 'layout', name: 'layout',
mode: 'out-in' mode: 'out-in'
}, },
pagesDir: 'pages',
router: { router: {
mode: 'history', mode: 'history',
base: '/', base: '/',

View File

@ -254,11 +254,11 @@ function cleanChildrenRoutes(routes, isChild = false) {
return routes return routes
} }
exports.createRoutes = function createRoutes(files, srcDir) { exports.createRoutes = function createRoutes(files, srcDir, pagesDir) {
let routes = [] let routes = []
files.forEach(file => { files.forEach(file => {
let keys = file let keys = file
.replace(/^pages/, '') .replace(`/^${pagesDir}/`, '')
.replace(/\.(vue|js)$/, '') .replace(/\.(vue|js)$/, '')
.replace(/\/{2,}/g, '/') .replace(/\/{2,}/g, '/')
.split('/') .split('/')

35
test/custom.pages.dir.js Normal file
View 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()
})

View File

@ -0,0 +1,5 @@
<template>
<div>
<h1>I have custom pages directory</h1>
</div>
</template>

View File

@ -0,0 +1,3 @@
module.exports = {
pagesDir: 'custom-pages'
}

View File

@ -0,0 +1,5 @@
{
"name": "custom-pages-dir",
"version": "1.0.0",
"dependencies": {}
}