mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-30 15:22:39 +00:00
feat: custom middleware directory
This commit is contained in:
parent
b3f2a67793
commit
856c1bf83c
@ -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) {
|
||||
|
@ -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}`)
|
||||
]
|
||||
|
@ -287,6 +287,7 @@ Options.defaults = {
|
||||
dir: {
|
||||
assets: 'assets',
|
||||
layouts: 'layouts',
|
||||
middleware: 'middleware',
|
||||
pages: 'pages',
|
||||
static: 'static'
|
||||
},
|
||||
|
@ -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
|
||||
})
|
||||
|
3
test/fixtures/custom-dirs/custom-middleware/user-agent.js
vendored
Normal file
3
test/fixtures/custom-dirs/custom-middleware/user-agent.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
export default function (context) {
|
||||
context.userAgent = process.server ? context.req.headers['user-agent'] : navigator.userAgent
|
||||
}
|
12
test/fixtures/custom-dirs/custom-pages/user-agent.vue
vendored
Normal file
12
test/fixtures/custom-dirs/custom-pages/user-agent.vue
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
<template>
|
||||
<pre>{{ userAgent }}</pre>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
middleware: 'user-agent',
|
||||
asyncData({ userAgent }) {
|
||||
return { userAgent }
|
||||
}
|
||||
}
|
||||
</script>
|
1
test/fixtures/custom-dirs/nuxt.config.js
vendored
1
test/fixtures/custom-dirs/nuxt.config.js
vendored
@ -3,6 +3,7 @@ module.exports = {
|
||||
dir: {
|
||||
assets: 'custom-assets',
|
||||
layouts: 'custom-layouts',
|
||||
middleware: 'custom-middleware',
|
||||
pages: 'custom-pages',
|
||||
static: 'custom-static'
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user