From 5af65527b04c153c4c1db997bd9da1944b14434f Mon Sep 17 00:00:00 2001 From: Dmitry Molotkov Date: Tue, 18 Sep 2018 19:06:55 +0300 Subject: [PATCH] allow plugin in directory with index.js file (#3908) * allow plugin in directory with index.js file * add test for plugin in dir * fix: refacto plugins key --- lib/builder/builder.js | 2 +- test/fixtures/basic/nuxt.config.js | 7 +++--- .../basic/plugins/dir-plugin/index.js | 3 +++ test/unit/basic.plugins.test.js | 25 +++++++++++++++++++ 4 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/basic/plugins/dir-plugin/index.js create mode 100644 test/unit/basic.plugins.test.js diff --git a/lib/builder/builder.js b/lib/builder/builder.js index 5eef0201fb..a465fd93e9 100644 --- a/lib/builder/builder.js +++ b/lib/builder/builder.js @@ -447,7 +447,7 @@ export default class Builder { // Check plugins exist then set alias to their real path await Promise.all(this.plugins.map(async (p) => { - const ext = path.extname(p.src) ? '' : '.+([^.])' + const ext = path.extname(p.src) ? '' : '{.+([^.]),/index.+([^.])}' const pluginFiles = await glob(`${p.src}${ext}`) if (!pluginFiles || pluginFiles.length === 0) { diff --git a/test/fixtures/basic/nuxt.config.js b/test/fixtures/basic/nuxt.config.js index 8223e006d1..0048ded112 100644 --- a/test/fixtures/basic/nuxt.config.js +++ b/test/fixtures/basic/nuxt.config.js @@ -8,9 +8,6 @@ export default { maxAge: ((60 * 60 * 24 * 365) * 2) } }, - plugins: [ - '~/plugins/vuex-module' - ], router: { extendRoutes(routes, resolve) { return [{ @@ -64,6 +61,10 @@ export default { '': true }, transition: false, + plugins: [ + '~/plugins/vuex-module', + '~/plugins/dir-plugin' + ], build: { scopeHoisting: true, postcss: [ diff --git a/test/fixtures/basic/plugins/dir-plugin/index.js b/test/fixtures/basic/plugins/dir-plugin/index.js new file mode 100644 index 0000000000..5f61014c46 --- /dev/null +++ b/test/fixtures/basic/plugins/dir-plugin/index.js @@ -0,0 +1,3 @@ +if (process.client) { + window.__test_plugin = true +} diff --git a/test/unit/basic.plugins.test.js b/test/unit/basic.plugins.test.js new file mode 100644 index 0000000000..2964435ad4 --- /dev/null +++ b/test/unit/basic.plugins.test.js @@ -0,0 +1,25 @@ +import { getPort, loadFixture, Nuxt } from '../utils' + +let port +const url = route => 'http://localhost:' + port + route + +let nuxt = null + +describe('with-config', () => { + beforeAll(async () => { + const config = await loadFixture('basic') + nuxt = new Nuxt(config) + port = await getPort() + await nuxt.listen(port, 'localhost') + }) + + test('/', async () => { + const window = await nuxt.renderAndGetWindow(url('/')) + expect(window.__test_plugin).toBe(true) + }) + + // Close server and ask nuxt to stop listening to file changes + afterAll(async () => { + await nuxt.close() + }) +})