From c4ea665c1a84220a6e50b7de5b1039404dc48fbb Mon Sep 17 00:00:00 2001 From: Alexandre Chopin Date: Mon, 6 Mar 2017 19:59:22 +0100 Subject: [PATCH 1/2] remove path to regexp --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index 0593896f87..048e37e265 100644 --- a/package.json +++ b/package.json @@ -72,7 +72,6 @@ "lodash": "^4.17.4", "lru-cache": "^4.0.2", "memory-fs": "^0.4.1", - "path-to-regexp": "^1.7.0", "pify": "^2.3.0", "post-compile-webpack-plugin": "^0.1.1", "preload-webpack-plugin": "^1.2.1", From cd16c2f6d2cf2c77742b8329c7952e97fc234f47 Mon Sep 17 00:00:00 2001 From: Alexandre Chopin Date: Mon, 6 Mar 2017 20:00:19 +0100 Subject: [PATCH 2/2] change generate.routeParams to generate.routes --- lib/build.js | 15 ++++++++------- lib/generate.js | 51 ++++++++++++++----------------------------------- lib/utils.js | 6 +++--- 3 files changed, 25 insertions(+), 47 deletions(-) diff --git a/lib/build.js b/lib/build.js index d48bead79b..3fbaa3ef3e 100644 --- a/lib/build.js +++ b/lib/build.js @@ -219,7 +219,7 @@ function * generateRoutesAndFiles () { this.options.router.extendRoutes(templateVars.router.routes, r) } // Routes for Generate command - this.routes = flatRoutes(templateVars.router.routes, '') + this.routes = flatRoutes(templateVars.router.routes) debug('Generating files...') if (layoutsFiles.includes('layouts/error.vue')) { templateVars.components.ErrorPage = r(this.srcDir, 'layouts/error.vue') @@ -340,13 +340,14 @@ function cleanChildrenRoutes (routes, isChild = false) { return routes } -function flatRoutes (router, path) { - let routes = [] +function flatRoutes (router, path = '', routes = []) { router.forEach((r) => { - if (r.children) { - routes.concat(flatRoutes(r.children, r.path)) - } else { - routes.push(path + r.path) + if (!r.path.includes(':') && !r.path.includes('*')) { + if (r.children) { + flatRoutes(r.children, path + r.path + '/', routes) + } else { + routes.push((r.path === '' && path[path.length - 1] === '/' ? path.slice(0, -1) : path) + r.path) + } } }) return routes diff --git a/lib/generate.js b/lib/generate.js index 61bde5e933..834512240f 100644 --- a/lib/generate.js +++ b/lib/generate.js @@ -4,10 +4,9 @@ const debug = require('debug')('nuxt:generate') import fs from 'fs-extra' import co from 'co' import pify from 'pify' -import pathToRegexp from 'path-to-regexp' import _ from 'lodash' import { resolve, join, dirname, sep } from 'path' -import { promisifyRouteParams } from './utils' +import { promisifyRoute } from './utils' import { minify } from 'html-minifier' const copy = pify(fs.copy) const remove = pify(fs.remove) @@ -16,7 +15,7 @@ const mkdirp = pify(fs.mkdirp) const defaults = { dir: 'dist', - routeParams: {} + routes: [] } export default function () { @@ -52,29 +51,24 @@ export default function () { debug('Static & build files copied') }) .then(() => { - // Resolve config.generate.routesParams promises before generating the routes - return resolveRouteParams(this.options.generate.routeParams) + // Resolve config.generate.routes promises before generating the routes + 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 */ - let routes = [] - this.routes.forEach((route) => { - if (route.includes(':') || route.includes('*')) { - 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) + generateRoutes.forEach((route) => { + if (this.routes.indexOf(route) < 0) { + this.routes.push(route) } }) + let routes = this.routes return co(function * () { while (routes.length) { yield routes.splice(0, 500).map((route) => { @@ -123,20 +117,3 @@ export default function () { 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) -} diff --git a/lib/utils.js b/lib/utils.js index 65ab11edee..4d93bf8304 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -31,12 +31,12 @@ export function urlJoin () { return [].slice.call(arguments).join('/').replace(/\/+/g, '/').replace(':/', '://') } -export function promisifyRouteParams (fn) { - // If routeParams[route] is an array +export function promisifyRoute (fn) { + // If routes is an array if (Array.isArray(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) { return new Promise((resolve, reject) => { fn(function (err, routeParams) {