refactor: vue takes precedence over js in pages and layouts

This commit is contained in:
Clark Du 2017-12-12 21:32:45 +08:00
parent a4e2a7cd65
commit bc3bcac337
No known key found for this signature in database
GPG Key ID: D0E5986AF78B86D9
4 changed files with 23 additions and 3 deletions

View File

@ -271,7 +271,9 @@ module.exports = class Builder {
hasErrorLayout = true
return
}
if (!templateVars.layouts[name] || /\.vue$/.test(file)) {
templateVars.layouts[name] = this.relativeToBuild(this.options.srcDir, file)
}
})
if (!templateVars.components.ErrorPage && hasErrorLayout) {
templateVars.components.ErrorPage = this.relativeToBuild(this.options.srcDir, 'layouts/error.vue')
@ -289,8 +291,14 @@ module.exports = class Builder {
// If user defined a custom method to create routes
if (this._nuxtPages) {
// Use nuxt.js createRoutes bases on pages/
const files = await glob('pages/**/*.{vue,js}', { cwd: this.options.srcDir })
templateVars.router.routes = createRoutes(files, this.options.srcDir)
const files = {};
(await glob('pages/**/*.{vue,js}', { cwd: this.options.srcDir })).forEach(f => {
const key = f.replace(/\.(js|vue)$/, '')
if (/\.vue$/.test(f) || !files[key]) {
files[key] = f
}
})
templateVars.router.routes = createRoutes(Object.values(files), this.options.srcDir)
} else {
templateVars.router.routes = this.options.build.createRoutes(this.options.srcDir)
}

View File

@ -250,6 +250,11 @@ test('/jsx', async t => {
t.true(html.includes('<h1>JSX Page</h1>'))
})
test('/js-link', async t => {
const { html } = await nuxt.renderRoute('/js-link')
t.true(html.includes('<h1>vue file is first-class</h1>'))
})
// Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => {
nuxt.close()

2
test/fixtures/basic/pages/js-link.js vendored Normal file
View File

@ -0,0 +1,2 @@
export default {
}

5
test/fixtures/basic/pages/js-link.vue vendored Normal file
View File

@ -0,0 +1,5 @@
<template>
<h1>vue file is first-class</h1>
</template>
<script src="./js-link.js"></script>