fix(builder): js layout (#4701)

[release]
This commit is contained in:
Xin Du (Clark) 2019-01-07 12:14:51 +00:00 committed by Pooya Parsa
parent b6457545be
commit af76e07b56
7 changed files with 35 additions and 5 deletions

View File

@ -349,7 +349,8 @@ export default class Builder {
} }
return return
} }
if (!templateVars.layouts[name] && /\.vue$/.test(file)) { // .vue file takes precedence over other extensions
if (!templateVars.layouts[name] || /\.vue$/.test(file)) {
templateVars.layouts[name] = this.relativeToBuild( templateVars.layouts[name] = this.relativeToBuild(
this.options.srcDir, this.options.srcDir,
file file
@ -382,6 +383,7 @@ export default class Builder {
ignore: this.options.ignore ignore: this.options.ignore
})).forEach((f) => { })).forEach((f) => {
const key = f.replace(new RegExp(`\\.(${this.supportedExtensions.join('|')})$`), '') const key = f.replace(new RegExp(`\\.(${this.supportedExtensions.join('|')})$`), '')
// .vue file takes precedence over other extensions
if (/\.vue$/.test(f) || !files[key]) { if (/\.vue$/.test(f) || !files[key]) {
files[key] = f.replace(/(['"])/g, '\\$1') files[key] = f.replace(/(['"])/g, '\\$1')
} }

10
test/fixtures/basic/layouts/custom.js vendored Normal file
View File

@ -0,0 +1,10 @@
export default {
render(h) {
return h('div',
[
h('h1', 'JS Layout'),
h('nuxt')
]
)
}
}

View File

@ -0,0 +1,5 @@
<template>
<div>
<Nuxt />
</div>
</template>

6
test/fixtures/basic/pages/custom.js vendored Normal file
View File

@ -0,0 +1,6 @@
export default {
layout: 'custom',
render(h) {
return h('h2', 'custom page')
}
}

View File

@ -1,7 +1,7 @@
<template> <template>
<div> <div>
Custom layout Custom layout
<br> <br>
<Nuxt /> <Nuxt />
</div> </div>
</template> </template>

View File

@ -1,7 +1,7 @@
<template> <template>
<div> <div>
Default layout Default layout
<br> <br>
<Nuxt /> <Nuxt />
</div> </div>
</template> </template>

View File

@ -331,6 +331,13 @@ describe('basic ssr', () => {
expect(html).toMatch('Hello unicode') expect(html).toMatch('Hello unicode')
}) })
test('/custom (js layout)', async () => {
const window = await nuxt.server.renderAndGetWindow(url('/custom'))
const html = window.document.body.innerHTML
expect(html).toMatch('<h1>JS Layout</h1>')
expect(html).toMatch('<h2>custom page</h2>')
})
// Close server and ask nuxt to stop listening to file changes // Close server and ask nuxt to stop listening to file changes
afterAll(async () => { afterAll(async () => {
await nuxt.close() await nuxt.close()