feat(builder, module): allow error layouts to be added through addLayout. closes #3194. (#3834)

This commit is contained in:
Alexander Lichter 2018-09-02 10:22:10 +01:00 committed by Pooya Parsa
parent 72479687d9
commit 6e8a51509c
6 changed files with 31 additions and 8 deletions

View File

@ -223,7 +223,6 @@ export default class Builder {
cwd: this.options.srcDir, cwd: this.options.srcDir,
ignore: this.options.ignore ignore: this.options.ignore
}) })
let hasErrorLayout = false
layoutsFiles.forEach((file) => { layoutsFiles.forEach((file) => {
const name = file const name = file
.split('/') .split('/')
@ -231,7 +230,12 @@ export default class Builder {
.join('/') .join('/')
.replace(/\.(vue|js)$/, '') .replace(/\.(vue|js)$/, '')
if (name === 'error') { if (name === 'error') {
hasErrorLayout = true if (!templateVars.components.ErrorPage) {
templateVars.components.ErrorPage = this.relativeToBuild(
this.options.srcDir,
file
)
}
return return
} }
if (!templateVars.layouts[name] || /\.vue$/.test(file)) { if (!templateVars.layouts[name] || /\.vue$/.test(file)) {
@ -241,12 +245,6 @@ export default class Builder {
) )
} }
}) })
if (!templateVars.components.ErrorPage && hasErrorLayout) {
templateVars.components.ErrorPage = this.relativeToBuild(
this.options.srcDir,
`${this.options.dir.layouts}/error.vue`
)
}
} }
// If no default layout, create its folder and add the default folder // If no default layout, create its folder and add the default folder
if (!templateVars.layouts.default) { if (!templateVars.layouts.default) {

View File

@ -71,6 +71,16 @@ export default class ModuleContainer {
// Add to nuxt layouts // Add to nuxt layouts
this.options.layouts[name || path.parse(src).name] = `./${dst}` this.options.layouts[name || path.parse(src).name] = `./${dst}`
// If error layout, set ErrorPage
if (name === 'error') {
this.addErrorLayout(dst)
}
}
addErrorLayout(dst) {
const relativeBuildDir = path.relative(this.options.rootDir, this.options.buildDir)
this.options.ErrorPage = `~/${relativeBuildDir}/${dst}`
} }
addServerMiddleware(middleware) { addServerMiddleware(middleware) {

View File

@ -0,0 +1,6 @@
import { resolve } from 'path'
export default function module() {
this.addLayout(
{ src: resolve(__dirname, 'some-error.vue') }, 'error')
}

View File

@ -0,0 +1,3 @@
<template>
<h1>You should see the error in a different Vue!</h1>
</template>

View File

@ -3,6 +3,7 @@ export default {
modules: [ modules: [
'~~/modules/basic', '~~/modules/basic',
'~/modules/hooks', '~/modules/hooks',
'~/modules/layout',
{ {
src: '~/modules/middleware', src: '~/modules/middleware',
options: { options: {

View File

@ -31,6 +31,11 @@ describe('module', () => {
expect(html.includes('<h1>Module Layouts</h1>')).toBe(true) expect(html.includes('<h1>Module Layouts</h1>')).toBe(true)
}) })
test('/404 should display the module error layout', async () => {
const { html } = await nuxt.renderRoute('/404')
expect(html).toContain('You should see the error in a different Vue!')
})
test('Hooks', () => { test('Hooks', () => {
expect(nuxt.__module_hook).toBe(1) expect(nuxt.__module_hook).toBe(1)
expect(nuxt.__renderer_hook).toBe(2) expect(nuxt.__renderer_hook).toBe(2)