feat: custom middleware directory

This commit is contained in:
Ricardo Gobbo de Souza 2018-02-03 21:24:45 -02:00
parent b3f2a67793
commit 856c1bf83c
7 changed files with 31 additions and 7 deletions

View File

@ -1,5 +1,5 @@
<% if (middleware) { %>
let files = require.context('@/middleware', false, /^\.\/(?!<%= ignorePrefix %>).*\.(<%= extensions %>)$/)
let files = require.context('@/<%= dir.middleware %>', false, /^\.\/(?!<%= ignorePrefix %>).*\.(<%= extensions %>)$/)
let filenames = files.keys()
function getModule (filename) {

View File

@ -250,7 +250,7 @@ module.exports = class Builder {
router: this.options.router,
env: this.options.env,
head: this.options.head,
middleware: existsSync(join(this.options.srcDir, 'middleware')),
middleware: existsSync(join(this.options.srcDir, this.options.dir.middleware)),
store: this.options.store,
css: this.options.css,
plugins: this.plugins,
@ -263,6 +263,7 @@ module.exports = class Builder {
: this.options.loading,
transition: this.options.transition,
layoutTransition: this.options.layoutTransition,
dir: this.options.dir,
components: {
ErrorPage: this.options.ErrorPage
? this.relativeToBuild(this.options.ErrorPage)
@ -641,7 +642,7 @@ module.exports = class Builder {
let patterns = [
r(src, this.options.dir.layouts),
r(src, 'store'),
r(src, 'middleware'),
r(src, this.options.dir.middleware),
r(src, `${this.options.dir.layouts}/*.{vue,js}`),
r(src, `${this.options.dir.layouts}/**/*.{vue,js}`)
]

View File

@ -287,6 +287,7 @@ Options.defaults = {
dir: {
assets: 'assets',
layouts: 'layouts',
middleware: 'middleware',
pages: 'pages',
static: 'static'
},

View File

@ -28,22 +28,28 @@ test.before('Init Nuxt.js', async t => {
t.true(logSpy.calledWithMatch('OPEN'))
})
test('/ (custom assets directory)', async t => {
test('custom assets directory', async t => {
const { html } = await nuxt.renderRoute('/')
t.true(html.includes('.global-css-selector'))
})
test('/ (custom layouts directory)', async t => {
test('custom layouts directory', async t => {
const { html } = await nuxt.renderRoute('/')
t.true(html.includes('<p>I have custom layouts directory</p>'))
})
test('/ (custom pages directory)', async t => {
test('custom middleware directory', async t => {
const window = await nuxt.renderAndGetWindow(url('/user-agent'))
const html = window.document.body.innerHTML
t.true(html.includes('<pre>Mozilla'))
})
test('custom pages directory', async t => {
const { html } = await nuxt.renderRoute('/')
t.true(html.includes('<h1>I have custom pages directory</h1>'))
})
test('Check /test.txt with custom static directory', async t => {
test('custom static directory', async t => {
const { headers } = await rp(url('/test.txt'), {
resolveWithFullResponse: true
})

View File

@ -0,0 +1,3 @@
export default function (context) {
context.userAgent = process.server ? context.req.headers['user-agent'] : navigator.userAgent
}

View File

@ -0,0 +1,12 @@
<template>
<pre>{{ userAgent }}</pre>
</template>
<script>
export default {
middleware: 'user-agent',
asyncData({ userAgent }) {
return { userAgent }
}
}
</script>

View File

@ -3,6 +3,7 @@ module.exports = {
dir: {
assets: 'custom-assets',
layouts: 'custom-layouts',
middleware: 'custom-middleware',
pages: 'custom-pages',
static: 'custom-static'
}