diff --git a/lib/build.js b/lib/build.js index fea707e5a2..5524cf32ef 100644 --- a/lib/build.js +++ b/lib/build.js @@ -155,17 +155,8 @@ function * generateRoutesAndFiles () { if (name === 'error') return layouts[name] = r(this.srcDir, file) }) - // Generate routes based on files const files = yield glob('pages/**/*.vue', { cwd: this.srcDir }) - this.routes = _.uniq(_.map(files, (file) => { - return file.replace(/^pages/, '').replace(/\.vue$/, '').replace(/\/index/g, '').replace(/_/g, ':').replace('', '/').replace(/\/{2,}/g, '/') - })) - if (typeof this.options.router.extendRoutes === 'function') { - // let the user extend the routes - this.options.router.extendRoutes(this.routes) - } // Interpret and move template files to .nuxt/ - debug('Generating files...') let templatesFiles = [ 'App.vue', 'client.js', @@ -210,6 +201,9 @@ function * generateRoutesAndFiles () { // let the user extend the routes this.options.router.extendRoutes(templateVars.router.routes) } + // Routes for Generate command + this.routes = flatRoutes(templateVars.router.routes, '') + debug('Generating files...') if (layoutsFiles.includes('layouts/error.vue')) { templateVars.components.ErrorPage = r(this.srcDir, 'layouts/error.vue') } @@ -329,6 +323,18 @@ function cleanChildrenRoutes (routes, isChild = false) { return routes } +function flatRoutes (router, path) { + let routes = [] + router.forEach((r) => { + if (r.children) { + routes.concat(flatRoutes(r.children, r.path)) + } else { + routes.push(path + r.path) + } + }) + return routes +} + function getWebpackClientConfig () { return clientWebpackConfig.call(this) }