From bc3bcac337a6759b8a82c655df87b7018e890a23 Mon Sep 17 00:00:00 2001 From: Clark Du Date: Tue, 12 Dec 2017 21:32:45 +0800 Subject: [PATCH] refactor: vue takes precedence over js in pages and layouts --- lib/builder/builder.js | 14 +++++++++++--- test/basic.ssr.test.js | 5 +++++ test/fixtures/basic/pages/js-link.js | 2 ++ test/fixtures/basic/pages/js-link.vue | 5 +++++ 4 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/basic/pages/js-link.js create mode 100644 test/fixtures/basic/pages/js-link.vue diff --git a/lib/builder/builder.js b/lib/builder/builder.js index 98befdbfc1..704fbb7389 100644 --- a/lib/builder/builder.js +++ b/lib/builder/builder.js @@ -271,7 +271,9 @@ module.exports = class Builder { hasErrorLayout = true return } - templateVars.layouts[name] = this.relativeToBuild(this.options.srcDir, file) + if (!templateVars.layouts[name] || /\.vue$/.test(file)) { + templateVars.layouts[name] = this.relativeToBuild(this.options.srcDir, file) + } }) if (!templateVars.components.ErrorPage && hasErrorLayout) { templateVars.components.ErrorPage = this.relativeToBuild(this.options.srcDir, 'layouts/error.vue') @@ -289,8 +291,14 @@ module.exports = class Builder { // If user defined a custom method to create routes if (this._nuxtPages) { // Use nuxt.js createRoutes bases on pages/ - const files = await glob('pages/**/*.{vue,js}', { cwd: this.options.srcDir }) - templateVars.router.routes = createRoutes(files, this.options.srcDir) + const files = {}; + (await glob('pages/**/*.{vue,js}', { cwd: this.options.srcDir })).forEach(f => { + const key = f.replace(/\.(js|vue)$/, '') + if (/\.vue$/.test(f) || !files[key]) { + files[key] = f + } + }) + templateVars.router.routes = createRoutes(Object.values(files), this.options.srcDir) } else { templateVars.router.routes = this.options.build.createRoutes(this.options.srcDir) } diff --git a/test/basic.ssr.test.js b/test/basic.ssr.test.js index aae111b8c2..9e8e58b93f 100755 --- a/test/basic.ssr.test.js +++ b/test/basic.ssr.test.js @@ -250,6 +250,11 @@ test('/jsx', async t => { t.true(html.includes('

JSX Page

')) }) +test('/js-link', async t => { + const { html } = await nuxt.renderRoute('/js-link') + t.true(html.includes('

vue file is first-class

')) +}) + // Close server and ask nuxt to stop listening to file changes test.after('Closing server and nuxt.js', t => { nuxt.close() diff --git a/test/fixtures/basic/pages/js-link.js b/test/fixtures/basic/pages/js-link.js new file mode 100644 index 0000000000..efba7fa697 --- /dev/null +++ b/test/fixtures/basic/pages/js-link.js @@ -0,0 +1,2 @@ +export default { +} diff --git a/test/fixtures/basic/pages/js-link.vue b/test/fixtures/basic/pages/js-link.vue new file mode 100644 index 0000000000..f69c87b3c5 --- /dev/null +++ b/test/fixtures/basic/pages/js-link.vue @@ -0,0 +1,5 @@ + + +