diff --git a/lib/builder/builder.js b/lib/builder/builder.js index 32fd3a32e1..655c2a099d 100644 --- a/lib/builder/builder.js +++ b/lib/builder/builder.js @@ -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)) diff --git a/lib/common/options.js b/lib/common/options.js index e685efe2a2..a2c12bc561 100755 --- a/lib/common/options.js +++ b/lib/common/options.js @@ -284,6 +284,7 @@ Options.defaults = { name: 'layout', mode: 'out-in' }, + pagesDir: 'pages', router: { mode: 'history', base: '/', diff --git a/lib/common/utils.js b/lib/common/utils.js index c8d93568b6..59e3f15c25 100644 --- a/lib/common/utils.js +++ b/lib/common/utils.js @@ -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('/') diff --git a/test/custom.pages.dir.js b/test/custom.pages.dir.js new file mode 100644 index 0000000000..00400c21fa --- /dev/null +++ b/test/custom.pages.dir.js @@ -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('

I have custom pages directory

')) +}) + +// Close server and ask nuxt to stop listening to file changes +test.after.always('Closing server and nuxt.js', async t => { + await nuxt.close() +}) diff --git a/test/fixtures/custom-pages-dir/custom-pages/index.vue b/test/fixtures/custom-pages-dir/custom-pages/index.vue new file mode 100644 index 0000000000..dc15a791ec --- /dev/null +++ b/test/fixtures/custom-pages-dir/custom-pages/index.vue @@ -0,0 +1,5 @@ + diff --git a/test/fixtures/custom-pages-dir/nuxt.config.js b/test/fixtures/custom-pages-dir/nuxt.config.js new file mode 100644 index 0000000000..0cf6019e1d --- /dev/null +++ b/test/fixtures/custom-pages-dir/nuxt.config.js @@ -0,0 +1,3 @@ +module.exports = { + pagesDir: 'custom-pages' +} diff --git a/test/fixtures/custom-pages-dir/package.json b/test/fixtures/custom-pages-dir/package.json new file mode 100644 index 0000000000..de28318ece --- /dev/null +++ b/test/fixtures/custom-pages-dir/package.json @@ -0,0 +1,5 @@ +{ + "name": "custom-pages-dir", + "version": "1.0.0", + "dependencies": {} +}