change generate.routeParams to generate.routes

This commit is contained in:
Alexandre Chopin 2017-03-06 20:00:19 +01:00
parent c4ea665c1a
commit cd16c2f6d2
3 changed files with 25 additions and 47 deletions

View File

@ -219,7 +219,7 @@ function * generateRoutesAndFiles () {
this.options.router.extendRoutes(templateVars.router.routes, r) this.options.router.extendRoutes(templateVars.router.routes, r)
} }
// Routes for Generate command // Routes for Generate command
this.routes = flatRoutes(templateVars.router.routes, '') this.routes = flatRoutes(templateVars.router.routes)
debug('Generating files...') 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')
@ -340,13 +340,14 @@ function cleanChildrenRoutes (routes, isChild = false) {
return routes return routes
} }
function flatRoutes (router, path) { function flatRoutes (router, path = '', routes = []) {
let routes = []
router.forEach((r) => { router.forEach((r) => {
if (r.children) { if (!r.path.includes(':') && !r.path.includes('*')) {
routes.concat(flatRoutes(r.children, r.path)) if (r.children) {
} else { flatRoutes(r.children, path + r.path + '/', routes)
routes.push(path + r.path) } else {
routes.push((r.path === '' && path[path.length - 1] === '/' ? path.slice(0, -1) : path) + r.path)
}
} }
}) })
return routes return routes

View File

@ -4,10 +4,9 @@ const debug = require('debug')('nuxt:generate')
import fs from 'fs-extra' import fs from 'fs-extra'
import co from 'co' import co from 'co'
import pify from 'pify' import pify from 'pify'
import pathToRegexp from 'path-to-regexp'
import _ from 'lodash' import _ from 'lodash'
import { resolve, join, dirname, sep } from 'path' import { resolve, join, dirname, sep } from 'path'
import { promisifyRouteParams } from './utils' import { promisifyRoute } from './utils'
import { minify } from 'html-minifier' import { minify } from 'html-minifier'
const copy = pify(fs.copy) const copy = pify(fs.copy)
const remove = pify(fs.remove) const remove = pify(fs.remove)
@ -16,7 +15,7 @@ const mkdirp = pify(fs.mkdirp)
const defaults = { const defaults = {
dir: 'dist', dir: 'dist',
routeParams: {} routes: []
} }
export default function () { export default function () {
@ -52,29 +51,24 @@ export default function () {
debug('Static & build files copied') debug('Static & build files copied')
}) })
.then(() => { .then(() => {
// Resolve config.generate.routesParams promises before generating the routes // Resolve config.generate.routes promises before generating the routes
return resolveRouteParams(this.options.generate.routeParams) return promisifyRoute(this.options.generate.routes)
.catch((e) => {
console.error(`Could not resolve routes`) // eslint-disable-line no-console
console.error(e) // eslint-disable-line no-console
process.exit(1)
})
}) })
.then(() => { .then((generateRoutes) => {
/* /*
** Generate html files from routes ** Generate html files from routes
*/ */
let routes = [] generateRoutes.forEach((route) => {
this.routes.forEach((route) => { if (this.routes.indexOf(route) < 0) {
if (route.includes(':') || route.includes('*')) { this.routes.push(route)
const routeParams = this.options.generate.routeParams[route]
if (!routeParams) {
console.error(`Could not generate the dynamic route ${route}, please add the mapping params in nuxt.config.js (generate.routeParams).`) // eslint-disable-line no-console
return process.exit(1)
}
const toPathRegexp = pathToRegexp.compile(route)
routes = routes.concat(routeParams.map((params) => {
return toPathRegexp(params)
}))
} else {
routes.push(route)
} }
}) })
let routes = this.routes
return co(function * () { return co(function * () {
while (routes.length) { while (routes.length) {
yield routes.splice(0, 500).map((route) => { yield routes.splice(0, 500).map((route) => {
@ -123,20 +117,3 @@ export default function () {
return this return this
}) })
} }
function resolveRouteParams (routeParams) {
let promises = []
Object.keys(routeParams).forEach(function (routePath) {
let promise = promisifyRouteParams(routeParams[routePath])
promise.then((routeParamsData) => {
routeParams[routePath] = routeParamsData
})
.catch((e) => {
console.error(`Could not resolve routeParams[${routePath}]`) // eslint-disable-line no-console
console.error(e) // eslint-disable-line no-console
process.exit(1)
})
promises.push(promise)
})
return Promise.all(promises)
}

View File

@ -31,12 +31,12 @@ export function urlJoin () {
return [].slice.call(arguments).join('/').replace(/\/+/g, '/').replace(':/', '://') return [].slice.call(arguments).join('/').replace(/\/+/g, '/').replace(':/', '://')
} }
export function promisifyRouteParams (fn) { export function promisifyRoute (fn) {
// If routeParams[route] is an array // If routes is an array
if (Array.isArray(fn)) { if (Array.isArray(fn)) {
return Promise.resolve(fn) return Promise.resolve(fn)
} }
// If routeParams[route] is a function expecting a callback // If routes is a function expecting a callback
if (fn.length === 1) { if (fn.length === 1) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fn(function (err, routeParams) { fn(function (err, routeParams) {