fix extendRoutes for generate

This commit is contained in:
Alexandre Chopin 2017-02-21 13:01:16 +01:00
parent 7b3c62739c
commit f5300e4e67

View File

@ -155,17 +155,8 @@ function * generateRoutesAndFiles () {
if (name === 'error') return if (name === 'error') return
layouts[name] = r(this.srcDir, file) layouts[name] = r(this.srcDir, file)
}) })
// Generate routes based on files
const files = yield glob('pages/**/*.vue', { cwd: this.srcDir }) 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/ // Interpret and move template files to .nuxt/
debug('Generating files...')
let templatesFiles = [ let templatesFiles = [
'App.vue', 'App.vue',
'client.js', 'client.js',
@ -210,6 +201,9 @@ function * generateRoutesAndFiles () {
// let the user extend the routes // let the user extend the routes
this.options.router.extendRoutes(templateVars.router.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')) { if (layoutsFiles.includes('layouts/error.vue')) {
templateVars.components.ErrorPage = r(this.srcDir, 'layouts/error.vue') templateVars.components.ErrorPage = r(this.srcDir, 'layouts/error.vue')
} }
@ -329,6 +323,18 @@ function cleanChildrenRoutes (routes, isChild = false) {
return routes 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 () { function getWebpackClientConfig () {
return clientWebpackConfig.call(this) return clientWebpackConfig.call(this)
} }