From 856c1bf83c42a6485176f9bec3c45fa05d2e10ac Mon Sep 17 00:00:00 2001 From: Ricardo Gobbo de Souza Date: Sat, 3 Feb 2018 21:24:45 -0200 Subject: [PATCH] feat: custom middleware directory --- lib/app/middleware.js | 2 +- lib/builder/builder.js | 5 +++-- lib/common/options.js | 1 + test/custom-dirs.test.js | 14 ++++++++++---- .../custom-dirs/custom-middleware/user-agent.js | 3 +++ .../custom-dirs/custom-pages/user-agent.vue | 12 ++++++++++++ test/fixtures/custom-dirs/nuxt.config.js | 1 + 7 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 test/fixtures/custom-dirs/custom-middleware/user-agent.js create mode 100644 test/fixtures/custom-dirs/custom-pages/user-agent.vue diff --git a/lib/app/middleware.js b/lib/app/middleware.js index fb5061c64c..7eea33260b 100644 --- a/lib/app/middleware.js +++ b/lib/app/middleware.js @@ -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) { diff --git a/lib/builder/builder.js b/lib/builder/builder.js index 6f9943c7a1..4c9d47c821 100644 --- a/lib/builder/builder.js +++ b/lib/builder/builder.js @@ -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}`) ] diff --git a/lib/common/options.js b/lib/common/options.js index 69db1dd6ff..5fdbe71133 100755 --- a/lib/common/options.js +++ b/lib/common/options.js @@ -287,6 +287,7 @@ Options.defaults = { dir: { assets: 'assets', layouts: 'layouts', + middleware: 'middleware', pages: 'pages', static: 'static' }, diff --git a/test/custom-dirs.test.js b/test/custom-dirs.test.js index c516101480..7201c0c7aa 100644 --- a/test/custom-dirs.test.js +++ b/test/custom-dirs.test.js @@ -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('

I have custom layouts directory

')) }) -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('
Mozilla'))
+})
+
+test('custom pages directory', async t => {
   const { html } = await nuxt.renderRoute('/')
   t.true(html.includes('

I have custom pages directory

')) }) -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 }) diff --git a/test/fixtures/custom-dirs/custom-middleware/user-agent.js b/test/fixtures/custom-dirs/custom-middleware/user-agent.js new file mode 100644 index 0000000000..662a76ef02 --- /dev/null +++ b/test/fixtures/custom-dirs/custom-middleware/user-agent.js @@ -0,0 +1,3 @@ +export default function (context) { + context.userAgent = process.server ? context.req.headers['user-agent'] : navigator.userAgent +} diff --git a/test/fixtures/custom-dirs/custom-pages/user-agent.vue b/test/fixtures/custom-dirs/custom-pages/user-agent.vue new file mode 100644 index 0000000000..904156ace0 --- /dev/null +++ b/test/fixtures/custom-dirs/custom-pages/user-agent.vue @@ -0,0 +1,12 @@ + + + diff --git a/test/fixtures/custom-dirs/nuxt.config.js b/test/fixtures/custom-dirs/nuxt.config.js index 6701e30b30..b98932dad7 100644 --- a/test/fixtures/custom-dirs/nuxt.config.js +++ b/test/fixtures/custom-dirs/nuxt.config.js @@ -3,6 +3,7 @@ module.exports = { dir: { assets: 'custom-assets', layouts: 'custom-layouts', + middleware: 'custom-middleware', pages: 'custom-pages', static: 'custom-static' }