feat: add layout on module

This commit is contained in:
Ricardo Gobbo de Souza 2018-02-07 09:58:48 -02:00
parent 8fcef54a23
commit f0a6bdd51a
6 changed files with 43 additions and 0 deletions

View File

@ -71,6 +71,14 @@ module.exports = class ModuleContainer {
}) })
} }
addLayout(template, name) {
const { dst, src } = this.addTemplate(template)
const srcPath = path.parse(src)
// Add to nuxt layouys
this.options.layouts[name || srcPath.name] = `./${dst}`
}
addServerMiddleware(middleware) { addServerMiddleware(middleware) {
this.options.serverMiddleware.push(middleware) this.options.serverMiddleware.push(middleware)
} }

View File

@ -8,6 +8,9 @@ module.exports = function basicModule(options, resolve) {
// Add a plugin // Add a plugin
this.addPlugin(path.resolve(__dirname, 'reverse.js')) this.addPlugin(path.resolve(__dirname, 'reverse.js'))
// Add a layout
this.addLayout(path.resolve(__dirname, 'layout.vue'))
// Extend build // Extend build
this.extendBuild((config, { isClient, isServer }) => { this.extendBuild((config, { isClient, isServer }) => {
// Do nothing! // Do nothing!

View File

@ -0,0 +1,6 @@
<template>
<div>
<h1>Module Layouts</h1>
<nuxt />
</div>
</template>

View File

@ -5,6 +5,7 @@ Vue.use(Router)
const indexPage = () => import('~/views/index.vue').then(m => m.default || m) const indexPage = () => import('~/views/index.vue').then(m => m.default || m)
const aboutPage = () => import('~/views/about.vue').then(m => m.default || m) const aboutPage = () => import('~/views/about.vue').then(m => m.default || m)
const layoutPage = () => import('~/views/layout.vue').then(m => m.default || m)
export function createRouter() { export function createRouter() {
return new Router({ return new Router({
@ -19,6 +20,11 @@ export function createRouter() {
path: '/about', path: '/about',
component: aboutPage, component: aboutPage,
name: 'about' name: 'about'
},
{
path: '/layout',
component: layoutPage,
name: 'layout'
} }
] ]
}) })

10
test/fixtures/module/views/layout.vue vendored Normal file
View File

@ -0,0 +1,10 @@
<template>
<div>
Layout on module
</div>
</template>
<script>
export default {
layout: 'layout'
}
</script>

View File

@ -47,6 +47,16 @@ test.serial('Plugin', async t => {
t.true(html.includes('<h1>TXUN</h1>'), 'plugin works') t.true(html.includes('<h1>TXUN</h1>'), 'plugin works')
}) })
test.serial('Layout', async t => {
t.true(
nuxt.options.layouts.layout.includes('layout'),
'layout added to config'
)
const { html } = await nuxt.renderRoute('/layout')
t.true(html.includes('<h1>Module Layouts</h1>'), 'layout works')
})
test.serial('Hooks', async t => { test.serial('Hooks', async t => {
t.is(nuxt.__module_hook, 1) t.is(nuxt.__module_hook, 1)
t.is(nuxt.__renderer_hook, 2) t.is(nuxt.__renderer_hook, 2)