Merge remote-tracking branch 'origin/0.10.0' into 0.10.0

# Conflicts:
#	lib/generate.js
#	lib/utils.js
This commit is contained in:
Sébastien Chopin 2017-03-16 18:54:37 +01:00
commit 0a595bd1c3
4 changed files with 25 additions and 48 deletions

View File

@ -220,7 +220,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')
@ -341,13 +341,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 { isUrl, promisifyRouteParams } from './utils' import { isUrl, 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

@ -35,12 +35,12 @@ export function isUrl (url) {
return (url.indexOf('http') === 0 || url.indexOf('//') === 0) return (url.indexOf('http') === 0 || url.indexOf('//') === 0)
} }
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) {

View File

@ -72,7 +72,6 @@
"lodash": "^4.17.4", "lodash": "^4.17.4",
"lru-cache": "^4.0.2", "lru-cache": "^4.0.2",
"memory-fs": "^0.4.1", "memory-fs": "^0.4.1",
"path-to-regexp": "^1.7.0",
"pify": "^2.3.0", "pify": "^2.3.0",
"post-compile-webpack-plugin": "^0.1.1", "post-compile-webpack-plugin": "^0.1.1",
"preload-webpack-plugin": "^1.2.1", "preload-webpack-plugin": "^1.2.1",